14 changed files with 255 additions and 7 deletions
-
1configure.ac
-
1include/Makefile.am
-
2include/tr/dynarray.h
-
45include/tr/heap.h
-
1include/trdata.h
-
5src/Makefile.am
-
2src/dynarray/Makefile.am
-
2src/dynarray/dynarray.c
-
2src/dynarray/find_first_free.c
-
2src/dynarray/put.c
-
13src/heap/Makefile.am
-
85src/heap/get.c
-
49src/heap/heap.c
-
52src/heap/put.c
@ -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_HEAP_H__ |
|||
#define __TR_HEAP_H__ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
#include "trbase.h" |
|||
#include "tr/dynarray.h" |
|||
|
|||
typedef int (*TR_HeapComp)(const void *, const void *); |
|||
|
|||
TR_CLASS(TR_Heap) { |
|||
TR_EXTENDS(TR_Dynarray); |
|||
TR_HeapComp comp; |
|||
}; |
|||
TR_INSTANCE_INIT(TR_Heap); |
|||
TR_CLASSVARS_DECL(TR_Heap) {}; |
|||
|
|||
void TR_heapPut(TR_Heap, const void *); |
|||
const void * TR_heapGet(TR_Heap); |
|||
|
|||
#endif // __TR_HEAP_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,13 @@ |
|||
ACLOCAL_AMFLAGS = -I m4 |
|||
AUTOMAKE_OPTIONS = subdir-objects |
|||
|
|||
AM_CFLAGS += -I../../include/ -std=c99 -DREENTRANT -lpthread |
|||
AM_LDFLAGS += -lpthread |
|||
|
|||
HEAP = heap.c put.c get.c |
|||
|
|||
noinst_LTLIBRARIES = libheap.la |
|||
|
|||
libheap_la_SOURCES = $(HEAP) |
|||
libheap_la_CFLAGS = $(AM_CFLAGS) |
|||
libheap_la_LIBADD = $(AM_LDFLAGS) |
|||
@ -0,0 +1,85 @@ |
|||
/** |
|||
* \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 <sys/types.h> |
|||
|
|||
#include "trbase.h" |
|||
#include "tr/heap.h" |
|||
#include "tr/dynarray.h" |
|||
|
|||
const void * |
|||
TR_heapGet(TR_Heap this) |
|||
{ |
|||
const void * value = TR_darrGet((TR_Dynarray)this, 0); |
|||
size_t left = 1; |
|||
size_t right = 2; |
|||
size_t idx; |
|||
|
|||
if (! value) { |
|||
return NULL; |
|||
} |
|||
|
|||
idx = TR_darrFindFirstFree((TR_Dynarray)this) - 1; |
|||
TR_darrGet((TR_Dynarray)this, 0) = NULL; |
|||
|
|||
if (0 == idx) { |
|||
return value; |
|||
} |
|||
|
|||
SWAP( |
|||
void *, |
|||
TR_darrGet((TR_Dynarray)this, 0), |
|||
TR_darrGet((TR_Dynarray)this, idx)); |
|||
idx = 0; |
|||
|
|||
while (left < ((TR_Dynarray)this)->size && |
|||
TR_darrGet((TR_Dynarray)this, left)) { |
|||
size_t change = left; |
|||
|
|||
if (right < ((TR_Dynarray)this)->size && |
|||
TR_darrGet((TR_Dynarray)this, right) && |
|||
0 > this->comp ( |
|||
TR_darrGet((TR_Dynarray)this, left), |
|||
TR_darrGet((TR_Dynarray)this, right))) { |
|||
change = right; |
|||
} |
|||
|
|||
if (0 > this->comp( |
|||
TR_darrGet((TR_Dynarray)this, idx), |
|||
TR_darrGet((TR_Dynarray)this, change))) { |
|||
SWAP( |
|||
void *, |
|||
TR_darrGet((TR_Dynarray)this, idx), |
|||
TR_darrGet((TR_Dynarray)this, change)); |
|||
idx = change; |
|||
left = (idx << 1) + 1; |
|||
right = left + 1; |
|||
} else { |
|||
break; |
|||
} |
|||
} |
|||
|
|||
return value; |
|||
} |
|||
|
|||
// 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 <stdarg.h> |
|||
|
|||
#include "trbase.h" |
|||
#include "tr/heap.h" |
|||
|
|||
static |
|||
int |
|||
heapCtor(void * _this, va_list * params) |
|||
{ |
|||
TR_Heap this = _this; |
|||
|
|||
this->comp = va_arg(*params, TR_HeapComp); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
heapDtor(void * _this) |
|||
{ |
|||
TR_PARENTCALL(TR_Heap, _this, TR_Class, dtor); |
|||
} |
|||
|
|||
TR_INIT_IFACE(TR_Class, heapCtor, heapDtor, NULL); |
|||
TR_CREATE_CLASS(TR_Heap, TR_Dynarray, NULL, TR_IF(TR_Class)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,52 @@ |
|||
/** |
|||
* \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 <sys/types.h> |
|||
|
|||
#include "trbase.h" |
|||
#include "tr/heap.h" |
|||
#include "tr/dynarray.h" |
|||
|
|||
void |
|||
TR_heapPut(TR_Heap this, const void * data) |
|||
{ |
|||
size_t idx = TR_darrPut((TR_Dynarray)this, data); |
|||
|
|||
while (idx) { |
|||
size_t parent = (idx - 1) >> 1; |
|||
|
|||
if (0 > this->comp( |
|||
TR_darrGet((TR_Dynarray)this, parent), |
|||
TR_darrGet((TR_Dynarray)this, idx))) { |
|||
SWAP( |
|||
void *, |
|||
TR_darrGet((TR_Dynarray)this, parent), |
|||
TR_darrGet((TR_Dynarray)this, idx)); |
|||
idx = parent; |
|||
} else { |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue