2015-06-03 17:35:44 +00:00
|
|
|
/* $OpenBSD$ */
|
2007-07-09 19:04:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2006 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ARRAY_H
|
|
|
|
#define ARRAY_H
|
|
|
|
|
2010-02-08 18:29:32 +00:00
|
|
|
#define ARRAY_INITIALIZER { NULL, 0, 0 }
|
|
|
|
|
2007-07-09 19:04:12 +00:00
|
|
|
#define ARRAY_DECL(n, c) \
|
|
|
|
struct n { \
|
|
|
|
c *list; \
|
|
|
|
u_int num; \
|
|
|
|
size_t space; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ARRAY_ITEM(a, i) ((a)->list[i])
|
|
|
|
#define ARRAY_ITEMSIZE(a) (sizeof *(a)->list)
|
2008-08-07 20:20:52 +00:00
|
|
|
#define ARRAY_INITIALSPACE(a) (10 * ARRAY_ITEMSIZE(a))
|
|
|
|
|
|
|
|
#define ARRAY_ENSURE(a, n) do { \
|
2008-09-29 16:58:02 +00:00
|
|
|
if (UINT_MAX - (n) < (a)->num) \
|
2008-08-07 20:20:52 +00:00
|
|
|
fatalx("number too big"); \
|
|
|
|
if (SIZE_MAX / ((a)->num + (n)) < ARRAY_ITEMSIZE(a)) \
|
|
|
|
fatalx("size too big"); \
|
|
|
|
if ((a)->space == 0) { \
|
|
|
|
(a)->space = ARRAY_INITIALSPACE(a); \
|
2014-10-08 17:35:58 +00:00
|
|
|
(a)->list = xrealloc((a)->list, (a)->space); \
|
2008-08-07 20:20:52 +00:00
|
|
|
} \
|
|
|
|
while ((a)->space <= ((a)->num + (n)) * ARRAY_ITEMSIZE(a)) { \
|
2014-10-08 17:35:58 +00:00
|
|
|
(a)->list = xreallocarray((a)->list, 2, (a)->space); \
|
2008-08-07 20:20:52 +00:00
|
|
|
(a)->space *= 2; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2007-07-09 19:04:12 +00:00
|
|
|
|
2010-06-06 00:27:08 +00:00
|
|
|
#define ARRAY_EMPTY(a) (((void *) (a)) == NULL || (a)->num == 0)
|
2007-07-09 19:04:12 +00:00
|
|
|
#define ARRAY_LENGTH(a) ((a)->num)
|
2007-08-27 08:08:19 +00:00
|
|
|
#define ARRAY_DATA(a) ((a)->list)
|
2007-07-09 19:04:12 +00:00
|
|
|
|
|
|
|
#define ARRAY_FIRST(a) ARRAY_ITEM(a, 0)
|
|
|
|
#define ARRAY_LAST(a) ARRAY_ITEM(a, (a)->num - 1)
|
|
|
|
|
|
|
|
#define ARRAY_INIT(a) do { \
|
|
|
|
(a)->num = 0; \
|
|
|
|
(a)->list = NULL; \
|
|
|
|
(a)->space = 0; \
|
|
|
|
} while (0)
|
2007-08-27 08:08:19 +00:00
|
|
|
#define ARRAY_CLEAR(a) do { \
|
|
|
|
(a)->num = 0; \
|
|
|
|
} while (0)
|
2007-07-09 19:04:12 +00:00
|
|
|
|
|
|
|
#define ARRAY_SET(a, i, s) do { \
|
|
|
|
(a)->list[i] = s; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ARRAY_ADD(a, s) do { \
|
2008-08-07 20:20:52 +00:00
|
|
|
ARRAY_ENSURE(a, 1); \
|
2007-07-09 19:04:12 +00:00
|
|
|
(a)->list[(a)->num] = s; \
|
|
|
|
(a)->num++; \
|
|
|
|
} while (0)
|
2008-06-20 08:36:20 +00:00
|
|
|
#define ARRAY_INSERT(a, i, s) do { \
|
2008-08-07 20:20:52 +00:00
|
|
|
ARRAY_ENSURE(a, 1); \
|
2008-06-20 08:36:20 +00:00
|
|
|
if ((i) < (a)->num) { \
|
|
|
|
memmove((a)->list + (i) + 1, (a)->list + (i), \
|
|
|
|
ARRAY_ITEMSIZE(a) * ((a)->num - (i))); \
|
|
|
|
} \
|
|
|
|
(a)->list[i] = s; \
|
|
|
|
(a)->num++; \
|
|
|
|
} while (0)
|
2007-07-09 19:04:12 +00:00
|
|
|
#define ARRAY_REMOVE(a, i) do { \
|
2008-06-16 07:01:41 +00:00
|
|
|
if ((i) < (a)->num - 1) { \
|
2007-07-09 19:04:12 +00:00
|
|
|
memmove((a)->list + (i), (a)->list + (i) + 1, \
|
2008-06-16 07:01:41 +00:00
|
|
|
ARRAY_ITEMSIZE(a) * ((a)->num - (i) - 1)); \
|
2007-07-09 19:04:12 +00:00
|
|
|
} \
|
|
|
|
(a)->num--; \
|
2009-10-28 23:12:38 +00:00
|
|
|
if ((a)->num == 0) \
|
2007-07-09 19:04:12 +00:00
|
|
|
ARRAY_FREE(a); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ARRAY_EXPAND(a, n) do { \
|
2008-08-07 20:20:52 +00:00
|
|
|
ARRAY_ENSURE(a, n); \
|
2007-07-09 19:04:12 +00:00
|
|
|
(a)->num += n; \
|
|
|
|
} while (0)
|
|
|
|
#define ARRAY_TRUNC(a, n) do { \
|
|
|
|
if ((a)->num > n) \
|
|
|
|
(a)->num -= n; \
|
|
|
|
else \
|
|
|
|
ARRAY_FREE(a); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ARRAY_CONCAT(a, b) do { \
|
2008-08-07 20:20:52 +00:00
|
|
|
ARRAY_ENSURE(a, (b)->num); \
|
2009-11-02 21:34:32 +00:00
|
|
|
memcpy((a)->list + (a)->num, (b)->list, (b)->num * ARRAY_ITEMSIZE(a)); \
|
2007-07-09 19:04:12 +00:00
|
|
|
(a)->num += (b)->num; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ARRAY_FREE(a) do { \
|
2012-07-11 19:34:16 +00:00
|
|
|
free((a)->list); \
|
2007-07-09 19:04:12 +00:00
|
|
|
ARRAY_INIT(a); \
|
|
|
|
} while (0)
|
|
|
|
#define ARRAY_FREEALL(a) do { \
|
|
|
|
ARRAY_FREE(a); \
|
2012-07-11 19:34:16 +00:00
|
|
|
free(a); \
|
2007-07-09 19:04:12 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#endif
|