2
Copyright (c) 2013-2014 Gustav Hartvigsson
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
#ifndef __H_DYNAMIC_ARRAY__
24
#define __H_DYNAMIC_ARRAY__
33
* This file contains an imlpementation of a "dynamic" array, it is usefule when
34
* dealing with lare amounts of data that may change over time, or with an
35
* unknowned numebr of items.
37
* Note that accsess time is constant, but write time is not guarenteed to be.
39
* When the size of the array is equal to the number of elements in it, it will
40
* re-allocate the array with a larger size.
44
* The padding that is added when expanding the array.
46
#define ARRAY_PADDING 8
49
* an opaque data structure that represents the dynamic array.
51
typedef struct DynamicArray DynamicArray;
56
* Represents a for each function.
58
* @param self the dynamic array.
59
* @param data the data to be passed to the each iteration.
60
* @returns data to be put in a new \c DynamicArray, if used with
61
* <tt>dynamic_array_for_each_with_return</tt>.
63
typedef _pointer (* ForEachFunc)(DynamicArray * self, _pointer item, _pointer data);
66
* Represents a function to be used when freeing some item in a dynamic array.
68
* @param obj The object to be freed.
70
typedef _pointer (* FreeFunc)(_pointer obj);
72
#endif /* __H_DEFS__ */
76
* Create a new dynamic array.
78
* @param len The length of the initial array.
79
* @param free_func The function to be used when freeing the items. Can be \c NULL.
80
* If free_func is NULL, it will use the standard library's
83
* Normally a function with the signature <tt> (DynamicArray * self,
84
_pointer item, _pointer data) </tt> should be used but cast to FreeFunc.
86
DynamicArray * dynamic_array_new (size_t len, FreeFunc free_func);
89
* Frees the dynamic array.
91
* after this is run the data will be lost.
93
void dynamic_array_free (DynamicArray * self);
97
* Get an item from the array.
99
_pointer dynamic_array_get (DynamicArray * self, size_t index);
102
* Get the length of the array.
104
size_t dynamic_array_len (DynamicArray * self);
108
* Get the size of the array, this is not the same as the length of the array.
109
* The size is the number of elements that can be allocated without resizing
112
* To get the length of the array use dynamic_array_len ().
114
size_t dynamic_array_size (DynamicArray * self);
118
* Add an item to the array.
120
void dynamic_array_add (DynamicArray * self, _pointer item);
123
* Dumps a copy of the array. Must be cast.
125
* Is not freed when the dynamic array is freed.
127
* Is null-terminated.
129
_pointer* dynamic_array_dump_array (DynamicArray * self);
132
* Use a function on the array.
134
void dynamic_array_for_each (DynamicArray * self, ForEachFunc func,
138
* same as dynamic_array_for_each (), with the difference that it returns a new
141
DynamicArray * dynamic_array_for_each_with_return (DynamicArray * self,
147
#endif /* #define __H_DYNAMIC_ARRAY__ */