Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
saytaM12 committed Apr 3, 2023
0 parents commit 8034fa6
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
obj
main
tail
test*
chatgpt.c
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/main",
"args": ["./src/tail.c", "-n", "20"],
"cwd": "${workspaceFolder}",
"preLaunchTask": "Makefile",
"env": {
"ASAN_OPTIONS": "detect_leaks=0"
}
}
]
}
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"makefile.launchConfigurations": [
{
"cwd": "/home/user/coding/ijc/2hw",
"binaryPath": "/home/user/coding/ijc/2hw/tail",
"binaryArgs": []
}
]
}
10 changes: 10 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Makefile",
"type": "shell",
"command": "make",
}
]
}
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CC := clang
CFLAGS := -g -Wall -std=c11 -pedantic -Wextra -O3 -fsanitize=address
CREATEDIR := $(shell mkdir -p obj)

.PHONY: all
all: tail #wordcount wordcount-dynamic
# a knihovny "libhtab.a", "libhtab.so

tail: obj/tail.o
$(CC) $(CFLAGS) -lm $^ -o $@

# \
wordcount: obj/wordcount.o obj/htab.o \
$(CC) $(CFLAGS) -lm $^ -o $@ \
\
wordcount-dynamic: obj/wordcount.o obj/htab.o \
$(CC) $(CFLAGS) -lm $^ -o $@


obj/tail.o: src/tail.c

obj/%.o: src/%.c
$(CC) $(CFLAGS) $< -c -o $@


.PHONY: clean
clean:
-rm -r ./obj/ primes primes-i steg-decode
107 changes: 107 additions & 0 deletions src/htab.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <stdio.h>
#include "htab.h"

struct htab_item {
struct htab_pair data;
struct htab_item* next;
};

struct htab {
size_t size;
size_t arr_size;
struct htab_item** arr_ptr;
};

size_t htab_hash_function(htab_key_t str) {
unsigned h = 0; // musí mít 32 bitů
const unsigned char *p;
for(p = (const unsigned char*)str; *p != '\0'; p++)
h = 65599 * h + *p;
return h;
}

htab_t *htab_init(const size_t n) {
htab_t *t = malloc(sizeof(htab_t));
if (!t) {
fputs("couldn't allocate memory for hash tab", stderr);
return NULL;
}

t->arr_ptr = malloc(n * sizeof(struct htab_item*));
if (!t->arr_ptr) {
fputs("couldn't allocate memory for array of pointers", stderr);
free(t);
return NULL;
}

t->size = 0;
t->arr_size = n;

return t;
}

size_t htab_size(const htab_t *t) {
return t->size;
}

size_t htab_bucket_count(const htab_t *t) {
return t->arr_size;
}

htab_pair_t *htab_find(const htab_t *t, htab_key_t key) {
size_t index = (htab_hash_function(key) % t->arr_size);
struct htab_item* curr_item = t->arr_ptr[index];
while (curr_item->next != NULL) {
if (strcmp(curr_item->data.key, key) == 0) {
return &(curr_item->data);
}

curr_item = curr_item->next;
}
return NULL;
}

htab_pair_t *htab_lookup_add(htab_t *t, htab_key_t key) {
htab_pair_t *return_pair;
if (return_pair = htab_find(t, key)) {
return_pair->value++;
return return_pair;
}

struct htab_item *item_to_add = malloc(sizeof(struct htab_item));
if (!item_to_add) {
fputs("couldn't allocate memory for item to add", stderr);
return NULL;
}
struct htab_pair data_to_add = {.key = malloc(sizeof(key)), .value = 1};
if (!data_to_add.key) {
fputs("couldn't allocate memory for key in item to add", stderr);
free(item_to_add);
return NULL;
}
data_to_add.key = key;
item_to_add->data = data_to_add;

size_t index = (htab_hash_function(key) % t->arr_size);
struct htab_item* curr_item = t->arr_ptr[index];
while (curr_item->next != NULL) {
curr_item = curr_item->next;
}

curr_item->next = item_to_add;
}

bool htab_erase(htab_t *t, htab_key_t key) {
}

void htab_for_each(const htab_t *t, void (*f)(htab_pair_t *data)) {
}

void htab_clear(htab_t *t) {
}

void htab_free(htab_t *t) {
}

void htab_statistics(const htab_t *t) {
}
49 changes: 49 additions & 0 deletions src/htab.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// htab.h -- rozhraní knihovny htab (řešení IJC-DU2)
// Licence: žádná (Public domain)

// následující řádky zabrání násobnému vložení:
#ifndef HTAB_H__
#define HTAB_H__

#include <string.h> // size_t
#include <stdbool.h> // bool

// Tabulka:
struct htab; // neúplná deklarace struktury - uživatel nevidí obsah
typedef struct htab htab_t; // typedef podle zadání

// Typy:
typedef const char * htab_key_t; // typ klíče
typedef int htab_value_t; // typ hodnoty

// Dvojice dat v tabulce:
typedef struct htab_pair {
htab_key_t key; // klíč
htab_value_t value; // asociovaná hodnota
} htab_pair_t; // typedef podle zadání

// Rozptylovací (hash) funkce (stejná pro všechny tabulky v programu)
// Pokud si v programu definujete stejnou funkci, použije se ta vaše.
size_t htab_hash_function(htab_key_t str);

// Funkce pro práci s tabulkou:
htab_t *htab_init(const size_t n); // konstruktor tabulky
size_t htab_size(const htab_t * t); // počet záznamů v tabulce
size_t htab_bucket_count(const htab_t * t); // velikost pole

htab_pair_t * htab_find(const htab_t * t, htab_key_t key); // hledání
htab_pair_t * htab_lookup_add(htab_t * t, htab_key_t key);

bool htab_erase(htab_t * t, htab_key_t key); // ruší zadaný záznam

// for_each: projde všechny záznamy a zavolá na ně funkci f
// Pozor: f nesmí měnit klíč .key ani přidávat/rušit položky
void htab_for_each(const htab_t * t, void (*f)(htab_pair_t *data));

void htab_clear(htab_t * t); // ruší všechny záznamy
void htab_free(htab_t * t); // destruktor tabulky

// výpočet a tisk statistik délky seznamů (min,max,avg) do stderr:
void htab_statistics(const htab_t * t);

#endif // HTAB_H__
Loading

0 comments on commit 8034fa6

Please sign in to comment.