summaryrefslogtreecommitdiffstats
path: root/clib/src/array_iter.c
blob: 921bf97a04183123adde6b8acd653a521531fb5b (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
/*
 * 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_iter.c
 * Author: Shaun Wetzstein <shaun@us.ibm.com>
 *  Descr: dynamic array
 *   Note:
 *   Date: 10/22/10
 */

#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

#include "assert.h"
#include "misc.h"
#include "hash.h"
#include "nargs.h"
#include "array_iter.h"

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

const char *__array_iter_msg[] = {
	"array_iter: unexpected NULL pointer",
	"array_iter: index out-of-range",
};

#define ARRAY_ITER_NULL         (__array_iter_msg[0])
#define ARRAY_ITER_RANGE        (__array_iter_msg[1])

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

static inline const void *__array_iter_bwd(array_iter_t * self)
{
	size_t low = array_low(self->array);
	const void *ret = NULL;

	if (low < self->idx)
		self->idx--;

	while (low < self->idx) {
		if (array_status(self->array, self->idx))
			break;
		self->idx--;
	}

	if (low < self->idx)
		ret = array_at(self->array, self->idx);

	return ret;
}

static inline const void *__array_iter_fwd(array_iter_t * self)
{
	size_t high = array_high(self->array);
	const void *ret = NULL;

	if (self->idx < high)
		self->idx++;

	while (self->idx < high) {
		if (array_status(self->array, self->idx))
			break;
		self->idx++;
	}

	if (self->idx < high)
		ret = array_at(self->array, self->idx);

	return ret;
}

void array_iter_init(array_iter_t * self, array_t * array, uint32_t flags)
{
	if (unlikely(self == NULL))
		throw_unexpected(ARRAY_ITER_NULL);
	if (unlikely(array == NULL))
		throw_unexpected(ARRAY_ITER_NULL);

	self->flags = flags;
	self->array = array;

	if (self->flags & AI_FLAG_BWD) {
		self->idx = array_high(self->array);
		__array_iter_bwd(self);
	} else {
		self->idx = array_low(self->array);
		__array_iter_fwd(self);
	}
}

void array_iter_clear(array_iter_t * self)
{
	if (unlikely(self == NULL))
		throw_unexpected(ARRAY_ITER_NULL);

	if (self->flags & AI_FLAG_BWD)
		self->idx = array_high(self->array);
	else
		self->idx = array_low(self->array);

	self->array = NULL;
}

const void *array_iter_elem(array_iter_t * self)
{
	if (unlikely(self == NULL))
		throw_unexpected(ARRAY_ITER_NULL);
	if (self->idx < array_low(self->array))
		throw_unexpected(ARRAY_ITER_RANGE);
	if (array_high(self->array) <= self->idx)
		throw_unexpected(ARRAY_ITER_RANGE);

	return array_status(self->array, self->idx) ?
	    array_at(self->array, self->idx) : NULL;
}

const void *array_iter_inc1(array_iter_t * self)
{
	return array_iter_inc2(self, 1);
}

const void *array_iter_inc2(array_iter_t * self, size_t inc)
{
	if (unlikely(self == NULL))
		throw_unexpected(ARRAY_ITER_NULL);

	const void *ret = NULL;

	if (self->flags & AI_FLAG_BWD)
		ret = __array_iter_bwd(self);
	else
		ret = __array_iter_fwd(self);

	return ret;
}

const void *array_iter_dec1(array_iter_t * self)
{
	return array_iter_dec2(self, 1);
}

const void *array_iter_dec2(array_iter_t * self, size_t dec)
{
	if (unlikely(self == NULL))
		throw_unexpected(ARRAY_ITER_NULL);

	const void *ret = NULL;

	if (self->flags & AI_FLAG_BWD)
		ret = __array_iter_fwd(self);
	else
		ret = __array_iter_bwd(self);

	return ret;
}

/* ======================================================================= */
OpenPOWER on IntegriCloud