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