|
16 | 16 | typedef struct Vector Vector; |
17 | 17 | typedef int (*VectorCompar)(const void*, const void*); |
18 | 18 | typedef int (*VectorRangeCallback)(void* item, void* data); |
| 19 | +typedef void (*VectorItemDestroy)(void* item, void* data); |
19 | 20 |
|
20 | 21 |
|
21 | 22 | typedef struct Vector { |
@@ -111,6 +112,17 @@ static __inline__ void* vector_at(Vector* v, size_t index, void* item) |
111 | 112 | return v->items + offset; |
112 | 113 | } |
113 | 114 |
|
| 115 | +static __inline__ void* vector_set_at(Vector* v, size_t index, void* item) |
| 116 | +{ |
| 117 | + if (v == NULL || v->items == NULL) return NULL; |
| 118 | + if (index >= v->length) return NULL; |
| 119 | + size_t offset = index * v->item_size; |
| 120 | + if (item) { |
| 121 | + memcpy(v->items + offset, item, v->item_size); |
| 122 | + } |
| 123 | + return v->items + offset; |
| 124 | +} |
| 125 | + |
114 | 126 | static __inline__ int vector_delete_at(Vector* v, size_t index) |
115 | 127 | { |
116 | 128 | if (v == NULL) return -EINVAL; |
@@ -175,10 +187,17 @@ static __inline__ int vector_range(Vector* v, void* from_key, void* to_key, |
175 | 187 | return 0; |
176 | 188 | } |
177 | 189 |
|
178 | | -static __inline__ void vector_clear(Vector* v) |
| 190 | +static __inline__ void vector_clear( |
| 191 | + Vector* v, VectorItemDestroy func, void* data) |
179 | 192 | { |
180 | 193 | if (v == NULL) return; |
181 | 194 | if (v->items == NULL) return; |
| 195 | + if (func) { |
| 196 | + for (size_t i = 0; i < v->length; i++) { |
| 197 | + void* item = v->items + (i * v->item_size); |
| 198 | + func(item, data); |
| 199 | + } |
| 200 | + } |
182 | 201 | memset(v->items, 0, v->capacity * v->item_size); |
183 | 202 | v->length = 0; |
184 | 203 | } |
|
0 commit comments