commit 15934c470824d0634d167863db0225b6f96d2b74 Author: jacekpoz Date: Sun Mar 10 00:45:57 2024 +0100 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff7692f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +l1 +.ccls-cache/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..5ed9e8e --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# aisd_lab + +this repo contains my solutions to algorithm and data structures labs exercises diff --git a/lab01/zad1/.ccls b/lab01/zad1/.ccls new file mode 100644 index 0000000..9f91bd0 --- /dev/null +++ b/lab01/zad1/.ccls @@ -0,0 +1,21 @@ +gcc +%c -std=c99 +%h +-Iinclude +-Wall +-Wextra +-Wpedantic +-Wstrict-aliasing +-Wfloat-equal +-Wundef +-Wshadow +-Wpointer-arith +-Wcast-align +-Wstrict-prototypes +-Wstrict-overflow=5 +-Wwrite-strings +-Wcast-qual +-Wswitch-default +-Wswitch-enum +-Wconversion +-Wunreachable-code diff --git a/lab01/zad1/.gitignore b/lab01/zad1/.gitignore new file mode 100644 index 0000000..cf48717 --- /dev/null +++ b/lab01/zad1/.gitignore @@ -0,0 +1,3 @@ +target/ +.ccls-cache/ +.direnv/ diff --git a/lab01/zad1/Makefile b/lab01/zad1/Makefile new file mode 100644 index 0000000..b576ede --- /dev/null +++ b/lab01/zad1/Makefile @@ -0,0 +1,35 @@ +CC = gcc +CFLAGS = -std=c99 -O3 -Wall -Wextra -Wpedantic -Wstrict-aliasing +CFLAGS += -Wfloat-equal -Wundef -Wshadow -Wpointer-arith -Wcast-align +CFLAGS += -Wstrict-prototypes -Wstrict-overflow=5 -Wwrite-strings +CFLAGS += -Wcast-qual -Wswitch-default -Wswitch-enum +CFLAGS += -Wconversion -Wunreachable-code +CFLAGS += -Iinclude +LDFLAGS = + +NAME = zad1 + +SRC = src +BIN = target + +_PROG = main.c queue.c +PROG = $(addprefix $(SRC)/, $(_PROG)) + +OBJ = $(_PROG:.c=.o) + +.PHONY: all clean + +all: dirs $(NAME) + +dirs: + mkdir -p $(BIN) + +$(NAME): $(OBJ) + $(CC) $(addprefix $(BIN)/, $^) $(LDFLAGS) -o $(BIN)/$@ + +%.o: src/%.c + $(CC) -c $< $(CFLAGS) -o $(BIN)/$@ + +clean: + rm -rf $(addprefix $(BIN)/, $(OBJ)) + rm -rf $(BIN)/$(NAME) diff --git a/lab01/zad1/include/queue.h b/lab01/zad1/include/queue.h new file mode 100644 index 0000000..a91dca8 --- /dev/null +++ b/lab01/zad1/include/queue.h @@ -0,0 +1,15 @@ +#ifndef _KIK_ZAD1_QUEUE_H +#define _KIK_ZAD1_QUEUE_H + +#include +#include + +typedef struct Queue Queue; + +Queue *queue_new(size_t size); + +void *queue_read(Queue *queue); + +bool queue_write(Queue *queue, void *value); + +#endif // _KIK_ZAD1_QUEUE_H diff --git a/lab01/zad1/src/main.c b/lab01/zad1/src/main.c new file mode 100644 index 0000000..c6f67b6 --- /dev/null +++ b/lab01/zad1/src/main.c @@ -0,0 +1,34 @@ +#include + +#include +#include +#include + +int main(void) { + srand((unsigned int)time(NULL)); + + Queue *q = queue_new(51); + + for (size_t i = 0; i < 50; ++i) { + long val = rand() % 1000; + printf("%zu: writing %ld to queue...\n", i, val); + if (queue_write(q, &val)) { + printf("%zu: successfully wrote %ld to fifo!\n", i, val); + } else { + printf("%zu: failed writing %ld to fifo :-(\n", i, val); + } + } + + for (size_t i = 0; i < 50; ++i) { + void *val_ptr = queue_read(q); + if (val_ptr == NULL) { + printf("%zu: fifo is empty!\n", i); + break; + } + + long val = *(long*)val_ptr; + printf("%zu: read %ld from fifo\n", i, val); + } + + return 0; +} diff --git a/lab01/zad1/src/queue.c b/lab01/zad1/src/queue.c new file mode 100644 index 0000000..a317d21 --- /dev/null +++ b/lab01/zad1/src/queue.c @@ -0,0 +1,44 @@ +#include + +#include +#include + +struct Queue { + size_t back; + size_t front; + size_t size; + void **data; +}; + +Queue *queue_new(size_t size) { + Queue *ret = malloc(sizeof(Queue)); + ret->back = 0; + ret->front = 0; + ret->size = size; + ret->data = malloc(size * sizeof(void*)); + + return ret; +} + +void *queue_read(Queue *q) { + if (q->front == q->back) { + return NULL; + } + + void* ret = q->data[q->front]; + q->data[q->front] = NULL; + + q->front = (q->front + 1) % q->size; + return ret; +} + +bool queue_write(Queue *q, void *value) { + if (((q->back + 1) % q->size) == q->front) { + return false; + } + + q->data[q->back] = value; + q->back = (q->back + 1) % q->size; + + return true; +}