/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),
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
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
    }
141 by Gustav Hartvigsson
* Made SMap overwrite the data when the item already exists in the map. This makes more sense
159
    
160
    SMapItem * tmp_item = _s_map_internal_find_item_in_bucket (self, bucket,
161
                                                              key);
162
    
163
    if (tmp_item) {
164
      s_dbg_print ("Key already exists in SMap... replacing it.\n");
165
      /* Since we know that the buckets current item points to the item (it
166
       * returns when it has found the item.) We can just remove it.
167
       */
168
      s_linked_list_remove_current (bucket, TRUE);
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
169
    }
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
170
    s_dbg_print ("2) Appending item to linked list");
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
171
    s_linked_list_append (bucket, item);
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
172
  }
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
173
  self->len++;
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
174
}
175
61 by Gustav Hartvigsson
* Made the code more easy to read.
176
spointer
177
s_map_get (SMap * self, spointer key) {
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
178
  return s_map_get_item(self, key)->value;
179
}
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
180
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
181
SMapItem *
182
s_map_get_item (SMap * self, spointer key) {
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
183
  SLinkedList * bucket = _s_map_internal_find_bucket (self, key);
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
184
  if (bucket) {
185
    SMapItem * item = _s_map_internal_find_item_in_bucket (self, bucket, key);
186
    if (item) {
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
187
      return item;
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
188
    }
189
  }
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
190
  return NULL;
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
191
}
192
193
void
194
s_map_remove (SMap * self, spointer key) {
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
195
  SLinkedList * bucket = _s_map_internal_find_bucket (self, key);
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
196
  if (bucket) {
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
197
    s_linked_list_head (bucket);
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
198
    SMapItem * item = _s_map_internal_find_item_in_bucket (self, bucket, key);
199
    if (item) {
200
      s_map_item_free (item, self->free_key, self->free_value);
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
201
      s_linked_list_remove_current (bucket, FALSE);
202
      self->len--;
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
203
      return;
204
    }
205
  }
206
  s_dbg_print ("Could not find item in SMap.\n");
207
}
208
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
209
void
210
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?
211
  s_dbg_print ("Running For Each on SMap");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
212
  for (size_t i = 0; i <= s_dynamic_array_last_item (self->array); i++) {
213
    SLinkedList * bucket = s_dynamic_array_get (self->array, i);
214
    if (bucket) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
215
      s_dbg_print ("Found bucket");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
216
      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?
217
      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.
218
      do {
219
        SMapItem * item = s_linked_list_get_current (bucket);
220
        foreach_func (self, item, user_data);
221
      } while (s_linked_list_next (bucket));
222
    }
223
  }
224
}
225
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
226
SLinkedList *
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
227
_s_map_internal_find_bucket (SMap * self,
228
                             spointer key) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
229
  s_dbg_print ("Looking for bucket");
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
230
  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?
231
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
232
  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?
233
  hash_t hash = hash_func (key)  % S_MAP_DEFAULT_NUMBER_OF_BUCKETS;
234
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
235
  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?
236
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
237
  return ret_val;
238
}
239
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
240
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
241
SMapItem *
242
_s_map_internal_find_item_in_bucket (SMap * self,
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
243
                                     SLinkedList * bucket,
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
244
                                     spointer 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 ("Looking for item in bucket");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
246
  s_linked_list_head (bucket);
247
  do {
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
248
    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?
249
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
250
    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?
251
      s_dbg_print ("Found Item in Bucket.");
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
252
      return item;
253
    }
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
254
  } 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?
255
14 by Gustav Hartvigsson
* Think I am 70% done with SMap now...
256
  return NULL;
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
257
}
258
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
259
void
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
260
_s_map_internal_free_map_items_for_each (SMap * map,
261
                          SMapItem * item,
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
262
                          sboolean free_data) {
263
  s_dbg_print ("runnnig: _s_map_internal_free_map_items_for_each");
264
  if (free_data) {
265
    s_dbg_print ("Freeing K:V pair");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
266
    s_map_item_free (item, map->free_key, map->free_value);
267
  } else {
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
268
    s_free (item);
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
269
  }
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
270
}
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
271
109.1.7 by Gustav Hartvigsson
* Fixed SMap, for the time being...
272
void
273
_internal_s_map_free_buckets (SLinkedList * bucket) {
274
  s_linked_list_free (bucket, FALSE);
275
}
276