first commit, vector implemented

AuthorKonata <konata@posteo.jp>
Date
Commita20ef80489e083da51f52ffc6dc5949781db157e
5 files changed, 213 insertions(+)
A.gitignore
@@ -0,0 +1,52 @@
1+# Prerequisites
2+*.d
3+
4+# Object files
5+*.o
6+*.ko
7+*.obj
8+*.elf
9+
10+# Linker output
11+*.ilk
12+*.map
13+*.exp
14+
15+# Precompiled Headers
16+*.gch
17+*.pch
18+
19+# Libraries
20+*.lib
21+*.a
22+*.la
23+*.lo
24+
25+# Shared objects (inc. Windows DLLs)
26+*.dll
27+*.so
28+*.so.*
29+*.dylib
30+
31+# Executables
32+*.exe
33+*.out
34+*.app
35+*.i*86
36+*.x86_64
37+*.hex
38+
39+# Debug files
40+*.dSYM/
41+*.su
42+*.idb
43+*.pdb
44+
45+# Kernel Module Compile Results
46+*.mod*
47+*.cmd
48+.tmp_versions/
49+modules.order
50+Module.symvers
51+Mkfile.old
52+dkms.conf
AMakefile
@@ -0,0 +1,3 @@
1+all:
2+ gcc -Og -g -march=native -shared -fPIC -fstrict-aliasing vector.c -o cutils.so -flto \
3+ -Wall -Werror -Wpedantic -Wnull-dereference -Wshadow -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wstrict-overflow=5 -Wunreachable-code -Wno-unused-parameter
Acutils.h
@@ -0,0 +1,4 @@
1+#ifndef CUTILS_H
2+#define CUTILS_H
3+#include <vector.h>
4+#endif //CUTILS_H
Avector.c
@@ -0,0 +1,121 @@
1+#include "vector.h"
2+
3+Vector* new_vector(void)
4+{
5+ Vector* vector = malloc(sizeof(*vector));
6+ if(!vector)
7+ return NULL;
8+ vector->items = malloc(sizeof(*vector->items)*VECTOR_DEFAULT_SIZE);
9+ if(!vector->items)
10+ {
11+ free(vector);
12+ return NULL;
13+ }
14+ vector->length = 0;
15+ vector->capacity = 4;
16+
17+ return vector;
18+}
19+
20+void* vector_at(const Vector* vector, size_t index)
21+{
22+ if(index >= vector->length)
23+ return NULL;
24+ else
25+ return vector->items[index];
26+}
27+
28+void* vector_pop(Vector* vector, size_t index)
29+{
30+ if(index >= vector->length)
31+ return NULL;
32+ else
33+ {
34+ void* tmp = vector->items[index];
35+ memmove(vector->items+index, vector->items+index+1, (vector->length-index-1)*sizeof(*vector->items));
36+ vector->length--;
37+ return tmp;
38+ }
39+}
40+
41+void vector_remove(Vector* vector, size_t index)
42+{
43+ if(index < vector->length)
44+ {
45+ free(vector->items[index]);
46+ memmove(vector->items+index, vector->items+index+1, (vector->length-index-1)*sizeof(*vector->items));
47+ vector->length--;
48+ }
49+}
50+
51+bool vector_push(Vector* vector, void* item)
52+{
53+ if(!vector_adjust_size(vector, ++vector->length))
54+ return false;
55+ vector->items[vector->length-1] = item;
56+ return true;
57+}
58+
59+FindReturn vector_find(const Vector* haystack, const void* needle, bool (*cmp)(const void*, const void*))
60+{
61+ FindReturn ret;
62+ for(size_t i = 0; i < haystack->length; i++)
63+ {
64+ if(cmp)
65+ {
66+ if(cmp(haystack->items[i],needle))
67+ {
68+ ret.found = true;
69+ ret.index = i;
70+ return ret;
71+ }
72+ } else {
73+ if(haystack->items[i] == needle)
74+ {
75+ ret.found = true;
76+ ret.index = i;
77+ return ret;
78+ }
79+ }
80+ }
81+ ret.found = false;
82+ return ret;
83+}
84+
85+bool vector_insert(Vector* vector, size_t index, void* item)
86+{
87+ if(!vector_adjust_size(vector, ++vector->length))
88+ return false;
89+
90+ memmove(vector->items+index+1, vector->items+index, (vector->length-index)*sizeof(*vector->items));
91+ vector->items[index] = item;
92+ vector->length++;
93+ return true;
94+}
95+
96+bool vector_adjust_size(Vector* vector, size_t size)
97+{
98+ while(vector->capacity < size)
99+ {
100+ void** tmp = vector->items;
101+ vector->items = realloc(vector->items, vector->capacity*2*(sizeof(*vector->items)));
102+ if(!vector->items)
103+ {
104+ vector->items = tmp;
105+ return false;
106+ }
107+ vector->capacity *= 2;
108+ }
109+ while(vector->capacity > (size*2))
110+ {
111+ void** tmp = vector->items;
112+ vector->items = realloc(vector->items, vector->capacity/2*(sizeof(*vector->items)));
113+ if(!vector->items)
114+ {
115+ vector->items = tmp;
116+ return false;
117+ }
118+ vector->capacity /= 2;
119+ }
120+ return true;
121+}
Avector.h
@@ -0,0 +1,33 @@
1+#ifndef VECTOR_H
2+#define VECTOR_H
3+#define VECTOR_DEFAULT_SIZE 4
4+#include <stdint.h>
5+#include <stdlib.h>
6+#include <string.h>
7+#include <assert.h>
8+#include <stdbool.h>
9+
10+typedef struct Vector
11+{
12+ void** items;
13+ size_t capacity;
14+ size_t length;
15+} Vector;
16+
17+typedef struct FindReturn
18+{
19+ bool found;
20+ size_t index; //if found is false, this contains rubbish
21+} FindReturn;
22+
23+Vector* new_vector(void);
24+void* vector_at(const Vector* vector, size_t index);
25+void* vector_pop(Vector* vector, size_t index);
26+bool vector_push(Vector* vector, void* item);
27+void vector_remove(Vector* vector, size_t index);
28+bool vector_adjust_size(Vector* vector, size_t size);
29+FindReturn vector_find(const Vector* haystack, const void* needle, bool (*cmp)(const void*, const void*));
30+bool vector_insert(Vector* vector, size_t index, void* item);
31+
32+
33+#endif //VECTOR_H