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" |
|
44
by Gustav Hartvigsson
* Started to structuce the dectumentation a little better. |
24 |
#include "baseobject.h" |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
25 |
#include <stdlib.h> |
26 |
||
|
53
by Gustav Hartvigsson
* Finnished up s_map_add () ??? |
27 |
struct SMapPrivate { |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
28 |
size_t len; // Length |
|
52
by Gustav Hartvigsson
* Reorderd CMakeLists.txt list of files |
29 |
SDynamicArray * array; /* This is what holds the buckets. These buckets are |
30 |
* in turn also SDynamicArrays; or in template speak:
|
|
31 |
* SDynamicArray<SDynamicArray<SMapItem*>*>*
|
|
32 |
*/
|
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
33 |
};
|
34 |
||
35 |
SMapItem * s_map_item_new (void * key, void * value) { |
|
36 |
SMapItem * self = malloc (sizeof (SMapItem)); |
|
37 |
self->key = key; |
|
38 |
self->value = value; |
|
|
52
by Gustav Hartvigsson
* Reorderd CMakeLists.txt list of files |
39 |
|
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
40 |
return self; |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
41 |
}
|
42 |
||
|
22
by Gustav Hartvigsson
* Made code compile |
43 |
void s_map_item_free (SMapItem * self, FuncPointer free_key, |
44 |
FuncPointer free_value) { |
|
45 |
FREEFUNC(free_key) (self->key); |
|
46 |
FREEFUNC(free_value) (self->value); |
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
47 |
free (self); |
48 |
}
|
|
49 |
||
|
53
by Gustav Hartvigsson
* Finnished up s_map_add () ??? |
50 |
|
51 |
||
52 |
SMap * s_map_new (CompFunc comp_func, |
|
53 |
HashFunc key_hash_func, |
|
54 |
FuncPointer free_key, |
|
55 |
FuncPointer free_value) { |
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
56 |
SMap * self = malloc (sizeof (SMap)); |
57 |
SMapClass * klass = malloc (sizeof (SMapClass)); |
|
58 |
|
|
59 |
self->priv = malloc (sizeof (SMapPrivate)); |
|
60 |
self->priv->len = 0; |
|
61 |
|
|
62 |
klass->is_equal = comp_func; |
|
63 |
|
|
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
64 |
/* free_* functions need to be checked if they are null and set the pointer |
|
53
by Gustav Hartvigsson
* Finnished up s_map_add () ??? |
65 |
* to free or s_base_object_free ()... Have to decide which...?
|
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
66 |
*/
|
67 |
klass->free_key = free_key; |
|
68 |
klass->free_value = free_value; |
|
|
44
by Gustav Hartvigsson
* Started to structuce the dectumentation a little better. |
69 |
|
|
53
by Gustav Hartvigsson
* Finnished up s_map_add () ??? |
70 |
/* if no func is set we have to use some other metod of |
71 |
*/
|
|
72 |
if (key_hash_func == NULL){ |
|
73 |
klass->key_hash_func = s_hash_object; |
|
74 |
} else { |
|
75 |
klass->key_hash_func = key_hash_func; |
|
76 |
} |
|
77 |
|
|
78 |
/* We do our own freeing of objects. */ |
|
79 |
self->priv->array = s_dynamic_array_new (S_MAP_DEFAULT_NUMBER_OF_BUCKETS, |
|
80 |
NULL); |
|
81 |
|
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
82 |
return self; |
83 |
}
|
|
84 |
||
85 |
void s_map_free (SMap * self) { |
|
|
22
by Gustav Hartvigsson
* Made code compile |
86 |
|
87 |
|
|
88 |
free (self->priv); |
|
89 |
free (self->klass); |
|
90 |
free (self); |
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
91 |
}
|
92 |
||
93 |
||
|
53
by Gustav Hartvigsson
* Finnished up s_map_add () ??? |
94 |
void s_map_add (SMap * self, spointer key, spointer value) { |
95 |
SDynamicArray * array = self->priv->array; |
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
96 |
SMapItem * item = s_map_item_new (key, value); |
|
53
by Gustav Hartvigsson
* Finnished up s_map_add () ??? |
97 |
|
98 |
/* We have to generate a key to use as an index in the DynamicArray. */ |
|
99 |
HashFunc hash_func = self->klass->key_hash_func; |
|
100 |
hash_t hash = hash_func (key); |
|
101 |
/* We need to mod it with the max size of the array. */ |
|
102 |
hash = hash % S_MAP_DEFAULT_NUMBER_OF_BUCKETS; |
|
103 |
|
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
104 |
if (self->priv->len == 0) { |
|
53
by Gustav Hartvigsson
* Finnished up s_map_add () ??? |
105 |
/* Since we know that there are no items in the array we can skip the |
106 |
* check if the new place is taken. and just and an array to it.
|
|
107 |
*/
|
|
108 |
SDynamicArray * bucket = s_dynamic_array_new (8, NULL); |
|
109 |
s_dynamic_array_set (array, hash, bucket); |
|
110 |
s_dynamic_array_set (bucket, 0, item); |
|
111 |
|
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
112 |
} else { |
|
53
by Gustav Hartvigsson
* Finnished up s_map_add () ??? |
113 |
/* Figure out if the bucket exists. */ |
114 |
SDynamicArray * bucket = NULL; |
|
115 |
if (s_dynamic_array_get (array, hash) == NULL) { |
|
116 |
bucket = s_dynamic_array_new (8, NULL); |
|
117 |
} else { |
|
118 |
/* We get the bucket */ |
|
119 |
bucket = S_DYNAMIC_ARRAY(s_dynamic_array_get (array, hash)); |
|
120 |
} |
|
121 |
size_t bucket_len = s_dynamic_array_last_item (bucket); |
|
122 |
s_dynamic_array_set (bucket, bucket_len, item); |
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
123 |
} |
124 |
}
|
|
125 |
||
|
54
by Gustav Hartvigsson
void * -> spointer |
126 |
spointer s_map_get (SMap * self, spointer key) { |
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
127 |
return NULL; |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
128 |
}
|
129 |
||
|
54
by Gustav Hartvigsson
void * -> spointer |
130 |
void s_map_remove (SMap * self, spointer key) { |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
131 |
|
132 |
}
|