diff --git a/Makefile b/Makefile index 775b2ed..1344536 100644 --- a/Makefile +++ b/Makefile @@ -58,10 +58,10 @@ TARGET = graftcp $(GRAFTCP_LOCAL_BIN) all:: $(TARGET) -graftcp: main.o graftcp.o util.o string-set.o cidr-trie.o conf.o +graftcp: main.o graftcp.o util.o cidr-trie.o conf.o $(CC) $^ -o $@ -libgraftcp.a: graftcp.o util.o string-set.o cidr-trie.o conf.o +libgraftcp.a: graftcp.o util.o cidr-trie.o conf.o $(AR) rcs $@ $^ %.o: %.c diff --git a/string-set.c b/string-set.c deleted file mode 100644 index 8625758..0000000 --- a/string-set.c +++ /dev/null @@ -1,136 +0,0 @@ -/* - * graftcp - * Copyright (C) 2018 Hmgle - * - * 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 -#include -#include -#include -#include - -#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])); - if (!set) { - fprintf(stderr, "calloc: %s\n", strerror(errno)); - exit(1); - } - 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)); - if (!p) { - fprintf(stderr, "calloc: %s\n", strerror(errno)); - exit(1); - } - 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); -} diff --git a/string-set.h b/string-set.h deleted file mode 100644 index fcb8845..0000000 --- a/string-set.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * graftcp - * Copyright (C) 2018 Hmgle - * - * 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. - */ -#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