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