/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
5.2.9 by Gustav Hartvigsson
* Added license to files
1
/*
2
Copyright (c) 2013-2014 Gustav Hartvigsson
3
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:
10
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
13
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
20
THE SOFTWARE.
21
*/
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
22
23
#ifndef __H_DYNAMIC_ARRAY__
24
#define __H_DYNAMIC_ARRAY__
25
#include <stdlib.h>
26
#include "defs.h"
30 by Gustav Hartvigsson
* Made the code compile using CMake.
27
#include "Func.h"
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
28
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
29
S_BEGIN_DECLS
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
30
31
/** @file
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
32
 * @defgroup SDynamicArray SDynamicArray
33
 * @addtogroup SDynamicArray SDynamicArray
34
 * @{
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
35
 *
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
36
 * SDynamicArray is an imlpementation of a dynamic array, it is usefule when
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
37
 * dealing with lare amounts of data that may change over time, or with an
38
 * unknowned numebr of items.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
39
 *
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
40
 * Note that accsess time is constant, but write time is not guarenteed to be.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
41
 *
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
42
 * When the size of the array is equal to the number of elements in it, it will
43
 * re-allocate the array with a larger size.
44
 */
45
46
/**
47
 * The padding that is added when expanding the array.
48
 */
49
#define ARRAY_PADDING 8
50
51
/**
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
52
 * An SDynamicArray is the standard implementation of a Dynamic Array in SSTS.
53
 *
54
 * It does not depend an SObject, because it should be albe to be used in,
55
 * SObject and SObject based classes.
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
56
 */
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
57
typedef struct SDynamicArray SDynamicArray;
58
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
59
60
#define S_DYNAMIC_ARRAY(k) ((SDynamicArray *)(k))
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
61
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
62
/**
63
 * Create a new dynamic array.
64
 *
65
 * @param len The length of the initial array.
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
66
 * @param free_func The function to be used when freeing the items. Can be NULL.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
67
 *        If free_func is NULL, it will use the standard library's
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
68
 *       <tt>free()</tt>.
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
69
 *
70
 * The free function should have the the signature <tt> (DynamicArray * self,
71
 * spointer item, spointer data) </tt> should be used but cast to FreeFunc.
72
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
73
S_EXPORTED
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
74
SDynamicArray *
75
s_dynamic_array_new (size_t len,
76
                     FreeFunc free_func);
77
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
78
S_EXPORTED
109.1.5 by Gustav Hartvigsson
* Getting closer to fixing the callbacks...
79
SDynamicArray *
80
s_dynamic_array_new_full (size_t len,
81
                          FreeFunc free_func,
82
                          FuncPointer to_json,
83
                          FuncPointer from_json);
84
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
85
/**
86
 * Same as s_dynamic_array_new() but with support for to_json and from_json
87
 * methods.
88
 *
89
 * @param to_json This function is used, on an item basis, to serialise the data
90
 *        in that item into a json representation.
91
 *
92
 * @param from_json This function talkes a string and marchal it into an object.
93
 *
94
 * The to_json function should have the signature:
95
 * <tt>char * name (spointer item)</tt> and return a string.
96
 * If this is not set, it will return <tt>"(pointer)"</tt>.
97
 *
98
 * The from_json function should have the signature:
99
 * <tt>spointer name (char * json)</tt>.
100
 * If this function is not set, the s_matrix_deserialize_json() function will
101
 * not work.
102
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
103
S_EXPORTED
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
104
SDynamicArray *
105
s_dynamic_array_new_json (size_t len,
106
                          FreeFunc free_func,
107
                          FuncPointer to_json,
108
                          FuncPointer from_json);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
109
110
/**
111
 * Frees the dynamic array.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
112
 *
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
113
 * after this is run the data will be lost.
114
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
115
S_EXPORTED
61 by Gustav Hartvigsson
* Made the code more easy to read.
116
void
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
117
s_dynamic_array_free (SDynamicArray * self,
118
		      sboolean free_data);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
119
120
121
/**
122
 * Get an item from the array.
123
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
124
spointer
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
125
s_dynamic_array_get (SDynamicArray * self,
126
		     size_t index);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
127
128
/**
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
129
 * Set an item at index.
130
 *
131
 * @param self The Dynamic Array to set the item on.
132
 * @param index The index at which to set the item.
133
 * @para item The item to put in the array.
134
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
135
S_EXPORTED
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
136
void
137
s_dynamic_array_set (SDynamicArray * self,
138
		     size_t index,
139
		     spointer item);
140
/**
141
 * Append an item to a dynamic array.
142
 * @note The item will not be added to the first free slot in the array, but
143
 *       in the first slot after the last item.
144
 *
145
 * @param self The dynamic array to add the item to.
146
 * @param item The item to add.
147
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
148
S_EXPORTED
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
149
void
150
s_dynamic_array_append (SDynamicArray * self,
151
			spointer item);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
152
153
/**
154
 * Get the size of the array, this is not the same as the length of the array.
155
 * The size is the number of elements that can be allocated without resizing
156
 * the array.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
157
 *
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
158
 * To get the length of the array use s_dynamic_array_len ().
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
159
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
160
S_EXPORTED
61 by Gustav Hartvigsson
* Made the code more easy to read.
161
size_t
162
s_dynamic_array_size (SDynamicArray * self);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
163
164
/**
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
165
 * Get the index of the last item in the array.
166
 *
167
 * @note
168
 * This is not the last item added to the array, necessary, it is the index
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
169
 * of the item that has the highest position in the array.
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
170
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
171
S_EXPORTED
61 by Gustav Hartvigsson
* Made the code more easy to read.
172
size_t
173
s_dynamic_array_last_item (SDynamicArray * self);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
174
175
/**
176
 * Dumps a copy of the array. Must be cast.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
177
 *
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
178
 * Is not freed when the dynamic array is freed.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
179
 *
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
180
 * Is null-terminated.
181
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
182
S_EXPORTED
61 by Gustav Hartvigsson
* Made the code more easy to read.
183
spointer *
184
s_dynamic_array_dump_array (SDynamicArray * self);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
185
186
/**
187
 * Use a function on the array.
188
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
189
S_EXPORTED
61 by Gustav Hartvigsson
* Made the code more easy to read.
190
void
191
s_dynamic_array_for_each (SDynamicArray * self, ForEachFunc func,
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
192
                          spointer data);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
193
70 by Gustav Hartvigsson
* removed s_dynamic_array_foreach_with_return.
194
#if 0
195
This function is removed, it would have been a pain in the arse to implement.
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
196
/** TODO
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
197
 * same as s_dynamic_array_for_each (), with the difference that it returns a new
198
 * SDynamicArray.
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
199
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
200
SDynamicArray *
201
s_dynamic_array_for_each_with_return (SDynamicArray * self,
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
202
                                      ForEachFunc func,
203
                                      spointer data);
70 by Gustav Hartvigsson
* removed s_dynamic_array_foreach_with_return.
204
#endif
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
205
206
/**
207
 * Returns a JSON representation of the array.
208
 *
209
 * If the to_json function is not set, this will return <tt>(pointer)</tt> for
210
 * each object.
211
 *
212
 * @param self The SDynamicArray to get the JSON representation from.
213
 *
214
 * @return a string with the JSON representation of the SDynamicArray.
215
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
216
S_EXPORTED
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
217
char *
218
s_dynamic_array_serialize_json (SDynamicArray * self);
219
220
/**
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
221
 * appends itmes to an the array
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
222
 *
223
 * If the from_json function is not set, this will couse undefined behaviour.
224
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
225
S_EXPORTED
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
226
void
227
s_dynamic_array_deserialize_json (SDynamicArray * self, char * json);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
228
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
229
/** @} */
230
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
231
S_END_DECLS
5.2.9 by Gustav Hartvigsson
* Added license to files
232
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
233
#endif /* #define __H_DYNAMIC_ARRAY__ */