/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
 */
73
SDynamicArray *
74
s_dynamic_array_new (size_t len,
75
                     FreeFunc free_func);
76
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
 */
101
SDynamicArray *
102
s_dynamic_array_new_json (size_t len,
103
                          FreeFunc free_func,
104
                          FuncPointer to_json,
105
                          FuncPointer from_json);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
106
107
/**
108
 * Frees the dynamic array.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
109
 *
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
110
 * after this is run the data will be lost.
111
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
112
void
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
113
s_dynamic_array_free (SDynamicArray * self,
114
		      sboolean free_data);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
115
116
117
/**
118
 * Get an item from the array.
119
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
120
spointer
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
121
s_dynamic_array_get (SDynamicArray * self,
122
		     size_t index);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
123
124
/**
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
125
 * Set an item at index.
126
 *
127
 * @param self The Dynamic Array to set the item on.
128
 * @param index The index at which to set the item.
129
 * @para item The item to put in the array.
130
 */
131
void
132
s_dynamic_array_set (SDynamicArray * self,
133
		     size_t index,
134
		     spointer item);
135
/**
136
 * Append an item to a dynamic array.
137
 * @note The item will not be added to the first free slot in the array, but
138
 *       in the first slot after the last item.
139
 *
140
 * @param self The dynamic array to add the item to.
141
 * @param item The item to add.
142
 */
143
void
144
s_dynamic_array_append (SDynamicArray * self,
145
			spointer item);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
146
147
/**
148
 * Get the size of the array, this is not the same as the length of the array.
149
 * The size is the number of elements that can be allocated without resizing
150
 * the array.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
151
 *
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
152
 * 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
153
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
154
size_t
155
s_dynamic_array_size (SDynamicArray * self);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
156
157
/**
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
158
 * Get the index of the last item in the array.
159
 *
160
 * @note
161
 * This is not the last item added to the array, necessary, it is the index
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
162
 * 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
163
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
164
size_t
165
s_dynamic_array_last_item (SDynamicArray * self);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
166
167
/**
168
 * Dumps a copy of the array. Must be cast.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
169
 *
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
170
 * Is not freed when the dynamic array is freed.
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
171
 *
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
172
 * Is null-terminated.
173
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
174
spointer *
175
s_dynamic_array_dump_array (SDynamicArray * self);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
176
177
/**
178
 * Use a function on the array.
179
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
180
void
181
s_dynamic_array_for_each (SDynamicArray * self, ForEachFunc func,
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
182
                          spointer data);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
183
70 by Gustav Hartvigsson
* removed s_dynamic_array_foreach_with_return.
184
#if 0
185
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
186
/** TODO
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
187
 * same as s_dynamic_array_for_each (), with the difference that it returns a new
188
 * SDynamicArray.
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
189
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
190
SDynamicArray *
191
s_dynamic_array_for_each_with_return (SDynamicArray * self,
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
192
                                      ForEachFunc func,
193
                                      spointer data);
70 by Gustav Hartvigsson
* removed s_dynamic_array_foreach_with_return.
194
#endif
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
195
196
/**
197
 * Returns a JSON representation of the array.
198
 *
199
 * If the to_json function is not set, this will return <tt>(pointer)</tt> for
200
 * each object.
201
 *
202
 * @param self The SDynamicArray to get the JSON representation from.
203
 *
204
 * @return a string with the JSON representation of the SDynamicArray.
205
 */
206
char *
207
s_dynamic_array_serialize_json (SDynamicArray * self);
208
209
/**
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
210
 * appends itmes to an the array
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
211
 *
212
 * If the from_json function is not set, this will couse undefined behaviour.
213
 */
214
void
215
s_dynamic_array_deserialize_json (SDynamicArray * self, char * json);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
216
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
217
/** @} */
218
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
219
S_END_DECLS
5.2.9 by Gustav Hartvigsson
* Added license to files
220
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
221
#endif /* #define __H_DYNAMIC_ARRAY__ */