forked from hmgle/graftcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-set.c
More file actions
126 lines (112 loc) · 2.54 KB
/
Copy pathstring-set.c
File metadata and controls
126 lines (112 loc) · 2.54 KB
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
/*
* graftcp
* Copyright (C) 2018 Hmgle <dustgle@gmail.com>
*
* 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 3 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.
*/
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "string-set.h"
/* see http://www.cse.yorku.ca/~oz/hash.html */
static unsigned long str_hash(const char *str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++) != '\0')
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
struct str_set *str_set_new()
{
struct str_set *set;
int i;
set = calloc(1, sizeof(*set) + 509 * sizeof(set->buckets[0]));
set->size = 509;
set->buckets = (struct member **)(set + 1);
for (i = 0; i < set->size; i++)
set->buckets[i] = NULL;
set->length = 0;
return set;
}
void str_set_put(struct str_set *set, const char *elem)
{
int i;
struct member *p;
assert(set);
assert(elem);
i = str_hash(elem) % set->size;
for (p = set->buckets[i]; p; p = p->link) {
if (strcmp(elem, p->element) == 0)
break;
}
if (p == NULL) {
p = calloc(1, sizeof(*p));
p->element = elem;
p->link = set->buckets[i];
set->buckets[i] = p;
set->length++;
} else {
p->element = elem;
}
}
char *str_set_remove(struct str_set *set, const char *elem)
{
int i;
struct member **pp;
assert(set);
assert(elem);
i = str_hash(elem) % set->size;
for (pp = &set->buckets[i]; *pp; pp = &(*pp)->link) {
if (strcmp(elem, (*pp)->element) == 0) {
struct member *p = *pp;
*pp = p->link;
elem = p->element;
free(p);
set->length--;
return (char *)elem;
}
}
return NULL;
}
int str_set_length(struct str_set *set)
{
assert(set);
return set->length;
}
int is_str_set_member(struct str_set *set, const void *elem)
{
int i;
struct member *p;
assert(set);
assert(elem);
i = str_hash(elem) % set->size;
for (p = set->buckets[i]; p; p = p->link) {
if (strcmp(elem, p->element) == 0)
break;
}
return p != NULL;
}
void str_set_free(struct str_set **set)
{
assert(set && *set);
if ((*set)->length > 0) {
int i;
struct member *p, *q;
for (i = 0; i < (*set)->size; i++) {
for (p = (*set)->buckets[i]; p; p = q) {
q = p->link;
free(p);
}
}
}
free(*set);
}