Browse Source

add possibility to inject extra data into treeWalk and hashEach and change queue detructor to only conditionally free data within the nodes.

1.0.0
Georg Hopp 12 years ago
parent
commit
f7108d4bab
  1. 2
      .gitignore
  2. 2
      include/tr/hash.h
  3. 3
      include/tr/queue.h
  4. 4
      include/tr/tree.h
  5. 2
      src/hash/cleanup.c
  6. 13
      src/hash/each.c
  7. 8
      src/queue/queue.c
  8. 2
      src/tree/destroy.c
  9. 4
      src/tree/walk.c

2
.gitignore

@ -39,4 +39,4 @@ gmon.out
test-driver
/assets/html/_documentation.html
tags
trdata.h
/trdata.h*

2
include/tr/hash.h

@ -42,7 +42,7 @@ void * TR_hashGet(TR_Hash, const char *, size_t);
void * TR_hashGetFirst(TR_Hash);
void * TR_hashDeleteByVal(TR_Hash, unsigned long);
void * TR_hashGetByVal(TR_Hash, unsigned long);
void TR_hashEach(TR_Hash, void (*)(const void*));
void TR_hashEach(TR_Hash, const void *, void (*)(const void *, const void *));
void TR_hashCleanup(TR_Hash);
#endif // __TR_HASH_H__

3
include/tr/queue.h

@ -45,7 +45,8 @@ TR_CLASS(TR_Queue) {
*/
TR_Queue first;
TR_Queue last;
size_t nmsg;
size_t nmsg;
int free_msgs;
};
TR_INSTANCE_INIT(TR_Queue);
TR_CLASSVARS_DECL(TR_Queue) {};

4
include/tr/tree.h

@ -39,12 +39,12 @@ TR_INSTANCE_INIT(TR_Tree);
TR_CLASSVARS_DECL(TR_Tree) {};
typedef int (*TR_TreeComp)(const void *, const void *);
typedef void (*TR_TreeAction)(const void *, const int);
typedef void (*TR_TreeAction)(const void *, const void *, const int);
void * TR_treeFind(TR_Tree, const void *, TR_TreeComp);
void * TR_treeInsert(TR_Tree *, const void *, TR_TreeComp);
void * TR_treeDelete(TR_Tree *, const void *, TR_TreeComp);
void TR_treeWalk(TR_Tree, TR_TreeAction);
void TR_treeWalk(TR_Tree, const void *, TR_TreeAction);
void TR_treeDestroy(TR_Tree *, TR_TreeAction);
#endif // __TR_TREE_H__

2
src/hash/cleanup.c

@ -27,7 +27,7 @@
static
inline
void
tDelete(const void * node, const int depth)
tDelete(const void * node, const void * data, const int depth)
{
TR_delete(node);
}

13
src/hash/each.c

@ -25,22 +25,25 @@
#include "tr/hash.h"
#include "tr/tree.h"
static void (*cb)(const void*);
static void (*cb)(const void*, const void*);
static
inline
void
walk(const void * node, const int depth)
walk(const void * node, const void * data, const int depth)
{
cb(node);
cb(node, data);
}
void
TR_hashEach(TR_Hash this, void (*callback)(const void*))
TR_hashEach(
TR_Hash this,
const void * data,
void (*callback)(const void *, const void *))
{
cb = callback;
TR_treeWalk(this->root, walk);
TR_treeWalk(this->root, data, walk);
}
// vim: set ts=4 sw=4:

8
src/queue/queue.c

@ -29,6 +29,10 @@ static
int
queueCtor(void * _this, va_list * params)
{
TR_Queue this = _this;
this->free_msgs = 1;
return 0;
}
@ -41,7 +45,9 @@ queueDtor(void * _this)
while (NULL != node) {
TR_Queue next = node->next;
TR_delete(node->msg);
if (this->free_msgs) {
TR_delete(node->msg);
}
TR_delete(node);
node = next;
}

2
src/tree/destroy.c

@ -47,7 +47,7 @@ TR_treeDestroy(TR_Tree * this, TR_TreeAction action)
TR_Tree parent = TR_TREE_PARENT(node);
if (action) {
action(node->data, depth);
action(node->data, NULL, depth);
}
previous = node;

4
src/tree/walk.c

@ -23,7 +23,7 @@
#include "tr/tree.h"
void
TR_treeWalk(TR_Tree this, TR_TreeAction action)
TR_treeWalk(TR_Tree this, const void * data, TR_TreeAction action)
{
TR_Tree previous = this;
TR_Tree node = this;
@ -39,7 +39,7 @@ TR_treeWalk(TR_Tree this, TR_TreeAction action)
if (NULL == TR_TREE_LEFT(node) || previous == TR_TREE_LEFT(node)) {
if (action) {
action(node->data, depth);
action(node->data, data, depth);
}
previous = node;

Loading…
Cancel
Save