1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/*
Copyright (c) 2013-2014 Gustav Hartvigsson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "Map.h"
#include "baseobject.h"
#include <stdlib.h>
struct SMapPrivate {
size_t len; // Length
SDynamicArray * array; /* This is what holds the buckets. These buckets are
* in turn also SDynamicArrays; or in template speak:
* SDynamicArray<SDynamicArray<SMapItem*>*>*
*/
};
SMapItem * s_map_item_new (void * key, void * value) {
SMapItem * self = malloc (sizeof (SMapItem));
self->key = key;
self->value = value;
return self;
}
void s_map_item_free (SMapItem * self, FuncPointer free_key,
FuncPointer free_value) {
FREEFUNC(free_key) (self->key);
FREEFUNC(free_value) (self->value);
free (self);
}
SMap * s_map_new (CompFunc comp_func,
HashFunc key_hash_func,
FuncPointer free_key,
FuncPointer free_value) {
SMap * self = malloc (sizeof (SMap));
SMapClass * klass = malloc (sizeof (SMapClass));
self->priv = malloc (sizeof (SMapPrivate));
self->priv->len = 0;
klass->is_equal = comp_func;
/* free_* functions need to be checked if they are null and set the pointer
* to free or s_base_object_free ()... Have to decide which...?
*/
klass->free_key = free_key;
klass->free_value = free_value;
/* if no func is set we have to use some other metod of
*/
if (key_hash_func == NULL){
klass->key_hash_func = s_hash_object;
} else {
klass->key_hash_func = key_hash_func;
}
/* We do our own freeing of objects. */
self->priv->array = s_dynamic_array_new (S_MAP_DEFAULT_NUMBER_OF_BUCKETS,
NULL);
return self;
}
void s_map_free (SMap * self) {
free (self->priv);
free (self->klass);
free (self);
}
void s_map_add (SMap * self, spointer key, spointer value) {
SDynamicArray * array = self->priv->array;
SMapItem * item = s_map_item_new (key, value);
/* We have to generate a key to use as an index in the DynamicArray. */
HashFunc hash_func = self->klass->key_hash_func;
hash_t hash = hash_func (key);
/* We need to mod it with the max size of the array. */
hash = hash % S_MAP_DEFAULT_NUMBER_OF_BUCKETS;
if (self->priv->len == 0) {
/* Since we know that there are no items in the array we can skip the
* check if the new place is taken. and just and an array to it.
*/
SDynamicArray * bucket = s_dynamic_array_new (8, NULL);
s_dynamic_array_set (array, hash, bucket);
s_dynamic_array_set (bucket, 0, item);
} else {
/* Figure out if the bucket exists. */
SDynamicArray * bucket = NULL;
if (s_dynamic_array_get (array, hash) == NULL) {
bucket = s_dynamic_array_new (8, NULL);
} else {
/* We get the bucket */
bucket = S_DYNAMIC_ARRAY(s_dynamic_array_get (array, hash));
}
size_t bucket_len = s_dynamic_array_last_item (bucket);
s_dynamic_array_set (bucket, bucket_len, item);
}
}
spointer s_map_get (SMap * self, spointer key) {
return NULL;
}
void s_map_remove (SMap * self, spointer key) {
}
|