Commit | Line | Data |
---|---|---|
a7868768 | 1 | /** |
1ff641f9 | 2 | * @file Vector.c |
a7868768 BA |
3 | */ |
4 | ||
5 | #include "cgds/Vector.h" | |
6 | ||
7 | ////////////////// | |
8 | // Vector logic // | |
9 | ////////////////// | |
10 | ||
11 | void _vector_init(Vector* vector, size_t dataSize) | |
12 | { | |
e45132ac BA |
13 | vector->datas = NULL; |
14 | vector->dataSize = dataSize; | |
15 | vector->size = 0; | |
16 | vector->capacity = 0; | |
a7868768 BA |
17 | } |
18 | ||
19 | Vector* _vector_new(size_t dataSize) | |
20 | { | |
e45132ac BA |
21 | Vector* vector = (Vector*) safe_malloc(sizeof (Vector)); |
22 | _vector_init(vector, dataSize); | |
23 | return vector; | |
a7868768 BA |
24 | } |
25 | ||
26 | Vector* vector_copy(Vector* vector) | |
27 | { | |
e45132ac BA |
28 | Vector* vectorCopy = _vector_new(vector->dataSize); |
29 | vectorCopy->size = vector->size; | |
30 | vectorCopy->capacity = vector->capacity; | |
1d60b10a BA |
31 | vectorCopy->datas = (void*) safe_malloc(vector->capacity * vector->dataSize); |
32 | memcpy(vectorCopy->datas, vector->datas, vector->size * vector->dataSize); | |
e45132ac | 33 | return vectorCopy; |
a7868768 BA |
34 | } |
35 | ||
1ff641f9 | 36 | bool vector_empty(Vector* vector) |
a7868768 | 37 | { |
e45132ac | 38 | return (vector->size == 0); |
a7868768 BA |
39 | } |
40 | ||
41 | UInt vector_size(Vector* vector) | |
42 | { | |
e45132ac | 43 | return vector->size; |
a7868768 BA |
44 | } |
45 | ||
46 | void _vector_realloc(Vector* vector, UInt newCapacity) | |
47 | { | |
1d60b10a BA |
48 | void* rellocatedDatas = (void*) safe_malloc(newCapacity * vector->dataSize); |
49 | memcpy(rellocatedDatas, vector->datas, vector->size * vector->dataSize); | |
e45132ac BA |
50 | safe_free(vector->datas); |
51 | vector->datas = rellocatedDatas; | |
52 | vector->capacity = newCapacity; | |
a7868768 BA |
53 | } |
54 | ||
55 | void _vector_push(Vector* vector, void* data) | |
56 | { | |
e45132ac BA |
57 | if (vector->size >= vector->capacity) |
58 | { | |
59 | UInt increasedCapacity = vector->capacity > 0 ? 2 * vector->capacity : 1; | |
60 | _vector_realloc(vector, increasedCapacity); | |
61 | } | |
1d60b10a BA |
62 | memcpy( |
63 | vector->datas + vector->size * vector->dataSize, | |
64 | data, | |
65 | vector->dataSize); | |
e45132ac | 66 | vector->size++; |
a7868768 BA |
67 | } |
68 | ||
69 | void vector_pop(Vector* vector) | |
70 | { | |
e45132ac BA |
71 | vector->size--; |
72 | if (vector_size(vector) <= (vector->capacity >> 1)) | |
73 | _vector_realloc(vector, vector->capacity >> 1); | |
a7868768 BA |
74 | } |
75 | ||
76 | void* _vector_get(Vector* vector, UInt index) | |
77 | { | |
1d60b10a | 78 | return vector->datas + index * vector->dataSize; |
a7868768 BA |
79 | } |
80 | ||
81 | void _vector_set(Vector* vector, UInt index, void* data) | |
82 | { | |
1d60b10a | 83 | memcpy(vector->datas + index * vector->dataSize, data, vector->dataSize); |
a7868768 BA |
84 | } |
85 | ||
86 | void vector_clear(Vector* vector) | |
87 | { | |
e45132ac BA |
88 | safe_free(vector->datas); |
89 | _vector_init(vector, vector->dataSize); | |
a7868768 BA |
90 | } |
91 | ||
92 | void vector_destroy(Vector* vector) | |
93 | { | |
e45132ac BA |
94 | vector_clear(vector); |
95 | safe_free(vector); | |
a7868768 BA |
96 | } |
97 | ||
98 | //////////////////// | |
99 | // Iterator logic // | |
100 | //////////////////// | |
101 | ||
102 | VectorIterator* vector_get_iterator(Vector* vector) | |
103 | { | |
e45132ac | 104 | VectorIterator* vectorI = |
1ff641f9 | 105 | (VectorIterator*) safe_malloc(sizeof (VectorIterator)); |
e45132ac BA |
106 | vectorI->vector = vector; |
107 | vectorI_reset_begin(vectorI); | |
108 | return vectorI; | |
a7868768 BA |
109 | } |
110 | ||
111 | void vectorI_reset_begin(VectorIterator* vectorI) | |
112 | { | |
e45132ac | 113 | vectorI->current = vectorI->vector->datas; |
a7868768 BA |
114 | } |
115 | ||
116 | void vectorI_reset_end(VectorIterator* vectorI) | |
117 | { | |
1d60b10a BA |
118 | vectorI->current = vectorI->vector->datas + |
119 | (vectorI->vector->size - 1) * vectorI->vector->dataSize; | |
a7868768 BA |
120 | } |
121 | ||
1ff641f9 | 122 | bool vectorI_has_data(VectorIterator* vectorI) |
a7868768 | 123 | { |
1d60b10a BA |
124 | return ( |
125 | vectorI->current >= vectorI->vector->datas && | |
126 | vectorI->current < | |
127 | vectorI->vector->datas + | |
128 | vectorI->vector->size * vectorI->vector->dataSize | |
129 | ); | |
a7868768 BA |
130 | } |
131 | ||
132 | void* _vectorI_get(VectorIterator* vectorI) | |
133 | { | |
1d60b10a | 134 | return vectorI->current; |
a7868768 BA |
135 | } |
136 | ||
137 | void _vectorI_set(VectorIterator* vectorI, void* data) | |
138 | { | |
1d60b10a | 139 | memcpy(vectorI->current, data, vectorI->vector->dataSize); |
a7868768 BA |
140 | } |
141 | ||
142 | void vectorI_move_next(VectorIterator* vectorI) | |
143 | { | |
1d60b10a | 144 | vectorI->current += vectorI->vector->dataSize; |
a7868768 BA |
145 | } |
146 | ||
147 | void vectorI_move_prev(VectorIterator* vectorI) | |
148 | { | |
1d60b10a | 149 | vectorI->current -= vectorI->vector->dataSize; |
a7868768 BA |
150 | } |
151 | ||
152 | void vectorI_destroy(VectorIterator* vectorI) | |
153 | { | |
e45132ac | 154 | safe_free(vectorI); |
a7868768 | 155 | } |