/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
61 by Gustav Hartvigsson
* Made the code more easy to read.
55
SMapItem *
56
s_map_item_new (void * key, void * value) {
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
57
  SMapItem * self = malloc (sizeof (SMapItem));
58
  self->key = key;
59
  self->value = value;
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
60
14 by Gustav Hartvigsson
* Think I am 70% done with SMap now...
61
  return self;
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
62
}
63
61 by Gustav Hartvigsson
* Made the code more easy to read.
64
void
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
65
s_map_item_free (SMapItem * self,
66
                 FreeFunc free_key,
67
                 FreeFunc free_value) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
68
  s_dbg_print ("Freeing Item!");
69
  if (free_key) {
70
    free_key (self->key);
71
  }
72
  if (free_value) {
73
    free_value (self->value);
74
  }
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
75
  free (self);
76
}
77
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
78
void
79
_internal_s_map_free_buckets (SLinkedList * self);
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
80
61 by Gustav Hartvigsson
* Made the code more easy to read.
81
SMap *
82
s_map_new (CompFunc comp_func,
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
83
           HashFunc key_hash_func,
84
           FreeFunc free_key,
85
           FreeFunc free_value) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
86
  s_dbg_print ("Freeing SMap");
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
87
  SMap * self = malloc (sizeof (SMap));
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
88
  self->len = 0;
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
89
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
90
  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?
91
14 by Gustav Hartvigsson
* Think I am 70% done with SMap now...
92
  /* free_* functions need to be checked if they are null and set the pointer
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
93
   * to free or s_base_object_free ()... Have to decide which...?
14 by Gustav Hartvigsson
* Think I am 70% done with SMap now...
94
   */
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
95
  self->free_key = free_key;
96
  self->free_value = free_value;
109.1.5 by Gustav Hartvigsson
* Getting closer to fixing the callbacks...
97
  
98
  /*
99
   * Creating a place to store the items.
100
   */
101
  self->array = s_dynamic_array_new (S_MAP_DEFAULT_NUMBER_OF_BUCKETS,
102
                                     FREEFUNC (s_map_item_free));
103
  
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
104
  /* if no func is set we have to use some other metod of
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
105
   */
106
  if (key_hash_func == NULL){
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
107
    self->key_hash_func = s_hash_object;
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
108
  } else {
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
109
    self->key_hash_func = key_hash_func;
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
110
  }
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
111
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
112
  return self;
113
}
114
61 by Gustav Hartvigsson
* Made the code more easy to read.
115
void
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
116
s_map_free (SMap * self, sboolean free_data) {
109.1.5 by Gustav Hartvigsson
* Getting closer to fixing the callbacks...
117
  
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
118
  s_map_for_each (self,
119
                  FOREACHFUNC(_s_map_internal_free_map_items_for_each),
109.1.5 by Gustav Hartvigsson
* Getting closer to fixing the callbacks...
120
                  free_data);
109.1.3 by Gustav Hartvigsson
* just commiting for the sake of commiting
121
109.1.5 by Gustav Hartvigsson
* Getting closer to fixing the callbacks...
122
  s_dynamic_array_free (self->array, FALSE);
22 by Gustav Hartvigsson
* Made code compile
123
  free (self);
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
124
}
125
61 by Gustav Hartvigsson
* Made the code more easy to read.
126
void
127
s_map_add (SMap * self, spointer key, spointer value) {
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
128
  SDynamicArray * array = self->array;
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
129
  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?
130
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
131
  /* We have to generate a key to use as an index in the DynamicArray. */
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
132
  HashFunc hash_func = self->key_hash_func;
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
133
  hash_t hash = hash_func (key);
134
  /* We need to mod it with the max size of the array. */
135
  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?
136
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
137
  if (self->len == 0) {
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
138
    /* Since we know that there are no items in the array we can skip the
139
     * check if the new place is taken. and just and an array to it.
140
     */
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
141
    s_dbg_print ("Adding bucket to bucket array");
109.1.3 by Gustav Hartvigsson
* just commiting for the sake of commiting
142
    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?
143
    s_dbg_print ("1) Appending item to linked list");
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
144
    s_dynamic_array_set (array, hash, bucket);
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
145
    s_linked_list_append (bucket, item);
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
146
  } else {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
147
    s_dbg_print ("SMap size in more than zero.");
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
148
    /* Figure out if the bucket exists. */
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
149
    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?
150
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
151
    if (bucket == NULL) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
152
      s_dbg_print ("Bucket does not exist");
109.1.3 by Gustav Hartvigsson
* just commiting for the sake of commiting
153
      bucket = s_linked_list_new (NULL);
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
154
    }
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
155
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
156
    if (_s_map_internal_find_item_in_bucket (self, bucket, key)) {
157
      s_warn_print ("Key already exists in SMap\n");
158
      return;
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
159
    }
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
160
    s_dbg_print ("2) Appending item to linked list");
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
161
    s_linked_list_append (bucket, item);
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
162
  }
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
163
  self->len++;
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
164
}
165
61 by Gustav Hartvigsson
* Made the code more easy to read.
166
spointer
167
s_map_get (SMap * self, spointer key) {
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
168
  spointer ret_val = NULL;
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
169
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
170
  SLinkedList * bucket = _s_map_internal_find_bucket (self, key);
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
171
  if (bucket) {
172
    SMapItem * item = _s_map_internal_find_item_in_bucket (self, bucket, key);
173
    if (item) {
174
      ret_val = item->value;
175
    }
176
  }
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
177
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
178
  return ret_val;
179
}
180
181
void
182
s_map_remove (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) {
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
185
    s_linked_list_head (bucket);
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
186
    SMapItem * item = _s_map_internal_find_item_in_bucket (self, bucket, key);
187
    if (item) {
188
      s_map_item_free (item, self->free_key, self->free_value);
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
189
      s_linked_list_remove_current (bucket, FALSE);
190
      self->len--;
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
191
      return;
192
    }
193
  }
194
  s_dbg_print ("Could not find item in SMap.\n");
195
}
196
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
197
void
198
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?
199
  s_dbg_print ("Running For Each on SMap");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
200
  for (size_t i = 0; i <= s_dynamic_array_last_item (self->array); i++) {
201
    SLinkedList * bucket = s_dynamic_array_get (self->array, i);
202
    if (bucket) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
203
      s_dbg_print ("Found bucket");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
204
      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?
205
      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.
206
      do {
207
        SMapItem * item = s_linked_list_get_current (bucket);
208
        foreach_func (self, item, user_data);
209
      } while (s_linked_list_next (bucket));
210
    }
211
  }
212
}
213
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
214
SLinkedList *
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
215
_s_map_internal_find_bucket (SMap * self,
216
                             spointer key) {
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 ("Looking for bucket");
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
218
  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?
219
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
220
  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?
221
  hash_t hash = hash_func (key)  % S_MAP_DEFAULT_NUMBER_OF_BUCKETS;
222
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
223
  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?
224
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
225
  return ret_val;
226
}
227
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
228
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
229
SMapItem *
230
_s_map_internal_find_item_in_bucket (SMap * self,
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
231
                                     SLinkedList * bucket,
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
232
                                     spointer key) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
233
  s_dbg_print ("Looking for item in bucket");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
234
  s_linked_list_head (bucket);
235
  do {
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
236
    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?
237
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
238
    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?
239
      s_dbg_print ("Found Item in Bucket.");
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
240
      return item;
241
    }
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
242
  } 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?
243
14 by Gustav Hartvigsson
* Think I am 70% done with SMap now...
244
  return NULL;
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
245
}
246
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
247
void
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
248
_s_map_internal_free_map_items_for_each (SMap * map,
249
                          SMapItem * item,
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
250
                          sboolean free_data) {
251
  s_dbg_print ("runnnig: _s_map_internal_free_map_items_for_each");
252
  if (free_data) {
253
    s_dbg_print ("Freeing K:V pair");
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
254
    s_map_item_free (item, map->free_key, map->free_value);
255
  } else {
256
    free (item);
257
  }
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
258
}
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
259