/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
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.
*/

#pragma once


#include <stdlib.h>

#include "defs.h"
#include "Func.h"

S_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;


#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 NULL.
 *        If free_func is NULL, it will use the standard library's
 *       <tt>free()</tt>.
 *
 * The free function should have the the signature <tt> (DynamicArray * self,
 * spointer item, spointer data) </tt> should be used but cast to FreeFunc.
 */
S_EXPORTED
SDynamicArray *
s_dynamic_array_new (size_t len,
                     FreeFunc free_func);

S_EXPORTED
SDynamicArray *
s_dynamic_array_new_full (size_t len,
                          FreeFunc free_func,
                          FuncPointer to_json,
                          FuncPointer from_json);

/**
 * Same as s_dynamic_array_new() but with support for to_json and from_json
 * methods.
 *
 * @param to_json This function is used, on an item basis, to serialise the data
 *        in that item into a json representation.
 *
 * @param from_json This function talkes a string and marchal it into an object.
 *
 * The to_json function should have the signature:
 * <tt>char * name (spointer item)</tt> and return a string.
 * If this is not set, it will return <tt>"(pointer)"</tt>.
 *
 * The from_json function should have the signature:
 * <tt>spointer name (char * json)</tt>.
 * If this function is not set, the s_matrix_deserialize_json() function will
 * not work.
 */
S_EXPORTED
SDynamicArray *
s_dynamic_array_new_json (size_t len,
                          FreeFunc free_func,
                          FuncPointer to_json,
                          FuncPointer from_json);

/**
 * Frees the dynamic array.
 *
 * after this is run the data will be lost.
 */
S_EXPORTED
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);

/**
 * Set an item at index.
 *
 * @param self The Dynamic Array to set the item on.
 * @param index The index at which to set the item.
 * @para item The item to put in the array.
 */
S_EXPORTED
void
s_dynamic_array_set (SDynamicArray * self,
                     size_t index,
                     spointer item);
/**
 * Append an item to a dynamic array.
 * @note The item will not be added to the first free slot in the array, but
 *       in the first slot after the last item.
 *
 * @param self The dynamic array to add the item to.
 * @param item The item to add.
 */
S_EXPORTED
void
s_dynamic_array_append (SDynamicArray * self,
                        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 ().
 */
S_EXPORTED
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
 * of the item that has the highest position in the array.
 */
S_EXPORTED
size_t
s_dynamic_array_last_item (SDynamicArray * self);

/**
 * Dumps a truncation copy of the array. Must be cast.
 *
 * @param self The Dynamic Array to dump.
 * @param out_size Where the size of the array is stored.
 *
 * @note Freed by caller.
 *
 * @warning You can not use s_dynamic_array_len to get the length output array.
 *
 */
S_EXPORTED
spointer *
s_dynamic_array_dump_array (SDynamicArray * self, size_t * out_size);

/**
 * Use a function on the array.
 */
S_EXPORTED
void
s_dynamic_array_for_each (SDynamicArray * self, ForEachFunc func,
                          spointer data);



/** @} */

S_END_DECLS