1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
* vi: set shiftwidth=2 tabstop=2 expandtab:
* :indentSize=2:tabSize=2:noTabs=true:
*/
#ifndef __H_DYNAMIC_ARRAY__
#define __H_DYNAMIC_ARRAY__
#include <stdlib.h>
/*
* This file, as the rest of the project is under MIT license.
* see http://opensource.org/licenses/MIT
*
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
*/
/** @file
* Dynamic Array.
*
* This file contains an imlpementation of a "dynamic" array, it is usefule when
* dealing with lare amounts of data that may change over time, or with an
* unknowned numebr of items.
*
* Note that accsess time is constant, but write time is not guarenteed to be.
*
* When the size of the array is equal to the number of elements in it, it will
* re-allocate the array with a larger size.
*/
/**
* The padding that is added when expanding the array.
*/
#define ARRAY_PADDING 8
/**
* an opaque data structure that represents the dynamic array.
*/
typedef struct _DynamicArray DynamicArray;
/**
* Represents a for each function.
*
* @param self the dynamic array.
* @param data the data to be passed to the each iteration.
* @returns data to be put in a new DynamicArray, if used with
* dynamic_array_for_each_with_return.
*/
typedef void * (* ForEachFunc)(DynamicArray * self, void * item, void * data);
/**
* Create a new dynamic array.
*
* @param len The length of the initial array.
* @param item_size The size of the items to be stored.
*/
DynamicArray * dynamic_array_new (size_t len);
/**
* Frees the dynamic array.
*
* after this is run the data will be lost.
*/
void dynamic_array_free (DynamicArray * self);
/**
* Get an item from the array.
*/
void * dynamic_array_get (DynamicArray * self, size_t index);
/**
* Get the length of the array.
*/
size_t dymanic_array_len (DynamicArray * self);
/**
* Get the size of the array, this is not the same as the length of the array.
* The size is the number of elements that can be allocated without resizing
* the array.
*
* To get the length of the array use dynamic_array_len ().
*/
size_t dynamic_array_size (DynamicArray * self);
/**
* Add an item to the array.
*/
void dynamic_array_add (DynamicArray * self, void * item);
/**
* Dumps a copy of the array. Must be cast.
*
* Is not freed when the dynamic array is freed.
*
* Is null-terminated.
*/
void ** dynamic_array_dump_array (DynamicArray * self);
/**
* Use a function on the array.
*/
void dynamic_array_for_each (DynamicArray * self, ForEachFunc func,
void * data);
/**
* same as dynamic_array_for_each (), with the difference that it returns a new
* DynamicArray.
*/
DynamicArray * dynamic_array_for_each_with_return (DynamicArray * self,
ForEachFunc func,
void * data);
#endif /* #define __H_DYNAMIC_ARRAY__ */
|