Changeset 4077 for branches/kendall-score/yat/utility/Ranking.h
- Timestamp:
- Aug 26, 2021, 6:07:09 AM (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/kendall-score/yat/utility/Ranking.h
r4074 r4077 32 32 #include <cstddef> 33 33 #include <functional> 34 #include <iostream> 35 #include <iterator> 34 36 #include <memory> 35 37 36 #include <iostream> // debug37 38 namespace theplu { 38 39 namespace yat { … … 109 110 110 111 /** 112 \return iterator pointing to the first element, i.e., the 113 node with the smallest value. 114 */ 115 iterator begin(void) 116 { 117 return iterator(impl_.left_most()); 118 } 119 120 121 /** 111 122 \return iterator pointing to the first element 112 123 */ 113 iterator begin(void)114 { 115 return iterator(impl_.left_most());124 const_iterator begin(void) const 125 { 126 return cbegin(); 116 127 } 117 128 … … 120 131 \return iterator pointing to the first element 121 132 */ 122 const_iterator begin(void) const123 {124 return cbegin();125 }126 127 128 /**129 \return iterator pointing to the first element130 */131 133 const_iterator cbegin(void) const 132 134 { … … 163 165 164 166 /** 165 \return reverse iterator pointing to first element 167 \return reverse iterator pointing to first element, i.e., the 168 node with the largest value. 166 169 */ 167 170 reverse_iterator rbegin(void) … … 247 250 248 251 /** 249 Remove elementpointed to by \c position.252 Remove node pointed to by \c position. 250 253 251 254 \return An iterator pointing to the element that follows the … … 298 301 /** 299 302 \brief insert with hint 303 304 If \c hint will follow the inserted element, the insertion is done 305 in constant time. Otherwise, the usual insert function is 306 called which takes logN. 300 307 */ 301 308 iterator insert(const_iterator hint, const T& element) 302 309 { 303 // FIXME use the hint 304 return insert(element); 310 std::unique_ptr<ranking::NodeValue<T>> 311 newnode(new ranking::NodeValue<T>(element)); 312 return insert(hint, std::move(newnode)); 305 313 } 306 314 … … 311 319 iterator insert(const_iterator hint, T&& element) 312 320 { 313 // FIXME use the hint 314 return insert(std::move(element)); 321 std::unique_ptr<ranking::NodeValue<T>> 322 newnode(new ranking::NodeValue<T>(std::move(element))); 323 return insert(hint, std::move(newnode)); 315 324 } 316 325 … … 370 379 Compare compare_; 371 380 ranking::Impl impl_; 381 382 iterator 383 insert_and_rebalance(std::unique_ptr<ranking::NodeValue<T>>&& element, 384 ranking::NodeBase& parent, bool left) 385 { 386 iterator result(element.get()); 387 impl_.insert_and_rebalance(std::move(element), parent, left); 388 YAT_ASSERT(validate()); 389 return result; 390 } 391 392 372 393 373 394 void print(const ranking::NodeBase* p, std::ostream& os) const … … 481 502 { 482 503 while (x) { 483 if (x->right_ && x->right_ != &impl_.head_)504 if (x->right_) 484 505 erase(right(x)); 485 506 ranking::NodeValue<T>* tmp = left(x); … … 496 517 iterator insert(std::unique_ptr<ranking::NodeValue<T>>&& element) 497 518 { 498 iterator result(element.get());499 519 500 520 if (empty()) { … … 511 531 512 532 YAT_ASSERT(validate()); 513 return result; 514 } 515 516 ranking::NodeBase* x = impl_.root_node(); 533 return iterator(root_node()); 534 } 535 536 return insert(impl_.root_node(), std::move(element)); 537 } 538 539 540 // Insert \a element into the subtree in which x is the root. 541 iterator insert(ranking::NodeBase* x, 542 std::unique_ptr<ranking::NodeValue<T>>&& element) 543 { 517 544 YAT_ASSERT(x); 518 545 YAT_ASSERT(!x->is_head_node()); … … 522 549 if (compare_(element->value(), value(x))) { 523 550 x = x->left_; 524 if (x == nullptr) { 525 impl_.insert_and_rebalance(std::move(element), *parent, 526 parent->left_); 527 if (impl_.left_most() == parent) 528 impl_.left_most() = parent->left_; 529 YAT_ASSERT(validate()); 530 return result; 531 } 551 if (x == nullptr) 552 return insert_and_rebalance(std::move(element), *parent, true); 532 553 } 533 554 else { 534 555 x = x->right_; 535 if (x == nullptr) { 536 impl_.insert_and_rebalance(std::move(element), *parent, 537 parent->right_); 538 YAT_ASSERT(validate()); 539 return result; 540 } 556 if (x == nullptr) 557 return insert_and_rebalance(std::move(element), *parent, false); 541 558 } 542 559 } 543 560 YAT_ASSERT(0 && "we can't reach here"); 544 return result; 561 return iterator(element.get()); 562 } 563 564 565 iterator insert(const_iterator hint, 566 std::unique_ptr<ranking::NodeValue<T>>&& element) 567 { 568 // special case when hint == end() 569 if (hint.node_ == head_node()) { 570 YAT_ASSERT(hint == cend()); 571 if (empty() || compare_(element->value(), value(impl_.right_most()))) 572 return insert(std::move(element)); 573 return insert_and_rebalance(std::move(element), *impl_.right_most(), 574 false); 575 } 576 577 YAT_ASSERT(hint != end()); 578 579 // value <= hint i.e. !(hint<value) 580 if (!compare_(*hint, element->value())) { 581 // if hint == begin 582 if (hint.node_ == impl_.left_most()) { 583 YAT_ASSERT(hint == cbegin()); 584 return insert_and_rebalance(std::move(element), 585 *impl_.left_most(), 586 true); 587 } 588 589 // prev <= value <= hint insert 590 YAT_ASSERT(hint != cbegin()); 591 auto before = std::prev(hint); 592 if (!compare_(element->value(), *before)) { 593 return insert(const_cast<ranking::NodeBase*>(hint.node_), 594 std::move(element)); 595 } 596 else { 597 return insert(std::move(element)); 598 } 599 } 600 // else value > hint 601 else { 602 YAT_ASSERT(compare_(*hint, element->value())); 603 // hint == rightmost 604 if (hint.node_ == impl_.right_most()) { 605 return insert_and_rebalance(std::move(element), 606 *impl_.right_most(), 607 false); 608 } 609 auto after = std::next(hint); 610 YAT_ASSERT(after != cend()); 611 // hint < value <= after 612 if (!compare_(*after, element->value())) { 613 return insert(const_cast<ranking::NodeBase*>(after.node_), 614 std::move(element)); 615 } 616 } 617 618 return insert(std::move(element)); 545 619 } 546 620
Note: See TracChangeset
for help on using the changeset viewer.