summaryrefslogtreecommitdiffstats
path: root/clib/array.h
blob: 8fce5ee0d32a78d24efbf4830058a89c204caee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * Copyright (c) International Business Machines Corp., 2014
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/*!
 * @file array.h
 * @brief Array container
 * @details An array class whose elements are partitioned into data `pages'
 *          that are allocated upon first-touch, possibly leading to a pages
 *          that are sparsely populated.
 * @details For example,
 * @code
 * #include <clib/array.h>
 * #include <clib/array_iter.h>
 *
 * int main(const int argc, const char * argv[]) {
 *     array_t a;
 *     array_init(&a, 4, 1024);
 *
 *     array_dump(&a, stdout);
 *
 *     array_put(&a, 52, (uint32_t[]){52});
 *     array_put(&a, 53, (uint32_t[]){53});
 *     array_put(&a, 167, (uint32_t[]){167});
 *     array_put(&a, 223, (uint32_t[]){223});
 *     array_put(&a, 78, (uint32_t[]){78});
 *     array_put(&a, 205, (uint32_t[]){205});
 *     array_put(&a, 183, (uint32_t[]){183});
 *     array_put(&a, 150, (uint32_t[]){150});
 *     array_put(&a, 90, (uint32_t[]){90});
 *     array_put(&a, 66, (uint32_t[]){66});
 *     array_put(&a, 91, (uint32_t[]){91});
 *     array_put(&a, 45, (uint32_t[]){45});
 *     array_put(&a, 211, (uint32_t[]){211});
 *
 *     uint32_t arr[] = {985,986,987,988,990,991,992,993,994};
 *     array_put(&a, 985, arr, 9);
 *
 *     array_iter_t it;
 *     array_iter_init(&it, &a, AI_FLAG_FWD);
 *
 *     uint32_t * j;
 *     array_for_each(&it, j) {
 *         printf("arr[%d]\n", *j);
 *     }
 *
 *     array_dump(&a, stdout);
 *     array_delete(&a);
 *
 *     return 0;
 * }
 * @endcode
 * @author Shaun Wetzstein <shaun@us.ibm.com>
 * @date 2010-2011
 */

#ifndef __ARRAY_H__
#define __ARRAY_H__

#include <stdbool.h>
#include <stdint.h>

#include "builtin.h"
#include "mqueue.h"
#include "tree.h"
#include "nargs.h"

/* ======================================================================= */

typedef struct array array_t;	//!< Alias for the @em array class

/*!
 * @brief Array container class
 */
struct array {
	uint32_t magic;		//!< Array magic number

	uint32_t page_size;	//!< Array data page size (in bytes)

	uint16_t elem_size;	//!< Array element size (in bytes)
	uint16_t elem_num;	//!< Array element count (per page)

	size_t size;		//!< Number of initialized elements
	size_t pages;		//!< Number of data pages allocated (currently)

	size_t low;		//!< @private
	size_t high;		//!< @private

	size_t map_size;	//!< @private
	tree_t tree;		//!< @private
};

/* ======================================================================= */

/*!
 * @brief Constructs an @em array container object
 * @details For example,
 * @code
 * ...
 * array_t ar;
 * array_init(&ar, 4, 1024);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @param elem_size [in] array element size, in bytes
 * @param elem_num [in] array element number, per data page
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern void array_init(array_t * self, size_t elem_size, size_t elem_num)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Destructs an @em array container object
 * @details Deallocate all backing storage associated with this \em array object
 * @details For example,
 * @code
 * ...
 * array_init(&ar, 4, 1024);
 * array_put(&ar, 524, &count);
 * array_delete(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @return None
 */
extern void array_delete(array_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Returns a reference to the element at position @em idx in the @em array
 * @details For example,
 * @code
 * ...
 * array_init(&ar, 4, 1024);
 * array_put(&ar, 524, &count);
 * printf("ar[524] = %d\n", *(int *)array_at(&ar, 524));
 * array_delete(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @param elem_num [in] array element index
 * @return Reference to array element at @em idx on SUCCESS
 * @throws UNEXPECTED if @em self pointer is NULL
 * @throws UNEXPECTED if array element at @em idx is uninitialized
 */
extern const void *array_at(array_t * self, size_t elem_num)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @fn void array_get(array_t * self, size_t elem_off, const void * ptr, size_t elem_num=1)
 * @brief Copy content from the @em array
 * @details Copies @em elem_num element(s) starting at position @em elem_off in the source @em array to destination pointer @em ptr
 * @note If the fourth parameter is omitted, it defaults to 1
 * @details For example,
 * @code
 * ...
 * array_init(&ar, 4, 1024);
 * array_put(&ar, 524, &count);
 * array_get(&ar, 524, &count);
 * array_delete(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @param elem_off [in] array element index
 * @param ptr [out] Destination storage pointer
 * @param elem_num [in] Desgination element count (optional)
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
/*! @cond */
#define array_put(...) STRCAT(array_put, NARGS(__VA_ARGS__))(__VA_ARGS__)
extern void array_get3(array_t * self, size_t elem_off, void *ptr)
/*! @cond */
__nonnull((1, 3)) /*! @endcond */ ;
extern void array_get4(array_t * self, size_t elem_off, void *ptr,
		       size_t elem_num)
/*! @cond */
__nonnull((1, 3)) /*! @endcond */ ;
/*! @endcond */

/*!
 * @fn void array_put(array_t * self, size_t elem_off, const void * ptr, size_t elem_num=1)
 * @brief Assign new content to the @em array
 * @details Copies @em elem_num element(s) from source pointer @em ptr to the destination @em array starting at position @em elem_off
 * @note If the fourth parameter is omitted, it defaults to 1
 * @details For example,
 * @code
 * ...
 * array_init(&ar, 4, 1024);
 * array_put(&ar, 524, &count);
 * array_get(&ar, 524, &count);
 * array_delete(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @param elem_off [in] array element index
 * @param ptr [in] Source storage pointer
 * @param elem_num [in] Source element count (optional)
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
/*! @cond */
#define array_put(...) STRCAT(array_put, NARGS(__VA_ARGS__))(__VA_ARGS__)
extern void array_put3(array_t * self, size_t elem_off, const void *ptr)
/*! @cond */
__nonnull((1, 3)) /*! @endcond */ ;
extern void array_put4(array_t * self, size_t elem_off, const void *ptr,
		       size_t elem_num)
/*! @cond */
__nonnull((1, 3)) /*! @endcond */ ;
/*! @endcond */

/*!
 * @fn bool array_status(array_t * self, size_t elem_num, bool state)
 * @brief Set or return the initialized status of the @em array element at position @em idx
 * @note If the third parameter is omitted, the element's current status is returned
 * @details For example,
 * @code
 * ...
 * array_init(&ar, 4, 1024);
 * array_put(&ar, 524, &count);
 * printf("%d %d\n", array_status(&ar, 524), array_status(&ar, 600));
 * array_delete(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @param elem_num [in] array element index
 * @param state [in] Element state (optional)
 * @return None
 * @throws UNEXPECTED if @em idx is out of bounds
 * @throws UNEXPECTED if @em self pointer is NULL
 */
/*! @cond */
#define array_status(...) STRCAT(array_status, NARGS(__VA_ARGS__))(__VA_ARGS__)
extern bool array_status2(array_t * self, size_t elem_num)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;
extern bool array_status3(array_t * self, size_t elem_num, bool state)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;
/*! @endcond */

/*!
 * @fn size_t array_size(array_t * self, size_t size = 1)
 * @brief Return or set the size of the @em array
 * @details Return or set the number of allocated elements in the @em array
 * @details For example,
 * @code
 * ...
 * array_init(&ar, 4, 1024);
 * array_size(&ar, 2040);
 * array_delete(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @param size [in] New array size
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
#define array_size(...) STRCAT(array_size, NARGS(__VA_ARGS__))(__VA_ARGS__)
extern size_t array_size1(array_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;
extern void array_size2(array_t * self, size_t size)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return pages of the @em array container
 * @details Return the number of pages in the @em array container
 * @details For example,
 * @code
 * ...
 * array_init(&ar, 4, 1024);
 * array_size(&ar, 2040);
 * printf("pages = %d\n", array_pages(&ar));
 * array_delete(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @return The number of pages that conform the array's content
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern size_t array_pages(array_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return capacity of the @em array
 * @details Return the number of allocated and unallocated elements in the @em array container
 * @details For example,
 * @code
 * ...
 * array_init(&ar, 4, 1024);
 * array_size(&ar, 2040);
 * printf("capacity = %d\n", array_capacity(&ar));
 * array_delete(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @return The number of total elements that conform the array's content
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern size_t array_capacity(array_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return @em array index lower bound
 * @details For example,
 * @code
 * ...
 * array_init(&ar, 4, 1024);
 * array_put(&ar, 7, &count);
 * array_put(&ar, 7000, &count);
 * printf("low = %d\n", array_low(&ar));
 * array_delete(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @return non-0 on success
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern size_t array_low(array_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Return @em array index upper bound
 * @details For example,
 * @code
 * ...
 * array_init(&ar, 4, 1024);
 * array_put(&ar, 7, &count);
 * array_put(&ar, 7000, &count);
 * printf("high = %d\n", array_high(&ar));
 * array_delete(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @return non-0 on success
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern size_t array_high(array_t * self)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Send (write) an @em array object to a message queue
 * @details For example,
 * @code
 * ...
 * array_init(&ar, 4, 1024);
 * array_put(&ar, 7, &count);
 * array_put(&ar, 7000, &count);
 * ...
 * mqueue mq;
 * mqueue_init(&mq, "my_server");
 * mqueue_create(&mq, gettid());
 * ...
 * array_send(&ar, &mq);
 * array_delete(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @return non-0 on success
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern void array_send(array_t * self, mqueue_t * mq)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief receive (read) an @em array object from a message queue
 * @details For example,
 * @code
 * ...
 * array ar;
 * ...
 * mqueue mq;
 * mqueue_open(&mq, path);
 * ...
 * array_receive(&ar, &mq);
 * array_dump(&ar);
 * ...
 * @endcode
 * @memberof array
 * @param self [in] array object @em self pointer
 * @return non-0 on success
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern void array_receive(array_t * self, mqueue_t * mq)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Pretty print the contents of an @em array to stdout
 * @memberof array
 * @param self [in] array object @em self pointer
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern void array_dump(array_t * self, FILE * out)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/* ======================================================================= */

#endif				/* __ARRAY_H__ */
OpenPOWER on IntegriCloud