[svn:parrot] r45353 - branches/avl_string_cache/include/parrot
bacek at svn.parrot.org
bacek at svn.parrot.org
Wed Mar 31 11:45:35 UTC 2010
Author: bacek
Date: Wed Mar 31 11:45:35 2010
New Revision: 45353
URL: https://trac.parrot.org/parrot/changeset/45353
Log:
Simplify and restyle AVL code.
- Drop redundant ##field into mangled names. We can't have more-than-one-tree per node.
- Reindent according to Parrot codestyle.
- Small optimization to avoid second call to compare() during lookups.
Modified:
branches/avl_string_cache/include/parrot/avl_tree.h
Modified: branches/avl_string_cache/include/parrot/avl_tree.h
==============================================================================
--- branches/avl_string_cache/include/parrot/avl_tree.h Wed Mar 31 11:45:15 2010 (r45352)
+++ branches/avl_string_cache/include/parrot/avl_tree.h Wed Mar 31 11:45:35 2010 (r45353)
@@ -66,157 +66,190 @@
/* Recursion prevents the following from being defined as macros. */
-#define TREE_DEFINE(node, field) \
- \
- struct node *TREE_BALANCE_##node##_##field(struct node *); \
- struct node *TREE_ROTL_##node##_##field(struct node *self); \
- struct node *TREE_ROTL_##node##_##field(struct node *self); \
- struct node *TREE_ROTR_##node##_##field(struct node *self); \
- struct node *TREE_BALANCE_##node##_##field(struct node *self); \
- struct node *TREE_INSERT_##node##_##field( \
- struct node *self, struct node *elm, \
- int (*compare)(struct node *lhs, struct node *rhs)); \
- struct node *TREE_FIND_##node##_##field( \
- struct node *self, struct node *elm, \
- int (*compare)(struct node *lhs, struct node *rhs)); \
- struct node *TREE_MOVE_RIGHT(struct node *self, struct node *rhs); \
- \
- struct node *TREE_ROTL_##node##_##field(struct node *self) \
- { \
- struct node *r= self->field.avl_right; \
- self->field.avl_right= r->field.avl_left; \
- r->field.avl_left= TREE_BALANCE_##node##_##field(self); \
- return TREE_BALANCE_##node##_##field(r); \
- } \
- \
- struct node *TREE_ROTR_##node##_##field(struct node *self) \
- { \
- struct node *l= self->field.avl_left; \
- self->field.avl_left= l->field.avl_right; \
- l->field.avl_right= TREE_BALANCE_##node##_##field(self); \
- return TREE_BALANCE_##node##_##field(l); \
- } \
- \
- struct node *TREE_BALANCE_##node##_##field(struct node *self) \
- { \
- int delta= TREE_DELTA(self, field); \
- \
- if (delta < -TREE_DELTA_MAX) \
- { \
- if (TREE_DELTA(self->field.avl_right, field) > 0) \
- self->field.avl_right= TREE_ROTR_##node##_##field(self->field.avl_right); \
- return TREE_ROTL_##node##_##field(self); \
- } \
- else if (delta > TREE_DELTA_MAX) \
- { \
- if (TREE_DELTA(self->field.avl_left, field) < 0) \
- self->field.avl_left= TREE_ROTL_##node##_##field(self->field.avl_left); \
- return TREE_ROTR_##node##_##field(self); \
- } \
- self->field.avl_height= 0; \
- if (self->field.avl_left && (self->field.avl_left->field.avl_height > self->field.avl_height)) \
- self->field.avl_height= self->field.avl_left->field.avl_height; \
- if (self->field.avl_right && (self->field.avl_right->field.avl_height > self->field.avl_height)) \
- self->field.avl_height= self->field.avl_right->field.avl_height; \
- self->field.avl_height += 1; \
- return self; \
- } \
- \
- struct node *TREE_INSERT_##node##_##field( \
- struct node *self, struct node *elm, \
- int (*compare)(struct node *lhs, struct node *rhs) \
- ) \
- { \
- if (!self) \
- return elm; \
- if (compare(elm, self) < 0) \
- self->field.avl_left= TREE_INSERT_##node##_##field(self->field.avl_left, elm, compare); \
- else \
- self->field.avl_right= TREE_INSERT_##node##_##field(self->field.avl_right, elm, compare); \
- return TREE_BALANCE_##node##_##field(self); \
- } \
- \
- struct node *TREE_FIND_##node##_##field( \
- struct node *self, struct node *elm, \
- int (*compare)(struct node *lhs, struct node *rhs) \
- ) \
- { \
- if (!self) \
- return 0; \
- if (compare(elm, self) == 0) \
- return self; \
- if (compare(elm, self) < 0) \
- return TREE_FIND_##node##_##field(self->field.avl_left, elm, compare); \
- else \
- return TREE_FIND_##node##_##field(self->field.avl_right, elm, compare); \
- } \
- \
- struct node *TREE_MOVE_RIGHT(struct node *self, struct node *rhs) \
- { \
- if (!self) \
- return rhs; \
- self->field.avl_right= TREE_MOVE_RIGHT(self->field.avl_right, rhs); \
- return TREE_BALANCE_##node##_##field(self); \
- } \
- \
- struct node *TREE_REMOVE_##node##_##field \
- (struct node *self, struct node *elm, int (*compare)(struct node *lhs, struct node *rhs)) \
- { \
- if (!self) return 0; \
- \
- if (compare(elm, self) == 0) \
- { \
- struct node *tmp= TREE_MOVE_RIGHT(self->field.avl_left, self->field.avl_right); \
- self->field.avl_left= 0; \
- self->field.avl_right= 0; \
- return tmp; \
- } \
- if (compare(elm, self) < 0) \
- self->field.avl_left= TREE_REMOVE_##node##_##field(self->field.avl_left, elm, compare); \
- else \
- self->field.avl_right= TREE_REMOVE_##node##_##field(self->field.avl_right, elm, compare); \
- return TREE_BALANCE_##node##_##field(self); \
- } \
- \
- void TREE_FORWARD_APPLY_ALL_##node##_##field \
- (struct node *self, void (*function)(struct node *node, void *data), void *data) \
- { \
- if (self) \
- { \
- TREE_FORWARD_APPLY_ALL_##node##_##field(self->field.avl_left, function, data); \
- function(self, data); \
- TREE_FORWARD_APPLY_ALL_##node##_##field(self->field.avl_right, function, data); \
- } \
- } \
- \
- void TREE_REVERSE_APPLY_ALL_##node##_##field \
- (struct node *self, void (*function)(struct node *node, void *data), void *data) \
- { \
- if (self) \
- { \
- TREE_REVERSE_APPLY_ALL_##node##_##field(self->field.avl_right, function, data); \
- function(self, data); \
- TREE_REVERSE_APPLY_ALL_##node##_##field(self->field.avl_left, function, data); \
- } \
- }
+#define TREE_DEFINE(node, field) \
+ \
+struct node *TREE_BALANCE_##node(struct node *); \
+ \
+struct node *TREE_ROTL_##node(struct node *self); \
+ \
+struct node *TREE_ROTR_##node(struct node *self); \
+ \
+struct node *TREE_BALANCE_##node(struct node *self); \
+ \
+struct node *TREE_INSERT_##node( \
+ struct node *self, struct node *elm, \
+ int (*compare)(struct node *lhs, struct node *rhs)); \
+ \
+struct node *TREE_FIND_##node( \
+ struct node *self, struct node *elm, \
+ int (*compare)(struct node *lhs, struct node *rhs)); \
+ \
+struct node *TREE_MOVE_RIGHT_##node(struct node *self, struct node *rhs); \
+ \
+struct node * \
+TREE_REMOVE_##node(struct node *self, struct node *elm, \
+ int (*compare)(struct node *lhs, struct node *rhs)); \
+ \
+void \
+TREE_FORWARD_APPLY_ALL_##node (struct node *self, \
+ void (*function)(struct node *node, void *data), \
+ void *data); \
+ \
+void \
+TREE_REVERSE_APPLY_ALL_##node(struct node *self, \
+ void (*function)(struct node *node, void *data), \
+ void *data); \
+ \
+struct node *TREE_ROTL_##node(struct node *self) \
+{ \
+ struct node *r = self->field.avl_right; \
+ self->field.avl_right = r->field.avl_left; \
+ r->field.avl_left = TREE_BALANCE_##node(self); \
+ return TREE_BALANCE_##node(r); \
+} \
+ \
+struct node *TREE_ROTR_##node(struct node *self) \
+{ \
+ struct node *l = self->field.avl_left; \
+ self->field.avl_left = l->field.avl_right; \
+ l->field.avl_right = TREE_BALANCE_##node(self); \
+ return TREE_BALANCE_##node(l); \
+} \
+ \
+struct node *TREE_BALANCE_##node(struct node *self) \
+{ \
+ int delta = TREE_DELTA(self, field); \
+ \
+ if (delta < -TREE_DELTA_MAX) { \
+ if (TREE_DELTA(self->field.avl_right, field) > 0) \
+ self->field.avl_right = TREE_ROTR_##node(self->field.avl_right); \
+ return TREE_ROTL_##node(self); \
+ } \
+ else if (delta > TREE_DELTA_MAX) { \
+ if (TREE_DELTA(self->field.avl_left, field) < 0) \
+ self->field.avl_left = TREE_ROTL_##node(self->field.avl_left); \
+ return TREE_ROTR_##node(self); \
+ } \
+ self->field.avl_height = 0; \
+ if (self->field.avl_left \
+ && (self->field.avl_left->field.avl_height > self->field.avl_height)) \
+ self->field.avl_height = self->field.avl_left->field.avl_height; \
+ if (self->field.avl_right \
+ && (self->field.avl_right->field.avl_height > self->field.avl_height)) \
+ self->field.avl_height = self->field.avl_right->field.avl_height; \
+ \
+ self->field.avl_height += 1; \
+ return self; \
+} \
+ \
+struct node * \
+TREE_INSERT_##node(struct node *self, struct node *elm, \
+ int (*compare)(struct node *lhs, struct node *rhs)) \
+{ \
+ if (!self) \
+ return elm; \
+ if (compare(elm, self) < 0) \
+ self->field.avl_left = TREE_INSERT_##node(self->field.avl_left, \
+ elm, compare); \
+ else \
+ self->field.avl_right = TREE_INSERT_##node(self->field.avl_right, \
+ elm, compare); \
+ return TREE_BALANCE_##node(self); \
+} \
+ \
+struct node * \
+TREE_FIND_##node(struct node *self, struct node *elm, \
+ int (*compare)(struct node *lhs, struct node *rhs)) \
+{ \
+ int cmp; \
+ if (!self) \
+ return 0; \
+ cmp = compare(elm, self); \
+ if (cmp == 0) \
+ return self; \
+ if (cmp < 0) \
+ return TREE_FIND_##node(self->field.avl_left, elm, compare); \
+ else \
+ return TREE_FIND_##node(self->field.avl_right, elm, compare); \
+} \
+ \
+struct node * \
+TREE_MOVE_RIGHT_##node(struct node *self, struct node *rhs) \
+{ \
+ if (!self) \
+ return rhs; \
+ \
+ self->field.avl_right = TREE_MOVE_RIGHT_##node(self->field.avl_right, rhs); \
+ return TREE_BALANCE_##node(self); \
+} \
+ \
+struct node * \
+TREE_REMOVE_##node(struct node *self, struct node *elm, \
+ int (*compare)(struct node *lhs, struct node *rhs)) \
+{ \
+ int cmp; \
+ if (!self) \
+ return 0; \
+ \
+ cmp = compare(elm, self); \
+ if (cmp == 0) \
+ { \
+ struct node *tmp = TREE_MOVE_RIGHT_##node(self->field.avl_left, \
+ self->field.avl_right); \
+ self->field.avl_left = 0; \
+ self->field.avl_right = 0; \
+ return tmp; \
+ } \
+ if (cmp < 0) \
+ self->field.avl_left = TREE_REMOVE_##node(self->field.avl_left, \
+ elm, compare); \
+ else \
+ self->field.avl_right= TREE_REMOVE_##node(self->field.avl_right, \
+ elm, compare); \
+ return TREE_BALANCE_##node(self); \
+} \
+ \
+void \
+TREE_FORWARD_APPLY_ALL_##node (struct node *self, \
+ void (*function)(struct node *node, void *data), \
+ void *data) \
+{ \
+ if (self) { \
+ TREE_FORWARD_APPLY_ALL_##node(self->field.avl_left, function, data); \
+ function(self, data); \
+ TREE_FORWARD_APPLY_ALL_##node(self->field.avl_right, function, data); \
+ } \
+} \
+ \
+void \
+TREE_REVERSE_APPLY_ALL_##node(struct node *self, \
+ void (*function)(struct node *node, void *data), \
+ void *data) \
+{ \
+ if (self) { \
+ TREE_REVERSE_APPLY_ALL_##node(self->field.avl_right, function, data); \
+ function(self, data); \
+ TREE_REVERSE_APPLY_ALL_##node(self->field.avl_left, function, data); \
+ } \
+}
#define TREE_INSERT(head, node, field, elm) \
- ((head)->th_root= TREE_INSERT_##node##_##field((head)->th_root, (elm), (head)->th_cmp))
+ ((head)->th_root= TREE_INSERT_##node((head)->th_root, (elm), (head)->th_cmp))
#define TREE_FIND(head, node, field, elm) \
- (TREE_FIND_##node##_##field((head)->th_root, (elm), (head)->th_cmp))
+ (TREE_FIND_##node((head)->th_root, (elm), (head)->th_cmp))
#define TREE_REMOVE(head, node, field, elm) \
- ((head)->th_root= TREE_REMOVE_##node##_##field((head)->th_root, (elm), (head)->th_cmp))
+ ((head)->th_root= TREE_REMOVE_##node((head)->th_root, (elm), (head)->th_cmp))
#define TREE_DEPTH(head, field) \
((head)->th_root->field.avl_height)
#define TREE_FORWARD_APPLY(head, node, field, function, data) \
- TREE_FORWARD_APPLY_ALL_##node##_##field((head)->th_root, function, data)
+ TREE_FORWARD_APPLY_ALL_##node((head)->th_root, function, data)
#define TREE_REVERSE_APPLY(head, node, field, function, data) \
- TREE_REVERSE_APPLY_ALL_##node##_##field((head)->th_root, function, data)
+ TREE_REVERSE_APPLY_ALL_##node((head)->th_root, function, data)
#define TREE_INIT(head, cmp) do { \
(head)->th_root= 0; \
More information about the parrot-commits
mailing list