44 template <
class KEY,
class VAL>
class asCMap {
49 int Insert(
const KEY &key,
const VAL &value);
104 #define ISRED(x) ((x != 0) && (x)->isRed) 105 #define ISBLACK(x) (!ISRED(x)) 107 template <
class KEY,
class VAL>
struct asSMapNode {
114 void Init(KEY k, VAL v) {
132 template <
class KEY,
class VAL>
138 template <
class KEY,
class VAL>
143 template <
class KEY,
class VAL>
146 int tmpCount = count;
151 other.root = tmpRoot;
152 other.count = tmpCount;
155 template <
class KEY,
class VAL>
161 template <
class KEY,
class VAL>
163 if (p == 0)
return -1;
176 template <
class KEY,
class VAL>
181 template <
class KEY,
class VAL>
191 nnode->value = value;
193 return Insert(nnode);
196 template <
class KEY,
class VAL>
204 if (nnode->key < p->key) {
222 BalanceInsert(nnode);
229 template <
class KEY,
class VAL>
232 while (node != root && node->parent->isRed) {
234 if (node->parent == node->parent->parent->left) {
242 node->parent->isRed =
false;
243 uncle->isRed =
false;
244 node->parent->parent->isRed =
true;
247 node = node->parent->parent;
253 if (node == node->parent->right) {
261 node->parent->isRed =
false;
262 node->parent->parent->isRed =
true;
263 RotateRight(node->parent->parent);
274 node->parent->isRed =
false;
275 uncle->isRed =
false;
276 node = node->parent->parent;
283 if (node == node->parent->left) {
291 node->parent->isRed =
false;
292 node->parent->parent->isRed =
true;
293 RotateLeft(node->parent->parent);
302 template <
class KEY,
class VAL>
307 else if (ISRED(root))
313 int left = 0, right = 0;
315 left = CheckIntegrity(node->left);
317 right = CheckIntegrity(node->right);
319 if (left != right || left == -1)
329 template <
class KEY,
class VAL>
335 else if (key == p->key) {
346 template <
class KEY,
class VAL>
349 asASSERT(node == cursor);
352 asDELETE(node, node_t);
355 template <
class KEY,
class VAL>
357 if (cursor == 0)
return 0;
364 if (node->left == 0 || node->right == 0)
367 remove = node->right;
368 while (
remove->left)
remove =
remove->left;
375 child =
remove->left;
377 child =
remove->right;
379 if (child) child->parent =
remove->parent;
381 if (
remove ==
remove->parent->left)
382 remove->parent->left = child;
384 remove->parent->right = child;
390 BalanceErase(child,
remove->parent);
394 if (
remove != node) {
396 if (node->parent->left == node)
397 node->parent->left =
remove;
399 node->parent->right =
remove;
403 remove->isRed = node->isRed;
404 remove->parent = node->parent;
406 remove->left = node->left;
407 if (
remove->left)
remove->left->parent =
remove;
408 remove->right = node->right;
409 if (
remove->right)
remove->right->parent =
remove;
419 template <
class KEY,
class VAL>
448 while (child != root && ISBLACK(child)) {
449 if (child == parent->left) {
453 if (ISRED(brother)) {
454 brother->isRed =
false;
455 parent->isRed =
true;
457 brother = parent->right;
461 if (brother == 0)
break;
462 if (ISBLACK(brother->left) && ISBLACK(brother->right)) {
465 parent->isRed =
false;
466 brother->isRed =
true;
470 brother->isRed =
true;
472 parent = child->parent;
475 if (ISBLACK(brother->right)) {
476 brother->left->isRed =
false;
477 brother->isRed =
true;
478 RotateRight(brother);
479 brother = parent->right;
483 brother->isRed = parent->isRed;
484 parent->isRed =
false;
485 brother->right->isRed =
false;
493 if (ISRED(brother)) {
494 brother->isRed =
false;
495 parent->isRed =
true;
497 brother = parent->left;
501 if (brother == 0)
break;
502 if (ISBLACK(brother->left) && ISBLACK(brother->right)) {
505 parent->isRed =
false;
506 brother->isRed =
true;
510 brother->isRed =
true;
512 parent = child->parent;
515 if (ISBLACK(brother->left)) {
516 brother->right->isRed =
false;
517 brother->isRed =
true;
519 brother = parent->left;
523 brother->isRed = parent->isRed;
524 parent->isRed =
false;
525 brother->left->isRed =
false;
533 child->isRed =
false;
536 template <
class KEY,
class VAL>
544 if (node->left == 0)
return -1;
551 if (parent->left == node)
554 parent->right = left;
556 left->parent = parent;
563 node->left = left->right;
564 if (node->left) node->left->parent = node;
573 template <
class KEY,
class VAL>
581 if (node->right == 0)
return -1;
588 if (parent->right == node)
589 parent->right = right;
591 parent->left = right;
593 right->parent = parent;
600 node->right = right->left;
601 if (node->right) node->right->parent = node;
605 node->parent = right;
610 template <
class KEY,
class VAL>
615 return cursor->value;
618 template <
class KEY,
class VAL>
623 return cursor->value;
626 template <
class KEY,
class VAL>
634 template <
class KEY,
class VAL>
637 if (root == 0)
return false;
645 template <
class KEY,
class VAL>
648 if (root == 0)
return false;
650 while ((*out)->right)
651 *out = (*out)->right;
656 template <
class KEY,
class VAL>
663 if (cursor->right == 0) {
665 while (cursor->parent && cursor->parent->right == cursor)
666 cursor = cursor->parent;
668 cursor = cursor->parent;
676 cursor = cursor->right;
678 cursor = cursor->left;
684 template <
class KEY,
class VAL>
691 if (cursor->left == 0) {
693 while (cursor->parent && cursor->parent->left == cursor)
694 cursor = cursor->parent;
696 cursor = cursor->parent;
705 cursor = cursor->left;
706 while (cursor->right)
707 cursor = cursor->right;
It remove(It first, It last, const T &val)
Definition: algorithm.h:466