Changeset 6b0a7efc in rtems
- Timestamp:
- 07/21/14 16:08:07 (9 years ago)
- Branches:
- 4.11, 5, master
- Children:
- d7a94693
- Parents:
- 87894c0
- git-author:
- Sebastian Huber <sebastian.huber@…> (07/21/14 16:08:07)
- git-committer:
- Sebastian Huber <sebastian.huber@…> (07/22/14 10:30:53)
- Location:
- cpukit/score/src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
cpukit/score/src/rbtree.c
r87894c0 r6b0a7efc 32 32 ) 33 33 { 34 size_t count;34 size_t count; 35 35 RBTree_Node *next; 36 36 37 37 /* TODO: Error message? */ 38 if (!the_rbtree) return; 38 if ( !the_rbtree ) 39 return; 39 40 40 41 /* could do sanity checks here */ … … 42 43 43 44 count = number_nodes; 44 next = starting_address; 45 next = starting_address; 46 45 47 while ( count-- ) { 46 48 _RBTree_Insert( the_rbtree, next, compare, is_unique ); -
cpukit/score/src/rbtreeextract.c
r87894c0 r6b0a7efc 22 22 * of the extract operation. 23 23 */ 24 static void _RBTree_Extract_validate( 25 RBTree_Node *the_node 26 ) 24 static void _RBTree_Extract_validate( RBTree_Node *the_node ) 27 25 { 28 RBTree_Node *parent, *sibling;26 RBTree_Node *parent, *sibling; 29 27 RBTree_Direction dir; 30 28 31 29 parent = the_node->parent; 32 if(!parent->parent) return; 33 34 sibling = _RBTree_Sibling(the_node); 30 31 if ( !parent->parent ) 32 return; 33 34 sibling = _RBTree_Sibling( the_node ); 35 35 36 36 /* continue to correct tree as long as the_node is black and not the root */ 37 while (!_RBTree_Is_red(the_node) && parent->parent) { 38 37 while ( !_RBTree_Is_red( the_node ) && parent->parent ) { 39 38 /* if sibling is red, switch parent (black) and sibling colors, 40 39 * then rotate parent left, making the sibling be the_node's grandparent. … … 42 41 * update sibling pointer. 43 42 */ 44 if ( _RBTree_Is_red(sibling)) {43 if ( _RBTree_Is_red( sibling ) ) { 45 44 parent->color = RBT_RED; 46 45 sibling->color = RBT_BLACK; 47 dir = the_node != parent->child[ 0];48 _RBTree_Rotate( parent, dir);49 sibling = parent->child[ _RBTree_Opposite_direction(dir)];46 dir = the_node != parent->child[ 0 ]; 47 _RBTree_Rotate( parent, dir ); 48 sibling = parent->child[ _RBTree_Opposite_direction( dir ) ]; 50 49 } 51 50 52 51 /* sibling is black, see if both of its children are also black. */ 53 if (!_RBTree_Is_red(sibling->child[RBT_RIGHT]) && 54 !_RBTree_Is_red(sibling->child[RBT_LEFT])) { 55 sibling->color = RBT_RED; 56 if (_RBTree_Is_red(parent)) { 57 parent->color = RBT_BLACK; 58 break; 59 } 60 the_node = parent; /* done if parent is red */ 61 parent = the_node->parent; 62 sibling = _RBTree_Sibling(the_node); 52 if ( !_RBTree_Is_red( sibling->child[ RBT_RIGHT ] ) && 53 !_RBTree_Is_red( sibling->child[ RBT_LEFT ] ) ) { 54 sibling->color = RBT_RED; 55 56 if ( _RBTree_Is_red( parent ) ) { 57 parent->color = RBT_BLACK; 58 break; 59 } 60 61 the_node = parent; /* done if parent is red */ 62 parent = the_node->parent; 63 sibling = _RBTree_Sibling( the_node ); 63 64 } else { 64 65 /* at least one of sibling's children is red. we now proceed in two … … 68 69 * Then switch the sibling and parent colors, and rotate through parent. 69 70 */ 70 dir = the_node != parent->child[0]; 71 if (!_RBTree_Is_red(sibling->child[_RBTree_Opposite_direction(dir)])) { 71 dir = the_node != parent->child[ 0 ]; 72 73 if ( 74 !_RBTree_Is_red( sibling->child[ _RBTree_Opposite_direction( dir ) ] ) 75 ) { 72 76 sibling->color = RBT_RED; 73 sibling->child[ dir]->color = RBT_BLACK;74 _RBTree_Rotate( sibling, _RBTree_Opposite_direction(dir));75 sibling = parent->child[ _RBTree_Opposite_direction(dir)];77 sibling->child[ dir ]->color = RBT_BLACK; 78 _RBTree_Rotate( sibling, _RBTree_Opposite_direction( dir ) ); 79 sibling = parent->child[ _RBTree_Opposite_direction( dir ) ]; 76 80 } 81 77 82 sibling->color = parent->color; 78 83 parent->color = RBT_BLACK; 79 sibling->child[ _RBTree_Opposite_direction(dir)]->color = RBT_BLACK;80 _RBTree_Rotate( parent, dir);84 sibling->child[ _RBTree_Opposite_direction( dir ) ]->color = RBT_BLACK; 85 _RBTree_Rotate( parent, dir ); 81 86 break; /* done */ 82 87 } 83 88 } /* while */ 84 if(!the_node->parent->parent) the_node->color = RBT_BLACK; 89 90 if ( !the_node->parent->parent ) 91 the_node->color = RBT_BLACK; 85 92 } 86 93 … … 93 100 */ 94 101 void _RBTree_Extract( 95 96 RBTree_Node*the_node97 102 RBTree_Control *the_rbtree, 103 RBTree_Node *the_node 104 ) 98 105 { 99 RBTree_Node *leaf, *target;100 RBTree_Color victim_color;106 RBTree_Node *leaf, *target; 107 RBTree_Color victim_color; 101 108 RBTree_Direction dir; 102 109 103 if (!the_node) return; 110 if ( !the_node ) 111 return; 104 112 105 113 /* check if min needs to be updated */ 106 if ( the_node == the_rbtree->first[RBT_LEFT]) {114 if ( the_node == the_rbtree->first[ RBT_LEFT ] ) { 107 115 RBTree_Node *next; 108 next = _RBTree_Successor( the_node);109 the_rbtree->first[ RBT_LEFT] = next;116 next = _RBTree_Successor( the_node ); 117 the_rbtree->first[ RBT_LEFT ] = next; 110 118 } 111 119 112 120 /* Check if max needs to be updated. min=max for 1 element trees so 113 121 * do not use else if here. */ 114 if ( the_node == the_rbtree->first[RBT_RIGHT]) {122 if ( the_node == the_rbtree->first[ RBT_RIGHT ] ) { 115 123 RBTree_Node *previous; 116 previous = _RBTree_Predecessor( the_node);117 the_rbtree->first[ RBT_RIGHT] = previous;124 previous = _RBTree_Predecessor( the_node ); 125 the_rbtree->first[ RBT_RIGHT ] = previous; 118 126 } 119 127 … … 125 133 */ 126 134 127 if (the_node->child[RBT_LEFT] && the_node->child[RBT_RIGHT]) { 128 target = the_node->child[RBT_LEFT]; /* find max in node->child[RBT_LEFT] */ 129 while (target->child[RBT_RIGHT]) target = target->child[RBT_RIGHT]; 135 if ( the_node->child[ RBT_LEFT ] && the_node->child[ RBT_RIGHT ] ) { 136 target = the_node->child[ RBT_LEFT ]; /* find max in node->child[RBT_LEFT] */ 137 138 while ( target->child[ RBT_RIGHT ] ) 139 target = target->child[ RBT_RIGHT ]; 130 140 131 141 /* if the target node has a child, need to move it up the tree into … … 135 145 * For now we store the color of the node being deleted in victim_color. 136 146 */ 137 leaf = target->child[RBT_LEFT]; 138 if(leaf) { 147 leaf = target->child[ RBT_LEFT ]; 148 149 if ( leaf ) { 139 150 leaf->parent = target->parent; 140 151 } else { 141 152 /* fix the tree here if the child is a null leaf. */ 142 _RBTree_Extract_validate(target); 143 } 153 _RBTree_Extract_validate( target ); 154 } 155 144 156 victim_color = target->color; 145 dir = target != target->parent->child[ 0];146 target->parent->child[ dir] = leaf;157 dir = target != target->parent->child[ 0 ]; 158 target->parent->child[ dir ] = leaf; 147 159 148 160 /* now replace the_node with target */ 149 dir = the_node != the_node->parent->child[ 0];150 the_node->parent->child[ dir] = target;161 dir = the_node != the_node->parent->child[ 0 ]; 162 the_node->parent->child[ dir ] = target; 151 163 152 164 /* set target's new children to the original node's children */ 153 target->child[RBT_RIGHT] = the_node->child[RBT_RIGHT]; 154 if (the_node->child[RBT_RIGHT]) 155 the_node->child[RBT_RIGHT]->parent = target; 156 target->child[RBT_LEFT] = the_node->child[RBT_LEFT]; 157 if (the_node->child[RBT_LEFT]) 158 the_node->child[RBT_LEFT]->parent = target; 165 target->child[ RBT_RIGHT ] = the_node->child[ RBT_RIGHT ]; 166 167 if ( the_node->child[ RBT_RIGHT ] ) 168 the_node->child[ RBT_RIGHT ]->parent = target; 169 170 target->child[ RBT_LEFT ] = the_node->child[ RBT_LEFT ]; 171 172 if ( the_node->child[ RBT_LEFT ] ) 173 the_node->child[ RBT_LEFT ]->parent = target; 159 174 160 175 /* finally, update the parent node and recolor. target has completely … … 171 186 * For now we store the color of the node being deleted in victim_color. 172 187 */ 173 leaf = the_node->child[RBT_LEFT] ? 174 the_node->child[RBT_LEFT] : the_node->child[RBT_RIGHT]; 175 if( leaf ) { 188 leaf = the_node->child[ RBT_LEFT ] ? 189 the_node->child[ RBT_LEFT ] : the_node->child[ RBT_RIGHT ]; 190 191 if ( leaf ) { 176 192 leaf->parent = the_node->parent; 177 193 } else { 178 194 /* fix the tree here if the child is a null leaf. */ 179 _RBTree_Extract_validate(the_node); 180 } 195 _RBTree_Extract_validate( the_node ); 196 } 197 181 198 victim_color = the_node->color; 182 199 183 200 /* remove the_node from the tree */ 184 dir = the_node != the_node->parent->child[ 0];185 the_node->parent->child[ dir] = leaf;201 dir = the_node != the_node->parent->child[ 0 ]; 202 the_node->parent->child[ dir ] = leaf; 186 203 } 187 204 … … 191 208 * 2. Deleted a black node, its child must be red. Paint child black. 192 209 */ 193 if ( victim_color == RBT_BLACK) { /* eliminate case 1 */194 if ( leaf) {210 if ( victim_color == RBT_BLACK ) { /* eliminate case 1 */ 211 if ( leaf ) { 195 212 leaf->color = RBT_BLACK; /* case 2 */ 196 213 } … … 198 215 199 216 /* Wipe the_node */ 200 _RBTree_Set_off_rbtree( the_node);217 _RBTree_Set_off_rbtree( the_node ); 201 218 202 219 /* set root to black, if it exists */ 203 if (the_rbtree->root) the_rbtree->root->color = RBT_BLACK; 220 if ( the_rbtree->root ) 221 the_rbtree->root->color = RBT_BLACK; 204 222 } -
cpukit/score/src/rbtreefind.c
r87894c0 r6b0a7efc 27 27 ) 28 28 { 29 RBTree_Node *iter_node = the_rbtree->root;30 RBTree_Node *found = NULL;29 RBTree_Node *iter_node = the_rbtree->root; 30 RBTree_Node *found = NULL; 31 31 32 32 while ( iter_node != NULL ) { 33 int compare_result = ( *compare )( the_node, iter_node );33 int compare_result = ( *compare )( the_node, iter_node ); 34 34 RBTree_Direction dir; 35 35 36 36 if ( _RBTree_Is_equal( compare_result ) ) { 37 37 found = iter_node; 38 38 39 if ( is_unique ) 39 40 break; -
cpukit/score/src/rbtreeinsert.c
r87894c0 r6b0a7efc 22 22 * append operation. 23 23 */ 24 static void _RBTree_Validate_insert( 25 RBTree_Node *the_node 26 ) 24 static void _RBTree_Validate_insert( RBTree_Node *the_node ) 27 25 { 28 RBTree_Node *u, *g;26 RBTree_Node *u, *g; 29 27 30 28 /* note: the insert root case is handled already */ 31 29 /* if the parent is black, nothing needs to be done 32 30 * otherwise may need to loop a few times */ 33 while ( _RBTree_Is_red(_RBTree_Parent(the_node))) {34 u = _RBTree_Parent_sibling( the_node);31 while ( _RBTree_Is_red( _RBTree_Parent( the_node ) ) ) { 32 u = _RBTree_Parent_sibling( the_node ); 35 33 g = the_node->parent->parent; 36 34 37 35 /* if uncle is red, repaint uncle/parent black and grandparent red */ 38 if (_RBTree_Is_red(u)) {36 if ( _RBTree_Is_red( u ) ) { 39 37 the_node->parent->color = RBT_BLACK; 40 38 u->color = RBT_BLACK; … … 42 40 the_node = g; 43 41 } else { /* if uncle is black */ 44 RBTree_Direction dir = the_node != the_node->parent->child[ 0];45 RBTree_Direction pdir = the_node->parent != g->child[ 0];42 RBTree_Direction dir = the_node != the_node->parent->child[ 0 ]; 43 RBTree_Direction pdir = the_node->parent != g->child[ 0 ]; 46 44 47 45 /* ensure node is on the same branch direction as parent */ 48 if ( dir != pdir) {49 _RBTree_Rotate( the_node->parent, pdir);50 the_node = the_node->child[ pdir];46 if ( dir != pdir ) { 47 _RBTree_Rotate( the_node->parent, pdir ); 48 the_node = the_node->child[ pdir ]; 51 49 } 50 52 51 the_node->parent->color = RBT_BLACK; 53 52 g->color = RBT_RED; 54 53 55 54 /* now rotate grandparent in the other branch direction (toward uncle) */ 56 _RBTree_Rotate( g, (1-pdir));55 _RBTree_Rotate( g, ( 1 - pdir ) ); 57 56 } 58 57 } 59 if(!the_node->parent->parent) the_node->color = RBT_BLACK; 58 59 if ( !the_node->parent->parent ) 60 the_node->color = RBT_BLACK; 60 61 } 61 62 … … 67 68 ) 68 69 { 69 if(!the_node) return (RBTree_Node*)-1; 70 if ( !the_node ) 71 return (RBTree_Node *) -1; 70 72 71 73 RBTree_Node *iter_node = the_rbtree->root; 72 int compare_result;73 74 74 if ( !iter_node) { /* special case: first node inserted */75 if ( !iter_node ) { /* special case: first node inserted */ 75 76 the_node->color = RBT_BLACK; 76 77 the_rbtree->root = the_node; 77 the_rbtree->first[ 0] = the_rbtree->first[1] = the_node;78 the_rbtree->first[ 0 ] = the_rbtree->first[ 1 ] = the_node; 78 79 the_node->parent = (RBTree_Node *) the_rbtree; 79 the_node->child[ RBT_LEFT] = the_node->child[RBT_RIGHT] = NULL;80 the_node->child[ RBT_LEFT ] = the_node->child[ RBT_RIGHT ] = NULL; 80 81 } else { 81 82 /* typical binary search tree insert, descend tree to leaf and insert */ 82 while (iter_node) { 83 compare_result = ( *compare )( the_node, iter_node ); 83 while ( iter_node ) { 84 int compare_result = ( *compare )( the_node, iter_node ); 85 84 86 if ( is_unique && _RBTree_Is_equal( compare_result ) ) 85 87 return iter_node; 88 86 89 RBTree_Direction dir = !_RBTree_Is_lesser( compare_result ); 87 if (!iter_node->child[dir]) { 88 the_node->child[RBT_LEFT] = the_node->child[RBT_RIGHT] = NULL; 90 91 if ( !iter_node->child[ dir ] ) { 92 the_node->child[ RBT_LEFT ] = the_node->child[ RBT_RIGHT ] = NULL; 89 93 the_node->color = RBT_RED; 90 iter_node->child[ dir] = the_node;94 iter_node->child[ dir ] = the_node; 91 95 the_node->parent = iter_node; 92 96 /* update min/max */ … … 95 99 _RBTree_First( the_rbtree, dir ) 96 100 ); 97 if ( (!dir && _RBTree_Is_lesser(compare_result)) || 98 (dir && _RBTree_Is_greater(compare_result)) ) { 99 the_rbtree->first[dir] = the_node; 101 102 if ( 103 ( !dir && _RBTree_Is_lesser( compare_result ) ) 104 || ( dir && _RBTree_Is_greater( compare_result ) ) 105 ) { 106 the_rbtree->first[ dir ] = the_node; 100 107 } 108 101 109 break; 102 110 } else { 103 iter_node = iter_node->child[ dir];111 iter_node = iter_node->child[ dir ]; 104 112 } 105 106 113 } /* while(iter_node) */ 107 114 108 115 /* verify red-black properties */ 109 _RBTree_Validate_insert( the_node);116 _RBTree_Validate_insert( the_node ); 110 117 } 111 return (RBTree_Node*)0; 118 119 return (RBTree_Node *) 0; 112 120 } -
cpukit/score/src/rbtreeiterate.c
r87894c0 r6b0a7efc 29 29 void _RBTree_Iterate( 30 30 const RBTree_Control *rbtree, 31 RBTree_Direction dir,32 RBTree_Visitor visitor,33 void *visitor_arg31 RBTree_Direction dir, 32 RBTree_Visitor visitor, 33 void *visitor_arg 34 34 ) 35 35 { 36 RBTree_Direction opp_dir = _RBTree_Opposite_direction( dir );36 RBTree_Direction opp_dir = _RBTree_Opposite_direction( dir ); 37 37 const RBTree_Node *current = _RBTree_First( rbtree, opp_dir ); 38 bool stop = false;38 bool stop = false; 39 39 40 40 while ( !stop && current != NULL ) { 41 stop = ( *visitor)( current, dir, visitor_arg );41 stop = ( *visitor )( current, dir, visitor_arg ); 42 42 43 43 current = _RBTree_Next( current, dir ); -
cpukit/score/src/rbtreenext.c
r87894c0 r6b0a7efc 30 30 RBTree_Node *_RBTree_Next( 31 31 const RBTree_Node *node, 32 RBTree_Direction dir32 RBTree_Direction dir 33 33 ) 34 34 { 35 35 RBTree_Direction opp_dir = _RBTree_Opposite_direction( dir ); 36 RBTree_Node *current = node->child [dir];37 RBTree_Node *next = NULL;36 RBTree_Node *current = node->child[ dir ]; 37 RBTree_Node *next = NULL; 38 38 39 39 if ( current != NULL ) { 40 40 next = current; 41 while ( (current = current->child [opp_dir]) != NULL ) { 41 42 while ( ( current = current->child[ opp_dir ] ) != NULL ) { 42 43 next = current; 43 44 } … … 45 46 RBTree_Node *parent = node->parent; 46 47 47 if ( parent->parent && node == parent->child [opp_dir] ) {48 if ( parent->parent && node == parent->child[ opp_dir ] ) { 48 49 next = parent; 49 50 } else { 50 while ( parent->parent && node == parent->child [dir] ) {51 while ( parent->parent && node == parent->child[ dir ] ) { 51 52 node = parent; 52 53 parent = parent->parent;
Note: See TracChangeset
for help on using the changeset viewer.