/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
Copyright (c) 2013-2014 Gustav Hartvigsson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "Map.h"
#include "LinkedList.h"
#include <stdlib.h>

struct
SMap {
  size_t len; // Length
  SDynamicArray * array; /**< This is what holds the buckets. These buckets are
                          * in turn also SDynamicArrays; or in template speak:
                          * SDynamicArray<SLinkedList<SMapItem*>*>*
                          */
  
  CompFunc is_equal; /**< method to check if items are equal. */
  HashFunc key_hash_func;
  FreeFunc free_key;
  FreeFunc free_value;
};

#if 0
void
_s_map_internal_free_array_for_each (SDynamicArray * obj,
                                     SLinkedList * item,
                                     SMap * self);

void
_s_map_internal_free_bucket_for_each (SDynamicArray * obj,
                                      SMapItem * item,
                                      SMap * self);
#endif

void
_s_map_internal_free_map_items_for_each (SMap * self,
                                         SMapItem * item,
                                         sboolean * free_data);

SLinkedList *
_s_map_internal_find_bucket (SMap * self,
                             spointer key);

SMapItem *
_s_map_internal_find_item_in_bucket (SMap * self,
                                     SLinkedList * bucket,
                                     spointer key);

SMapItem *
s_map_item_new (void * key, void * value) {
  SMapItem * self = malloc (sizeof (SMapItem));
  self->key = key;
  self->value = value;
  
  return self;
}

void
s_map_item_free (SMapItem * self,
                 FreeFunc free_key,
                 FreeFunc free_value) {
  free_key (self->key);
  free_value (self->value);
  free (self);
}



SMap *
s_map_new (CompFunc comp_func,
           HashFunc key_hash_func,
           FreeFunc free_key,
           FreeFunc free_value) {
  SMap * self = malloc (sizeof (SMap));
  self->len = 0;
  
  self->is_equal = comp_func;
  
  /* free_* functions need to be checked if they are null and set the pointer
   * to free or s_base_object_free ()... Have to decide which...?
   */
  if (free_key) {
    self->free_key = free_key;
  } else {
    self->free_key = FREEFUNC(free);
  }
  if (free_value) {
    self->free_value = free_value;
  } else {
    self->free_value = FREEFUNC(free);
  }
  
  
  /* if no func is set we have to use some other metod of 
   */
  if (key_hash_func == NULL){
    self->key_hash_func = s_hash_object;
  } else {
    self->key_hash_func = key_hash_func;
  }
  
  self->array = s_dynamic_array_new (S_MAP_DEFAULT_NUMBER_OF_BUCKETS,
                                     FREEFUNC(s_linked_list_free));
  
  return self;
}

void
s_map_free (SMap * self, sboolean free_data) {
  s_map_for_each (self,
                  FOREACHFUNC(_s_map_internal_free_map_items_for_each),
                  &free_data);
  s_dynamic_array_free (self->array, TRUE);
  free (self);
}

void
s_map_add (SMap * self, spointer key, spointer value) {
  SDynamicArray * array = self->array;
  SMapItem * item = s_map_item_new (key, value);
  
  /* We have to generate a key to use as an index in the DynamicArray. */
  HashFunc hash_func = self->key_hash_func;
  hash_t hash = hash_func (key);
  /* We need to mod it with the max size of the array. */
  hash = hash % S_MAP_DEFAULT_NUMBER_OF_BUCKETS;
  
  if (self->len == 0) {
    /* Since we know that there are no items in the array we can skip the
     * check if the new place is taken. and just and an array to it.
     */
    SLinkedList * bucket = s_linked_list_new (NULL);
    s_dynamic_array_set (array, hash, bucket);
    s_linked_list_append (bucket, item);
    
  } else {
    /* Figure out if the bucket exists. */
    SLinkedList * bucket = _s_map_internal_find_bucket (self, key);
    
    if (bucket == NULL) {
      bucket = s_linked_list_new (NULL);
    }
    
    if (_s_map_internal_find_item_in_bucket (self, bucket, key)) {
      s_warn_print ("Key already exists in SMap\n");
      return;
    }
    s_linked_list_append (bucket, item);
  }
  self->len++;
}

spointer
s_map_get (SMap * self, spointer key) {
  spointer ret_val = NULL;
  
  SLinkedList * bucket = _s_map_internal_find_bucket (self, key);
  if (bucket) {
    SMapItem * item = _s_map_internal_find_item_in_bucket (self, bucket, key);
    if (item) {
      ret_val = item->value;
    }
  }
  
  return ret_val;
}

void
s_map_remove (SMap * self, spointer key) {
  SLinkedList * bucket = _s_map_internal_find_bucket (self, key);
  if (bucket) {
    s_linked_list_head (bucket);
    SMapItem * item = _s_map_internal_find_item_in_bucket (self, bucket, key);
    if (item) {
      s_map_item_free (item, self->free_key, self->free_value);
      s_linked_list_remove_current (bucket, FALSE);
      self->len--;
      return;
    }
  }
  s_dbg_print ("Could not find item in SMap.\n");
}

void
s_map_for_each (SMap * self, ForEachFunc foreach_func, spointer user_data) {
  for (size_t i = 0; i <= s_dynamic_array_last_item (self->array); i++) {
    SLinkedList * bucket = s_dynamic_array_get (self->array, i);
    if (bucket) {
      s_linked_list_head (bucket);
      do {
        SMapItem * item = s_linked_list_get_current (bucket);
        foreach_func (self, item, user_data);
      } while (s_linked_list_next (bucket));
    }
  }
}

SLinkedList *
_s_map_internal_find_bucket (SMap * self,
                             spointer key) {
  SLinkedList * ret_val = NULL;
  
  HashFunc hash_func = self->key_hash_func;
  hash_t hash = hash_func (key);
  
  ret_val = s_dynamic_array_get (self->array, hash);
  
  return ret_val;
}


SMapItem *
_s_map_internal_find_item_in_bucket (SMap * self,
                                     SLinkedList * bucket,
                                     spointer key) {
  s_linked_list_head (bucket);
  do {
    SMapItem * item = SMAPITEM (s_linked_list_get_current (bucket));
    if (item && self->is_equal (item->key, key)) {
      return item;
    }
  } while (s_linked_list_next (bucket));
  
  return NULL;
}

#if 0
void
_s_map_internal_free_array_for_each (SDynamicArray * obj,
                                     SLinkedList * item,
                                     SMap * self) {
  s_linked_list_for_each (item,
                          FOREACHFUNC(_s_map_internal_free_bucket_for_each),
                          self);
  s_linked_list_free (item, FALSE);
}
#endif

void
_s_map_internal_free_map_items_for_each (SMap * map,
                          SMapItem * item,
                          sboolean * free_data) {
  if ((*free_data)) {
    s_map_item_free (item, map->free_key, map->free_value);
  } else {
    free (item);
  }
}