Commit b32823cd302f2c4359c1576053d0341e3ef67fc6

Authored by Georg Hopp
1 parent 49c7f8be

add heap code

@@ -65,5 +65,6 @@ AC_CONFIG_FILES([Makefile @@ -65,5 +65,6 @@ AC_CONFIG_FILES([Makefile
65 src/queue/Makefile 65 src/queue/Makefile
66 src/tree/Makefile 66 src/tree/Makefile
67 src/dynarray/Makefile 67 src/dynarray/Makefile
  68 + src/heap/Makefile
68 include/Makefile]) 69 include/Makefile])
69 AC_OUTPUT 70 AC_OUTPUT
@@ -5,4 +5,5 @@ nobase_include_HEADERS = trdata.h \ @@ -5,4 +5,5 @@ nobase_include_HEADERS = trdata.h \
5 tr/queue.h \ 5 tr/queue.h \
6 tr/tree.h \ 6 tr/tree.h \
7 tr/dynarray.h \ 7 tr/dynarray.h \
  8 + tr/heap.h \
8 tr/interface/hashable.h 9 tr/interface/hashable.h
@@ -36,7 +36,7 @@ TR_CLASSVARS_DECL(TR_Dynarray) {}; @@ -36,7 +36,7 @@ TR_CLASSVARS_DECL(TR_Dynarray) {};
36 36
37 size_t TR_darrPut(TR_Dynarray, const void *); 37 size_t TR_darrPut(TR_Dynarray, const void *);
38 void TR_darrPutAt(TR_Dynarray, const void *, size_t); 38 void TR_darrPutAt(TR_Dynarray, const void *, size_t);
39 -size_t TR_darrFindLastIndex(TR_Dynarray); 39 +size_t TR_darrFindFirstFree(TR_Dynarray);
40 40
41 #define TR_darrGet(this, idx) ((this)->data[(idx)]) 41 #define TR_darrGet(this, idx) ((this)->data[(idx)])
42 42
  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_HEAP_H__
  24 +#define __TR_HEAP_H__
  25 +
  26 +#include <sys/types.h>
  27 +
  28 +#include "trbase.h"
  29 +#include "tr/dynarray.h"
  30 +
  31 +typedef int (*TR_HeapComp)(const void *, const void *);
  32 +
  33 +TR_CLASS(TR_Heap) {
  34 + TR_EXTENDS(TR_Dynarray);
  35 + TR_HeapComp comp;
  36 +};
  37 +TR_INSTANCE_INIT(TR_Heap);
  38 +TR_CLASSVARS_DECL(TR_Heap) {};
  39 +
  40 +void TR_heapPut(TR_Heap, const void *);
  41 +const void * TR_heapGet(TR_Heap);
  42 +
  43 +#endif // __TR_HEAP_H__
  44 +
  45 +// vim: set ts=4 sw=4:
@@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
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/dynarray.h"
  10 +#include "tr/heap.h"
10 #include "tr/interface/hashable.h" 11 #include "tr/interface/hashable.h"
11 12
12 #endif // __TR_DATA_H__ 13 #endif // __TR_DATA_H__
@@ -8,7 +8,8 @@ TRDATALIBS = cbuf/libcbuf.la \ @@ -8,7 +8,8 @@ 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 + dynarray/libdynarray.la \
  12 + heap/libheap.la
12 13
13 lib_LTLIBRARIES = libtrdata.la 14 lib_LTLIBRARIES = libtrdata.la
14 15
@@ -17,4 +18,4 @@ libtrdata_la_CFLAGS = $(AM_CFLAGS) @@ -17,4 +18,4 @@ libtrdata_la_CFLAGS = $(AM_CFLAGS)
17 libtrdata_la_LIBADD = $(TRDATALIBS) 18 libtrdata_la_LIBADD = $(TRDATALIBS)
18 libtrdata_la_LDFLAGS = -version-info 0:0:0 $(AM_LDFLAGS) 19 libtrdata_la_LDFLAGS = -version-info 0:0:0 $(AM_LDFLAGS)
19 20
20 -SUBDIRS = cbuf hash queue tree dynarray 21 +SUBDIRS = cbuf hash queue tree dynarray heap
@@ -4,7 +4,7 @@ AUTOMAKE_OPTIONS = subdir-objects @@ -4,7 +4,7 @@ AUTOMAKE_OPTIONS = subdir-objects
4 AM_CFLAGS += -I../../include/ -std=c99 -DREENTRANT -lpthread 4 AM_CFLAGS += -I../../include/ -std=c99 -DREENTRANT -lpthread
5 AM_LDFLAGS += -lpthread 5 AM_LDFLAGS += -lpthread
6 6
7 -DYNARRAY = dynarray.c put.c find_last_index.c 7 +DYNARRAY = dynarray.c put.c find_first_free.c
8 8
9 noinst_LTLIBRARIES = libdynarray.la 9 noinst_LTLIBRARIES = libdynarray.la
10 10
@@ -41,7 +41,7 @@ _darrResize(TR_Dynarray this, size_t index) @@ -41,7 +41,7 @@ _darrResize(TR_Dynarray this, size_t index)
41 41
42 static 42 static
43 int 43 int
44 -darrCtor(void * _this) 44 +darrCtor(void * _this, va_list * params)
45 { 45 {
46 return 0; 46 return 0;
47 } 47 }
@@ -26,7 +26,7 @@ @@ -26,7 +26,7 @@
26 #include "tr/dynarray.h" 26 #include "tr/dynarray.h"
27 27
28 size_t 28 size_t
29 -TR_darrFindLastIndex(TR_Dynarray this) 29 +TR_darrFindFirstFree(TR_Dynarray this)
30 { 30 {
31 size_t i = this->size; 31 size_t i = this->size;
32 32
@@ -42,7 +42,7 @@ _darrResize(TR_Dynarray this, size_t index) @@ -42,7 +42,7 @@ _darrResize(TR_Dynarray this, size_t index)
42 size_t 42 size_t
43 TR_darrPut(TR_Dynarray this, const void * data) 43 TR_darrPut(TR_Dynarray this, const void * data)
44 { 44 {
45 - size_t i = TR_darrFindLastIndex(this); 45 + size_t i = TR_darrFindFirstFree(this);
46 46
47 TR_darrPutAt(this, data, i); 47 TR_darrPutAt(this, data, i);
48 48
  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 +HEAP = heap.c put.c get.c
  8 +
  9 +noinst_LTLIBRARIES = libheap.la
  10 +
  11 +libheap_la_SOURCES = $(HEAP)
  12 +libheap_la_CFLAGS = $(AM_CFLAGS)
  13 +libheap_la_LIBADD = $(AM_LDFLAGS)
  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 +#include <sys/types.h>
  25 +
  26 +#include "trbase.h"
  27 +#include "tr/heap.h"
  28 +#include "tr/dynarray.h"
  29 +
  30 +const void *
  31 +TR_heapGet(TR_Heap this)
  32 +{
  33 + const void * value = TR_darrGet((TR_Dynarray)this, 0);
  34 + size_t left = 1;
  35 + size_t right = 2;
  36 + size_t idx;
  37 +
  38 + if (! value) {
  39 + return NULL;
  40 + }
  41 +
  42 + idx = TR_darrFindFirstFree((TR_Dynarray)this) - 1;
  43 + TR_darrGet((TR_Dynarray)this, 0) = NULL;
  44 +
  45 + if (0 == idx) {
  46 + return value;
  47 + }
  48 +
  49 + SWAP(
  50 + void *,
  51 + TR_darrGet((TR_Dynarray)this, 0),
  52 + TR_darrGet((TR_Dynarray)this, idx));
  53 + idx = 0;
  54 +
  55 + while (left < ((TR_Dynarray)this)->size &&
  56 + TR_darrGet((TR_Dynarray)this, left)) {
  57 + size_t change = left;
  58 +
  59 + if (right < ((TR_Dynarray)this)->size &&
  60 + TR_darrGet((TR_Dynarray)this, right) &&
  61 + 0 > this->comp (
  62 + TR_darrGet((TR_Dynarray)this, left),
  63 + TR_darrGet((TR_Dynarray)this, right))) {
  64 + change = right;
  65 + }
  66 +
  67 + if (0 > this->comp(
  68 + TR_darrGet((TR_Dynarray)this, idx),
  69 + TR_darrGet((TR_Dynarray)this, change))) {
  70 + SWAP(
  71 + void *,
  72 + TR_darrGet((TR_Dynarray)this, idx),
  73 + TR_darrGet((TR_Dynarray)this, change));
  74 + idx = change;
  75 + left = (idx << 1) + 1;
  76 + right = left + 1;
  77 + } else {
  78 + break;
  79 + }
  80 + }
  81 +
  82 + return value;
  83 +}
  84 +
  85 +// vim: set ts=4 sw=4:
  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/heap.h"
  27 +
  28 +static
  29 +int
  30 +heapCtor(void * _this, va_list * params)
  31 +{
  32 + TR_Heap this = _this;
  33 +
  34 + this->comp = va_arg(*params, TR_HeapComp);
  35 +
  36 + return 0;
  37 +}
  38 +
  39 +static
  40 +void
  41 +heapDtor(void * _this)
  42 +{
  43 + TR_PARENTCALL(TR_Heap, _this, TR_Class, dtor);
  44 +}
  45 +
  46 +TR_INIT_IFACE(TR_Class, heapCtor, heapDtor, NULL);
  47 +TR_CREATE_CLASS(TR_Heap, TR_Dynarray, NULL, TR_IF(TR_Class));
  48 +
  49 +// vim: set ts=4 sw=4:
  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 +#include <sys/types.h>
  25 +
  26 +#include "trbase.h"
  27 +#include "tr/heap.h"
  28 +#include "tr/dynarray.h"
  29 +
  30 +void
  31 +TR_heapPut(TR_Heap this, const void * data)
  32 +{
  33 + size_t idx = TR_darrPut((TR_Dynarray)this, data);
  34 +
  35 + while (idx) {
  36 + size_t parent = (idx - 1) >> 1;
  37 +
  38 + if (0 > this->comp(
  39 + TR_darrGet((TR_Dynarray)this, parent),
  40 + TR_darrGet((TR_Dynarray)this, idx))) {
  41 + SWAP(
  42 + void *,
  43 + TR_darrGet((TR_Dynarray)this, parent),
  44 + TR_darrGet((TR_Dynarray)this, idx));
  45 + idx = parent;
  46 + } else {
  47 + break;
  48 + }
  49 + }
  50 +}
  51 +
  52 +// vim: set ts=4 sw=4:
Please register or login to post a comment