summaryrefslogtreecommitdiff
path: root/rushs/tinyprintf/heap
diff options
context:
space:
mode:
Diffstat (limited to 'rushs/tinyprintf/heap')
-rw-r--r--rushs/tinyprintf/heap/Makefile14
-rw-r--r--rushs/tinyprintf/heap/add.c39
-rw-r--r--rushs/tinyprintf/heap/create.c19
-rw-r--r--rushs/tinyprintf/heap/del.c9
-rw-r--r--rushs/tinyprintf/heap/heap.h20
-rw-r--r--rushs/tinyprintf/heap/pop.c49
-rw-r--r--rushs/tinyprintf/heap/print.c27
7 files changed, 0 insertions, 177 deletions
diff --git a/rushs/tinyprintf/heap/Makefile b/rushs/tinyprintf/heap/Makefile
deleted file mode 100644
index 2ed972b..0000000
--- a/rushs/tinyprintf/heap/Makefile
+++ /dev/null
@@ -1,14 +0,0 @@
-CC = gcc
-CFLAGS = -Wall -Werror -Wvla -Wextra -std=c99 -pedantic
-SRC = add.c del.c print.c pop.c create.c
-OBJ = $(SRC:.c=.o)
-
-.PHONY: library clean
-
-library: $(OBJ)
- ar csr libheap.a $(OBJ)
-
-$(OBJ): $(SRC)
-
-clean:
- $(RM) libheap.a $(OBJ)
diff --git a/rushs/tinyprintf/heap/add.c b/rushs/tinyprintf/heap/add.c
deleted file mode 100644
index 78a4db8..0000000
--- a/rushs/tinyprintf/heap/add.c
+++ /dev/null
@@ -1,39 +0,0 @@
-#include <stdlib.h>
-
-#include "heap.h"
-
-void heapify(int arr[], int n, int i)
-{
- if (i == 0)
- return;
- int parent = (i - 1) / 2;
- if (parent >= 0)
- {
- if (arr[i] > arr[parent])
- {
- int tmp = arr[i];
- arr[i] = arr[parent];
- arr[parent] = tmp;
- heapify(arr, n, parent);
- }
- }
-}
-
-void add(struct heap *heap, int val)
-{
- if (heap->size == heap->capacity)
- {
- heap->array = realloc(heap->array, heap->capacity * 2 * sizeof(int));
- if (heap->array == NULL)
- {
- free(heap);
- return;
- }
- heap->capacity *= 2;
- }
-
- heap->array[heap->size] = val;
- heap->size++;
-
- heapify(heap->array, heap->size, heap->size - 1);
-}
diff --git a/rushs/tinyprintf/heap/create.c b/rushs/tinyprintf/heap/create.c
deleted file mode 100644
index f0675ad..0000000
--- a/rushs/tinyprintf/heap/create.c
+++ /dev/null
@@ -1,19 +0,0 @@
-#include <stdlib.h>
-
-#include "heap.h"
-
-struct heap *create_heap(void)
-{
- struct heap *res = malloc(sizeof(struct heap));
- if (res == NULL)
- return NULL;
- res->size = 0;
- res->capacity = 8;
- res->array = malloc(8 * sizeof(int));
- if (res->array == NULL)
- {
- free(res);
- return NULL;
- }
- return res;
-}
diff --git a/rushs/tinyprintf/heap/del.c b/rushs/tinyprintf/heap/del.c
deleted file mode 100644
index 4d2ae35..0000000
--- a/rushs/tinyprintf/heap/del.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <stdlib.h>
-
-#include "heap.h"
-
-void delete_heap(struct heap *heap)
-{
- free(heap->array);
- free(heap);
-}
diff --git a/rushs/tinyprintf/heap/heap.h b/rushs/tinyprintf/heap/heap.h
deleted file mode 100644
index 085f436..0000000
--- a/rushs/tinyprintf/heap/heap.h
+++ /dev/null
@@ -1,20 +0,0 @@
-#ifndef HEAP_H
-#define HEAP_H
-
-// size_t
-#include <stddef.h>
-
-struct heap
-{
- size_t size;
- size_t capacity;
- int *array;
-};
-
-struct heap *create_heap(void);
-void add(struct heap *heap, int val);
-int pop(struct heap *heap);
-void delete_heap(struct heap *heap);
-void print_heap(const struct heap *heap);
-
-#endif /* !HEAP_H */
diff --git a/rushs/tinyprintf/heap/pop.c b/rushs/tinyprintf/heap/pop.c
deleted file mode 100644
index 55e063f..0000000
--- a/rushs/tinyprintf/heap/pop.c
+++ /dev/null
@@ -1,49 +0,0 @@
-#include <assert.h>
-#include <stdlib.h>
-
-#include "heap.h"
-
-void heapify2(struct heap *h, size_t i)
-{
- size_t max = i;
- if (2 * i + 1 < h->size && h->array[2 * i + 1] > h->array[max])
- max = 2 * i + 1;
- if (2 * i + 2 < h->size && h->array[2 * i + 2] > h->array[max])
- max = 2 * i + 2;
- if (max != i)
- {
- int tmp = h->array[i];
- h->array[i] = h->array[max];
- h->array[max] = tmp;
- heapify2(h, max);
- }
-}
-
-int pop(struct heap *heap)
-{
- assert(heap->size != 0);
- if (heap->size == 1)
- {
- heap->size--;
- return heap->array[0];
- }
- else
- {
- int res = heap->array[0];
- heap->array[0] = heap->array[heap->size - 1];
- heap->size--;
- heapify2(heap, 0);
- if (heap->size < heap->capacity / 2 && heap->capacity > 8)
- {
- heap->array =
- realloc(heap->array, (heap->capacity / 2) * sizeof(int));
- if (heap->array == NULL)
- {
- free(heap);
- return -1;
- }
- heap->capacity /= 2;
- }
- return res;
- }
-}
diff --git a/rushs/tinyprintf/heap/print.c b/rushs/tinyprintf/heap/print.c
deleted file mode 100644
index f5bbe95..0000000
--- a/rushs/tinyprintf/heap/print.c
+++ /dev/null
@@ -1,27 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "heap.h"
-
-void print_rec(const struct heap *h, size_t i, int root)
-{
- if (i >= h->size)
- return;
- if (!root)
- printf(" ");
- else
- root = 0;
- printf("%d", h->array[i]);
- if (i == h->size - 1)
- {
- return;
- }
- print_rec(h, i * 2 + 1, root);
- print_rec(h, i * 2 + 2, root);
-}
-
-void print_heap(const struct heap *heap)
-{
- print_rec(heap, 0, 1);
- printf("\n");
-}