Showing
13 changed files
with
78 additions
and
35 deletions
@@ -37,16 +37,16 @@ TR_CLASS(TR_Hash) { | @@ -37,16 +37,16 @@ TR_CLASS(TR_Hash) { | ||
37 | TR_INSTANCE_INIT(TR_Hash); | 37 | TR_INSTANCE_INIT(TR_Hash); |
38 | TR_CLASSVARS_DECL(TR_Hash) {}; | 38 | TR_CLASSVARS_DECL(TR_Hash) {}; |
39 | 39 | ||
40 | -#define TR_hashEmpty(hash) (NULL == (hash)->tree->root) | ||
41 | - | ||
42 | -void * TR_hashAdd(TR_Hash, void *); | ||
43 | -void * TR_hashDelete(TR_Hash, const char *, size_t); | ||
44 | -void * TR_hashGet(TR_Hash, const char *, size_t); | ||
45 | -void * TR_hashGetFirst(TR_Hash); | ||
46 | -void * TR_hashDeleteByVal(TR_Hash, unsigned long); | ||
47 | -void * TR_hashGetByVal(TR_Hash, unsigned long); | ||
48 | -void TR_hashEach(TR_Hash, const void *, void (*)(const void *, const void *)); | ||
49 | -void TR_hashCleanup(TR_Hash); | 40 | +//#define TR_hashEmpty(hash) (NULL == (hash)->tree->root) |
41 | + | ||
42 | +void * TR_hashAdd(TR_Hash, void *); | ||
43 | +void * TR_hashDelete(TR_Hash, const char *, size_t); | ||
44 | +void * TR_hashGet(TR_Hash, const char *, size_t); | ||
45 | +void * TR_hashGetFirst(TR_Hash); | ||
46 | +void * TR_hashDeleteByVal(TR_Hash, unsigned long); | ||
47 | +void * TR_hashGetByVal(TR_Hash, unsigned long); | ||
48 | +unsigned long TR_hashEach(TR_Hash, const void *, void (*)(const void *, const void *)); | ||
49 | +void TR_hashCleanup(TR_Hash); | ||
50 | 50 | ||
51 | #endif // __TR_HASH_H__ | 51 | #endif // __TR_HASH_H__ |
52 | 52 |
@@ -50,11 +50,11 @@ TR_CLASSVARS_DECL(TR_Tree) {}; | @@ -50,11 +50,11 @@ TR_CLASSVARS_DECL(TR_Tree) {}; | ||
50 | typedef int (*TR_TreeComp)(const void *, const void *); | 50 | typedef int (*TR_TreeComp)(const void *, const void *); |
51 | typedef void (*TR_TreeAction)(const void *, const void *, const int); | 51 | typedef void (*TR_TreeAction)(const void *, const void *, const int); |
52 | 52 | ||
53 | -void * TR_treeFind(TR_Tree, const void *, TR_TreeComp); | ||
54 | -void * TR_treeInsert(TR_Tree, const void *, TR_TreeComp); | ||
55 | -void * TR_treeDelete(TR_Tree, const void *, TR_TreeComp); | ||
56 | -void TR_treeWalk(TR_Tree, const void *, TR_TreeAction); | ||
57 | -void TR_treeDestroy(TR_Tree, TR_TreeAction); | 53 | +void * TR_treeFind(TR_Tree, const void *, TR_TreeComp); |
54 | +void * TR_treeInsert(TR_Tree, const void *, TR_TreeComp); | ||
55 | +void * TR_treeDelete(TR_Tree, const void *, TR_TreeComp); | ||
56 | +unsigned long TR_treeWalk(TR_Tree, const void *, TR_TreeAction); | ||
57 | +void TR_treeDestroy(TR_Tree, TR_TreeAction); | ||
58 | 58 | ||
59 | #endif // __TR_TREE_H__ | 59 | #endif // __TR_TREE_H__ |
60 | 60 |
@@ -34,7 +34,7 @@ walk(const void * node, const void * data, const int depth) | @@ -34,7 +34,7 @@ walk(const void * node, const void * data, const int depth) | ||
34 | cb(node, data); | 34 | cb(node, data); |
35 | } | 35 | } |
36 | 36 | ||
37 | -void | 37 | +unsigned long |
38 | TR_hashEach( | 38 | TR_hashEach( |
39 | TR_Hash this, | 39 | TR_Hash this, |
40 | const void * data, | 40 | const void * data, |
@@ -42,7 +42,7 @@ TR_hashEach( | @@ -42,7 +42,7 @@ TR_hashEach( | ||
42 | { | 42 | { |
43 | cb = callback; | 43 | cb = callback; |
44 | 44 | ||
45 | - TR_treeWalk(this->tree, data, walk); | 45 | + return TR_treeWalk(this->tree, data, walk); |
46 | } | 46 | } |
47 | 47 | ||
48 | // vim: set ts=4 sw=4: | 48 | // vim: set ts=4 sw=4: |
@@ -29,10 +29,12 @@ | @@ -29,10 +29,12 @@ | ||
29 | void | 29 | void |
30 | TR_queueDestroy(TR_Queue this) | 30 | TR_queueDestroy(TR_Queue this) |
31 | { | 31 | { |
32 | - TR_Queue node = this->first; | 32 | + TR_Queue node; |
33 | 33 | ||
34 | pthread_mutex_lock(&(this->lock)); | 34 | pthread_mutex_lock(&(this->lock)); |
35 | 35 | ||
36 | + node = this->first; | ||
37 | + | ||
36 | while (NULL != node) { | 38 | while (NULL != node) { |
37 | TR_Queue next = node->next; | 39 | TR_Queue next = node->next; |
38 | if (this->free_msgs) { | 40 | if (this->free_msgs) { |
@@ -20,6 +20,8 @@ | @@ -20,6 +20,8 @@ | ||
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | */ | 21 | */ |
22 | 22 | ||
23 | +#include <pthread.h> | ||
24 | + | ||
23 | #include "trbase.h" | 25 | #include "trbase.h" |
24 | #include "tr/queue.h" | 26 | #include "tr/queue.h" |
25 | 27 | ||
@@ -28,7 +30,9 @@ TR_queueFind(TR_Queue this, void * msg) | @@ -28,7 +30,9 @@ TR_queueFind(TR_Queue this, void * msg) | ||
28 | { | 30 | { |
29 | TR_Queue node; | 31 | TR_Queue node; |
30 | 32 | ||
33 | + pthread_mutex_lock(&(this->lock)); | ||
31 | for (node = this->first; node && node->msg != msg; node = node->next); | 34 | for (node = this->first; node && node->msg != msg; node = node->next); |
35 | + pthread_mutex_unlock(&(this->lock)); | ||
32 | 36 | ||
33 | return node; | 37 | return node; |
34 | } | 38 | } |
@@ -20,6 +20,8 @@ | @@ -20,6 +20,8 @@ | ||
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | */ | 21 | */ |
22 | 22 | ||
23 | +#include <pthread.h> | ||
24 | + | ||
23 | #include "trbase.h" | 25 | #include "trbase.h" |
24 | #include "tr/queue.h" | 26 | #include "tr/queue.h" |
25 | 27 | ||
@@ -28,7 +30,9 @@ TR_queueFindParent(TR_Queue this, void * msg) | @@ -28,7 +30,9 @@ TR_queueFindParent(TR_Queue this, void * msg) | ||
28 | { | 30 | { |
29 | TR_Queue node; | 31 | TR_Queue node; |
30 | 32 | ||
33 | + pthread_mutex_lock(&(this->lock)); | ||
31 | for (node = this; node->next && node->next->msg != msg; node = node->next); | 34 | for (node = this; node->next && node->next->msg != msg; node = node->next); |
35 | + pthread_mutex_unlock(&(this->lock)); | ||
32 | 36 | ||
33 | return node->next ? node : NULL; | 37 | return node->next ? node : NULL; |
34 | } | 38 | } |
@@ -28,10 +28,12 @@ | @@ -28,10 +28,12 @@ | ||
28 | void | 28 | void |
29 | TR_queuePut(TR_Queue this, void * msg) | 29 | TR_queuePut(TR_Queue this, void * msg) |
30 | { | 30 | { |
31 | - TR_Queue node = (this->last)? this->last : this; | 31 | + TR_Queue node; |
32 | 32 | ||
33 | pthread_mutex_lock(&(this->lock)); | 33 | pthread_mutex_lock(&(this->lock)); |
34 | 34 | ||
35 | + node = (this->last)? this->last : this; | ||
36 | + | ||
35 | node->next = TR_new(TR_Queue); | 37 | node->next = TR_new(TR_Queue); |
36 | this->last = node->next; | 38 | this->last = node->next; |
37 | 39 |
@@ -28,10 +28,12 @@ | @@ -28,10 +28,12 @@ | ||
28 | void | 28 | void |
29 | TR_queuePutFirst(TR_Queue this, void * msg) | 29 | TR_queuePutFirst(TR_Queue this, void * msg) |
30 | { | 30 | { |
31 | - TR_Queue current_first = this->first; | 31 | + TR_Queue current_first; |
32 | 32 | ||
33 | pthread_mutex_lock(&(this->lock)); | 33 | pthread_mutex_lock(&(this->lock)); |
34 | 34 | ||
35 | + current_first = this->first; | ||
36 | + | ||
35 | this->first = this->next = TR_new(TR_Queue); | 37 | this->first = this->next = TR_new(TR_Queue); |
36 | this->first->next = current_first; | 38 | this->first->next = current_first; |
37 | this->first->msg = msg; | 39 | this->first->msg = msg; |
@@ -28,7 +28,7 @@ | @@ -28,7 +28,7 @@ | ||
28 | void * | 28 | void * |
29 | TR_treeDelete(TR_Tree this, const void * search, TR_TreeComp comp) | 29 | TR_treeDelete(TR_Tree this, const void * search, TR_TreeComp comp) |
30 | { | 30 | { |
31 | - TR_TreeNode node = this->root; | 31 | + TR_TreeNode node; |
32 | TR_TreeNode del_node; | 32 | TR_TreeNode del_node; |
33 | TR_TreeNode sibling; | 33 | TR_TreeNode sibling; |
34 | int found; | 34 | int found; |
@@ -36,6 +36,8 @@ TR_treeDelete(TR_Tree this, const void * search, TR_TreeComp comp) | @@ -36,6 +36,8 @@ TR_treeDelete(TR_Tree this, const void * search, TR_TreeComp comp) | ||
36 | 36 | ||
37 | pthread_mutex_lock(&(this->lock)); | 37 | pthread_mutex_lock(&(this->lock)); |
38 | 38 | ||
39 | + node = this->root; | ||
40 | + | ||
39 | TR_TREE_FIND(node, search, found, comp); | 41 | TR_TREE_FIND(node, search, found, comp); |
40 | 42 | ||
41 | /* | 43 | /* |
@@ -26,10 +26,15 @@ | @@ -26,10 +26,15 @@ | ||
26 | void | 26 | void |
27 | TR_treeDestroy(TR_Tree this, TR_TreeAction action) | 27 | TR_treeDestroy(TR_Tree this, TR_TreeAction action) |
28 | { | 28 | { |
29 | - TR_TreeNode previous = this->root; | ||
30 | - TR_TreeNode node = this->root; | ||
31 | - int depth = 1; | 29 | + TR_TreeNode previous; |
30 | + TR_TreeNode node; | ||
31 | + int depth = 1; | ||
32 | 32 | ||
33 | + pthread_mutex_lock(&(this->lock)); | ||
34 | + | ||
35 | + previous = this->root; | ||
36 | + node = this->root; | ||
37 | + | ||
33 | /* | 38 | /* |
34 | * I think this has something like O(n+log(n)) on a ballanced | 39 | * I think this has something like O(n+log(n)) on a ballanced |
35 | * tree because I have to traverse back the rightmost leaf to | 40 | * tree because I have to traverse back the rightmost leaf to |
@@ -83,6 +88,8 @@ TR_treeDestroy(TR_Tree this, TR_TreeAction action) | @@ -83,6 +88,8 @@ TR_treeDestroy(TR_Tree this, TR_TreeAction action) | ||
83 | } | 88 | } |
84 | 89 | ||
85 | this->root = NULL; | 90 | this->root = NULL; |
91 | + | ||
92 | + pthread_mutex_unlock(&(this->lock)); | ||
86 | } | 93 | } |
87 | 94 | ||
88 | // vim: set ts=4 sw=4: | 95 | // vim: set ts=4 sw=4: |
@@ -25,16 +25,19 @@ | @@ -25,16 +25,19 @@ | ||
25 | void * | 25 | void * |
26 | TR_treeFind(TR_Tree this, const void * search, TR_TreeComp comp) | 26 | TR_treeFind(TR_Tree this, const void * search, TR_TreeComp comp) |
27 | { | 27 | { |
28 | - int found; | ||
29 | - TR_TreeNode node = this->root; | 28 | + int found; |
29 | + TR_TreeNode node; | ||
30 | + void * retval; | ||
30 | 31 | ||
31 | pthread_mutex_lock(&(this->lock)); | 32 | pthread_mutex_lock(&(this->lock)); |
32 | 33 | ||
34 | + node = this->root; | ||
33 | TR_TREE_FIND(node, search, found, comp); | 35 | TR_TREE_FIND(node, search, found, comp); |
36 | + retval = found == 0 ? node->data : NULL; | ||
34 | 37 | ||
35 | pthread_mutex_unlock(&(this->lock)); | 38 | pthread_mutex_unlock(&(this->lock)); |
36 | 39 | ||
37 | - return found == 0 ? node->data : NULL; | 40 | + return retval; |
38 | } | 41 | } |
39 | 42 | ||
40 | // vim: set ts=4 sw=4: | 43 | // vim: set ts=4 sw=4: |
@@ -28,12 +28,15 @@ | @@ -28,12 +28,15 @@ | ||
28 | void * | 28 | void * |
29 | TR_treeInsert(TR_Tree this, const void * search, TR_TreeComp comp) | 29 | TR_treeInsert(TR_Tree this, const void * search, TR_TreeComp comp) |
30 | { | 30 | { |
31 | - TR_TreeNode node = this->root; | ||
32 | - TR_TreeNode new_node = NULL; | ||
33 | - int found; | 31 | + TR_TreeNode new_node = NULL; |
32 | + TR_TreeNode node; | ||
33 | + int found; | ||
34 | + void * retval; | ||
34 | 35 | ||
35 | pthread_mutex_lock(&(this->lock)); | 36 | pthread_mutex_lock(&(this->lock)); |
36 | 37 | ||
38 | + node = this->root; | ||
39 | + | ||
37 | /* | 40 | /* |
38 | * insert the node or return the one in tree if comparison | 41 | * insert the node or return the one in tree if comparison |
39 | * succeeds. | 42 | * succeeds. |
@@ -77,10 +80,11 @@ TR_treeInsert(TR_Tree this, const void * search, TR_TreeComp comp) | @@ -77,10 +80,11 @@ TR_treeInsert(TR_Tree this, const void * search, TR_TreeComp comp) | ||
77 | * new node at this point...now rabalance the tree | 80 | * new node at this point...now rabalance the tree |
78 | */ | 81 | */ |
79 | TR_TREE_BALANCE_INSERT(&(this->root), node); | 82 | TR_TREE_BALANCE_INSERT(&(this->root), node); |
83 | + retval = new_node->data; | ||
80 | 84 | ||
81 | pthread_mutex_unlock(&(this->lock)); | 85 | pthread_mutex_unlock(&(this->lock)); |
82 | 86 | ||
83 | - return new_node->data; | 87 | + return retval; |
84 | } | 88 | } |
85 | 89 | ||
86 | // vim: set ts=4 sw=4: | 90 | // vim: set ts=4 sw=4: |
@@ -20,14 +20,22 @@ | @@ -20,14 +20,22 @@ | ||
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | */ | 21 | */ |
22 | 22 | ||
23 | +#include <pthread.h> | ||
24 | + | ||
23 | #include "tr/tree.h" | 25 | #include "tr/tree.h" |
24 | 26 | ||
25 | -void | 27 | +unsigned long |
26 | TR_treeWalk(TR_Tree this, const void * data, TR_TreeAction action) | 28 | TR_treeWalk(TR_Tree this, const void * data, TR_TreeAction action) |
27 | { | 29 | { |
28 | - TR_TreeNode previous = this->root; | ||
29 | - TR_TreeNode node = this->root; | ||
30 | - int depth = 1; | 30 | + TR_TreeNode previous; |
31 | + TR_TreeNode node; | ||
32 | + int depth = 1; | ||
33 | + unsigned long processed = 0; | ||
34 | + | ||
35 | + pthread_mutex_lock(&(this->lock)); | ||
36 | + | ||
37 | + previous = this->root; | ||
38 | + node = this->root; | ||
31 | 39 | ||
32 | while (NULL != node) { | 40 | while (NULL != node) { |
33 | if (previous == TR_TREE_RIGHT(node)) { | 41 | if (previous == TR_TREE_RIGHT(node)) { |
@@ -40,6 +48,7 @@ TR_treeWalk(TR_Tree this, const void * data, TR_TreeAction action) | @@ -40,6 +48,7 @@ TR_treeWalk(TR_Tree this, const void * data, TR_TreeAction action) | ||
40 | if (NULL == TR_TREE_LEFT(node) || previous == TR_TREE_LEFT(node)) { | 48 | if (NULL == TR_TREE_LEFT(node) || previous == TR_TREE_LEFT(node)) { |
41 | if (action) { | 49 | if (action) { |
42 | action(node->data, data, depth); | 50 | action(node->data, data, depth); |
51 | + processed++; | ||
43 | } | 52 | } |
44 | previous = node; | 53 | previous = node; |
45 | 54 | ||
@@ -56,6 +65,10 @@ TR_treeWalk(TR_Tree this, const void * data, TR_TreeAction action) | @@ -56,6 +65,10 @@ TR_treeWalk(TR_Tree this, const void * data, TR_TreeAction action) | ||
56 | depth++; | 65 | depth++; |
57 | } | 66 | } |
58 | } | 67 | } |
68 | + | ||
69 | + pthread_mutex_unlock(&(this->lock)); | ||
70 | + | ||
71 | + return processed; | ||
59 | } | 72 | } |
60 | 73 | ||
61 | // vim: set ts=4 sw=4: | 74 | // vim: set ts=4 sw=4: |
Please
register
or
login
to post a comment