Showing
11 changed files
with
377 additions
and
10 deletions
| ... | ... | @@ -71,11 +71,12 @@ AC_CONFIG_FILES([Makefile |
| 71 | 71 | tests/Makefile |
| 72 | 72 | src/Makefile |
| 73 | 73 | src/cbuf/Makefile |
| 74 | + src/dynarray/Makefile | |
| 74 | 75 | src/hash/Makefile |
| 76 | + src/heap/Makefile | |
| 75 | 77 | src/list/Makefile |
| 76 | 78 | src/queue/Makefile |
| 79 | + src/set/Makefile | |
| 77 | 80 | src/tree/Makefile |
| 78 | - src/dynarray/Makefile | |
| 79 | - src/heap/Makefile | |
| 80 | 81 | include/Makefile]) |
| 81 | 82 | AC_OUTPUT | ... | ... |
| 1 | 1 | nobase_include_HEADERS = trdata.h \ |
| 2 | 2 | tr/cbuf.h \ |
| 3 | + tr/dynarray.h \ | |
| 3 | 4 | tr/hash.h \ |
| 5 | + tr/heap.h \ | |
| 4 | 6 | tr/hash_value.h \ |
| 5 | 7 | tr/list.h \ |
| 6 | 8 | tr/queue.h \ |
| 9 | + tr/set.h \ | |
| 7 | 10 | tr/tree.h \ |
| 8 | - tr/dynarray.h \ | |
| 9 | - tr/heap.h \ | |
| 10 | 11 | tr/interface/hashable.h \ |
| 11 | 12 | tr/interface/iterable.h | ... | ... |
include/tr/set.h
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * Holds requests ready for processing. | |
| 4 | + * | |
| 5 | + * \todo change this to a real queue. | |
| 6 | + * | |
| 7 | + * \author Georg Hopp | |
| 8 | + * | |
| 9 | + * \copyright | |
| 10 | + * Copyright © 2014 Georg Hopp | |
| 11 | + * | |
| 12 | + * This program is free software: you can redistribute it and/or modify | |
| 13 | + * it under the terms of the GNU General Public License as published by | |
| 14 | + * the Free Software Foundation, either version 3 of the License, or | |
| 15 | + * (at your option) any later version. | |
| 16 | + * | |
| 17 | + * This program is distributed in the hope that it will be useful, | |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 20 | + * GNU General Public License for more details. | |
| 21 | + * | |
| 22 | + * You should have received a copy of the GNU General Public License | |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +#ifndef __TR_SET_H__ | |
| 27 | +#define __TR_SET_H__ | |
| 28 | + | |
| 29 | +#include <sys/types.h> | |
| 30 | + | |
| 31 | +#include "trbase.h" | |
| 32 | + | |
| 33 | + | |
| 34 | +TR_CLASS(TR_Set) { | |
| 35 | + size_t size; | |
| 36 | + size_t used; | |
| 37 | + void ** data; | |
| 38 | + int free_msgs; | |
| 39 | + | |
| 40 | + // for iterable interface | |
| 41 | + size_t current; | |
| 42 | +}; | |
| 43 | +TR_INSTANCE_INIT(TR_Set); | |
| 44 | +TR_CLASSVARS_DECL(TR_Set) {}; | |
| 45 | + | |
| 46 | +void TR_setAdd(TR_Set, void *); | |
| 47 | +size_t TR_setFind(TR_Set, void *); | |
| 48 | +void TR_setDelete(TR_Set, void *); | |
| 49 | + | |
| 50 | +#define TR_setEmpty(this) (this->used == 0) | |
| 51 | +#define TR_setSize(this) ((this)->used) | |
| 52 | + | |
| 53 | +#endif // __TR_SET_H__ | |
| 54 | + | |
| 55 | +// vim: set ts=4 sw=4: | ... | ... |
| ... | ... | @@ -2,13 +2,14 @@ |
| 2 | 2 | #define __TR_DATA_H__ |
| 3 | 3 | |
| 4 | 4 | #include "tr/cbuf.h" |
| 5 | +#include "tr/dynarray.h" | |
| 5 | 6 | #include "tr/hash.h" |
| 6 | 7 | #include "tr/hash_value.h" |
| 8 | +#include "tr/heap.h" | |
| 7 | 9 | #include "tr/list.h" |
| 8 | 10 | #include "tr/queue.h" |
| 11 | +#include "tr/set.h" | |
| 9 | 12 | #include "tr/tree.h" |
| 10 | -#include "tr/dynarray.h" | |
| 11 | -#include "tr/heap.h" | |
| 12 | 13 | #include "tr/interface/hashable.h" |
| 13 | 14 | #include "tr/interface/iterable.h" |
| 14 | 15 | ... | ... |
| ... | ... | @@ -5,12 +5,13 @@ AM_CFLAGS += -I../include/ -std=c99 |
| 5 | 5 | AM_LDFLAGS += |
| 6 | 6 | |
| 7 | 7 | TRDATALIBS = cbuf/libcbuf.la \ |
| 8 | + dynarray/libdynarray.la \ | |
| 8 | 9 | hash/libhash.la \ |
| 10 | + heap/libheap.la \ | |
| 9 | 11 | list/liblist.la \ |
| 10 | 12 | queue/libqueue.la \ |
| 11 | - tree/libtree.la \ | |
| 12 | - dynarray/libdynarray.la \ | |
| 13 | - heap/libheap.la | |
| 13 | + set/libset.la \ | |
| 14 | + tree/libtree.la | |
| 14 | 15 | |
| 15 | 16 | ITERABLE = interface/iterable.c |
| 16 | 17 | |
| ... | ... | @@ -21,4 +22,4 @@ libtrdata_la_CFLAGS = $(AM_CFLAGS) |
| 21 | 22 | libtrdata_la_LIBADD = $(TRDATALIBS) |
| 22 | 23 | libtrdata_la_LDFLAGS = -version-info 0:0:0 $(AM_LDFLAGS) |
| 23 | 24 | |
| 24 | -SUBDIRS = cbuf hash list queue tree dynarray heap | |
| 25 | +SUBDIRS = cbuf dynarray hash heap list queue set tree | ... | ... |
src/set/Makefile.am
0 → 100644
| 1 | +ACLOCAL_AMFLAGS = -I m4 | |
| 2 | +AUTOMAKE_OPTIONS = subdir-objects | |
| 3 | + | |
| 4 | +AM_CFLAGS += -I../../include/ -std=c99 | |
| 5 | +AM_LDFLAGS += | |
| 6 | + | |
| 7 | +SET = set.c \ | |
| 8 | + add.c \ | |
| 9 | + find.c \ | |
| 10 | + delete.c | |
| 11 | + | |
| 12 | +noinst_LTLIBRARIES = libset.la | |
| 13 | + | |
| 14 | +libset_la_SOURCES = $(SET) | |
| 15 | +libset_la_CFLAGS = $(AM_CFLAGS) | |
| 16 | +libset_la_LIBADD = $(AM_LDFLAGS) | ... | ... |
src/set/_resize.h
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2014 Georg Hopp | |
| 8 | + * | |
| 9 | + * This program is free software: you can redistribute it and/or modify | |
| 10 | + * it under the terms of the GNU General Public License as published by | |
| 11 | + * the Free Software Foundation, either version 3 of the License, or | |
| 12 | + * (at your option) any later version. | |
| 13 | + * | |
| 14 | + * This program is distributed in the hope that it will be useful, | |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | + * GNU General Public License for more details. | |
| 18 | + * | |
| 19 | + * You should have received a copy of the GNU General Public License | |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 21 | + */ | |
| 22 | +#ifndef __TR_SET_RESIZE_H__ | |
| 23 | +#define __TR_SET_RESIZE_H__ | |
| 24 | + | |
| 25 | +#include "trbase.h" | |
| 26 | +#include "tr/set.h" | |
| 27 | + | |
| 28 | +inline | |
| 29 | +void | |
| 30 | +_TR_setResize(TR_Set this) | |
| 31 | +{ | |
| 32 | + void ** new = TR_calloc(this->size+1, sizeof(void*)); | |
| 33 | + size_t new_size = TR_getUsableSize(new) / sizeof(void *); | |
| 34 | + | |
| 35 | +#define GROWTH (new_size - this->size) | |
| 36 | + | |
| 37 | + memcpy(new, this->data, this->size * sizeof(void*)); | |
| 38 | + TR_MEM_FREE(this->data); | |
| 39 | + this->size = new_size; | |
| 40 | + this->data = new; | |
| 41 | +} | |
| 42 | + | |
| 43 | +#endif // __TR_SET_RESIZE_H__ | |
| 44 | + | |
| 45 | +// vim: set ts=4 sw=4: | ... | ... |
src/set/add.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2014 Georg Hopp | |
| 8 | + * | |
| 9 | + * This program is free software: you can redistribute it and/or modify | |
| 10 | + * it under the terms of the GNU General Public License as published by | |
| 11 | + * the Free Software Foundation, either version 3 of the License, or | |
| 12 | + * (at your option) any later version. | |
| 13 | + * | |
| 14 | + * This program is distributed in the hope that it will be useful, | |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | + * GNU General Public License for more details. | |
| 18 | + * | |
| 19 | + * You should have received a copy of the GNU General Public License | |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 21 | + */ | |
| 22 | + | |
| 23 | +#include "trbase.h" | |
| 24 | +#include "tr/set.h" | |
| 25 | + | |
| 26 | +#include "_resize.h" | |
| 27 | + | |
| 28 | +extern inline void _TR_setResize(TR_Set); | |
| 29 | + | |
| 30 | +void | |
| 31 | +TR_setAdd(TR_Set this, void * msg) | |
| 32 | +{ | |
| 33 | + size_t pos = TR_setFind(this, msg); | |
| 34 | + | |
| 35 | + if (this->data[pos] == msg) { | |
| 36 | + return; | |
| 37 | + } | |
| 38 | + | |
| 39 | + if (this->used == this->size) { | |
| 40 | + _TR_setResize(this); | |
| 41 | + } | |
| 42 | + | |
| 43 | + if (pos != this->used) { | |
| 44 | + memmove( | |
| 45 | + this->data+pos+1, | |
| 46 | + this->data+pos, | |
| 47 | + (this->used-pos)*sizeof(void*)); | |
| 48 | + } | |
| 49 | + | |
| 50 | + this->data[pos] = msg; | |
| 51 | + ++this->used; | |
| 52 | +} | |
| 53 | + | |
| 54 | +// vim: set ts=4 sw=4: | ... | ... |
src/set/delete.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2014 Georg Hopp | |
| 8 | + * | |
| 9 | + * This program is free software: you can redistribute it and/or modify | |
| 10 | + * it under the terms of the GNU General Public License as published by | |
| 11 | + * the Free Software Foundation, either version 3 of the License, or | |
| 12 | + * (at your option) any later version. | |
| 13 | + * | |
| 14 | + * This program is distributed in the hope that it will be useful, | |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | + * GNU General Public License for more details. | |
| 18 | + * | |
| 19 | + * You should have received a copy of the GNU General Public License | |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 21 | + */ | |
| 22 | + | |
| 23 | +#include "trbase.h" | |
| 24 | +#include "tr/set.h" | |
| 25 | + | |
| 26 | +#include "_resize.h" | |
| 27 | + | |
| 28 | +void | |
| 29 | +TR_setDelete(TR_Set this, void * msg) | |
| 30 | +{ | |
| 31 | + size_t pos = TR_setFind(this, msg); | |
| 32 | + | |
| 33 | + if (this->data[pos] != msg) { | |
| 34 | + return; | |
| 35 | + } | |
| 36 | + | |
| 37 | + if (pos != this->used - 1) { | |
| 38 | + memmove( | |
| 39 | + this->data+pos, | |
| 40 | + this->data+pos+1, | |
| 41 | + (this->used-pos-1)*sizeof(void*)); | |
| 42 | + } | |
| 43 | + | |
| 44 | + this->data[this->used-1] = NULL; | |
| 45 | + --this->used; | |
| 46 | +} | |
| 47 | + | |
| 48 | +// vim: set ts=4 sw=4: | ... | ... |
src/set/find.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2014 Georg Hopp | |
| 8 | + * | |
| 9 | + * This program is free software: you can redistribute it and/or modify | |
| 10 | + * it under the terms of the GNU General Public License as published by | |
| 11 | + * the Free Software Foundation, either version 3 of the License, or | |
| 12 | + * (at your option) any later version. | |
| 13 | + * | |
| 14 | + * This program is distributed in the hope that it will be useful, | |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | + * GNU General Public License for more details. | |
| 18 | + * | |
| 19 | + * You should have received a copy of the GNU General Public License | |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 21 | + */ | |
| 22 | + | |
| 23 | +#include "trbase.h" | |
| 24 | +#include "tr/set.h" | |
| 25 | + | |
| 26 | +size_t | |
| 27 | +TR_setFind(TR_Set this, void * search) | |
| 28 | +{ | |
| 29 | +#define MID(a, b) (((b)-(a))/2) | |
| 30 | + | |
| 31 | + size_t start = 0; | |
| 32 | + size_t end = this->used > 0 ? this->used-1 : 0; | |
| 33 | + size_t i = MID(start, end); | |
| 34 | + | |
| 35 | + while (this->data[i] != search && start < end) { | |
| 36 | + if (this->data[i] < search) { | |
| 37 | + start = i+1 > end ? end : i+1; | |
| 38 | + } else { | |
| 39 | + end = i <= start || i-1 < start ? start : i-1; | |
| 40 | + } | |
| 41 | + i = start+MID(start, end); | |
| 42 | + } | |
| 43 | + | |
| 44 | + if (this->data[i] < search && this->used != 0) ++i; | |
| 45 | + | |
| 46 | + return i; | |
| 47 | +} | |
| 48 | + | |
| 49 | +// vim: set ts=4 sw=4: | ... | ... |
src/set/set.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2014 Georg Hopp | |
| 8 | + * | |
| 9 | + * This program is free software: you can redistribute it and/or modify | |
| 10 | + * it under the terms of the GNU General Public License as published by | |
| 11 | + * the Free Software Foundation, either version 3 of the License, or | |
| 12 | + * (at your option) any later version. | |
| 13 | + * | |
| 14 | + * This program is distributed in the hope that it will be useful, | |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | + * GNU General Public License for more details. | |
| 18 | + * | |
| 19 | + * You should have received a copy of the GNU General Public License | |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 21 | + */ | |
| 22 | + | |
| 23 | +#include <stdarg.h> | |
| 24 | + | |
| 25 | +#include "trbase.h" | |
| 26 | +#include "tr/set.h" | |
| 27 | +#include "tr/interface/iterable.h" | |
| 28 | + | |
| 29 | +static | |
| 30 | +int | |
| 31 | +setCtor(void * _this, va_list * params) | |
| 32 | +{ | |
| 33 | + TR_Set this = _this; | |
| 34 | + | |
| 35 | + this->data = (void **)TR_calloc(32, sizeof(void *)); | |
| 36 | + this->size = TR_getUsableSize(this->data) / sizeof(void *); | |
| 37 | + this->free_msgs = 1; | |
| 38 | + | |
| 39 | + return 0; | |
| 40 | +} | |
| 41 | + | |
| 42 | +static | |
| 43 | +void | |
| 44 | +setDtor(void * _this) | |
| 45 | +{ | |
| 46 | + TR_Set this = _this; | |
| 47 | + size_t i; | |
| 48 | + | |
| 49 | + if (this->free_msgs) { | |
| 50 | + for (i = 0; i < this->used; i++) { | |
| 51 | + if (this->data[i]) { | |
| 52 | + TR_delete(this->data[i]); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + TR_MEM_FREE(this->data); | |
| 58 | +} | |
| 59 | + | |
| 60 | +static | |
| 61 | +void * | |
| 62 | +setCurrent(void * _this) | |
| 63 | +{ | |
| 64 | + TR_Set this = _this; | |
| 65 | + return this->data[this->current]; | |
| 66 | +} | |
| 67 | + | |
| 68 | +static | |
| 69 | +void | |
| 70 | +setNext(void * _this) | |
| 71 | +{ | |
| 72 | + TR_Set this = _this; | |
| 73 | + ++this->current; | |
| 74 | +} | |
| 75 | + | |
| 76 | +static | |
| 77 | +void | |
| 78 | +setRewind(void * _this) | |
| 79 | +{ | |
| 80 | + TR_Set this = _this; | |
| 81 | + this->current = 0; | |
| 82 | +} | |
| 83 | + | |
| 84 | +static | |
| 85 | +int | |
| 86 | +setValid(void * _this) | |
| 87 | +{ | |
| 88 | + TR_Set this = _this; | |
| 89 | + return this->current < this->used; | |
| 90 | +} | |
| 91 | + | |
| 92 | +TR_INIT_IFACE(TR_Class, setCtor, setDtor, NULL); | |
| 93 | +TR_INIT_IFACE(TR_Iterable, setCurrent, setNext, setRewind, setValid); | |
| 94 | +TR_CREATE_CLASS(TR_Set, NULL, NULL, TR_IF(TR_Class), TR_IF(TR_Iterable)); | |
| 95 | + | |
| 96 | +// vim: set ts=4 sw=4: | ... | ... |
Please
register
or
login
to post a comment