File: numarray.c

package info (click to toggle)
universal-ctags 0%2Bgit20181215-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,444 kB
  • sloc: ansic: 84,242; vhdl: 5,924; sh: 5,830; perl: 1,743; cpp: 1,599; cs: 1,193; python: 812; sql: 572; f90: 534; php: 479; yacc: 459; fortran: 341; makefile: 325; asm: 311; objc: 284; ruby: 261; xml: 245; java: 157; tcl: 133; cobol: 122; lisp: 113; erlang: 61; ada: 55; ml: 49; awk: 43
file content (175 lines) | stat: -rw-r--r-- 6,161 bytes parent folder | download
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
/*
*   Copyright (c) 1999-2002, Darren Hiebert
*
*   This source code is released for free distribution under the terms of the
*   GNU General Public License version 2 or (at your option) any later version.
*
*   This module contains functions managing resizable pointer arrays.
*/

/*
*   INCLUDE FILES
*/
#include "general.h"  /* must always come first */

#include "debug.h"
#include "numarray.h"
#include "routines.h"

#include <stdlib.h>
#include <string.h>

#define impNumArray(prefix,Prefix,type)									\
																		\
	struct s##Prefix##Array {											\
		unsigned int max;												\
		unsigned int count;												\
		type *array;													\
	};																	\
																		\
	extern prefix##Array *prefix##ArrayNew (void)						\
	{																	\
		prefix##Array* const result = xMalloc (1, prefix##Array);		\
		result->max = 8;												\
		result->count = 0;												\
		result->array = xMalloc (result->max, type);					\
		return result;													\
	}																	\
																		\
	extern void prefix##ArrayAdd (prefix##Array *const current, type num) \
	{																	\
		Assert (current != NULL);										\
		if (current->count == current->max)								\
		{																\
			current->max *= 2;											\
			current->array = xRealloc (current->array, current->max, type);	\
		}																\
		current->array [current->count++] = num;						\
	}																	\
																		\
	extern void prefix##ArrayRemoveLast (prefix##Array *const current)	\
	{																	\
		Assert (current != NULL);										\
		Assert (current->count > 0);									\
		--current->count;												\
	}																	\
																		\
	extern void prefix##ArrayCombine (prefix##Array *const current, prefix##Array *const from) \
	{																	\
		unsigned int i;													\
		Assert (current != NULL);										\
		Assert (from != NULL);											\
		for (i = 0  ;  i < from->count  ;  ++i)							\
			prefix##ArrayAdd (current, from->array [i]);				\
		from->count = 0;												\
		prefix##ArrayDelete (from);										\
	}																	\
																		\
	extern unsigned int prefix##ArrayCount (const prefix##Array *const current)	\
	{																	\
		Assert (current != NULL);										\
		return current->count;											\
	}																	\
																		\
	extern type prefix##ArrayItem (const prefix##Array *const current, const unsigned int indx)	\
	{																	\
		Assert (current != NULL);										\
		return current->array [indx];									\
	}																	\
																		\
	extern type prefix##ArrayLast (const prefix##Array *const current)	\
	{																	\
		Assert (current != NULL);										\
		Assert (current->count > 0);									\
		return current->array [current->count - 1];						\
	}																	\
																		\
	extern void prefix##ArrayClear (prefix##Array *const current)		\
	{																	\
		Assert (current != NULL);										\
		current->count = 0;												\
	}																	\
																		\
	extern void prefix##ArrayDelete (prefix##Array *const current)		\
	{																	\
		if (current != NULL)											\
		{																\
			prefix##ArrayClear (current);								\
			eFree (current->array);										\
			eFree (current);											\
		}																\
	}																	\
																		\
	extern bool prefix##ArrayHasTest (const prefix##Array *const current, \
									  bool (*test)(const type num, void *userData),	\
									  void *userData)					\
	{																	\
		bool result = false;											\
		unsigned int i;													\
		Assert (current != NULL);										\
		for (i = 0  ;  ! result  &&  i < current->count  ;  ++i)		\
			result = (*test)(current->array [i], userData);				\
		return result;													\
	}																	\
																		\
	static bool prefix##Eq (const type num, void *userData)				\
	{																	\
		type *num0 = userData;											\
		return (num == *num0);											\
	}																	\
																		\
	extern bool prefix##ArrayHas (const prefix##Array *const current, type num)	\
	{																	\
		return prefix##ArrayHasTest (current, prefix##Eq, &num);		\
	}																	\
																		\
	extern void prefix##ArrayReverse (const prefix##Array *const current) \
	{																	\
		unsigned int i, j;												\
		type tmp;														\
																		\
		Assert (current != NULL);										\
		for (i = 0, j = current->count - 1 ; i <  (current->count / 2); ++i, --j) \
		{																\
			tmp = current->array[i];									\
			current->array[i] = current->array[j];						\
			current->array[j] = tmp;									\
		}																\
	}																	\
																		\
	extern void prefix##ArrayDeleteItem (prefix##Array* const current, unsigned int indx) \
	{																	\
		memmove (current->array + indx, current->array + indx + 1,		\
				 (current->count - indx) * sizeof (*current->array));	\
		--current->count;												\
	}																	\
	static int prefix##GreaterThan(const void *a, const void *b)		\
    {																	\
		type an = *(type *)a;											\
		type bn = *(type *)b;											\
		if (an > bn)													\
			return 1;													\
		else if (an == bn)												\
			return 0;													\
		else															\
			return -1;													\
	}																	\
	static int prefix##LessThan(const void *a, const void *b)			\
	{																	\
		return prefix##GreaterThan (b, a);								\
	}																	\
	extern void prefix##ArraySort (prefix##Array *const current, bool descendingOrder) \
	{																	\
		if (descendingOrder)											\
			qsort (current->array, current->count, sizeof (type), prefix##GreaterThan); \
		else															\
			qsort (current->array, current->count, sizeof (type), prefix##LessThan); \
	}

/* We expect the linker we use is enough clever to delete dead code. */
impNumArray(char, Char, char)
impNumArray(uchar, Uchar, unsigned char)
impNumArray(int, Int, int)
impNumArray(uint, Uint, unsigned int)
impNumArray(long, Long, long)
impNumArray(ulong, Ulong, unsigned long)