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