/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
5.2.7 by Gustav Hartvigsson
* Switched licence to a more permisive one.
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
*/
22
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
23
#include "Map.h"
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
24
#include "LinkedList.h"
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
25
#include <stdlib.h>
26
61 by Gustav Hartvigsson
* Made the code more easy to read.
27
struct
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
28
SMap {
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
29
  size_t len; // Length
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
30
  SDynamicArray * array; /*  This is what holds the buckets. These buckets are
52 by Gustav Hartvigsson
* Reorderd CMakeLists.txt list of files
31
                          * in turn also SDynamicArrays; or in template speak:
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
32
                          * SDynamicArray<SLinkedList<SMapItem*>*>*
52 by Gustav Hartvigsson
* Reorderd CMakeLists.txt list of files
33
                          */
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
34
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
35
  CompFunc is_equal; /*  method to check if items are equal. */
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
36
  HashFunc key_hash_func;
37
  FreeFunc free_key;
38
  FreeFunc free_value;
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
39
};
40
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
41
void
42
_s_map_internal_free_map_items_for_each (SMap * self,
43
                                         SMapItem * item,
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
44
                                         sboolean free_data);
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
45
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
46
SLinkedList *
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
47
_s_map_internal_find_bucket (SMap * self,
48
                             spointer key);
49
50
SMapItem *
51
_s_map_internal_find_item_in_bucket (SMap * self,
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
52
                                     SLinkedList * bucket,
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
53
                                     spointer key);
54
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
55
void
56
_internal_s_map_free_buckets (SLinkedList * bucket);
57
61 by Gustav Hartvigsson
* Made the code more easy to read.
58
SMapItem *
59
s_map_item_new (void * key, void * value) {
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
60
  SMapItem * self = s_malloc (sizeof (SMapItem));
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
61
  self->key = key;
62
  self->value = value;
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
63
14 by Gustav Hartvigsson
* Think I am 70% done with SMap now...
64
  return self;
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
65
}
66
61 by Gustav Hartvigsson
* Made the code more easy to read.
67
void
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
68
s_map_item_free (SMapItem * self,
69
                 FreeFunc free_key,
70
                 FreeFunc free_value) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
71
  s_dbg_print ("Freeing Item!");
72
  if (free_key) {
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
73
    s_dbg_print ("Freeing Key");
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
74
    free_key (self->key);
75
  }
76
  if (free_value) {
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
77
    s_dbg_print ("Freeing Value");
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
78
    free_value (self->value);
79
  }
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
80
  s_free (self);
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
81
}
82
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
83
61 by Gustav Hartvigsson
* Made the code more easy to read.
84
SMap *
85
s_map_new (CompFunc comp_func,
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
86
           HashFunc key_hash_func,
87
           FreeFunc free_key,
88
           FreeFunc free_value) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
89
  s_dbg_print ("Freeing SMap");
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
90
  SMap * self = s_malloc (sizeof (SMap));
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
91
  self->len = 0;
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
92
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
93
  self->is_equal = comp_func;
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
94
14 by Gustav Hartvigsson
* Think I am 70% done with SMap now...
95
  /* free_* functions need to be checked if they are null and set the pointer
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
96
   * to free or s_base_object_free ()... Have to decide which...?
14 by Gustav Hartvigsson
* Think I am 70% done with SMap now...
97
   */
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
98
  self->free_key = free_key;
99
  self->free_value = free_value;
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
100
109.1.5 by Gustav Hartvigsson
* Getting closer to fixing the callbacks...
101
  /*
102
   * Creating a place to store the items.
103
   */
104
  self->array = s_dynamic_array_new (S_MAP_DEFAULT_NUMBER_OF_BUCKETS,
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
105
                                     FREEFUNC(_internal_s_map_free_buckets));
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
106
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
107
  /* if no func is set we have to use some other metod of
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
108
   */
109
  if (key_hash_func == NULL){
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
110
    self->key_hash_func = s_hash_object;
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
111
  } else {
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
112
    self->key_hash_func = key_hash_func;
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
113
  }
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
114
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
115
  return self;
116
}
117
61 by Gustav Hartvigsson
* Made the code more easy to read.
118
void
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
119
s_map_free (SMap * self, sboolean free_data) {
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
120
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
121
  s_map_for_each (self,
122
                  FOREACHFUNC(_s_map_internal_free_map_items_for_each),
109.1.5 by Gustav Hartvigsson
* Getting closer to fixing the callbacks...
123
                  free_data);
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
124
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
125
  s_dynamic_array_free (self->array, TRUE);
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
126
  s_free (self);
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
127
}
128
61 by Gustav Hartvigsson
* Made the code more easy to read.
129
void
130
s_map_add (SMap * self, spointer key, spointer value) {
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
131
  SDynamicArray * array = self->array;
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
132
  SMapItem * item = s_map_item_new (key, value);
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
133
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
134
  /* We have to generate a key to use as an index in the DynamicArray. */
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
135
  HashFunc hash_func = self->key_hash_func;
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
136
  hash_t hash = hash_func (key);
137
  /* We need to mod it with the max size of the array. */
138
  hash = hash % S_MAP_DEFAULT_NUMBER_OF_BUCKETS;
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
139
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
140
  if (self->len == 0) {
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
141
    /* Since we know that there are no items in the array we can skip the
142
     * check if the new place is taken. and just and an array to it.
143
     */
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
144
    s_dbg_print ("Adding bucket to bucket array");
109.1.3 by Gustav Hartvigsson
* just commiting for the sake of commiting
145
    SLinkedList * bucket = s_linked_list_new (NULL);
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
146
    s_dbg_print ("1) Appending item to linked list");
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
147
    s_dynamic_array_set (array, hash, bucket);
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
148
    s_linked_list_append (bucket, item);
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
149
  } else {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
150
    s_dbg_print ("SMap size in more than zero.");
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
151
    /* Figure out if the bucket exists. */
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
152
    SLinkedList * bucket = _s_map_internal_find_bucket (self, key);
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
153
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
154
    if (bucket == NULL) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
155
      s_dbg_print ("Bucket does not exist");
109.1.3 by Gustav Hartvigsson
* just commiting for the sake of commiting
156
      bucket = s_linked_list_new (NULL);
109.1.8 by Gustav Hartvigsson
* Fixed SMap so the callbacks will work... What a single line of code can do..
157
      s_dynamic_array_set (array, hash, bucket);
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
158
    }
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
159
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
160
    if (_s_map_internal_find_item_in_bucket (self, bucket, key)) {
161
      s_warn_print ("Key already exists in SMap\n");
162
      return;
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
163
    }
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
164
    s_dbg_print ("2) Appending item to linked list");
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
165
    s_linked_list_append (bucket, item);
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
166
  }
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
167
  self->len++;
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
168
}
169
61 by Gustav Hartvigsson
* Made the code more easy to read.
170
spointer
171
s_map_get (SMap * self, spointer key) {
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
172
  return s_map_get_item(self, key)->value;
173
}
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
174
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
175
SMapItem *
176
s_map_get_item (SMap * self, spointer key) {
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
177
  SLinkedList * bucket = _s_map_internal_find_bucket (self, key);
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
178
  if (bucket) {
179
    SMapItem * item = _s_map_internal_find_item_in_bucket (self, bucket, key);
180
    if (item) {
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
181
      return item;
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
182
    }
183
  }
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
184
  return NULL;
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
185
}
186
187
void
188
s_map_remove (SMap * self, spointer key) {
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
189
  SLinkedList * bucket = _s_map_internal_find_bucket (self, key);
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
190
  if (bucket) {
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
191
    s_linked_list_head (bucket);
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
192
    SMapItem * item = _s_map_internal_find_item_in_bucket (self, bucket, key);
193
    if (item) {
194
      s_map_item_free (item, self->free_key, self->free_value);
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
195
      s_linked_list_remove_current (bucket, FALSE);
196
      self->len--;
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
197
      return;
198
    }
199
  }
200
  s_dbg_print ("Could not find item in SMap.\n");
201
}
202
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
203
void
204
s_map_for_each (SMap * self, ForEachFunc foreach_func, spointer user_data) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
205
  s_dbg_print ("Running For Each on SMap");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
206
  for (size_t i = 0; i <= s_dynamic_array_last_item (self->array); i++) {
207
    SLinkedList * bucket = s_dynamic_array_get (self->array, i);
208
    if (bucket) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
209
      s_dbg_print ("Found bucket");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
210
      s_linked_list_head (bucket);
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
211
      s_dbg_print ("No of items in list: %zd.", s_linked_list_len (bucket));
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
212
      do {
213
        SMapItem * item = s_linked_list_get_current (bucket);
214
        foreach_func (self, item, user_data);
215
      } while (s_linked_list_next (bucket));
216
    }
217
  }
218
}
219
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
220
SLinkedList *
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
221
_s_map_internal_find_bucket (SMap * self,
222
                             spointer key) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
223
  s_dbg_print ("Looking for bucket");
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
224
  SLinkedList * ret_val = NULL;
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
225
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
226
  HashFunc hash_func = self->key_hash_func;
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
227
  hash_t hash = hash_func (key)  % S_MAP_DEFAULT_NUMBER_OF_BUCKETS;
228
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
229
  ret_val = s_dynamic_array_get (self->array, hash);
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
230
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
231
  return ret_val;
232
}
233
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
234
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
235
SMapItem *
236
_s_map_internal_find_item_in_bucket (SMap * self,
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
237
                                     SLinkedList * bucket,
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
238
                                     spointer key) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
239
  s_dbg_print ("Looking for item in bucket");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
240
  s_linked_list_head (bucket);
241
  do {
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
242
    SMapItem * item = SMAPITEM (s_linked_list_get_current (bucket));
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
243
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
244
    if (item && self->is_equal (item->key, key)) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
245
      s_dbg_print ("Found Item in Bucket.");
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
246
      return item;
247
    }
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
248
  } while (s_linked_list_next (bucket));
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
249
14 by Gustav Hartvigsson
* Think I am 70% done with SMap now...
250
  return NULL;
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
251
}
252
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
253
void
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
254
_s_map_internal_free_map_items_for_each (SMap * map,
255
                          SMapItem * item,
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
256
                          sboolean free_data) {
257
  s_dbg_print ("runnnig: _s_map_internal_free_map_items_for_each");
258
  if (free_data) {
259
    s_dbg_print ("Freeing K:V pair");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
260
    s_map_item_free (item, map->free_key, map->free_value);
261
  } else {
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
262
    s_free (item);
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
263
  }
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
264
}
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
265
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
266
void
267
_internal_s_map_free_buckets (SLinkedList * bucket) {
268
  s_linked_list_free (bucket, FALSE);
269
}
270