bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
1 |
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
|
2 |
* vi: set shiftwidth=2 tabstop=2 expandtab:
|
|
3 |
* :indentSize=2:tabSize=2:noTabs=true:
|
|
4 |
*/
|
|
5 |
||
6 |
/*
|
|
5.2.9
by Gustav Hartvigsson
* Added license to files |
7 |
Copyright (c) 2013-2014 Gustav Hartvigsson
|
8 |
||
9 |
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10 |
of this software and associated documentation files (the "Software"), to deal
|
|
11 |
in the Software without restriction, including without limitation the rights
|
|
12 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13 |
copies of the Software, and to permit persons to whom the Software is
|
|
14 |
furnished to do so, subject to the following conditions:
|
|
15 |
||
16 |
The above copyright notice and this permission notice shall be included in
|
|
17 |
all copies or substantial portions of the Software.
|
|
18 |
||
19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
25 |
THE SOFTWARE.
|
|
26 |
*/
|
|
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
27 |
|
30
by Gustav Hartvigsson
* Made the code compile using CMake. |
28 |
#include "DynamicArray.h" |
29 |
||
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
30 |
struct
|
31 |
SDynamicArray { |
|
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
32 |
FreeFunc free_func; |
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
33 |
FuncPointer to_json; |
34 |
FuncPointer from_json; |
|
35 |
size_t max_size; |
|
36 |
size_t last_item; |
|
30
by Gustav Hartvigsson
* Made the code compile using CMake. |
37 |
spointer * array; |
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
38 |
};
|
39 |
||
61
by Gustav Hartvigsson
* Made the code more easy to read. |
40 |
void
|
41 |
_private_for_each_item_free (SDynamicArray * self, spointer item, |
|
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
42 |
spointer data); |
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
43 |
|
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
44 |
SDynamicArray * |
109.1.5
by Gustav Hartvigsson
* Getting closer to fixing the callbacks... |
45 |
s_dynamic_array_new_full (size_t len, |
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
46 |
FreeFunc free_func, |
47 |
FuncPointer to_json, |
|
48 |
FuncPointer from_json) { |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
49 |
SDynamicArray * self = s_malloc (sizeof(SDynamicArray)); |
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
50 |
|
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
51 |
self->max_size = len; |
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
52 |
|
48
by Gustav Hartvigsson
* Finnished SLinkedList. |
53 |
self->last_item = 0; |
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
54 |
|
55 |
|
|
108
by Gustav Hartvigsson
libssts: |
56 |
if (free_func == NULL) { |
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
57 |
self->free_func = s_free; |
108
by Gustav Hartvigsson
libssts: |
58 |
} else { |
59 |
self->free_func = free_func; |
|
60 |
} |
|
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
61 |
self->to_json = to_json; |
62 |
self->from_json = from_json; |
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
63 |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
64 |
self->array = s_calloc (len ,sizeof (spointer)); |
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
65 |
|
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
66 |
return self; |
67 |
}
|
|
68 |
||
69 |
SDynamicArray * |
|
70 |
s_dynamic_array_new (size_t len, |
|
71 |
FreeFunc free_func) { |
|
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
72 |
|
109.1.5
by Gustav Hartvigsson
* Getting closer to fixing the callbacks... |
73 |
SDynamicArray * self = s_dynamic_array_new_full (len, free_func, NULL, NULL); |
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
74 |
|
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
75 |
return self; |
76 |
}
|
|
77 |
||
78 |
SDynamicArray * |
|
79 |
s_dynamic_array_new_json (size_t len, |
|
80 |
FreeFunc free_func, |
|
81 |
FuncPointer to_json, |
|
82 |
FuncPointer from_json) { |
|
109.1.5
by Gustav Hartvigsson
* Getting closer to fixing the callbacks... |
83 |
SDynamicArray * self = s_dynamic_array_new_full (len, free_func, to_json, from_json); |
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
84 |
return self; |
85 |
}
|
|
86 |
||
61
by Gustav Hartvigsson
* Made the code more easy to read. |
87 |
void
|
88 |
s_dynamic_array_free (SDynamicArray * self, sboolean free_data) { |
|
48
by Gustav Hartvigsson
* Finnished SLinkedList. |
89 |
if (free_data) { |
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
90 |
if (self->free_func != NULL) { |
109.1.1
by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList? |
91 |
s_dbg_print ("SDynamicArray: free data."); |
107
by Gustav Hartvigsson
* Removed depricaded functions from SObject code. |
92 |
s_dynamic_array_for_each (self, |
93 |
FOREACHFUNC (_private_for_each_item_free), |
|
94 |
NULL); |
|
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
95 |
} |
96 |
} |
|
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
97 |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
98 |
s_free (self->array); |
99 |
s_free (self); |
|
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
100 |
}
|
101 |
||
61
by Gustav Hartvigsson
* Made the code more easy to read. |
102 |
spointer
|
103 |
s_dynamic_array_get (SDynamicArray * self, size_t index) { |
|
109.1.1
by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList? |
104 |
if (index > self->max_size) { |
105 |
s_err_print ("Index out of range:\n\tMax index: %zd requested index:%zd", |
|
106 |
self->max_size, index); |
|
107 |
return NULL; |
|
108 |
} |
|
109 |
if (self->max_size < 1) { |
|
110 |
return NULL; |
|
111 |
} |
|
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
112 |
return self->array[index]; |
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
113 |
}
|
114 |
||
61
by Gustav Hartvigsson
* Made the code more easy to read. |
115 |
void
|
116 |
s_dynamic_array_set (SDynamicArray * self, size_t index, spointer item) { |
|
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
117 |
if (self->max_size <= index) { |
118 |
size_t new_size = round_up (index + ARRAY_PADDING, ARRAY_PADDING) + 1; |
|
48
by Gustav Hartvigsson
* Finnished SLinkedList. |
119 |
s_dbg_print ("Index: %zu current array size: %zu new array size: %zu ", |
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
120 |
index, self->max_size, new_size); |
121 |
|
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
122 |
size_t old_size = self->max_size; |
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
123 |
|
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
124 |
self->max_size = new_size; |
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
125 |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
126 |
self->array = s_realloc (self->array, |
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
127 |
(sizeof (spointer) * (self->max_size))); |
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
128 |
/* Zero new momery */ |
108
by Gustav Hartvigsson
libssts: |
129 |
for (size_t i = old_size; i < new_size; i++) { |
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
130 |
self->array[i] = NULL; |
131 |
} |
|
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
132 |
} |
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
133 |
|
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
134 |
if (self->last_item < index) { |
135 |
self->last_item = index; |
|
136 |
} |
|
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
137 |
|
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
138 |
self->array[index] = item; |
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
139 |
}
|
140 |
||
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
141 |
void
|
142 |
s_dynamic_array_append (SDynamicArray * self, |
|
143 |
spointer item) { |
|
144 |
size_t next_pos = s_dynamic_array_last_item (self) + 1; |
|
145 |
s_dynamic_array_set (self, next_pos, item); |
|
146 |
}
|
|
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 |
return self->max_size; |
151 |
}
|
|
152 |
||
61
by Gustav Hartvigsson
* Made the code more easy to read. |
153 |
size_t
|
154 |
s_dynamic_array_last_item (SDynamicArray * self) { |
|
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
155 |
return self->last_item; |
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
156 |
}
|
157 |
||
61
by Gustav Hartvigsson
* Made the code more easy to read. |
158 |
spointer * |
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
159 |
s_dynamic_array_dump_array (SDynamicArray * self, |
160 |
size_t * out_size) { |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
161 |
spointer * ret_val = s_calloc (self->last_item + 1, sizeof (* self->array)); |
150
by Gustav Hartvigsson
* Fixed the tests in the CMake file |
162 |
|
163 |
for (size_t i = 0; i <= self->last_item; i++) { |
|
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
164 |
ret_val[i] = self->array[i]; |
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
165 |
} |
150
by Gustav Hartvigsson
* Fixed the tests in the CMake file |
166 |
*out_size = self->last_item + 1; |
167 |
ret_val[*out_size] = NULL; |
|
168 |
ret_val = s_realloc (ret_val, *out_size * sizeof (* self->array)); |
|
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
169 |
return ret_val; |
170 |
}
|
|
171 |
||
61
by Gustav Hartvigsson
* Made the code more easy to read. |
172 |
void
|
162
by Gustav Hartvigsson
* stuff to do with the unfinished base32 implementation. |
173 |
s_dynamic_array_for_each (SDynamicArray * self, |
174 |
ForEachFunc func, |
|
175 |
spointer data) { |
|
109.1.1
by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList? |
176 |
s_dbg_print ("Size: %zd", self->max_size); |
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
177 |
for (int i = 0; i < self->max_size && i <= self->last_item; i++) { |
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
178 |
spointer item = self->array[i]; |
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
179 |
if (item) { |
109.1.1
by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList? |
180 |
s_dbg_print ("Item Found."); |
47
by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h |
181 |
func (self, item, data); |
182 |
} |
|
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
183 |
} |
184 |
}
|
|
185 |
||
186 |
||
61
by Gustav Hartvigsson
* Made the code more easy to read. |
187 |
void
|
188 |
_private_for_each_item_free (SDynamicArray * self, spointer item, |
|
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
189 |
spointer data) { |
104
by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function... |
190 |
s_dbg_print ("Removing item from array"); |
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
191 |
FreeFunc func = self->free_func; |
109.1.7
by Gustav Hartvigsson
* Fixed SMap, for the time being... |
192 |
if (func) { |
193 |
func (item); |
|
194 |
} |
|
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
195 |
}
|
196 |