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