gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/container/tree-map.go (about) 1 package container 2 3 import "gitee.com/sy_183/go-common/generic" 4 5 const ( 6 treeMapColorNone = iota 7 treeMapColorRed 8 treeMapColorBlack 9 ) 10 11 type TreeMapEntry[K, V any] struct { 12 key K 13 value V 14 left *TreeMapEntry[K, V] 15 right *TreeMapEntry[K, V] 16 parent *TreeMapEntry[K, V] 17 color int 18 } 19 20 func NewTreeMapEntry[K, V any](key K, value V) *TreeMapEntry[K, V] { 21 return &TreeMapEntry[K, V]{key: key, value: value} 22 } 23 24 func (e *TreeMapEntry[K, V]) Key() K { 25 return e.key 26 } 27 28 func (e *TreeMapEntry[K, V]) Value() V { 29 return e.value 30 } 31 32 func (e *TreeMapEntry[K, V]) Set(v V) { 33 e.value = v 34 } 35 36 func (e *TreeMapEntry[K, V]) next() *TreeMapEntry[K, V] { 37 if e.right != nil { 38 p := e.right 39 for p.left != nil { 40 p = p.left 41 } 42 return p 43 } else { 44 p := e.parent 45 ch := e 46 for p != nil && ch == p.right { 47 ch = p 48 p = p.parent 49 } 50 return p 51 } 52 } 53 54 func (e *TreeMapEntry[K, V]) prev() *TreeMapEntry[K, V] { 55 if e.left != nil { 56 p := e.left 57 for p.right != nil { 58 p = p.right 59 } 60 return p 61 } else { 62 p := e.parent 63 ch := e 64 for p != nil && ch == p.left { 65 ch = p 66 p = p.parent 67 } 68 return p 69 } 70 } 71 72 func (e *TreeMapEntry[K, V]) Next() *TreeMapEntry[K, V] { 73 if e.Removed() { 74 return nil 75 } 76 return e.next() 77 } 78 79 func (e *TreeMapEntry[K, V]) Prev() *TreeMapEntry[K, V] { 80 if e.Removed() { 81 return nil 82 } 83 return e.prev() 84 } 85 86 func (e *TreeMapEntry[K, V]) Removed() bool { 87 return e.color == treeMapColorNone 88 } 89 90 type Comparator[V any] func(o1, o2 V) int 91 92 type TreeMap[K, V any] struct { 93 entries []*TreeMapEntry[K, V] 94 root *TreeMapEntry[K, V] 95 head *TreeMapEntry[K, V] 96 tail *TreeMapEntry[K, V] 97 cpr Comparator[K] 98 } 99 100 func NewTreeMap[K, V any](cpr Comparator[K], entries int) *TreeMap[K, V] { 101 m := &TreeMap[K, V]{ 102 entries: make([]*TreeMapEntry[K, V], entries), 103 cpr: cpr, 104 } 105 m.init() 106 return m 107 } 108 109 func NewOrderedKeyTreeMap[K generic.Ordered, V any](entries int) *TreeMap[K, V] { 110 return NewTreeMap[K, V](OrderedCompare[K], entries) 111 } 112 113 func NewComparableKeyTreeMap[K Comparable, V any](entries int) *TreeMap[K, V] { 114 return NewTreeMap[K, V](nil, entries) 115 } 116 117 func (m *TreeMap[K, V]) init() { 118 rawEntries := make([]TreeMapEntry[K, V], len(m.entries)) 119 for i := range rawEntries { 120 m.entries[i] = &rawEntries[i] 121 } 122 m.entries = m.entries[:0] 123 } 124 125 func (m *TreeMap[K, V]) Init(cpr Comparator[K], entries int) { 126 *m = TreeMap[K, V]{ 127 entries: make([]*TreeMapEntry[K, V], entries), 128 cpr: cpr, 129 } 130 m.init() 131 } 132 133 func (m *TreeMap[K, V]) IsNil() bool { 134 return m.cpr == nil 135 } 136 137 func (m *TreeMap[K, V]) Len() int { 138 return len(m.entries) 139 } 140 141 func (m *TreeMap[K, V]) Empty() bool { 142 return len(m.entries) == 0 143 } 144 145 func treeMapGetEntry[K any, V any](root *TreeMapEntry[K, V], key K, cpr Comparator[K]) *TreeMapEntry[K, V] { 146 t := root 147 if t == nil { 148 return nil 149 } 150 if root == nil { 151 return root 152 } 153 if cpr != nil { 154 for t != nil { 155 cmp := cpr(key, t.key) 156 if cmp < 0 { 157 t = t.left 158 } else if cmp > 0 { 159 t = t.right 160 } else { 161 return t 162 } 163 } 164 } else { 165 com := (any(key)).(Comparable) 166 for t != nil { 167 cmp := com.Compare((any(t.key)).(Comparable)) 168 if cmp < 0 { 169 t = t.left 170 } else if cmp > 0 { 171 t = t.right 172 } else { 173 return t 174 } 175 } 176 } 177 return nil 178 } 179 180 func (m *TreeMap[K, V]) getEntry(key K) *TreeMapEntry[K, V] { 181 t := m.root 182 if t == nil { 183 return nil 184 } 185 if m.cpr != nil { 186 for t != nil { 187 cmp := m.cpr(key, t.key) 188 if cmp < 0 { 189 t = t.left 190 } else if cmp > 0 { 191 t = t.right 192 } else { 193 return t 194 } 195 } 196 } else { 197 com := (any(key)).(Comparable) 198 for t != nil { 199 cmp := com.Compare((any(t.key)).(Comparable)) 200 if cmp < 0 { 201 t = t.left 202 } else if cmp > 0 { 203 t = t.right 204 } else { 205 return t 206 } 207 } 208 } 209 return nil 210 } 211 212 func (m *TreeMap[K, V]) Has(key K) bool { 213 return m.getEntry(key) != nil 214 } 215 216 func (m *TreeMap[K, V]) Get(key K) (value V) { 217 if e := m.getEntry(key); e != nil { 218 return e.value 219 } 220 return 221 } 222 223 func (m *TreeMap[K, V]) GetEntry(key K) *TreeMapEntry[K, V] { 224 return m.getEntry(key) 225 } 226 227 func (m *TreeMap[K, V]) Load(key K) (value V, exist bool) { 228 if e := m.getEntry(key); e != nil { 229 return e.value, true 230 } 231 return 232 } 233 234 func (m *TreeMap[K, V]) Head() (value V, exist bool) { 235 if m.head == nil { 236 return value, false 237 } 238 return m.head.value, true 239 } 240 241 func (m *TreeMap[K, V]) HeadEntry() *TreeMapEntry[K, V] { 242 return m.head 243 } 244 245 func (m *TreeMap[K, V]) Tail() (value V, exist bool) { 246 if m.tail == nil { 247 return value, false 248 } 249 return m.tail.value, true 250 } 251 252 func (m *TreeMap[K, V]) TailEntry() *TreeMapEntry[K, V] { 253 return m.tail 254 } 255 256 func (m *TreeMap[K, V]) Range(f func(key K, value V) bool) bool { 257 var next *TreeMapEntry[K, V] 258 for cur := m.head; cur != nil; cur = next { 259 next = cur.next() 260 if !f(cur.key, cur.value) { 261 return false 262 } 263 } 264 return true 265 } 266 267 func (m *TreeMap[K, V]) RangeEntries(f func(e *TreeMapEntry[K, V]) bool) bool { 268 var next *TreeMapEntry[K, V] 269 for cur := m.head; cur != nil; cur = next { 270 next = cur.next() 271 if !f(cur) { 272 return false 273 } 274 } 275 return true 276 } 277 278 func (m *TreeMap[K, V]) Keys() (keys []K) { 279 var next *TreeMapEntry[K, V] 280 for cur := m.head; cur != nil; cur = next { 281 next = cur.next() 282 keys = append(keys, cur.key) 283 } 284 return 285 } 286 287 func (m *TreeMap[K, V]) Values() (values []V) { 288 var next *TreeMapEntry[K, V] 289 for cur := m.head; cur != nil; cur = next { 290 next = cur.next() 291 values = append(values, cur.value) 292 } 293 return 294 } 295 296 func (m *TreeMap[K, V]) grow() int { 297 l := len(m.entries) 298 if l == cap(m.entries) { 299 m.entries = append(m.entries, nil) 300 m.entries = m.entries[:cap(m.entries)] 301 rawEntries := make([]TreeMapEntry[K, V], cap(m.entries)-l) 302 for i := range rawEntries { 303 m.entries[i+l] = &rawEntries[i] 304 } 305 m.entries = m.entries[:l] 306 } 307 return l 308 } 309 310 func (m *TreeMap[K, V]) newEntry(key K, value V, color int) (e *TreeMapEntry[K, V]) { 311 l := m.grow() 312 m.entries = m.entries[:l+1] 313 e = m.entries[l] 314 e.key = key 315 e.value = value 316 e.color = color 317 return 318 } 319 320 func (m *TreeMap[K, V]) freeEntry(e *TreeMapEntry[K, V]) { 321 l := len(m.entries) 322 m.entries[l-1] = e 323 m.entries = m.entries[:l-1] 324 } 325 326 func (*TreeMap[K, V]) colorOf(p *TreeMapEntry[K, V]) int { 327 if p == nil { 328 return treeMapColorBlack 329 } 330 return p.color 331 } 332 333 func (*TreeMap[K, V]) parentOf(p *TreeMapEntry[K, V]) *TreeMapEntry[K, V] { 334 if p == nil { 335 return nil 336 } 337 return p.parent 338 } 339 340 func (*TreeMap[K, V]) setColor(p *TreeMapEntry[K, V], color int) { 341 if p != nil { 342 p.color = color 343 } 344 } 345 346 func (*TreeMap[K, V]) leftOf(p *TreeMapEntry[K, V]) *TreeMapEntry[K, V] { 347 if p == nil { 348 return nil 349 } 350 return p.left 351 } 352 353 func (*TreeMap[K, V]) rightOf(p *TreeMapEntry[K, V]) *TreeMapEntry[K, V] { 354 if p == nil { 355 return nil 356 } 357 return p.right 358 } 359 360 func (m *TreeMap[K, V]) rotateLeft(p *TreeMapEntry[K, V]) { 361 if p != nil { 362 r := p.right 363 p.right = r.left 364 if r.left != nil { 365 r.left.parent = p 366 } 367 r.parent = p.parent 368 if p.parent == nil { 369 m.root = r 370 } else if p.parent.left == p { 371 p.parent.left = r 372 } else { 373 p.parent.right = r 374 } 375 r.left = p 376 p.parent = r 377 } 378 } 379 380 func (m *TreeMap[K, V]) rotateRight(p *TreeMapEntry[K, V]) { 381 if p != nil { 382 l := p.left 383 p.left = l.right 384 if l.right != nil { 385 l.right.parent = p 386 } 387 l.parent = p.parent 388 if p.parent == nil { 389 m.root = l 390 } else if p.parent.right == p { 391 p.parent.right = l 392 } else { 393 p.parent.left = l 394 } 395 l.right = p 396 p.parent = l 397 } 398 } 399 400 func (m *TreeMap[K, V]) redirectRelation(p, l, r, o, n *TreeMapEntry[K, V]) { 401 if p != nil { 402 if p.left == o { 403 p.left = n 404 } else if p.right == o { 405 p.right = n 406 } else { 407 panic("parent not parent of old") 408 } 409 } 410 if l != nil { 411 l.parent = n 412 } 413 if r != nil { 414 r.parent = n 415 } 416 } 417 418 func (m *TreeMap[K, V]) replaceRelation(old, e *TreeMapEntry[K, V]) { 419 e.parent = old.parent 420 e.left, e.right = old.left, old.right 421 e.color = old.color 422 m.redirectRelation(old.parent, old.left, old.right, old, e) 423 if m.root == old { 424 m.root = e 425 } 426 if m.head == old { 427 m.head = e 428 } 429 if m.tail == old { 430 m.tail = e 431 } 432 old.parent = nil 433 old.left, old.right = nil, nil 434 old.color = treeMapColorNone 435 } 436 437 func (m *TreeMap[K, V]) swapRelation(e1, e2 *TreeMapEntry[K, V]) { 438 p1, l1, r1 := e1.parent, e1.left, e1.right 439 p2, l2, r2 := e2.parent, e2.left, e2.right 440 m.redirectRelation(p1, l1, r1, e1, e2) 441 m.redirectRelation(p2, l2, r2, e2, e1) 442 e1.parent, e2.parent = e2.parent, e1.parent 443 e1.left, e2.left = e2.left, e1.left 444 e1.right, e2.right = e2.right, e1.right 445 e1.color, e2.color = e2.color, e1.color 446 if m.root == e1 { 447 m.root = e2 448 } else if m.root == e2 { 449 m.root = e1 450 } 451 if m.head == e1 { 452 m.head = e2 453 } else if m.head == e2 { 454 m.head = e1 455 } 456 if m.tail == e1 { 457 m.tail = e2 458 } else if m.tail == e2 { 459 m.tail = e1 460 } 461 } 462 463 func (m *TreeMap[K, V]) fixAfterInsertion(x *TreeMapEntry[K, V]) { 464 x.color = treeMapColorRed 465 466 for x != nil && x != m.root && x.parent.color == treeMapColorRed { 467 if m.parentOf(x) == m.leftOf(m.parentOf(m.parentOf(x))) { 468 y := m.rightOf(m.parentOf(m.parentOf(x))) 469 if m.colorOf(y) == treeMapColorRed { 470 m.setColor(m.parentOf(x), treeMapColorBlack) 471 m.setColor(y, treeMapColorBlack) 472 m.setColor(m.parentOf(m.parentOf(x)), treeMapColorRed) 473 x = m.parentOf(m.parentOf(x)) 474 } else { 475 if x == m.rightOf(m.parentOf(x)) { 476 x = m.parentOf(x) 477 m.rotateLeft(x) 478 } 479 m.setColor(m.parentOf(x), treeMapColorBlack) 480 m.setColor(m.parentOf(m.parentOf(x)), treeMapColorRed) 481 m.rotateRight(m.parentOf(m.parentOf(x))) 482 } 483 } else { 484 y := m.leftOf(m.parentOf(m.parentOf(x))) 485 if m.colorOf(y) == treeMapColorRed { 486 m.setColor(m.parentOf(x), treeMapColorBlack) 487 m.setColor(y, treeMapColorBlack) 488 m.setColor(m.parentOf(m.parentOf(x)), treeMapColorRed) 489 x = m.parentOf(m.parentOf(x)) 490 } else { 491 if x == m.leftOf(m.parentOf(x)) { 492 x = m.parentOf(x) 493 m.rotateRight(x) 494 } 495 m.setColor(m.parentOf(x), treeMapColorBlack) 496 m.setColor(m.parentOf(m.parentOf(x)), treeMapColorRed) 497 m.rotateLeft(m.parentOf(m.parentOf(x))) 498 } 499 } 500 } 501 m.root.color = treeMapColorBlack 502 } 503 504 func (m *TreeMap[K, V]) addEntryToEmpty(e *TreeMapEntry[K, V]) { 505 m.root = e 506 m.head, m.tail = e, e 507 } 508 509 func (m *TreeMap[K, V]) addEntryToParent(e, p *TreeMapEntry[K, V], addToLeft bool) { 510 e.parent = p 511 if addToLeft { 512 p.left = e 513 if p == m.head { 514 m.head = e 515 } 516 } else { 517 p.right = e 518 if p == m.tail { 519 m.tail = e 520 } 521 } 522 m.fixAfterInsertion(e) 523 } 524 525 const ( 526 treeMapStore = iota 527 treeMapLoadOrStore 528 treeMapReplace 529 treeMapPut 530 ) 531 532 func (m *TreeMap[K, V]) add(key K, value V, mode int) (old V, exist bool) { 533 t := m.root 534 if t == nil { 535 m.addEntryToEmpty(m.newEntry(key, value, treeMapColorBlack)) 536 return value, false 537 } 538 var parent *TreeMapEntry[K, V] 539 var cmp int 540 if m.cpr != nil { 541 for t != nil { 542 parent = t 543 cmp = m.cpr(key, t.key) 544 if cmp < 0 { 545 t = t.left 546 } else if cmp > 0 { 547 t = t.right 548 } else { 549 switch mode { 550 case treeMapPut: 551 m.replaceRelation(t, m.newEntry(key, value, treeMapColorBlack)) 552 return value, false 553 case treeMapReplace, treeMapStore: 554 old = t.value 555 t.value = value 556 return old, true 557 case treeMapLoadOrStore: 558 return t.value, true 559 } 560 } 561 } 562 } else { 563 com := (any(key)).(Comparable) 564 for t != nil { 565 parent = t 566 cmp = com.Compare((any(t.key)).(Comparable)) 567 if cmp < 0 { 568 t = t.left 569 } else if cmp > 0 { 570 t = t.right 571 } else { 572 switch mode { 573 case treeMapPut: 574 m.replaceRelation(t, m.newEntry(key, value, treeMapColorBlack)) 575 return value, false 576 case treeMapReplace, treeMapStore: 577 old = t.value 578 t.value = value 579 return old, true 580 case treeMapLoadOrStore: 581 return t.value, true 582 } 583 } 584 } 585 } 586 if mode != treeMapReplace { 587 m.addEntryToParent(m.newEntry(key, value, treeMapColorBlack), parent, cmp < 0) 588 return value, false 589 } 590 return 591 } 592 593 func (m *TreeMap[K, V]) Store(key K, value V) { 594 m.add(key, value, treeMapStore) 595 } 596 597 func (m *TreeMap[K, V]) Set(key K, value V) (old V, exist bool) { 598 return m.add(key, value, treeMapStore) 599 } 600 601 func (m *TreeMap[K, V]) LoadOrStore(key K, value V) (old V, exist bool) { 602 return m.add(key, value, treeMapLoadOrStore) 603 } 604 605 func (m *TreeMap[K, V]) Replace(ket K, value V) bool { 606 _, exist := m.add(ket, value, treeMapReplace) 607 return !exist 608 } 609 610 func (m *TreeMap[K, V]) Put(key K, value V) (old V, exist bool) { 611 return m.add(key, value, treeMapPut) 612 } 613 614 func (m *TreeMap[K, V]) fixAfterDeletion(x *TreeMapEntry[K, V]) { 615 for x != m.root && m.colorOf(x) == treeMapColorBlack { 616 if x == m.leftOf(m.parentOf(x)) { 617 sib := m.rightOf(m.parentOf(x)) 618 619 if m.colorOf(sib) == treeMapColorRed { 620 m.setColor(sib, treeMapColorBlack) 621 m.setColor(m.parentOf(x), treeMapColorRed) 622 m.rotateLeft(m.parentOf(x)) 623 sib = m.rightOf(m.parentOf(x)) 624 } 625 626 if m.colorOf(m.leftOf(sib)) == treeMapColorBlack && 627 m.colorOf(m.rightOf(sib)) == treeMapColorBlack { 628 m.setColor(sib, treeMapColorRed) 629 x = m.parentOf(x) 630 } else { 631 if m.colorOf(m.rightOf(sib)) == treeMapColorBlack { 632 m.setColor(m.leftOf(sib), treeMapColorBlack) 633 m.setColor(sib, treeMapColorRed) 634 m.rotateRight(sib) 635 sib = m.rightOf(m.parentOf(x)) 636 } 637 m.setColor(sib, m.colorOf(m.parentOf(x))) 638 m.setColor(m.parentOf(x), treeMapColorBlack) 639 m.setColor(m.rightOf(sib), treeMapColorBlack) 640 m.rotateLeft(m.parentOf(x)) 641 x = m.root 642 } 643 } else { // symmetric 644 sib := m.leftOf(m.parentOf(x)) 645 646 if m.colorOf(sib) == treeMapColorRed { 647 m.setColor(sib, treeMapColorBlack) 648 m.setColor(m.parentOf(x), treeMapColorRed) 649 m.rotateRight(m.parentOf(x)) 650 sib = m.leftOf(m.parentOf(x)) 651 } 652 653 if m.colorOf(m.rightOf(sib)) == treeMapColorBlack && 654 m.colorOf(m.leftOf(sib)) == treeMapColorBlack { 655 m.setColor(sib, treeMapColorRed) 656 x = m.parentOf(x) 657 } else { 658 if m.colorOf(m.leftOf(sib)) == treeMapColorBlack { 659 m.setColor(m.rightOf(sib), treeMapColorBlack) 660 m.setColor(sib, treeMapColorRed) 661 m.rotateLeft(sib) 662 sib = m.leftOf(m.parentOf(x)) 663 } 664 m.setColor(sib, m.colorOf(m.parentOf(x))) 665 m.setColor(m.parentOf(x), treeMapColorBlack) 666 m.setColor(m.leftOf(sib), treeMapColorBlack) 667 m.rotateRight(m.parentOf(x)) 668 x = m.root 669 } 670 } 671 } 672 673 m.setColor(x, treeMapColorBlack) 674 } 675 676 func (m *TreeMap[K, V]) deleteEntry(p *TreeMapEntry[K, V]) { 677 // If strictly internal, copy successor's element to p and then make p 678 // point to successor. 679 if p.left != nil && p.right != nil { 680 m.swapRelation(p, p.next()) 681 } // p has 2 children 682 683 if p == m.head { 684 m.head = p.next() 685 } 686 if p == m.tail { 687 m.tail = p.prev() 688 } 689 690 // Start fixup at replacement node, if it exists. 691 var replacement *TreeMapEntry[K, V] 692 if p.left != nil { 693 replacement = p.left 694 } else { 695 replacement = p.right 696 } 697 698 if replacement != nil { 699 // Link replacement to parent 700 replacement.parent = p.parent 701 if p.parent == nil { 702 m.root = replacement 703 } else if p == p.parent.left { 704 p.parent.left = replacement 705 } else { 706 p.parent.right = replacement 707 } 708 709 // Null out links so they are OK to use by fixAfterDeletion. 710 p.left, p.right, p.parent = nil, nil, nil 711 712 // Fix replacement 713 if p.color == treeMapColorBlack { 714 m.fixAfterDeletion(replacement) 715 } 716 } else if p.parent == nil { // return if we are the only node. 717 m.root = nil 718 } else { // No children. Use self as phantom replacement and unlink. 719 if p.color == treeMapColorBlack { 720 m.fixAfterDeletion(p) 721 } 722 723 if p.parent != nil { 724 if p == p.parent.left { 725 p.parent.left = nil 726 } else if p == p.parent.right { 727 p.parent.right = nil 728 } 729 p.parent = nil 730 } 731 } 732 733 p.color = treeMapColorNone 734 m.freeEntry(p) 735 } 736 737 func (m *TreeMap[K, V]) Remove(key K) (old V, exist bool) { 738 if e := m.getEntry(key); e != nil { 739 old, exist = e.value, true 740 m.deleteEntry(e) 741 } 742 return 743 } 744 745 func (m *TreeMap[K, V]) RemoveEntry(e *TreeMapEntry[K, V]) bool { 746 if e.Removed() { 747 return false 748 } 749 m.deleteEntry(e) 750 return true 751 } 752 753 func (m *TreeMap[K, V]) Delete(key K) { 754 if e := m.getEntry(key); e != nil { 755 m.deleteEntry(e) 756 } 757 return 758 } 759 760 func (m *TreeMap[K, V]) Shift() (v V, exist bool) { 761 if m.head != nil { 762 v, exist = m.head.value, true 763 m.deleteEntry(m.head) 764 } 765 return 766 } 767 768 func (m *TreeMap[K, V]) ShiftKey() (k K, exist bool) { 769 if m.head != nil { 770 k, exist = m.head.key, true 771 m.deleteEntry(m.head) 772 } 773 return 774 } 775 776 func (m *TreeMap[K, V]) Pop() (v V, exist bool) { 777 if m.tail != nil { 778 v, exist = m.tail.value, true 779 m.deleteEntry(m.tail) 780 } 781 return 782 } 783 784 func (m *TreeMap[K, V]) PopKey() (k K, exist bool) { 785 if m.tail != nil { 786 k, exist = m.tail.key, true 787 m.deleteEntry(m.tail) 788 } 789 return 790 } 791 792 func (m *TreeMap[K, V]) Clear() { 793 for _, entry := range m.entries { 794 entry.color = treeMapColorNone 795 entry.parent, entry.left, entry.right = nil, nil, nil 796 } 797 m.entries = m.entries[:0] 798 m.root, m.head, m.tail = nil, nil, nil 799 }