bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
1 |
#include "Map.h" |
2 |
#include "baseobject.h" |
|
3 |
#include <stdlib.h> |
|
4 |
||
5 |
||
6 |
struct _SMapPrivate { |
|
7 |
size_t len; // Length |
|
8 |
SMapItem * items[]; // Use calloc to deal with memory. |
|
9 |
};
|
|
10 |
||
11 |
void method_map_deinit (SMap * Self); |
|
12 |
||
13 |
SMapItem * s_map_item_new (void * key, void * value) { |
|
14 |
SMapItem * self = malloc (sizeof (SMapItem)); |
|
15 |
self->key = key; |
|
16 |
self->value = value; |
|
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
17 |
return self; |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
18 |
}
|
19 |
||
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
20 |
void s_map_item_free (SMapItem * self, MethodFunc free_key, |
21 |
MethodFunc free_value) { |
|
22 |
free_key (self->key); |
|
23 |
free_value (self->value); |
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
24 |
free (self); |
25 |
}
|
|
26 |
||
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
27 |
SMap * s_map_new ( CompFunc comp_func, MethodFunc free_key, |
28 |
MethodFunc free_value) { |
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
29 |
SMap * self = malloc (sizeof (SMap)); |
30 |
SMapClass * klass = malloc (sizeof (SMapClass)); |
|
31 |
|
|
32 |
s_base_object_set_class ((SBaseObjectInstance *) self, klass); |
|
33 |
|
|
34 |
s_base_object_set_deinit_method ((SBaseObjectInstance *) self, method_map_deinit); |
|
35 |
|
|
36 |
self->priv = malloc (sizeof (SMapPrivate)); |
|
37 |
|
|
38 |
self->priv->len = 0; |
|
39 |
|
|
40 |
klass->is_equal = comp_func; |
|
41 |
|
|
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
42 |
/* free_* functions need to be checked if they are null and set the pointer |
43 |
* to free or s_base_object_free ()... Have to decite which...
|
|
44 |
*/
|
|
45 |
klass->free_key = free_key; |
|
46 |
klass->free_value = free_value; |
|
47 |
||
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
48 |
return self; |
49 |
}
|
|
50 |
||
51 |
void s_map_free (SMap * self) { |
|
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
52 |
s_base_object_free (self); |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
53 |
}
|
54 |
||
55 |
||
56 |
void s_map_add (SMap * self ,void * key, void * value) { |
|
57 |
SMapItem ** items = self->priv->items; |
|
58 |
SMapItem * item = s_map_item_new (key, value); |
|
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
59 |
size_t * len = self->priv->len; |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
60 |
if (self->priv->len == 0) { |
61 |
items = realloc (items, sizeof(SMapItem)); |
|
62 |
items[0] = item; |
|
63 |
self->priv->len++; |
|
64 |
} else { |
|
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
65 |
items = reallec (items, sizeof(SMapItem) * (self->priv->len + 1)); |
66 |
items[len-1] = item; |
|
67 |
self->priv->len++; |
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
68 |
} |
69 |
}
|
|
70 |
||
71 |
void * s_map_get (SMap * self, void * key) { |
|
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
72 |
SMapItem ** items = self->priv->items; |
73 |
size_t * len = self->priv->len; |
|
74 |
SMapClass * klass = (SMapClass *) s_base_object_get_class ((SBaseObjectInstance *) self); |
|
75 |
CompFunc comp_func = klass->comp_func; |
|
76 |
for (int i == 0; i < self->priv->len; i++) { |
|
77 |
bool is_item = comp_func (key, items[i]); |
|
78 |
if (is_item) { |
|
79 |
return items[i]; |
|
80 |
} |
|
81 |
} |
|
82 |
return NULL; |
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
83 |
}
|
84 |
||
|
15
by Gustav Hartvigsson
* Added some notes... bah |
85 |
void s_map_remove (SMap * self, void * key) { |
86 |
||
87 |
}
|
|
88 |
||
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
89 |
void method_map_deinit (SMap * self) { |
90 |
|
|
91 |
}
|