/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
1
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
2
 * vi: set shiftwidth=2 tabstop=2 expandtab:
3
 * :indentSize=2:tabSize=2:noTabs=true:
4
 */
5
6
/*
5.2.9 by Gustav Hartvigsson
* Added license to files
7
Copyright (c) 2013-2014 Gustav Hartvigsson
8
9
Permission is hereby granted, free of charge, to any person obtaining a copy
10
of this software and associated documentation files (the "Software"), to deal
11
in the Software without restriction, including without limitation the rights
12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
15
16
The above copyright notice and this permission notice shall be included in
17
all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
THE SOFTWARE.
26
*/
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
27
30 by Gustav Hartvigsson
* Made the code compile using CMake.
28
#include "DynamicArray.h"
29
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
30
struct DynamicArray {
31
  size_t max_size;
32
  size_t len;
33
  FreeFunc free_func;
30 by Gustav Hartvigsson
* Made the code compile using CMake.
34
  spointer * array;
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
35
};
36
30 by Gustav Hartvigsson
* Made the code compile using CMake.
37
void _private_for_each_item_free (DynamicArray * self, spointer item,
38
                                    spointer data);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
39
40
DynamicArray * dynamic_array_new (size_t len, FreeFunc free_func) {
41
  DynamicArray * self = malloc (sizeof(DynamicArray));
42
  
43
  self->max_size = len;
44
  self->len = 0;
45
  
46
  self->free_func = free_func;
47
  
48
  self->array = malloc (len * sizeof (* self->array));
49
  
50
  return self;
51
}
52
53
void dynamic_array_free (DynamicArray * self) {
54
  if (self->free_func != NULL) {
55
    dynamic_array_for_each (self, (ForEachFunc) _private_for_each_item_free,
56
                            NULL);
57
  } else {
58
    for (int i = 0; i < self->len; i++) {
59
      free (self->array[i]);
60
    }
61
  }
62
  free (self->array);
63
  free (self);
64
}
65
30 by Gustav Hartvigsson
* Made the code compile using CMake.
66
spointer dynamic_array_get (DynamicArray * self, size_t index) {
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
67
  return self->array[index];
68
}
69
70
size_t dynamic_array_len (DynamicArray * self) {
71
  return self->len;
72
}
73
74
size_t dynamic_array_size (DynamicArray * self) {
75
  return self->max_size;
76
}
77
30 by Gustav Hartvigsson
* Made the code compile using CMake.
78
void dynamic_array_add (DynamicArray * self, spointer data) {
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
79
  if (self->len >= self->max_size - 1) {
80
    self->array = realloc (self->array,
81
                          (sizeof (* self->array) *
82
                          (self->max_size + ARRAY_PADDING)));
83
    self->max_size = self->max_size + ARRAY_PADDING;
84
  }
85
  self->array[self->len] = data;
86
  self->len++;
87
}
88
30 by Gustav Hartvigsson
* Made the code compile using CMake.
89
spointer * dynamic_array_dump_array (DynamicArray * self) {
90
  spointer * ret_val = malloc (sizeof (* self->array) * (self->len + 1));
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
91
  for (int i = 0; i < self->len; i++) {
92
    ret_val[i] = self->array[i];
93
  }
94
  ret_val[self->len] = NULL;
95
  return ret_val;
96
}
97
98
void dynamic_array_for_each (DynamicArray * self, ForEachFunc func,
30 by Gustav Hartvigsson
* Made the code compile using CMake.
99
                             spointer data) {
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
100
  for (int i = 0; i < self->len; i++) {
101
    func (self, self->array[i], data);
102
  }
103
}
104
105
30 by Gustav Hartvigsson
* Made the code compile using CMake.
106
void _private_for_each_item_free (DynamicArray * self, spointer item,
107
                                    spointer data) {
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
108
  FreeFunc func = self->free_func;
109
  func (item);
110
}
111