[go: up one dir, main page]

File: string_set.c

package info (click to toggle)
cctools 9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,624 kB
  • sloc: ansic: 192,539; python: 20,827; cpp: 20,199; sh: 11,719; perl: 4,106; xml: 3,688; makefile: 1,224
file content (295 lines) | stat: -rw-r--r-- 5,266 bytes parent folder | download | duplicates (3)
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
/*
Copyright (C) 2013- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/

#include "string_set.h"
#include "hash_table.h"

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

#define DEFAULT_SIZE 127
#define DEFAULT_LOAD 0.75
#define DEFAULT_FUNC hash_string

struct entry {
	char *element;
	unsigned hash;
	struct entry *next;
};

struct string_set {
	hash_func_t hash_func;
	int size;
	int bucket_count;
	struct entry **buckets;
	int ibucket;
	struct entry *ientry;
};

struct string_set *string_set_create(int bucket_count, hash_func_t func)
{
	struct string_set *s;

	s = (struct string_set *) malloc(sizeof(struct string_set));
	if(!s)
		return 0;

	if(bucket_count == 0)
		bucket_count = DEFAULT_SIZE;

	if(!func)
		func = DEFAULT_FUNC;

	s->hash_func = func;
	s->bucket_count = bucket_count;
	s->buckets = (struct entry **) calloc(bucket_count, sizeof(struct entry *));
	if(!s->buckets) {
		free(s);
		return 0;
	}

	s->size = 0;

	return s;
}

struct string_set *string_set_duplicate(struct string_set *s)
{
	struct string_set *s2;

	s2 = string_set_create(0, s->hash_func);
	string_set_first_element(s);
	char *element;
	while(string_set_next_element(s, &element))
		string_set_insert(s2, element);

	return s2;

}

struct string_set *string_set_union(struct string_set *s1, struct string_set *s2)
{

	struct string_set *s = string_set_duplicate(s1);

	string_set_first_element(s2);
	char *element;
	while(string_set_next_element(s2, &element))
		string_set_insert(s, element);

	return s;

}

void string_set_clear(struct string_set *s)
{
	struct entry *e, *f;
	int i;

	for(i = 0; i < s->bucket_count; i++) {
		e = s->buckets[i];
		while(e) {
			f = e->next;
			free(e->element);
			free(e);
			e = f;
		}
	}

	for(i = 0; i < s->bucket_count; i++) {
		s->buckets[i] = 0;
	}
}

void string_set_delete(struct string_set *s)
{
	string_set_clear(s);
	free(s->buckets);
	free(s);
}

int string_set_size(struct string_set *s)
{
	return s->size;
}

int string_set_lookup(struct string_set *s, const char *element)
{
	struct entry *e;
	uint64_t hash, index;

	hash = s->hash_func(element);
	index = hash % s->bucket_count;
	e = s->buckets[index];

	while(e) {
		if(hash == e->hash && !strcmp(element, e->element)) {
			return 1;
		}
		e = e->next;
	}

	return 0;
}

static int string_set_double_buckets(struct string_set *s)
{
	struct string_set *sn = string_set_create(2 * s->bucket_count, s->hash_func);

	if(!sn)
		return 0;

	/* Move elements to new string_set */
	char *element;
	string_set_first_element(s);
	while(string_set_next_element(s, &element) )
		if(!string_set_insert(sn, element))
		{
			string_set_delete(sn);
			return 0;
		}

	/* Delete all elements */
	struct entry *e, *f;
	int i;
	for(i = 0; i < s->bucket_count; i++) {
		e = s->buckets[i];
		while(e) {
			f = e->next;
			free(e->element);
			free(e);
			e = f;
		}
	}

	/* Make the old point to the new */
	free(s->buckets);
	s->buckets      = sn->buckets;
	s->bucket_count = sn->bucket_count;
	s->size         = sn->size;

	/* Delete reference to new, so old is safe */
	free(sn);

	return 1;
}

int string_set_insert(struct string_set *s, const char *element)
{
	struct entry *e;
	uint64_t hash, index;

	if( ((float) s->size / s->bucket_count) > DEFAULT_LOAD )
		string_set_double_buckets(s);

	hash = s->hash_func(element);
	index = hash % s->bucket_count;
	e = s->buckets[index];

	while(e) {
		if(hash == e->hash && !strcmp(element, e->element)) {
			return 1;
		}
		e = e->next;
	}

	e = (struct entry *) malloc(sizeof(struct entry));
	if(!e)
		return 0;

	e->element = strdup(element);
	if(!e->element) {
		free(e);
		return 0;
	}

	e->hash = hash;
	e->next = s->buckets[index];
	s->buckets[index] = e;
	s->size++;

	return 1;
}

int string_set_insert_string_set(struct string_set *s, struct string_set *s2)
{
	string_set_first_element(s2);
	int additions = 0;
	char *element;
	while(string_set_next_element(s2, &element)){
		additions += string_set_insert(s, element);
	}

	return additions;
}

int string_set_push(struct string_set *s, const char *element)
{
  return string_set_insert(s, element);
}

int string_set_remove(struct string_set *s, const char *element)
{
	struct entry *e, *f;
	uint64_t hash, index;

	hash = s->hash_func(element);
	index = hash % s->bucket_count;
	e = s->buckets[index];
	f = 0;

	while(e) {
		if(hash == e->hash && !strcmp(element, e->element)) {
			if(f) {
				f->next = e->next;
			} else {
				s->buckets[index] = e->next;
			}
			free(e->element);
			free(e);
			s->size--;
			return 1;
		}
		f = e;
		e = e->next;
	}

	return 0;
}

void string_set_first_element(struct string_set *s)
{
	s->ientry = 0;
	for(s->ibucket = 0; s->ibucket < s->bucket_count; s->ibucket++) {
		s->ientry = s->buckets[s->ibucket];
		if(s->ientry)
			break;
	}
}

int string_set_next_element(struct string_set *s, char **element)
{
	if(s->ientry) {
		*element = (char *) s->ientry->element;

		s->ientry = s->ientry->next;
		if(!s->ientry) {
			s->ibucket++;
			for(; s->ibucket < s->bucket_count; s->ibucket++) {
				s->ientry = s->buckets[s->ibucket];
				if(s->ientry)
					break;
			}
		}

		return 1;
	} else {
		return 0;
	}
}

/* vim: set noexpandtab tabstop=4: */