/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to src/DynamicArray.h

  • Committer: Gustav Hartvigsson
  • Date: 2014-11-24 13:59:19 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: gustav.hartvigsson@gmail.com-20141124135919-3x0u978u98uzjmet
* Copied over LinkedList and DynamicArray from c_sdl_js

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#ifndef __H_DYNAMIC_ARRAY__
 
7
#define __H_DYNAMIC_ARRAY__
 
8
#include <stdlib.h>
 
9
#include "defs.h"
 
10
 
 
11
/* This file, as the rest of the project is under MIT license.
 
12
 * see http://opensource.org/licenses/MIT
 
13
 *
 
14
 * Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
 
15
 */
 
16
 
 
17
/** @file
 
18
 * Dynamic Array.
 
19
 *
 
20
 * This file contains an imlpementation of a "dynamic" array, it is usefule when
 
21
 * dealing with lare amounts of data that may change over time, or with an
 
22
 * unknowned numebr of items.
 
23
 * 
 
24
 * Note that accsess time is constant, but write time is not guarenteed to be.
 
25
 * 
 
26
 * When the size of the array is equal to the number of elements in it, it will
 
27
 * re-allocate the array with a larger size.
 
28
 */
 
29
 
 
30
/**
 
31
 * The padding that is added when expanding the array.
 
32
 */
 
33
#define ARRAY_PADDING 8
 
34
 
 
35
/**
 
36
 * an opaque data structure that represents the dynamic array.
 
37
 */
 
38
typedef struct DynamicArray DynamicArray;
 
39
 
 
40
#if 0
 
41
#ifndef __H_DEFS__
 
42
/**
 
43
 * Represents a for each function.
 
44
 * 
 
45
 * @param self the dynamic array.
 
46
 * @param data the data to be passed to the each iteration.
 
47
 * @returns data to be put in a new \c DynamicArray, if used with
 
48
 *          <tt>dynamic_array_for_each_with_return</tt>.
 
49
 */
 
50
typedef _pointer (* ForEachFunc)(DynamicArray * self, _pointer item, _pointer data);
 
51
 
 
52
/**
 
53
 * Represents a function to be used when freeing some item in a dynamic array.
 
54
 *
 
55
 * @param obj The object to be freed.
 
56
 */
 
57
typedef _pointer (* FreeFunc)(_pointer obj);
 
58
 
 
59
#endif /* __H_DEFS__ */
 
60
#endif /* #if 0*/
 
61
 
 
62
/**
 
63
 * Create a new dynamic array.
 
64
 *
 
65
 * @param len The length of the initial array.
 
66
 * @param free_func The function to be used when freeing the items. Can be \c NULL.
 
67
 *        If free_func is NULL, it will use the standard library's 
 
68
 *       <tt>free()</tt>.
 
69
 *        
 
70
 *        Normally a function with the signature <tt> (DynamicArray * self,
 
71
          _pointer item, _pointer data) </tt> should be used but cast to FreeFunc.
 
72
 */
 
73
DynamicArray * dynamic_array_new (size_t len, FreeFunc free_func);
 
74
 
 
75
/**
 
76
 * Frees the dynamic array.
 
77
 * 
 
78
 * after this is run the data will be lost.
 
79
 */
 
80
void dynamic_array_free (DynamicArray * self);
 
81
 
 
82
 
 
83
/**
 
84
 * Get an item from the array.
 
85
 */
 
86
_pointer dynamic_array_get (DynamicArray * self, size_t index);
 
87
 
 
88
/**
 
89
 * Get the length of the array.
 
90
 */
 
91
size_t dynamic_array_len (DynamicArray * self);
 
92
 
 
93
 
 
94
/**
 
95
 * Get the size of the array, this is not the same as the length of the array.
 
96
 * The size is the number of elements that can be allocated without resizing
 
97
 * the array.
 
98
 * 
 
99
 * To get the length of the array use dynamic_array_len ().
 
100
 */
 
101
size_t dynamic_array_size (DynamicArray * self);
 
102
 
 
103
 
 
104
/**
 
105
 * Add an item to the array.
 
106
 */
 
107
void dynamic_array_add (DynamicArray * self, _pointer item);
 
108
 
 
109
/**
 
110
 * Dumps a copy of the array. Must be cast.
 
111
 * 
 
112
 * Is not freed when the dynamic array is freed.
 
113
 * 
 
114
 * Is null-terminated.
 
115
 */
 
116
_pointer* dynamic_array_dump_array (DynamicArray * self);
 
117
 
 
118
/**
 
119
 * Use a function on the array.
 
120
 */
 
121
void dynamic_array_for_each (DynamicArray * self, ForEachFunc func,
 
122
                             _pointer data);
 
123
 
 
124
/** TODO
 
125
 * same as dynamic_array_for_each (), with the difference that it returns a new
 
126
 * DynamicArray.
 
127
 */
 
128
DynamicArray * dynamic_array_for_each_with_return (DynamicArray * self,
 
129
                                                   ForEachFunc func,
 
130
                                                   _pointer data);
 
131
 
 
132
#endif /* #define __H_DYNAMIC_ARRAY__ */