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; |
|
17 |
}
|
|
18 |
||
19 |
void s_map_item_free (SMapItem * self) { |
|
20 |
/* FIXME TODO: |
|
21 |
* Need to figure a method to determin if objects (keys and values) are
|
|
22 |
* BaseObjectInstance children or not and use the appropriate method for
|
|
23 |
* freeing.
|
|
24 |
*/
|
|
25 |
free (self); |
|
26 |
}
|
|
27 |
||
28 |
SMap * s_map_new ( CompFunc comp_func ) { |
|
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 |
|
|
42 |
return self; |
|
43 |
}
|
|
44 |
||
45 |
void s_map_free (SMap * self) { |
|
46 |
//TODO |
|
47 |
}
|
|
48 |
||
49 |
||
50 |
void s_map_add (SMap * self ,void * key, void * value) { |
|
51 |
SMapItem ** items = self->priv->items; |
|
52 |
SMapItem * item = s_map_item_new (key, value); |
|
53 |
if (self->priv->len == 0) { |
|
54 |
|
|
55 |
items = realloc (items, sizeof(SMapItem)); |
|
56 |
items[0] = item; |
|
57 |
self->priv->len++; |
|
58 |
} else { |
|
59 |
//TODO |
|
60 |
} |
|
61 |
}
|
|
62 |
||
63 |
void * s_map_get (SMap * self, void * key) { |
|
64 |
|
|
65 |
}
|
|
66 |
||
67 |
void method_map_deinit (SMap * self) { |
|
68 |
|
|
69 |
}
|