-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
176 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
111.111.123.222 | ||
192.168.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#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++) | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef _STRING_SET_H | ||
#define _STRING_SET_H | ||
|
||
struct str_set { | ||
int length; | ||
int size; | ||
struct member { | ||
struct member *link; | ||
const char *element; | ||
} **buckets; | ||
}; | ||
|
||
struct str_set *str_set_new(); | ||
void str_set_put(struct str_set *set, const char *elem); | ||
char *str_set_remove(struct str_set *set, const char *elem); | ||
int str_set_length(struct str_set *set); | ||
int is_str_set_member(struct str_set *set, const void *elem); | ||
void str_set_free(struct str_set **set); | ||
|
||
#endif |