/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 15:25:23 UTC
  • mfrom: (5.2.9 simple_type_system_SMap)
  • Revision ID: gustav.hartvigsson@gmail.com-20141124152523-jvcnlsu5t1i44tt8
* Merged with the SMap branch

Show diffs side-by-side

added added

removed removed

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