File: alist.c

package info (click to toggle)
teknap 1.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,428 kB
  • ctags: 6,297
  • sloc: ansic: 55,591; makefile: 545; sh: 112
file content (307 lines) | stat: -rw-r--r-- 6,715 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
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
/*
 * alist.c -- resizeable arrays.
 * Written by Jeremy Nelson
 * Copyright 1997 EPIC Software Labs
 *
 * This file presumes a good deal of chicanery.  Specifically, it assumes
 * that your compiler will allocate disparate structures congruently as 
 * long as the members match as to their type and location.  This is
 * critically important for how this code works, and all hell will break
 * loose if your compiler doesnt do this.  Every compiler i know of does
 * it, which is why im assuming it, even though im not allowed to assume it.
 *
 * This file is hideous.  Ill kill each and every one of you who made
 * me do this. ;-)
 * $Id: alist.c,v 1.1.1.1 2000/06/22 09:35:35 edwards Exp $
 */

#define _cs_alist_hash_
#define _ci_alist_hash_
#include "alist.h"
#include "ircaux.h"
#include "output.h"


u_32int_t	bin_ints = 0;
u_32int_t	lin_ints = 0;
u_32int_t	bin_chars = 0;
u_32int_t	lin_chars = 0;
u_32int_t	alist_searches = 0;
u_32int_t	char_searches = 0;
 
static void check_array_size (Array *list);
void move_array_items (Array *list, int start, int end, int dir);

Array_item *add_to_array (Array *array, Array_item *item)
{
	int count;
	int location = 0;
	Array_item *ret = (void *) 0 ;
	u_32int_t       mask;    

	if (array->hash == HASH_INSENSITIVE)
		item->hash = ci_alist_hash(item->name, &mask);
	else
		item->hash = cs_alist_hash(item->name, &mask);

	check_array_size(array);
	if (array->max)
	{
		find_array_item(array, item->name, &count, &location);
		if (count < 0)
		{
			ret = ((Array_item *) (( array ) -> list [ (  location ) ])) ;
			array->max--;
		}
		else
			move_array_items(array, location, array->max, 1);
	}

	array->list[location] = item;
	array->max++;
	return ret;
}

 


Array_item *remove_from_array (Array *array, char *name)
{
	int count, location = 0;

	if (array->max)
	{
		find_array_item(array, name, &count, &location);
		if (count >= 0)
			return (void *) 0 ;

		return array_pop(array, location);
	}
	return (void *) 0 ;	 
}

 
Array_item *array_pop (Array *array, int which)
{
	Array_item *ret = (void *) 0 ;

	if (which < 0 || which >= array->max)
		return (void *) 0 ;

	ret = ((Array_item *) (( array ) -> list [ (  which ) ])) ;
	move_array_items(array, which + 1, array->max, -1);
	array->max--;
	return ret;
}
  
 


Array_item *remove_all_from_array (Array *array, char *name)
{
	int count, location = 0;
	Array_item *ret = (void *) 0 ;

	if (array->max)
	{
		find_array_item(array, name, &count, &location);
		if (count == 0)
			return (void *) 0 ;
		ret = ((Array_item *) (( array ) -> list [ (  location ) ])) ;
		move_array_items(array, location + 1, array->max, -1);
		array->max--;
		return ret;
	}
	return (void *) 0 ;	 
}

Array_item *array_lookup (Array *array, char *name, int wild, int delete)
{
	int count, location;

	if (delete)
		return remove_from_array(array, name);
	else
		return find_array_item(array, name, &count, &location);
}

static void check_array_size (Array *array)
{
	if (array->total_max == 0)
		array->total_max = 6;		 
	else if (array->max == array->total_max-1)
		array->total_max *= 2;
	else if (array->max * 3 < array->total_max)
		array->total_max /= 2;
	else
		return;
	n_realloc     ((void **)& ( array->list ), sizeof(  Array_item * ) * (  array->total_max ), __FILE__, __LINE__) ;
}

 




void move_array_items (Array *array, int start, int end, int dir)
{
	int i;

	if (dir > 0)
	{
		for (i = end; i >= start; i--)
			((( array ) -> list [ (  i + dir ) ]))  = ((Array_item *) (( array ) -> list [ (  i ) ])) ;
		for (i = dir; i > 0; i--)
			((( array ) -> list [ (  start + i - 1 ) ]))  = (void *) 0 ;
	}
	else if (dir < 0)
	{
		for (i = start; i <= end; i++)
			((( array ) -> list [ (  i + dir ) ]))  = ((Array_item *) (( array ) -> list [ (  i ) ])) ;
		for (i = end - dir + 1; i <= end; i++)
			((( array ) -> list [ (  i ) ]))  = (void *) 0 ;
	}
}

Array_item *find_array_item (Array *set, char *name, int *cnt, int *loc)
{
	size_t	len = strlen(name);
	int	c = 0, 
		pos = 0, 
		min, 
		max;
	u_32int_t mask, hash;

	if (set->hash == HASH_INSENSITIVE)
		hash = ci_alist_hash(name, &mask);
	else
		hash = cs_alist_hash(name, &mask);
	
	*cnt = 0;
	if (!set->list || !set->max)
	{
		*loc = 0;
		return (void *) 0 ;
	}

	alist_searches++;
	max = set->max - 1;
	min = 0;
	
	while (max >= min)
	{
		bin_ints++;
		pos = (max - min) / 2 + min;
		c = (hash & mask) - (((Array_item *) (( set ) -> list [ (  pos ) ])) ->hash & mask);
		if (c == 0)
			break;
		else if (c < 0)
			max = pos - 1;
		else
			min = pos + 1;
	}

	if (c != 0)
	{
		if (c > 0)
			*loc = pos + 1;
		else
			*loc = pos;
		return (void *) 0 ;
	}

	min = max = pos;
	while ((min > 0) && (hash & mask) == (((Array_item *) (( set ) -> list [ (  min ) ])) ->hash & mask))
		min--, lin_ints++;
	while ((max < set->max - 1) && (hash &mask) == (((Array_item *) (( set ) -> list [ (  max ) ])) ->hash & mask))
		max++, lin_ints++;

	char_searches++;

	while (max >= min)
	{
		bin_chars++;
		pos = (max - min) / 2 + min;
		c = set->func(name, ((Array_item *) (( set ) -> list [ (  pos ) ])) ->name, len);
		if (c == 0)
			break;
		else if (c < 0)
			max = pos - 1;
		else
			min = pos + 1;
	}


	if (c != 0)
	{
		if (c > 0)
			*loc = pos + 1;
		else
			*loc = pos;
		return (void *) 0 ;
	}
	*cnt = 1;
	min = pos - 1;
	while (min >= 0 && !set->func(name, ((Array_item *) (( set ) -> list [ (  min ) ])) ->name, len))
		(*cnt)++, min--, lin_chars++;
	min++;
	max = pos + 1;
	while (max < set->max && !set->func(name, ((Array_item *) (( set ) -> list [ (  max ) ])) ->name, len))
		(*cnt)++, max++, lin_chars++;

	if (strlen(((Array_item *) (( set ) -> list [ (  min ) ])) ->name) == len)
		*cnt *= -1;

	if (loc)
		*loc = min;

	return ((Array_item *) (( set ) -> list [ (  min ) ])) ;
}

void * find_fixed_array_item (void *list, size_t size, int howmany, char *name, int *cnt, int *loc)
{
	int	len = strlen(name),
		min = 0,
		max = howmany,
		old_pos = -1,
		pos,
		c;

	*cnt = 0;

	while (1)
	{
		pos = (max + min) / 2;
		if (pos == old_pos)
		{
			*loc = pos;
			return (void *) 0 ;
		}
		old_pos = pos;

		c = strncmp(name, (*(Array_item *) ( list  + (   pos  *   size  ))) .name, len);
		if (c == 0)
			break;
		else if (c > 0)
			min = pos;
		else
			max = pos;
	}
	*cnt = 1;

	min = pos - 1;
	while (min >= 0 && !strncmp(name, (*(Array_item *) ( list  + (   min  *   size  ))) .name, len))
		(*cnt)++, min--;
	min++;

	max = pos + 1;
	while ((max < howmany) && !strncmp(name, (*(Array_item *) ( list  + (   max  *   size  ))) .name, len))
		(*cnt)++, max++;

	if (strlen((*(Array_item *) ( list  + (   min  *   size  ))) .name) == len)
		*cnt *= -1;
	if (loc)
		*loc = min;
	return (void *)& (*(Array_item *) ( list  + (   min  *   size  ))) ;
}