Changeset 5b04fc7 in mainline
- Timestamp:
- 2006-04-01T18:39:25Z (19 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- b9b14a83
- Parents:
- 0cb56f5d
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
generic/src/adt/btree.c
r0cb56f5d r5b04fc7 50 50 static void _btree_remove(btree_t *t, __native key, btree_node_t *node); 51 51 static void node_initialize(btree_node_t *node); 52 static void node_insert_key_and_lsubtree(btree_node_t *node, __native key, void *value, btree_node_t * rsubtree);52 static void node_insert_key_and_lsubtree(btree_node_t *node, __native key, void *value, btree_node_t *lsubtree); 53 53 static void node_insert_key_and_rsubtree(btree_node_t *node, __native key, void *value, btree_node_t *rsubtree); 54 static void node_remove_key_and_lsubtree(btree_node_t *node, __native key); 55 static void node_remove_key_and_rsubtree(btree_node_t *node, __native key); 54 56 static btree_node_t *node_split(btree_node_t *node, __native key, void *value, btree_node_t *rsubtree, __native *median); 55 57 static btree_node_t *node_combine(btree_node_t *node); 56 static void node_remove_key_and_lsubtree(btree_node_t *node, __native key);57 static void node_remove_key_and_rsubtree(btree_node_t *node, __native key);58 58 static index_t find_key_by_subtree(btree_node_t *node, btree_node_t *subtree, bool right); 59 59 static void rotate_from_right(btree_node_t *lnode, btree_node_t *rnode, index_t idx); … … 155 155 156 156 if (LEAF_NODE(node)) { 157 list_ append(&rnode->leaf_link, &node->leaf_link);157 list_prepend(&rnode->leaf_link, &node->leaf_link); 158 158 } 159 159 … … 190 190 btree_node_t *lnode; 191 191 192 panic("%s needs testing and is disabled in revision %s\n", __FUNCTION__, REVISION);193 192 lnode = leaf_node; 194 193 if (!lnode) { … … 437 436 } 438 437 439 /** Split full B-tree node and insert new key-value-right-subtree triplet.440 *441 * This function will split a node and return pointer to a newly created442 * node containing keys greater than or equal to the greater of medians443 * (or median) of the old keys and the newly added key. It will also write444 * the median key to a memory address supplied by the caller.445 *446 * If the node being split is an index node, the median will not be447 * included in the new node. If the node is a leaf node,448 * the median will be copied there.449 *450 * @param node B-tree node wich is going to be split.451 * @param key The key to be inserted.452 * @param value Pointer to the value to be inserted.453 * @param rsubtree Pointer to the right subtree of the key being added.454 * @param median Address in memory, where the median key will be stored.455 *456 * @return Newly created right sibling of node.457 */458 btree_node_t *node_split(btree_node_t *node, __native key, void *value, btree_node_t *rsubtree, __native *median)459 {460 btree_node_t *rnode;461 int i, j;462 463 ASSERT(median);464 ASSERT(node->keys == BTREE_MAX_KEYS);465 466 /*467 * Use the extra space to store the extra node.468 */469 node_insert_key_and_rsubtree(node, key, value, rsubtree);470 471 /*472 * Compute median of keys.473 */474 *median = MEDIAN_HIGH(node);475 476 /*477 * Allocate and initialize new right sibling.478 */479 rnode = (btree_node_t *) malloc(sizeof(btree_node_t), 0);480 node_initialize(rnode);481 rnode->parent = node->parent;482 rnode->depth = node->depth;483 484 /*485 * Copy big keys, values and subtree pointers to the new right sibling.486 * If this is an index node, do not copy the median.487 */488 i = (int) INDEX_NODE(node);489 for (i += MEDIAN_HIGH_INDEX(node), j = 0; i < node->keys; i++, j++) {490 rnode->key[j] = node->key[i];491 rnode->value[j] = node->value[i];492 rnode->subtree[j] = node->subtree[i];493 494 /*495 * Fix parent links in subtrees.496 */497 if (rnode->subtree[j])498 rnode->subtree[j]->parent = rnode;499 500 }501 rnode->subtree[j] = node->subtree[i];502 if (rnode->subtree[j])503 rnode->subtree[j]->parent = rnode;504 505 rnode->keys = j; /* Set number of keys of the new node. */506 node->keys /= 2; /* Shrink the old node. */507 508 return rnode;509 }510 511 /** Combine node with any of its siblings.512 *513 * The siblings are required to be below the fill factor.514 *515 * @param node Node to combine with one of its siblings.516 *517 * @return Pointer to the rightmost of the two nodes.518 */519 btree_node_t *node_combine(btree_node_t *node)520 {521 index_t idx;522 btree_node_t *rnode;523 int i;524 525 ASSERT(!ROOT_NODE(node));526 527 idx = find_key_by_subtree(node->parent, node, false);528 if (idx == node->parent->keys) {529 /*530 * Rightmost subtree of its parent, combine with the left sibling.531 */532 idx--;533 rnode = node;534 node = node->parent->subtree[idx];535 } else {536 rnode = node->parent->subtree[idx + 1];537 }538 539 /* Index nodes need to insert parent node key in between left and right node. */540 if (INDEX_NODE(node))541 node->key[node->keys++] = node->parent->key[idx];542 543 /* Copy the key-value-subtree triplets from the right node. */544 for (i = 0; i < rnode->keys; i++) {545 node->key[node->keys + i] = rnode->key[i];546 node->value[node->keys + i] = rnode->value[i];547 if (INDEX_NODE(node)) {548 node->subtree[node->keys + i] = rnode->subtree[i];549 rnode->subtree[i]->parent = node;550 }551 }552 if (INDEX_NODE(node)) {553 node->subtree[node->keys + i] = rnode->subtree[i];554 rnode->subtree[i]->parent = node;555 }556 557 node->keys += rnode->keys;558 559 return rnode;560 }561 562 438 /** Remove key and its left subtree pointer from B-tree node. 563 439 * … … 615 491 } 616 492 493 /** Split full B-tree node and insert new key-value-right-subtree triplet. 494 * 495 * This function will split a node and return pointer to a newly created 496 * node containing keys greater than or equal to the greater of medians 497 * (or median) of the old keys and the newly added key. It will also write 498 * the median key to a memory address supplied by the caller. 499 * 500 * If the node being split is an index node, the median will not be 501 * included in the new node. If the node is a leaf node, 502 * the median will be copied there. 503 * 504 * @param node B-tree node wich is going to be split. 505 * @param key The key to be inserted. 506 * @param value Pointer to the value to be inserted. 507 * @param rsubtree Pointer to the right subtree of the key being added. 508 * @param median Address in memory, where the median key will be stored. 509 * 510 * @return Newly created right sibling of node. 511 */ 512 btree_node_t *node_split(btree_node_t *node, __native key, void *value, btree_node_t *rsubtree, __native *median) 513 { 514 btree_node_t *rnode; 515 int i, j; 516 517 ASSERT(median); 518 ASSERT(node->keys == BTREE_MAX_KEYS); 519 520 /* 521 * Use the extra space to store the extra node. 522 */ 523 node_insert_key_and_rsubtree(node, key, value, rsubtree); 524 525 /* 526 * Compute median of keys. 527 */ 528 *median = MEDIAN_HIGH(node); 529 530 /* 531 * Allocate and initialize new right sibling. 532 */ 533 rnode = (btree_node_t *) malloc(sizeof(btree_node_t), 0); 534 node_initialize(rnode); 535 rnode->parent = node->parent; 536 rnode->depth = node->depth; 537 538 /* 539 * Copy big keys, values and subtree pointers to the new right sibling. 540 * If this is an index node, do not copy the median. 541 */ 542 i = (int) INDEX_NODE(node); 543 for (i += MEDIAN_HIGH_INDEX(node), j = 0; i < node->keys; i++, j++) { 544 rnode->key[j] = node->key[i]; 545 rnode->value[j] = node->value[i]; 546 rnode->subtree[j] = node->subtree[i]; 547 548 /* 549 * Fix parent links in subtrees. 550 */ 551 if (rnode->subtree[j]) 552 rnode->subtree[j]->parent = rnode; 553 554 } 555 rnode->subtree[j] = node->subtree[i]; 556 if (rnode->subtree[j]) 557 rnode->subtree[j]->parent = rnode; 558 559 rnode->keys = j; /* Set number of keys of the new node. */ 560 node->keys /= 2; /* Shrink the old node. */ 561 562 return rnode; 563 } 564 565 /** Combine node with any of its siblings. 566 * 567 * The siblings are required to be below the fill factor. 568 * 569 * @param node Node to combine with one of its siblings. 570 * 571 * @return Pointer to the rightmost of the two nodes. 572 */ 573 btree_node_t *node_combine(btree_node_t *node) 574 { 575 index_t idx; 576 btree_node_t *rnode; 577 int i; 578 579 ASSERT(!ROOT_NODE(node)); 580 581 idx = find_key_by_subtree(node->parent, node, false); 582 if (idx == node->parent->keys) { 583 /* 584 * Rightmost subtree of its parent, combine with the left sibling. 585 */ 586 idx--; 587 rnode = node; 588 node = node->parent->subtree[idx]; 589 } else { 590 rnode = node->parent->subtree[idx + 1]; 591 } 592 593 /* Index nodes need to insert parent node key in between left and right node. */ 594 if (INDEX_NODE(node)) 595 node->key[node->keys++] = node->parent->key[idx]; 596 597 /* Copy the key-value-subtree triplets from the right node. */ 598 for (i = 0; i < rnode->keys; i++) { 599 node->key[node->keys + i] = rnode->key[i]; 600 node->value[node->keys + i] = rnode->value[i]; 601 if (INDEX_NODE(node)) { 602 node->subtree[node->keys + i] = rnode->subtree[i]; 603 rnode->subtree[i]->parent = node; 604 } 605 } 606 if (INDEX_NODE(node)) { 607 node->subtree[node->keys + i] = rnode->subtree[i]; 608 rnode->subtree[i]->parent = node; 609 } 610 611 node->keys += rnode->keys; 612 613 return rnode; 614 } 615 617 616 /** Find key by its left or right subtree. 618 617 * … … 879 878 { 880 879 int i, depth = t->root->depth; 881 link_t head; 882 880 link_t head, *cur; 881 882 printf("Printing B-tree:\n"); 883 883 list_initialize(&head); 884 884 list_append(&t->root->bfs_link, &head); … … 906 906 printf("("); 907 907 for (i = 0; i < node->keys; i++) { 908 printf("%d ,", node->key[i]);908 printf("%d%s", node->key[i], i < node->keys - 1 ? "," : ""); 909 909 if (node->depth && node->subtree[i]) { 910 910 list_append(&node->subtree[i]->bfs_link, &head); … … 917 917 } 918 918 printf("\n"); 919 } 919 920 printf("Printing list of leaves:\n"); 921 for (cur = t->leaf_head.next; cur != &t->leaf_head; cur = cur->next) { 922 btree_node_t *node; 923 924 node = list_get_instance(cur, btree_node_t, leaf_link); 925 926 ASSERT(node); 927 928 printf("("); 929 for (i = 0; i < node->keys; i++) 930 printf("%d%s", node->key[i], i < node->keys - 1 ? "," : ""); 931 printf(")"); 932 } 933 printf("\n"); 934 } -
generic/src/mm/slab.c
r0cb56f5d r5b04fc7 40 40 * - cache coloring 41 41 * - dynamic magazine growing (different magazine sizes are already 42 * supported, but we would need to adjust allocati ngstrategy)42 * supported, but we would need to adjust allocation strategy) 43 43 * 44 44 * The SLAB allocator supports per-CPU caches ('magazines') to facilitate -
test/btree/btree1/test.c
r0cb56f5d r5b04fc7 41 41 btree_create(&t); 42 42 43 printf("Inserting keys.\n"); 44 btree_insert(&t, 19, data, NULL); 45 btree_insert(&t, 20, data, NULL); 46 btree_insert(&t, 21, data, NULL); 47 btree_insert(&t, 0, data, NULL); 48 btree_insert(&t, 25, data, NULL); 49 btree_insert(&t, 22, data, NULL); 50 btree_insert(&t, 26, data, NULL); 43 51 btree_insert(&t, 23, data, NULL); 44 52 btree_insert(&t, 24, data, NULL); … … 56 64 btree_insert(&t, 3, data, NULL); 57 65 btree_insert(&t, 6, data, NULL); 58 btree_insert(&t, 19, data, NULL);59 btree_insert(&t, 20, data, NULL);60 btree_insert(&t, 21, data, NULL);61 btree_insert(&t, 0, data, NULL);62 btree_insert(&t, 25, data, NULL);63 btree_insert(&t, 22, data, NULL);64 btree_insert(&t, 26, data, NULL);65 66 btree_insert(&t, 10, data, NULL); 66 67 btree_insert(&t, 11, data, NULL); … … 79 80 btree_print(&t); 80 81 82 printf("Removing keys.\n"); 81 83 btree_remove(&t, 50, NULL); 84 btree_remove(&t, 49, NULL); 85 btree_remove(&t, 51, NULL); 86 btree_remove(&t, 46, NULL); 87 btree_remove(&t, 45, NULL); 88 btree_remove(&t, 48, NULL); 89 btree_remove(&t, 53, NULL); 90 btree_remove(&t, 47, NULL); 91 btree_remove(&t, 52, NULL); 92 btree_remove(&t, 54, NULL); 93 btree_remove(&t, 65, NULL); 94 btree_remove(&t, 60, NULL); 95 btree_remove(&t, 99, NULL); 96 btree_remove(&t, 97, NULL); 97 btree_remove(&t, 57, NULL); 98 btree_remove(&t, 58, NULL); 99 btree_remove(&t, 61, NULL); 100 btree_remove(&t, 64, NULL); 101 btree_remove(&t, 56, NULL); 102 btree_remove(&t, 41, NULL); 82 103 104 for (i = 5; i < 20; i++) 105 btree_remove(&t, i, NULL); 106 107 btree_remove(&t, 2, NULL); 108 btree_remove(&t, 43, NULL); 109 btree_remove(&t, 22, NULL); 110 btree_remove(&t, 100, NULL); 111 btree_remove(&t, 98, NULL); 112 btree_remove(&t, 96, NULL); 113 btree_remove(&t, 66, NULL); 114 btree_remove(&t, 1, NULL); 115 116 for (i = 70; i < 90; i++) 117 btree_remove(&t, i, NULL); 118 119 btree_remove(&t, 20, NULL); 120 btree_remove(&t, 0, NULL); 121 btree_remove(&t, 40, NULL); 122 btree_remove(&t, 3, NULL); 123 btree_remove(&t, 4, NULL); 124 btree_remove(&t, 21, NULL); 125 btree_remove(&t, 44, NULL); 126 btree_remove(&t, 55, NULL); 127 btree_remove(&t, 62, NULL); 128 btree_remove(&t, 26, NULL); 129 btree_remove(&t, 27, NULL); 130 btree_remove(&t, 28, NULL); 131 btree_remove(&t, 29, NULL); 132 btree_remove(&t, 30, NULL); 133 btree_remove(&t, 31, NULL); 134 btree_remove(&t, 32, NULL); 135 btree_remove(&t, 33, NULL); 136 btree_remove(&t, 93, NULL); 137 btree_remove(&t, 95, NULL); 138 btree_remove(&t, 94, NULL); 139 btree_remove(&t, 69, NULL); 140 btree_remove(&t, 68, NULL); 141 btree_remove(&t, 92, NULL); 142 btree_remove(&t, 91, NULL); 143 btree_remove(&t, 67, NULL); 144 btree_remove(&t, 63, NULL); 145 btree_remove(&t, 90, NULL); 146 btree_remove(&t, 59, NULL); 147 btree_remove(&t, 23, NULL); 148 btree_remove(&t, 24, NULL); 149 btree_remove(&t, 25, NULL); 150 btree_remove(&t, 37, NULL); 151 btree_remove(&t, 38, NULL); 152 btree_remove(&t, 42, NULL); 153 btree_remove(&t, 39, NULL); 154 btree_remove(&t, 34, NULL); 155 btree_remove(&t, 35, NULL); 156 btree_remove(&t, 36, NULL); 157 158 btree_print(&t); 83 159 }
Note:
See TracChangeset
for help on using the changeset viewer.