/simpletypesystem/trunk

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