187d1d157Sdholland /*-
287d1d157Sdholland * Copyright (c) 2009 The NetBSD Foundation, Inc.
387d1d157Sdholland * All rights reserved.
487d1d157Sdholland *
587d1d157Sdholland * This code is derived from software contributed to The NetBSD Foundation
687d1d157Sdholland * by David A. Holland.
787d1d157Sdholland *
887d1d157Sdholland * Redistribution and use in source and binary forms, with or without
987d1d157Sdholland * modification, are permitted provided that the following conditions
1087d1d157Sdholland * are met:
1187d1d157Sdholland * 1. Redistributions of source code must retain the above copyright
1287d1d157Sdholland * notice, this list of conditions and the following disclaimer.
1387d1d157Sdholland * 2. Redistributions in binary form must reproduce the above copyright
1487d1d157Sdholland * notice, this list of conditions and the following disclaimer in the
1587d1d157Sdholland * documentation and/or other materials provided with the distribution.
1687d1d157Sdholland *
1787d1d157Sdholland * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1887d1d157Sdholland * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1987d1d157Sdholland * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2087d1d157Sdholland * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2187d1d157Sdholland * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2287d1d157Sdholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2387d1d157Sdholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2487d1d157Sdholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2587d1d157Sdholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2687d1d157Sdholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2787d1d157Sdholland * POSSIBILITY OF SUCH DAMAGE.
2887d1d157Sdholland */
2987d1d157Sdholland
3087d1d157Sdholland #include <stdlib.h>
3187d1d157Sdholland #include <string.h>
3287d1d157Sdholland
3387d1d157Sdholland #define ARRAYINLINE
3487d1d157Sdholland #include "array.h"
3587d1d157Sdholland
3687d1d157Sdholland struct array *
array_create(void)3787d1d157Sdholland array_create(void)
3887d1d157Sdholland {
3987d1d157Sdholland struct array *a;
4087d1d157Sdholland
4187d1d157Sdholland a = malloc(sizeof(*a));
4287d1d157Sdholland if (a != NULL) {
4387d1d157Sdholland array_init(a);
4487d1d157Sdholland }
4587d1d157Sdholland return a;
4687d1d157Sdholland }
4787d1d157Sdholland
4887d1d157Sdholland void
array_destroy(struct array * a)4987d1d157Sdholland array_destroy(struct array *a)
5087d1d157Sdholland {
5187d1d157Sdholland array_cleanup(a);
5287d1d157Sdholland free(a);
5387d1d157Sdholland }
5487d1d157Sdholland
5587d1d157Sdholland void
array_init(struct array * a)5687d1d157Sdholland array_init(struct array *a)
5787d1d157Sdholland {
5887d1d157Sdholland a->num = a->max = 0;
5987d1d157Sdholland a->v = NULL;
6087d1d157Sdholland }
6187d1d157Sdholland
6287d1d157Sdholland void
array_cleanup(struct array * a)6387d1d157Sdholland array_cleanup(struct array *a)
6487d1d157Sdholland {
6587d1d157Sdholland arrayassert(a->num == 0);
6687d1d157Sdholland free(a->v);
6787d1d157Sdholland #ifdef ARRAYS_CHECKED
6887d1d157Sdholland a->v = NULL;
6987d1d157Sdholland #endif
7087d1d157Sdholland }
7187d1d157Sdholland
7287d1d157Sdholland int
array_setsize(struct array * a,unsigned num)7387d1d157Sdholland array_setsize(struct array *a, unsigned num)
7487d1d157Sdholland {
7587d1d157Sdholland unsigned newmax;
7687d1d157Sdholland
7787d1d157Sdholland if (num > a->max) {
7887d1d157Sdholland newmax = a->max;
7987d1d157Sdholland while (num > newmax) {
8087d1d157Sdholland newmax = newmax ? newmax*2 : 4;
8187d1d157Sdholland }
82*a373ad58Snia if (reallocarr(&a->v, newmax, sizeof(*a->v)) != 0)
8387d1d157Sdholland return -1;
8487d1d157Sdholland a->max = newmax;
8587d1d157Sdholland }
8687d1d157Sdholland a->num = num;
8787d1d157Sdholland return 0;
8887d1d157Sdholland }
8987d1d157Sdholland
9087d1d157Sdholland int
array_insert(struct array * a,unsigned index_)9187d1d157Sdholland array_insert(struct array *a, unsigned index_)
9287d1d157Sdholland {
9387d1d157Sdholland unsigned movers;
9487d1d157Sdholland
9587d1d157Sdholland arrayassert(a->num <= a->max);
9687d1d157Sdholland arrayassert(index_ < a->num);
9787d1d157Sdholland
9887d1d157Sdholland movers = a->num - index_;
9987d1d157Sdholland
10087d1d157Sdholland if (array_setsize(a, a->num + 1)) {
10187d1d157Sdholland return -1;
10287d1d157Sdholland }
10387d1d157Sdholland
10487d1d157Sdholland memmove(a->v + index_+1, a->v + index_, movers*sizeof(*a->v));
10587d1d157Sdholland return 0;
10687d1d157Sdholland }
10787d1d157Sdholland
10887d1d157Sdholland void
array_remove(struct array * a,unsigned index_)10987d1d157Sdholland array_remove(struct array *a, unsigned index_)
11087d1d157Sdholland {
11187d1d157Sdholland unsigned movers;
11287d1d157Sdholland
11387d1d157Sdholland arrayassert(a->num <= a->max);
11487d1d157Sdholland arrayassert(index_ < a->num);
11587d1d157Sdholland
11687d1d157Sdholland movers = a->num - (index_ + 1);
11787d1d157Sdholland memmove(a->v + index_, a->v + index_+1, movers*sizeof(*a->v));
11887d1d157Sdholland a->num--;
11987d1d157Sdholland }
120