File: alist.c

package info (click to toggle)
epic5 3.0.3-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 5,328 kB
  • sloc: ansic: 75,810; makefile: 648; ruby: 227; python: 215; sh: 78; perl: 13
file content (345 lines) | stat: -rw-r--r-- 8,987 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * alist.c -- resizeable arrays (dicts)
 *
 * Copyright 1997, 2015 EPIC Software Labs
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. All redistributions, whether of source code or in binary form must
 *    retain the the above copyright notice, the above paragraph (the one
 *    permitting redistribution), this list of conditions, and the following
 *    disclaimer.
 * 2. The names of the author(s) may not be used to endorse or promote 
 *    products derived from this software without specific prior written
 *    permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 * SUCH DAMAGE.
 */
#define __need_cs_alist_hash__
#define __need_ci_alist_hash__
#include "irc.h"
#include "alist.h"
#include "ircaux.h"
#include "output.h"

#define ALIST_ITEM(alist, loc) ((alist_item_ *) ((alist) -> list [ (loc) ]))
#define LALIST_ITEM(alist, loc) (((alist) -> list [ (loc) ]))

/* Function decls */
static	void	check_alist_size (alist *list);
	void 	move_alist_items (alist *list, int start, int end, int dir);

alist **all_alists = NULL;
int	all_alists_size = 0;

/*
 * Returns an entry that has been displaced, if any.
 * XXX 'item' shall be replaced with 'char *name' and 'void *data'
 */
void *	add_to_alist (alist *a, const char *name, void *item)
{
	int 		count;
	int 		location = 0;
	void *		ret = NULL;
	uint32_t	mask; 	/* Dummy var */
	alist_item_ *	item_;

	/* Initialize our internal item */
	item_ = (alist_item_ *)new_malloc(sizeof(alist_item_));
	item_->name = NULL;
	malloc_strcpy(&item_->name, name);	
	if (a->hash == HASH_INSENSITIVE)
		item_->hash = ci_alist_hash(item_->name, &mask);
	else
		item_->hash = cs_alist_hash(item_->name, &mask);
	item_->data = item;

	check_alist_size(a);
	if (a->max)
	{
		find_alist_item(a, name, &count, &location);
		if (count < 0)
		{
			ret = ALIST_ITEM(a, location)->data;
			a->max--;
		}
		else
			move_alist_items(a, location, a->max, 1);
	}

	a->list[location] = item_;
	a->max++;
	return ret;
}

/*
 * Returns the entry that has been removed, if any.
 */
void *	remove_from_alist (alist *a, const char *name)
{
	int 	count, 
		location = 0;

	if (a->max)
	{
		find_alist_item(a, name, &count, &location);
		if (count >= 0)
			return NULL;

		return alist_pop(a, location);
	}
	return NULL;	/* Cant delete whats not there */
}

/* Remove the 'which'th item from the given alist */
void *	alist_pop (alist *a, int which)
{
	alist_item_ *item_ = NULL;
	void *ret = NULL;

	if (which < 0 || which >= a->max)
		return NULL;

	item_ = ALIST_ITEM(a, which);
	ret = item_->data;

	move_alist_items(a, which + 1, a->max, -1);
	a->max--;
	check_alist_size(a);

	new_free(&item_->name);
	new_free((char **)&item_);
	return ret;
}

void *	alist_lookup (alist *a, const char *name, int wild, int rem)
{
	int 	count, 
		location;

	if (rem)
		return remove_from_alist(a, name);
	else
		return find_alist_item(a, name, &count, &location);
}

static void	check_alist_size (alist *a)
{
	if (a->total_max && (a->total_max < a->max))
		panic(1, "alist->max < alist->total_max");

	if (a->total_max == 0)
	{
		new_free(&a->list);		/* Immortality isn't necessarily a good thing. */
		a->total_max = 6;		/* Good size to start with */
	}
	else if (a->max == a->total_max - 1) /* Colten suggested this */
		a->total_max *= 2;
	else if ((a->total_max > 6) && (a->max * 3 < a->total_max))
		a->total_max /= 2;
	else
		return;

	RESIZE(a->list, alist_item_ *, a->total_max);
}

/*
 * Move ``start'' through ``end'' alist elements ``dir'' places up
 * in the alist.  If ``dir'' is negative, move them down in the alist.
 * Fill in the vacated spots with NULLs.
 */
void	move_alist_items (alist *a, int start, int end, int dir)
{
	int 	i;

	if (dir > 0)
	{
		for (i = end; i >= start; i--)
			LALIST_ITEM(a, i + dir) = ALIST_ITEM(a, i);
		for (i = dir; i > 0; i--)
			LALIST_ITEM(a, start + i - 1) = NULL;
	}
	else if (dir < 0)
	{
		for (i = start; i <= end; i++)
			LALIST_ITEM(a, i + dir) = ALIST_ITEM(a, i);
		for (i = end - dir + 1; i <= end; i++)
			LALIST_ITEM(a, i) = NULL;
	}
}


/*
 * This is just a generalization of the old function  ``find_command''
 *
 * You give it an alist_item and a name, and it gives you the number of
 * items that are completed by that name, and where you could find/put that
 * item in the list.  It returns the alist_item most appropriate.
 *
 * If ``cnt'' is less than -1, then there was one exact match and one or
 *	more ambiguous matches in addition.  The exact match's location 
 *	is put into ``loc'' and its entry is returned.  The ambigous matches
 *	are (of course) immediately subsequent to it.
 *
 * If ``cnt'' is -1, then there was one exact match.  Its location is
 *	put into ``loc'' and its entry is returned.
 *
 * If ``cnt'' is zero, then there were no matches for ``name'', but ``loc''
 * 	is set to the location in the alist in which you could place the
 * 	specified name in the sorted list.
 *
 * If ``cnt'' is one, then there was one command that non-ambiguously 
 * 	completes ``name''
 *
 * If ``cnt'' is greater than one, then there was exactly ``cnt'' number
 *	of entries that completed ``name'', but they are all ambiguous.
 *	The entry that is lowest alphabetically is returned, and its
 *	location is put into ``loc''.
 */
void *	find_alist_item (alist *set, const char *name, int *cnt, int *loc)
{
	size_t		len = strlen(name);
	intmax_t	c = 0;
	int		pos = 0, 
			tospot, /* :-) */
			min, 
			max;
	uint32_t	mask;
	uint32_t	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 NULL;
	}

	/*
	 * The first search is a cheap refinement.  Using the hash
	 * value, find a range of symbols that have the same first
	 * four letters as 'name'.  Since we're doing all integer
	 * comparisons, its cheaper than the full blown string compares.
	 */
	/*
	 * OK, well all of that has been rolled into these two loops,
	 * designed to find the start and end of the range directly.
	 */

	tospot = max = set->max - 1;
	min = 0;

	while (max >= min)
	{
		pos = (max - min) / 2 + min;
		c = (intmax_t)(hash & mask) - (intmax_t)(ALIST_ITEM(set, pos)->hash & mask);
		if (c == 0) {
			c = set->func(name, ALIST_ITEM(set, pos)->name, len);
		}
		if (c == 0) {
			if (max == pos)
				break;
			max = pos;
		}
		else if (c < 0)
			tospot = max = pos - 1;
		else
			min = pos + 1;
	}

	/*
	 * If we can't find a symbol that qualifies, then we can just drop
	 * out here.  This is good because a "pass" (lookup for a symbol that
	 * does not exist) requires only cheap integer comparisons.
	 */
	if (c != 0)
	{
		if (c > 0)
			*loc = pos + 1;
		else
			*loc = pos;
		return NULL;
	}

	/*
	 * At this point, min is set to the first matching name in
	 * the range and tospot is set to a higher position than
	 * the last.  These are used to refine the next search.
	 */

	max = tospot;
	tospot = pos;

	while (max >= min)
	{
		pos = (min - max) / 2 + max;  /* Don't ask */
		c = (intmax_t)(hash & mask) - (intmax_t)(ALIST_ITEM(set, pos)->hash & mask);
		if (c == 0) {
			c = set->func(name, ALIST_ITEM(set, pos)->name, len);
		}
		if (c == 0) {
			if (min == pos)
				break;
			min = pos;
		}
		else if (c < 0)
			max = pos - 1;
		else
			min = pos + 1;
	}

	min = tospot;

	/*
	 * If we've gotten this far, then we've found at least
	 * one appropriate entry.  So we set *cnt to one
	 */
	*cnt = 1 + pos - min;

	/*
	 * Alphabetically, the string that would be identical to
	 * 'name' would be the first item in a string of items that
	 * all began with 'name'.  That is to say, if there is an
	 * exact match, its sitting under item 'min'.  So we check
	 * for that and whack the count appropriately.
	 */
	if (0 == (ALIST_ITEM(set, min)->name)[len])
		*cnt *= -1;

	/*
	 * Then we tell the caller where the lowest match is,
	 * in case they want to insert here.
	 */
	if (loc)
		*loc = min;

	/*
	 * Then we return the first item that matches.
	 */
	return ALIST_ITEM(set, min)->data;
}

void *	get_alist_item (alist *set, int location)
{
	if (location < 0 || location > set->max)
		return NULL;

	return ALIST_ITEM(set, location)->data;
}