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