11 changed files with 377 additions and 10 deletions
-
5configure.ac
-
5include/Makefile.am
-
55include/tr/set.h
-
5include/trdata.h
-
9src/Makefile.am
-
16src/set/Makefile.am
-
45src/set/_resize.h
-
54src/set/add.c
-
48src/set/delete.c
-
49src/set/find.c
-
96src/set/set.c
@ -1,11 +1,12 @@ |
|||
nobase_include_HEADERS = trdata.h \
|
|||
tr/cbuf.h \
|
|||
tr/dynarray.h \
|
|||
tr/hash.h \
|
|||
tr/heap.h \
|
|||
tr/hash_value.h \
|
|||
tr/list.h \
|
|||
tr/queue.h \
|
|||
tr/set.h \
|
|||
tr/tree.h \
|
|||
tr/dynarray.h \
|
|||
tr/heap.h \
|
|||
tr/interface/hashable.h \
|
|||
tr/interface/iterable.h |
|||
@ -0,0 +1,55 @@ |
|||
/** |
|||
* \file |
|||
* Holds requests ready for processing. |
|||
* |
|||
* \todo change this to a real queue. |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2014 Georg Hopp |
|||
* |
|||
* 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. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#ifndef __TR_SET_H__ |
|||
#define __TR_SET_H__ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
#include "trbase.h" |
|||
|
|||
|
|||
TR_CLASS(TR_Set) { |
|||
size_t size; |
|||
size_t used; |
|||
void ** data; |
|||
int free_msgs; |
|||
|
|||
// for iterable interface |
|||
size_t current; |
|||
}; |
|||
TR_INSTANCE_INIT(TR_Set); |
|||
TR_CLASSVARS_DECL(TR_Set) {}; |
|||
|
|||
void TR_setAdd(TR_Set, void *); |
|||
size_t TR_setFind(TR_Set, void *); |
|||
void TR_setDelete(TR_Set, void *); |
|||
|
|||
#define TR_setEmpty(this) (this->used == 0) |
|||
#define TR_setSize(this) ((this)->used) |
|||
|
|||
#endif // __TR_SET_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,16 @@ |
|||
ACLOCAL_AMFLAGS = -I m4 |
|||
AUTOMAKE_OPTIONS = subdir-objects |
|||
|
|||
AM_CFLAGS += -I../../include/ -std=c99 |
|||
AM_LDFLAGS += |
|||
|
|||
SET = set.c \
|
|||
add.c \
|
|||
find.c \
|
|||
delete.c |
|||
|
|||
noinst_LTLIBRARIES = libset.la |
|||
|
|||
libset_la_SOURCES = $(SET) |
|||
libset_la_CFLAGS = $(AM_CFLAGS) |
|||
libset_la_LIBADD = $(AM_LDFLAGS) |
|||
@ -0,0 +1,45 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2014 Georg Hopp |
|||
* |
|||
* 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. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
#ifndef __TR_SET_RESIZE_H__ |
|||
#define __TR_SET_RESIZE_H__ |
|||
|
|||
#include "trbase.h" |
|||
#include "tr/set.h" |
|||
|
|||
inline |
|||
void |
|||
_TR_setResize(TR_Set this) |
|||
{ |
|||
void ** new = TR_calloc(this->size+1, sizeof(void*)); |
|||
size_t new_size = TR_getUsableSize(new) / sizeof(void *); |
|||
|
|||
#define GROWTH (new_size - this->size) |
|||
|
|||
memcpy(new, this->data, this->size * sizeof(void*)); |
|||
TR_MEM_FREE(this->data); |
|||
this->size = new_size; |
|||
this->data = new; |
|||
} |
|||
|
|||
#endif // __TR_SET_RESIZE_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,54 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2014 Georg Hopp |
|||
* |
|||
* 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. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include "trbase.h" |
|||
#include "tr/set.h" |
|||
|
|||
#include "_resize.h" |
|||
|
|||
extern inline void _TR_setResize(TR_Set); |
|||
|
|||
void |
|||
TR_setAdd(TR_Set this, void * msg) |
|||
{ |
|||
size_t pos = TR_setFind(this, msg); |
|||
|
|||
if (this->data[pos] == msg) { |
|||
return; |
|||
} |
|||
|
|||
if (this->used == this->size) { |
|||
_TR_setResize(this); |
|||
} |
|||
|
|||
if (pos != this->used) { |
|||
memmove( |
|||
this->data+pos+1, |
|||
this->data+pos, |
|||
(this->used-pos)*sizeof(void*)); |
|||
} |
|||
|
|||
this->data[pos] = msg; |
|||
++this->used; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,48 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2014 Georg Hopp |
|||
* |
|||
* 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. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include "trbase.h" |
|||
#include "tr/set.h" |
|||
|
|||
#include "_resize.h" |
|||
|
|||
void |
|||
TR_setDelete(TR_Set this, void * msg) |
|||
{ |
|||
size_t pos = TR_setFind(this, msg); |
|||
|
|||
if (this->data[pos] != msg) { |
|||
return; |
|||
} |
|||
|
|||
if (pos != this->used - 1) { |
|||
memmove( |
|||
this->data+pos, |
|||
this->data+pos+1, |
|||
(this->used-pos-1)*sizeof(void*)); |
|||
} |
|||
|
|||
this->data[this->used-1] = NULL; |
|||
--this->used; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,49 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2014 Georg Hopp |
|||
* |
|||
* 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. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include "trbase.h" |
|||
#include "tr/set.h" |
|||
|
|||
size_t |
|||
TR_setFind(TR_Set this, void * search) |
|||
{ |
|||
#define MID(a, b) (((b)-(a))/2) |
|||
|
|||
size_t start = 0; |
|||
size_t end = this->used > 0 ? this->used-1 : 0; |
|||
size_t i = MID(start, end); |
|||
|
|||
while (this->data[i] != search && start < end) { |
|||
if (this->data[i] < search) { |
|||
start = i+1 > end ? end : i+1; |
|||
} else { |
|||
end = i <= start || i-1 < start ? start : i-1; |
|||
} |
|||
i = start+MID(start, end); |
|||
} |
|||
|
|||
if (this->data[i] < search && this->used != 0) ++i; |
|||
|
|||
return i; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,96 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2014 Georg Hopp |
|||
* |
|||
* 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. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include <stdarg.h> |
|||
|
|||
#include "trbase.h" |
|||
#include "tr/set.h" |
|||
#include "tr/interface/iterable.h" |
|||
|
|||
static |
|||
int |
|||
setCtor(void * _this, va_list * params) |
|||
{ |
|||
TR_Set this = _this; |
|||
|
|||
this->data = (void **)TR_calloc(32, sizeof(void *)); |
|||
this->size = TR_getUsableSize(this->data) / sizeof(void *); |
|||
this->free_msgs = 1; |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
setDtor(void * _this) |
|||
{ |
|||
TR_Set this = _this; |
|||
size_t i; |
|||
|
|||
if (this->free_msgs) { |
|||
for (i = 0; i < this->used; i++) { |
|||
if (this->data[i]) { |
|||
TR_delete(this->data[i]); |
|||
} |
|||
} |
|||
} |
|||
|
|||
TR_MEM_FREE(this->data); |
|||
} |
|||
|
|||
static |
|||
void * |
|||
setCurrent(void * _this) |
|||
{ |
|||
TR_Set this = _this; |
|||
return this->data[this->current]; |
|||
} |
|||
|
|||
static |
|||
void |
|||
setNext(void * _this) |
|||
{ |
|||
TR_Set this = _this; |
|||
++this->current; |
|||
} |
|||
|
|||
static |
|||
void |
|||
setRewind(void * _this) |
|||
{ |
|||
TR_Set this = _this; |
|||
this->current = 0; |
|||
} |
|||
|
|||
static |
|||
int |
|||
setValid(void * _this) |
|||
{ |
|||
TR_Set this = _this; |
|||
return this->current < this->used; |
|||
} |
|||
|
|||
TR_INIT_IFACE(TR_Class, setCtor, setDtor, NULL); |
|||
TR_INIT_IFACE(TR_Iterable, setCurrent, setNext, setRewind, setValid); |
|||
TR_CREATE_CLASS(TR_Set, NULL, NULL, TR_IF(TR_Class), TR_IF(TR_Iterable)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue