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