1
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
2
* vi: set shiftwidth=2 tabstop=2 expandtab:
3
* :indentSize=2:tabSize=2:noTabs=true:
6
#ifndef __H_DYNAMIC_ARRAY__
7
#define __H_DYNAMIC_ARRAY__
11
/* This file, as the rest of the project is under MIT license.
12
* see http://opensource.org/licenses/MIT
14
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
20
* This file contains an imlpementation of a "dynamic" array, it is usefule when
21
* dealing with lare amounts of data that may change over time, or with an
22
* unknowned numebr of items.
24
* Note that accsess time is constant, but write time is not guarenteed to be.
26
* When the size of the array is equal to the number of elements in it, it will
27
* re-allocate the array with a larger size.
31
* The padding that is added when expanding the array.
33
#define ARRAY_PADDING 8
36
* an opaque data structure that represents the dynamic array.
38
typedef struct DynamicArray DynamicArray;
43
* Represents a for each function.
45
* @param self the dynamic array.
46
* @param data the data to be passed to the each iteration.
47
* @returns data to be put in a new \c DynamicArray, if used with
48
* <tt>dynamic_array_for_each_with_return</tt>.
50
typedef _pointer (* ForEachFunc)(DynamicArray * self, _pointer item, _pointer data);
53
* Represents a function to be used when freeing some item in a dynamic array.
55
* @param obj The object to be freed.
57
typedef _pointer (* FreeFunc)(_pointer obj);
59
#endif /* __H_DEFS__ */
63
* Create a new dynamic array.
65
* @param len The length of the initial array.
66
* @param free_func The function to be used when freeing the items. Can be \c NULL.
67
* If free_func is NULL, it will use the standard library's
70
* Normally a function with the signature <tt> (DynamicArray * self,
71
_pointer item, _pointer data) </tt> should be used but cast to FreeFunc.
73
DynamicArray * dynamic_array_new (size_t len, FreeFunc free_func);
76
* Frees the dynamic array.
78
* after this is run the data will be lost.
80
void dynamic_array_free (DynamicArray * self);
84
* Get an item from the array.
86
_pointer dynamic_array_get (DynamicArray * self, size_t index);
89
* Get the length of the array.
91
size_t dynamic_array_len (DynamicArray * self);
95
* Get the size of the array, this is not the same as the length of the array.
96
* The size is the number of elements that can be allocated without resizing
99
* To get the length of the array use dynamic_array_len ().
101
size_t dynamic_array_size (DynamicArray * self);
105
* Add an item to the array.
107
void dynamic_array_add (DynamicArray * self, _pointer item);
110
* Dumps a copy of the array. Must be cast.
112
* Is not freed when the dynamic array is freed.
114
* Is null-terminated.
116
_pointer* dynamic_array_dump_array (DynamicArray * self);
119
* Use a function on the array.
121
void dynamic_array_for_each (DynamicArray * self, ForEachFunc func,
125
* same as dynamic_array_for_each (), with the difference that it returns a new
128
DynamicArray * dynamic_array_for_each_with_return (DynamicArray * self,
132
#endif /* #define __H_DYNAMIC_ARRAY__ */