Showing
8 changed files
with
182 additions
and
2 deletions
| @@ -64,5 +64,6 @@ AC_CONFIG_FILES([Makefile | @@ -64,5 +64,6 @@ AC_CONFIG_FILES([Makefile | ||
| 64 | src/hash/Makefile | 64 | src/hash/Makefile |
| 65 | src/queue/Makefile | 65 | src/queue/Makefile |
| 66 | src/tree/Makefile | 66 | src/tree/Makefile |
| 67 | + src/dynarray/Makefile | ||
| 67 | include/Makefile]) | 68 | include/Makefile]) |
| 68 | AC_OUTPUT | 69 | AC_OUTPUT |
include/tr/dynarray.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 | + | ||
| 23 | +#ifndef __TR_DYNARRAY_H__ | ||
| 24 | +#define __TR_DYNARRAY_H__ | ||
| 25 | + | ||
| 26 | +#include <sys/types.h> | ||
| 27 | + | ||
| 28 | +#include "trbase.h" | ||
| 29 | + | ||
| 30 | +TR_CLASS(TR_Dynarray) { | ||
| 31 | + size_t size; | ||
| 32 | + const void ** data; | ||
| 33 | +}; | ||
| 34 | +TR_INSTANCE_INIT(TR_Dynarray); | ||
| 35 | +TR_CLASSVARS_DECL(TR_Dynarray) {}; | ||
| 36 | + | ||
| 37 | +size_t TR_darrPut(TR_Dynarray, const void *); | ||
| 38 | +void TR_darrPutAt(TR_Dynarray, const void *, size_t); | ||
| 39 | + | ||
| 40 | +#define TR_darrGet(this, idx) ((this)->data[(idx)]) | ||
| 41 | + | ||
| 42 | +#endif // __TR_DYNARRAY_H__ | ||
| 43 | + | ||
| 44 | +// vim: set ts=4 sw=4: |
| @@ -6,6 +6,7 @@ | @@ -6,6 +6,7 @@ | ||
| 6 | #include "tr/hash_value.h" | 6 | #include "tr/hash_value.h" |
| 7 | #include "tr/queue.h" | 7 | #include "tr/queue.h" |
| 8 | #include "tr/tree.h" | 8 | #include "tr/tree.h" |
| 9 | +#include "tr/dynarray.h" | ||
| 9 | #include "tr/interface/hashable.h" | 10 | #include "tr/interface/hashable.h" |
| 10 | 11 | ||
| 11 | #endif // __TR_DATA_H__ | 12 | #endif // __TR_DATA_H__ |
| @@ -7,7 +7,8 @@ AM_LDFLAGS += -lpthread | @@ -7,7 +7,8 @@ AM_LDFLAGS += -lpthread | ||
| 7 | TRDATALIBS = cbuf/libcbuf.la \ | 7 | TRDATALIBS = cbuf/libcbuf.la \ |
| 8 | hash/libhash.la \ | 8 | hash/libhash.la \ |
| 9 | queue/libqueue.la \ | 9 | queue/libqueue.la \ |
| 10 | - tree/libtree.la | 10 | + tree/libtree.la \ |
| 11 | + dynarray/libdynarray.la | ||
| 11 | 12 | ||
| 12 | lib_LTLIBRARIES = libtrdata.la | 13 | lib_LTLIBRARIES = libtrdata.la |
| 13 | 14 | ||
| @@ -16,4 +17,4 @@ libtrdata_la_CFLAGS = $(AM_CFLAGS) | @@ -16,4 +17,4 @@ libtrdata_la_CFLAGS = $(AM_CFLAGS) | ||
| 16 | libtrdata_la_LIBADD = $(TRDATALIBS) | 17 | libtrdata_la_LIBADD = $(TRDATALIBS) |
| 17 | libtrdata_la_LDFLAGS = -version-info 0:0:0 $(AM_LDFLAGS) | 18 | libtrdata_la_LDFLAGS = -version-info 0:0:0 $(AM_LDFLAGS) |
| 18 | 19 | ||
| 19 | -SUBDIRS = cbuf hash queue tree | 20 | +SUBDIRS = cbuf hash queue tree dynarray |
src/dynarray/Makefile.am
0 → 100644
| 1 | +ACLOCAL_AMFLAGS = -I m4 | ||
| 2 | +AUTOMAKE_OPTIONS = subdir-objects | ||
| 3 | + | ||
| 4 | +AM_CFLAGS += -I../../include/ -std=c99 -DREENTRANT -lpthread | ||
| 5 | +AM_LDFLAGS += -lpthread | ||
| 6 | + | ||
| 7 | +DYNARRAY = dynarray.c put.c | ||
| 8 | + | ||
| 9 | +noinst_LTLIBRARIES = libdynarray.la | ||
| 10 | + | ||
| 11 | +libdynarray_la_SOURCES = $(DYNARRAY) | ||
| 12 | +libdynarray_la_CFLAGS = $(AM_CFLAGS) | ||
| 13 | +libdynarray_la_LIBADD = $(AM_LDFLAGS) |
src/dynarray/dynarray.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/dynarray.h" | ||
| 27 | + | ||
| 28 | +static | ||
| 29 | +inline | ||
| 30 | +void | ||
| 31 | +_darrResize(TR_Dynarray this, size_t index) | ||
| 32 | +{ | ||
| 33 | + if (index > (long)this->size - 1) { | ||
| 34 | + void * new_data = TR_calloc(index + 1, sizeof(void *)); | ||
| 35 | + memcpy(new_data, this->data, this->size * sizeof(void *)); | ||
| 36 | + TR_MEM_FREE(this->data); | ||
| 37 | + this->data = (const void **)new_data; | ||
| 38 | + this->size = TR_getUsableSize(new_data) / sizeof(void *); | ||
| 39 | + } | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +static | ||
| 43 | +int | ||
| 44 | +darrCtor(void * _this) | ||
| 45 | +{ | ||
| 46 | + return 0; | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +static | ||
| 50 | +void | ||
| 51 | +darrDtor(void * _this) | ||
| 52 | +{ | ||
| 53 | + TR_MEM_FREE(((TR_Dynarray)_this)->data); | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +TR_INIT_IFACE(TR_Class, darrCtor, darrDtor, NULL); | ||
| 57 | +TR_CREATE_CLASS(TR_Dynarray, NULL, NULL, TR_IF(TR_Class)); | ||
| 58 | + | ||
| 59 | +// vim: set ts=4 sw=4: |
src/dynarray/put.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/dynarray.h" | ||
| 27 | + | ||
| 28 | +static | ||
| 29 | +inline | ||
| 30 | +void | ||
| 31 | +_darrResize(TR_Dynarray this, size_t index) | ||
| 32 | +{ | ||
| 33 | + if (!this->size || index > this->size - 1) { | ||
| 34 | + void * new_data = TR_calloc(index + 1, sizeof(void *)); | ||
| 35 | + memcpy(new_data, this->data, this->size * sizeof(void *)); | ||
| 36 | + TR_MEM_FREE(this->data); | ||
| 37 | + this->data = (const void **)new_data; | ||
| 38 | + this->size = TR_getUsableSize(new_data) / sizeof(void *); | ||
| 39 | + } | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +size_t | ||
| 43 | +TR_darrPut(TR_Dynarray this, const void * data) | ||
| 44 | +{ | ||
| 45 | + size_t i = this->size; | ||
| 46 | + | ||
| 47 | + while (this->data && ! this->data[--i]); | ||
| 48 | + TR_darrPutAt(this, data, this->data && this->data[i] ? i+1 : i); | ||
| 49 | + | ||
| 50 | + return i; | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +void | ||
| 54 | +TR_darrPutAt(TR_Dynarray this, const void * data, size_t idx) | ||
| 55 | +{ | ||
| 56 | + _darrResize(this, idx); | ||
| 57 | + this->data[idx] = data; | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +// vim: set ts=4 sw=4: |
Please
register
or
login
to post a comment