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