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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
Copyright (c) 2013-2014 Gustav Hartvigsson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef __H_DYNAMIC_ARRAY__
#define __H_DYNAMIC_ARRAY__
#include <stdlib.h>
#include "defs.h"
#include "Func.h"
BEGIN_DECLS
/** @file
* @defgroup SDynamicArray SDynamicArray
* @addtogroup SDynamicArray SDynamicArray
* @{
*
* SDynamicArray is 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 SDynamicArray is the standard implementation of a Dynamic Array in SSTS.
*
* It does not depend an SObject, because it should be albe to be used in,
* SObject and SObject based classes.
*/
typedef struct SDynamicArray SDynamicArray;
typedef struct SDynamicArrayPrivate SDynamicArrayPrivate;
struct
SDynamicArray {
SDynamicArrayPrivate * priv;
size_t max_size;
size_t last_item;
};
#define S_DYNAMIC_ARRAY(k) (SDynamicArray *)(k)
/**
* Create a new dynamic array.
*
* @param len The length of the initial array.
* @param free_func The function to be used when freeing the items. Can be \c NULL.
* If free_func is NULL, it will use the standard library's
* <tt>free()</tt>.
*
* Normally a function with the signature <tt> (DynamicArray * self,
spointer item, spointer data) </tt> should be used but cast to FreeFunc.
*/
SDynamicArray *
s_dynamic_array_new (size_t len, FreeFunc free_func);
/**
* Frees the dynamic array.
*
* after this is run the data will be lost.
*/
void
s_dynamic_array_free (SDynamicArray * self, sboolean free_data);
/**
* Get an item from the array.
*/
spointer
s_dynamic_array_get (SDynamicArray * self, size_t index);
/**
*
*/
void
s_dynamic_array_set (SDynamicArray * self, size_t index, spointer item);
/**
* 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 s_dynamic_array_len ().
*/
size_t
s_dynamic_array_size (SDynamicArray * self);
/**
* Get the index of the last item in the array.
*
* @note
* This is not the last item added to the array, necessary, it is the index
* if the item that has the highest position in the array.
*/
size_t
s_dynamic_array_last_item (SDynamicArray * self);
/**
* Dumps a copy of the array. Must be cast.
*
* Is not freed when the dynamic array is freed.
*
* Is null-terminated.
*/
spointer *
s_dynamic_array_dump_array (SDynamicArray * self);
/**
* Use a function on the array.
*/
void
s_dynamic_array_for_each (SDynamicArray * self, ForEachFunc func,
spointer data);
/** TODO
* same as s_dynamic_array_for_each (), with the difference that it returns a new
* SDynamicArray.
*/
SDynamicArray *
s_dynamic_array_for_each_with_return (SDynamicArray * self,
ForEachFunc func,
spointer data);
/** @} */
END_DECLS
#endif /* #define __H_DYNAMIC_ARRAY__ */
|