1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
|
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2016 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_INTRUSIVE_LIST_INCLUDED
#define ETL_INTRUSIVE_LIST_INCLUDED
#include "platform.h"
#include "nullptr.h"
#include "type_traits.h"
#include "exception.h"
#include "error_handler.h"
#include "intrusive_links.h"
#include "static_assert.h"
#include "algorithm.h"
#include "iterator.h"
#include "functional.h"
#include <stddef.h>
#include "private/minmax_push.h"
namespace etl
{
//***************************************************************************
/// Exception for the intrusive_list.
///\ingroup intrusive_list
//***************************************************************************
class intrusive_list_exception : public exception
{
public:
intrusive_list_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
/// Empty exception for the intrusive_list.
///\ingroup intrusive_list
//***************************************************************************
class intrusive_list_empty : public intrusive_list_exception
{
public:
intrusive_list_empty(string_type file_name_, numeric_type line_number_)
: intrusive_list_exception(ETL_ERROR_TEXT("intrusive_list:empty", ETL_INTRUSIVE_LIST_FILE_ID"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// Iterator exception for the intrusive_list.
///\ingroup intrusive_list
//***************************************************************************
class intrusive_list_iterator_exception : public intrusive_list_exception
{
public:
intrusive_list_iterator_exception(string_type file_name_, numeric_type line_number_)
: intrusive_list_exception(ETL_ERROR_TEXT("intrusive_list:iterator", ETL_INTRUSIVE_LIST_FILE_ID"B"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// Unsorted exception for the intrusive_list.
///\ingroup intrusive_list
//***************************************************************************
class intrusive_list_unsorted : public intrusive_list_exception
{
public:
intrusive_list_unsorted(string_type file_name_, numeric_type line_number_)
: intrusive_list_exception(ETL_ERROR_TEXT("intrusive_list:unsorted", ETL_INTRUSIVE_LIST_FILE_ID"C"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// Already exception for the intrusive_list.
///\ingroup intrusive_list
//***************************************************************************
class intrusive_list_value_is_already_linked : public intrusive_list_exception
{
public:
intrusive_list_value_is_already_linked(string_type file_name_, numeric_type line_number_)
: intrusive_list_exception(ETL_ERROR_TEXT("intrusive_list:value is already linked", ETL_INTRUSIVE_LIST_FILE_ID"E"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// Base for intrusive list.
///\ingroup intrusive_list
//***************************************************************************
template <typename TLink>
class intrusive_list_base
{
public:
// Node typedef.
typedef TLink link_type;
//*************************************************************************
/// Assigns a range of values to the intrusive_list.
/// If ETL_THROW_EXCEPTIONS & ETL_DEBUG are defined emits a
/// intrusive_list_iterator_exception if the iterators are reversed.
//*************************************************************************
template <typename TIterator>
void assign(TIterator first, TIterator last)
{
#if ETL_IS_DEBUG_BUILD
intmax_t d = etl::distance(first, last);
ETL_ASSERT(d >= 0, ETL_ERROR(intrusive_list_iterator_exception));
#endif
initialise();
link_type* p_last_link = &terminal_link;
// Add all of the elements.
while (first != last)
{
link_type& link = *first++;
etl::link_splice<link_type>(p_last_link, link);
p_last_link = &link;
++current_size;
}
}
//*************************************************************************
/// Pushes a value to the front of the intrusive_list.
//*************************************************************************
void push_front(link_type& value)
{
ETL_ASSERT_OR_RETURN(!value.is_linked(), ETL_ERROR(intrusive_list_value_is_already_linked));
insert_link(terminal_link, value);
}
//*************************************************************************
/// Removes a value from the front of the intrusive_list.
//*************************************************************************
void pop_front()
{
#if defined(ETL_CHECK_PUSH_POP)
ETL_ASSERT(!empty(), ETL_ERROR(intrusive_list_empty));
#endif
disconnect_link(get_head());
}
//*************************************************************************
/// Pushes a value to the back of the intrusive_list.
//*************************************************************************
void push_back(link_type& value)
{
ETL_ASSERT_OR_RETURN(!value.is_linked(), ETL_ERROR(intrusive_list_value_is_already_linked));
insert_link(terminal_link.link_type::etl_previous, value);
}
//*************************************************************************
/// Removes a value from the back of the intrusive_list.
//*************************************************************************
void pop_back()
{
#if defined(ETL_CHECK_PUSH_POP)
ETL_ASSERT(!empty(), ETL_ERROR(intrusive_list_empty));
#endif
disconnect_link(get_tail());
}
//*************************************************************************
/// Clears the intrusive_list.
//*************************************************************************
void clear()
{
// Unlink all of the items.
link_type* p_unlink = terminal_link.etl_next;
while (p_unlink != &terminal_link)
{
link_type* p_next = p_unlink->etl_next;
p_unlink->clear();
p_unlink = p_next;
}
initialise();
}
//*************************************************************************
/// Reverses the list.
//*************************************************************************
void reverse()
{
if (is_trivial_list())
{
return;
}
link_type* pnode = terminal_link.etl_next;
while (pnode != &terminal_link)
{
pnode->reverse();
pnode = pnode->etl_previous; // Now we've reversed it, we must go to the previous node.
}
// Terminal node.
pnode->reverse();
}
//*************************************************************************
/// Returns true if the list has no elements.
//*************************************************************************
bool empty() const
{
return (terminal_link.link_type::etl_next == &terminal_link);
}
//*************************************************************************
/// Returns the number of elements.
//*************************************************************************
size_t size() const
{
return current_size;
}
protected:
/// The link that acts as the intrusive_list start & end.
link_type terminal_link;
size_t current_size; ///< Counts the number of elements in the list.
//*************************************************************************
/// Destructor
//*************************************************************************
~intrusive_list_base()
{
}
//*************************************************************************
/// Is the intrusive_list a trivial length?
//*************************************************************************
bool is_trivial_list() const
{
return (terminal_link.link_type::etl_next == &terminal_link) || (terminal_link.link_type::etl_next->etl_next == &terminal_link);
}
//*************************************************************************
/// Insert a link.
//*************************************************************************
void insert_link(link_type& previous, link_type& new_link)
{
// Connect to the intrusive_list.
etl::link_splice<link_type>(previous, new_link);
++current_size;
}
//*************************************************************************
/// Insert a link.
//*************************************************************************
void insert_link(link_type* previous, link_type& new_link)
{
// Connect to the intrusive_list.
etl::link_splice<link_type>(previous, new_link);
++current_size;
}
//*************************************************************************
/// Insert a link.
//*************************************************************************
void insert_link(link_type& previous, link_type* new_link)
{
// Connect to the intrusive_list.
etl::link_splice<link_type>(previous, new_link);
++current_size;
}
//*************************************************************************
/// Insert a link.
//*************************************************************************
void insert_link(link_type* previous, link_type* new_link)
{
// Connect to the intrusive_list.
etl::link_splice<link_type>(previous, new_link);
++current_size;
}
//*************************************************************************
/// Remove a link.
//*************************************************************************
void disconnect_link(link_type& link)
{
etl::unlink<link_type>(link);
--current_size;
}
//*************************************************************************
/// Remove a link.
//*************************************************************************
void disconnect_link(link_type* link)
{
etl::unlink<link_type>(*link);
--current_size;
}
//*************************************************************************
/// Get the head link.
//*************************************************************************
link_type* get_head()
{
return terminal_link.etl_next;
}
//*************************************************************************
/// Get the head link.
//*************************************************************************
const link_type* get_head() const
{
return terminal_link.etl_next;
}
//*************************************************************************
/// Get the tail link.
//*************************************************************************
link_type* get_tail()
{
return terminal_link.etl_previous;
}
//*************************************************************************
/// Get the tail link.
//*************************************************************************
const link_type* get_tail() const
{
return terminal_link.etl_previous;
}
//*************************************************************************
/// Initialise the intrusive_list.
//*************************************************************************
void initialise()
{
etl::link(terminal_link, terminal_link);
current_size = 0;
}
//*************************************************************************
/// Tests if the link is in this list.
//*************************************************************************
bool is_link_in_list(link_type& search_link) const
{
link_type* p_link = terminal_link.link_type::etl_next;
while (p_link != &terminal_link)
{
if (&search_link == p_link)
{
return true;
}
p_link = p_link->link_type::etl_next;
}
return false;
}
//*************************************************************************
/// Remove the specified node from the list.
/// Returns ETL_NULLPTR if the link was not in this list or was the last in the list.
//*************************************************************************
link_type* remove_link(link_type& link)
{
link_type* result = ETL_NULLPTR;
if (is_link_in_list(link))
{
link_type* p_next = link.etl_next;
disconnect_link(link);
if (p_next != &terminal_link)
{
result = p_next;
}
}
return result;
}
//*************************************************************************
/// Removes a range of links.
//*************************************************************************
link_type* remove_link_range(link_type* p_first, link_type* p_last)
{
// Join the ends.
etl::link<link_type>(p_first->etl_previous, p_last);
while (p_first != p_last)
{
link_type* p_next = p_first->etl_next;
p_first->clear();
p_first = p_next;
}
if (p_last == &terminal_link)
{
return ETL_NULLPTR;
}
else
{
return p_last;
}
}
};
//***************************************************************************
/// An intrusive list.
///\ingroup intrusive_list
///\note TLink must be a base of TValue.
//***************************************************************************
template <typename TValue, typename TLink>
class intrusive_list : public etl::intrusive_list_base<TLink>
{
public:
// Node typedef.
typedef typename etl::intrusive_list_base<TLink>::link_type link_type;
typedef intrusive_list<TValue, TLink> list_type;
typedef TValue node_type;
// STL style typedefs.
typedef TValue value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
//*************************************************************************
/// iterator.
//*************************************************************************
class iterator : public etl::iterator<ETL_OR_STD::bidirectional_iterator_tag, value_type>
{
public:
friend class intrusive_list;
friend class const_iterator;
iterator()
: p_value(ETL_NULLPTR)
{
}
iterator(const iterator& other)
: p_value(other.p_value)
{
}
iterator& operator ++()
{
// Read the appropriate 'etl_next'.
p_value = p_value->etl_next;
return *this;
}
iterator operator ++(int)
{
iterator temp(*this);
// Read the appropriate 'etl_next'.
p_value = p_value->etl_next;
return temp;
}
iterator& operator --()
{
// Read the appropriate 'etl_previous'.
p_value = p_value->etl_previous;
return *this;
}
iterator operator --(int)
{
iterator temp(*this);
// Read the appropriate 'etl_previous'.
p_value = p_value->etl_previous;
return temp;
}
iterator& operator =(const iterator& other)
{
p_value = other.p_value;
return *this;
}
reference operator *() const
{
#include "private/diagnostic_null_dereference_push.h"
return *static_cast<pointer>(p_value);
#include "private/diagnostic_pop.h"
}
pointer operator &() const
{
return static_cast<pointer>(p_value);
}
pointer operator ->() const
{
return static_cast<pointer>(p_value);
}
friend bool operator == (const iterator& lhs, const iterator& rhs)
{
return lhs.p_value == rhs.p_value;
}
friend bool operator != (const iterator& lhs, const iterator& rhs)
{
return !(lhs == rhs);
}
private:
iterator(link_type* value)
: p_value(value)
{
}
link_type* p_value;
};
//*************************************************************************
/// const_iterator
//*************************************************************************
class const_iterator : public etl::iterator<ETL_OR_STD::bidirectional_iterator_tag, const value_type>
{
public:
friend class intrusive_list;
const_iterator()
: p_value(ETL_NULLPTR)
{
}
const_iterator(const typename intrusive_list::iterator& other)
: p_value(other.p_value)
{
}
const_iterator(const const_iterator& other)
: p_value(other.p_value)
{
}
const_iterator& operator ++()
{
// Read the appropriate 'etl_next'.
p_value = p_value->etl_next;
return *this;
}
const_iterator operator ++(int)
{
const_iterator temp(*this);
// Read the appropriate 'etl_next'.
p_value = p_value->etl_next;
return temp;
}
const_iterator& operator --()
{
// Read the appropriate 'etl_previous'.
p_value = p_value->etl_previous;
return *this;
}
const_iterator operator --(int)
{
const_iterator temp(*this);
// Read the appropriate 'etl_previous'.
p_value = p_value->etl_previous;
return temp;
}
const_iterator& operator =(const const_iterator& other)
{
p_value = other.p_value;
return *this;
}
const_reference operator *() const
{
return *static_cast<const_pointer>(p_value);
}
const_pointer operator &() const
{
return static_cast<const_pointer>(p_value);
}
const_pointer operator ->() const
{
return static_cast<const_pointer>(p_value);
}
friend bool operator == (const const_iterator& lhs, const const_iterator& rhs)
{
return lhs.p_value == rhs.p_value;
}
friend bool operator != (const const_iterator& lhs, const const_iterator& rhs)
{
return !(lhs == rhs);
}
private:
const_iterator(const link_type* value)
: p_value(value)
{
}
const link_type* p_value;
};
typedef typename etl::iterator_traits<iterator>::difference_type difference_type;
//*************************************************************************
/// Constructor.
//*************************************************************************
intrusive_list()
{
this->initialise();
}
//*************************************************************************
/// Destructor.
//*************************************************************************
~intrusive_list()
{
this->clear();
}
//*************************************************************************
/// Constructor from range
//*************************************************************************
template <typename TIterator>
intrusive_list(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
{
this->assign(first, last);
}
//*************************************************************************
/// Gets the beginning of the intrusive_list.
//*************************************************************************
iterator begin()
{
return iterator(this->get_head());
}
//*************************************************************************
/// Gets the beginning of the intrusive_list.
//*************************************************************************
const_iterator begin() const
{
return const_iterator(this->get_head());
}
//*************************************************************************
/// Gets the beginning of the intrusive_list.
//*************************************************************************
const_iterator cbegin() const
{
return const_iterator(this->get_head());
}
//*************************************************************************
/// Gets the end of the intrusive_list.
//*************************************************************************
iterator end()
{
return iterator(&this->terminal_link);
}
//*************************************************************************
/// Gets the end of the intrusive_list.
//*************************************************************************
const_iterator end() const
{
return const_iterator(&this->terminal_link);
}
//*************************************************************************
/// Gets the end of the intrusive_list.
//*************************************************************************
const_iterator cend() const
{
return const_iterator(&this->terminal_link);
}
//*************************************************************************
/// Gets a reference to the first element.
//*************************************************************************
reference front()
{
return *static_cast<pointer>(this->get_head());
}
//*************************************************************************
/// Gets a const reference to the first element.
//*************************************************************************
const_reference front() const
{
return *static_cast<const_pointer>(this->get_head());
}
//*************************************************************************
/// Gets a reference to the last element.
//*************************************************************************
reference back()
{
return *static_cast<pointer>(this->get_tail());
}
//*************************************************************************
/// Gets a const reference to the last element.
//*************************************************************************
const_reference back() const
{
return *static_cast<const_pointer>(this->get_tail());
}
//*************************************************************************
/// Inserts a value to the intrusive_list before the specified position.
//*************************************************************************
iterator insert(const_iterator position, value_type& value)
{
this->insert_link(position.p_value->link_type::etl_previous, value);
return iterator(&value);
}
//*************************************************************************
/// Inserts a range of values to the intrusive_list after the specified position.
//*************************************************************************
template <typename TIterator>
void insert(const_iterator position, TIterator first, TIterator last)
{
while (first != last)
{
// Set up the next free link.
this->insert_link(*position.p_value->link_type::etl_previous, *first);
++first;
}
}
//*************************************************************************
/// Erases the value at the specified position.
//*************************************************************************
iterator erase(iterator position)
{
iterator next(position);
++next;
this->disconnect_link(*position.p_value);
return next;
}
//*************************************************************************
/// Erases the value at the specified position.
//*************************************************************************
iterator erase(const_iterator position)
{
iterator next(position);
++next;
this->disconnect_link(*position.p_value);
return next;
}
//*************************************************************************
/// Erases a range of elements.
/// Clears the links after erasing if AUTO or CHECKED.
//*************************************************************************
iterator erase(const_iterator first, const_iterator last)
{
const link_type* cp_first = first.p_value;
const link_type* cp_last = last.p_value;
link_type* p_first = const_cast<link_type*>(cp_first);
link_type* p_last = const_cast<link_type*>(cp_last);
this->current_size -= etl::distance(first, last);
p_last = this->remove_link_range(p_first, p_last);
if (p_last == ETL_NULLPTR)
{
return end();
}
else
{
return iterator(static_cast<pointer>(p_last));
}
}
//*************************************************************************
/// Erases the specified node.
//*************************************************************************
node_type* erase(node_type& node)
{
return static_cast<node_type*>(this->remove_link(node));
}
//*************************************************************************
/// Removes all but the one element from every consecutive group of equal
/// elements in the container.
//*************************************************************************
template <typename TIsEqual>
void unique(TIsEqual isEqual)
{
if (this->empty())
{
return;
}
iterator i_item = begin();
++i_item;
iterator i_previous = begin();
while (i_item != end())
{
if (isEqual(*i_previous, *i_item))
{
i_item = erase(i_item);
}
else
{
i_previous = i_item;
++i_item;
}
}
}
//*************************************************************************
/// Sort using in-place merge sort algorithm.
//*************************************************************************
void sort()
{
sort(etl::less<value_type>());
}
//*************************************************************************
/// Stable sort using in-place merge sort algorithm.
/// Copyright 2001 Simon Tatham.
///
/// Permission is hereby granted, free of charge, to any person
/// obtaining a copy of this software and associated documentation
/// files (the "Software"), to deal in the Software without
/// restriction, including without limitation the rights to use,
/// copy, modify, merge, publish, distribute, sublicense, and/or
/// sell copies of the Software, and to permit persons to whom the
/// Software is furnished to do so, subject to the following
/// conditions:
///
/// The above copyright notice and this permission notice shall be
/// included in all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
/// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
/// NONINFRINGEMENT. IN NO EVENT SHALL SIMON TATHAM BE LIABLE FOR
/// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
/// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
/// SOFTWARE.
//*************************************************************************
template <typename TCompare>
void sort(TCompare compare)
{
iterator i_left;
iterator i_right;
iterator i_node;
iterator i_head;
iterator i_tail;
int list_size = 1;
int number_of_merges;
int left_size;
int right_size;
if (this->is_trivial_list())
{
return;
}
while (true)
{
i_left = begin();
i_head = end();
i_tail = end();
number_of_merges = 0; // Count the number of merges we do in this pass.
while (i_left != end())
{
++number_of_merges; // There exists a merge to be done.
i_right = i_left;
left_size = 0;
// Step 'list_size' places along from left
for (int i = 0; i < list_size; ++i)
{
++left_size;
++i_right;
if (i_right == end())
{
break;
}
}
// If right hasn't fallen off end, we have two lists to merge.
right_size = list_size;
// Now we have two lists. Merge them.
while (left_size > 0 || (right_size > 0 && i_right != end()))
{
// Decide whether the next node of merge comes from left or right.
if (left_size == 0)
{
// Left is empty. The node must come from right.
i_node = i_right++;
--right_size;
}
else if (right_size == 0 || i_right == end())
{
// Right is empty. The node must come from left.
i_node = i_left++;
--left_size;
}
else if (!compare(*i_right, *i_left))
{
// First node of left is lower or same. The node must come from left.
i_node = i_left++;
--left_size;
}
else
{
// First node of right is lower. The node must come from right.
i_node = i_right;
++i_right;
--right_size;
}
// Add the next node to the merged head.
if (i_head == end())
{
etl::link<link_type>(i_head.p_value, i_node.p_value);
i_head = i_node;
i_tail = i_node;
}
else
{
etl::link<link_type>(i_tail.p_value, i_node.p_value);
i_tail = i_node;
}
etl::link<link_type>(i_tail.p_value, this->terminal_link);
}
// Now left has stepped `list_size' places along, and right has too.
i_left = i_right;
}
// If we have done only one merge, we're finished.
if (number_of_merges <= 1) // Allow for number_of_merges == 0, the empty head case
{
return;
}
// Otherwise repeat, merging lists twice the size
list_size *= 2;
}
}
//*************************************************************************
// Removes the values specified.
//*************************************************************************
void remove(const_reference value)
{
iterator i_item = begin();
while (i_item != end())
{
if (*i_item == value)
{
i_item = erase(i_item);
}
else
{
++i_item;
}
}
}
//*************************************************************************
/// Removes according to a predicate.
//*************************************************************************
template <typename TPredicate>
void remove_if(TPredicate predicate)
{
iterator i_item = begin();
while (i_item != end())
{
if (predicate(*i_item))
{
i_item = erase(i_item);
}
else
{
++i_item;
}
}
}
//*************************************************************************
/// Splice another list into this one.
//*************************************************************************
void splice(iterator position, list_type& other)
{
// No point splicing to ourself!
if (&other != this)
{
if (!other.empty())
{
link_type& first = *other.get_head();
link_type& last = *other.get_tail();
if (&other != this)
{
this->current_size += other.size();
}
link_type& after = *position.p_value;
link_type& before = *after.etl_previous;
etl::link<link_type>(before, first);
etl::link<link_type>(last, after);
other.initialise();
}
}
}
//*************************************************************************
/// Splice an element from another list into this one.
//*************************************************************************
void splice(iterator position, list_type& other, iterator isource)
{
link_type& before = *position.p_value->link_type::etl_previous;
etl::unlink<link_type>(*isource.p_value);
etl::link_splice<link_type>(before, *isource.p_value);
if (&other != this)
{
++this->current_size;
--other.current_size;
}
}
//*************************************************************************
/// Splice a range of elements from another list into this one.
//*************************************************************************
void splice(iterator position, list_type& other, iterator begin_, iterator end_)
{
if (!other.empty())
{
if (&other != this)
{
size_t n = etl::distance(begin_, end_);
this->current_size += n;
other.current_size -= n;
}
link_type& first = *begin_.p_value;
link_type& last = *end_.p_value->link_type::etl_previous;
// Unlink from the source list.
etl::unlink(first, last);
// Fix our links.
link_type& before = *position.p_value->link_type::etl_previous;
etl::link_splice<link_type>(before, first, last);
}
}
//*************************************************************************
/// Merge another list into this one. Both lists should be sorted.
//*************************************************************************
void merge(list_type& other)
{
merge(other, etl::less<value_type>());
}
//*************************************************************************
/// Merge another list into this one. Both lists should be sorted.
//*************************************************************************
template <typename TCompare>
void merge(list_type& other, TCompare compare)
{
if ((this != &other) && !other.empty())
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(etl::is_sorted(other.begin(), other.end(), compare), ETL_ERROR(intrusive_list_unsorted));
ETL_ASSERT(etl::is_sorted(begin(), end(), compare), ETL_ERROR(intrusive_list_unsorted));
#endif
link_type* other_begin = other.get_head();
link_type* other_end = &other.terminal_link;
link_type* this_begin = this->get_head();
link_type* this_end = &this->terminal_link;
while ((this_begin != this_end) && (other_begin != other_end))
{
// Find the place to insert.
while ((this_begin != this_end) && !(compare(*static_cast<pointer>(other_begin), *static_cast<pointer>(this_begin))))
{
this_begin = this_begin->etl_next;
}
// Insert.
if (this_begin != this_end)
{
while ((other_begin != other_end) && (compare(*static_cast<pointer>(other_begin), *static_cast<pointer>(this_begin))))
{
link_type* value = other_begin;
other_begin = other_begin->etl_next;
etl::link_splice<link_type>(*this_begin->etl_previous, *value);
}
}
}
// Any left over?
if ((this_begin == this_end) && (other_begin != other_end))
{
etl::link_splice<link_type>(*this->get_tail(), *other_begin, *other_end->etl_previous);
}
this->current_size += other.size();
other.initialise();
}
}
private:
// Disabled.
intrusive_list(const intrusive_list& other);
intrusive_list& operator = (const intrusive_list& rhs);
};
}
#include "private/minmax_pop.h"
#endif
|