diff --git a/.clang-tidy b/.clang-tidy index 0acc936cb51..cd677fbb128 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -140,7 +140,6 @@ Checks: [ WarningsAsErrors: '*' HeaderFilterRegex: '(src|test|tools).*' -ExcludeHeaderFilterRegex: '.*src/(colony|list)\.h$' FormatStyle: none CheckOptions: - key: cata-large-inline-function.MaxStatements diff --git a/src/colony.h b/src/colony.h index b311a2e6992..7ab453c1e37 100644 --- a/src/colony.h +++ b/src/colony.h @@ -1,3526 +1,15 @@ -// This software is a modified version of the original. -// The original license is as follows: - -// Copyright (c) 2019, Matthew Bentley (mattreecebentley@gmail.com) www.plflib.org - -// zLib license (https://www.zlib.net/zlib_license.html): -// This software is provided 'as-is', without any express or implied -// warranty. In no event will the authors be held liable for any damages -// arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it -// freely, subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; you must not -// claim that you wrote the original software. If you use this software -// in a product, an acknowledgement in the product documentation would be -// appreciated but is not required. -// 2. Altered source versions must be plainly marked as such, and must not be -// misrepresented as being the original software. -// 3. This notice may not be removed or altered from any source distribution. - #pragma once #ifndef CATA_SRC_COLONY_H #define CATA_SRC_COLONY_H -// Compiler-specific defines used by colony: -#define COLONY_CONSTEXPR -#define COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept -#define COLONY_NOEXCEPT_SWAP(the_allocator) noexcept +#include -// TODO: Switch to these when we move to C++17 -// #define COLONY_CONSTEXPR constexpr -// #define COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) -// #define COLONY_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits::propagate_on_container_swap::value) - -// Note: GCC creates faster code without forcing inline -#if defined(_MSC_VER) -#define COLONY_FORCE_INLINE __forceinline -#else -#define COLONY_FORCE_INLINE -#endif - -/* whole GCC 6 family */ -#if __GNUC__ == 6 -/* GCC 6.5 at least complains about type punning, but nothing else does. */ -#pragma GCC diagnostic ignored "-Wstrict-aliasing" -#endif - -// TODO: get rid of these defines -#define COLONY_CONSTRUCT(the_allocator, allocator_instance, location, ...) std::allocator_traits::construct(allocator_instance, location, __VA_ARGS__) -#define COLONY_DESTROY(the_allocator, allocator_instance, location) std::allocator_traits::destroy(allocator_instance, location) -#define COLONY_ALLOCATE(the_allocator, allocator_instance, size, hint) std::allocator_traits::allocate(allocator_instance, size, hint) -#define COLONY_ALLOCATE_INITIALIZATION(the_allocator, size, hint) std::allocator_traits::allocate(*this, size, hint) -#define COLONY_DEALLOCATE(the_allocator, allocator_instance, location, size) std::allocator_traits::deallocate(allocator_instance, location, size) - -#include // std::sort and std::fill_n -#include // offsetof, used in blank() -#include // memset, memcpy -#include -#include // std::bidirectional_iterator_tag -#include // std::numeric_limits -#include // std::allocator -#include // std::is_trivially_destructible, etc -#include // std::move - -#include "cata_assert.h" +#include // IWYU pragma: export namespace cata { - -template , typename element_skipfield_type = unsigned short > -// Empty base class optimization - inheriting allocator functions -class colony : private element_allocator_type -// Note: unsigned short is equivalent to uint_least16_t i.e. Using 16-bit unsigned integer in best-case scenario, greater-than-16-bit unsigned integer where platform doesn't support 16-bit types -{ - public: - // Standard container typedefs: - using value_type = element_type; - using allocator_type = element_allocator_type; - using skipfield_type = element_skipfield_type; - - // NOLINTNEXTLINE(bugprone-sizeof-expression) - using aligned_element_type = std::aligned_storage_t < sizeof( element_type ), - ( alignof( element_type ) > ( sizeof( element_skipfield_type ) * 2 ) ) ? alignof( element_type ) : - ( sizeof( element_skipfield_type ) * 2 ) >; - - using size_type = typename std::allocator_traits::size_type; - using difference_type = typename std::allocator_traits::difference_type; - using reference = element_type&; - using const_reference = const element_type&; - using pointer = typename std::allocator_traits::pointer; - using const_pointer = typename std::allocator_traits::const_pointer; - - // Iterator declarations: - template class colony_iterator; - using iterator = colony_iterator; - using const_iterator = colony_iterator; - friend class colony_iterator; // Using above typedef name here is illegal under C++03 - friend class colony_iterator; - - template class colony_reverse_iterator; - using reverse_iterator = colony_reverse_iterator; - using const_reverse_iterator = colony_reverse_iterator; - friend class colony_reverse_iterator; - friend class colony_reverse_iterator; - - private: - - struct group; // forward declaration for typedefs below - - using aligned_element_allocator_type = typename - std::allocator_traits::template rebind_alloc; - using group_allocator_type = typename std::allocator_traits::template - rebind_alloc; - using skipfield_allocator_type = typename std::allocator_traits::template - rebind_alloc; - - // Using uchar as the generic allocator type, as sizeof is always guaranteed to be 1 byte regardless of the number of bits in a byte on given computer, whereas for example, uint8_t would fail on machines where there are more than 8 bits in a byte eg. Texas Instruments C54x DSPs. - using uchar_allocator_type = typename std::allocator_traits::template - rebind_alloc; - // Different typedef to 'pointer' - this is a pointer to the over aligned element type, not the original element type - using aligned_pointer_type = typename - std::allocator_traits::pointer; - using group_pointer_type = typename std::allocator_traits::pointer; - using skipfield_pointer_type = typename std::allocator_traits::pointer; - using uchar_pointer_type = typename std::allocator_traits::pointer; - - using pointer_allocator_type = typename std::allocator_traits::template - rebind_alloc; - - // Colony groups: - - // Empty base class optimization (EBCO) - inheriting allocator functions - struct group : private uchar_allocator_type { - aligned_pointer_type - last_endpoint; // The address that is one past the highest cell number that's been used so far in this group - does not change with erase command but may change with insert (if no previously-erased locations are available) - is necessary because an iterator cannot access the colony's end_iterator. Most-used variable in colony use (operator ++, --) so first in struct - group_pointer_type - next_group; // Next group in the intrusive list of all groups. NULL if no next group - const aligned_pointer_type elements; // Element storage - const skipfield_pointer_type - skipfield; // Skipfield storage. The element and skipfield arrays are allocated contiguously, hence the skipfield pointer also functions as a 'one-past-end' pointer for the elements array. There will always be one additional skipfield node allocated compared to the number of elements. This is to ensure a faster ++ iterator operation (fewer checks are required when this is present). The extra node is unused and always zero, but checked, and not having it will result in out-of-bounds memory errors. - group_pointer_type - previous_group; // previous group in the intrusive list of all groups. NULL if no preceding group - skipfield_type - free_list_head; // The index of the last erased element in the group. The last erased element will, in turn, contain the number of the index of the next erased element, and so on. If this is == maximum skipfield_type value then free_list is empty i.e. no erasures have occurred in the group (or if they have, the erased locations have then been reused via insert()). - const skipfield_type - capacity; // The element capacity of this particular group - skipfield_type - number_of_elements; // indicates total number of active elements in group - changes with insert and erase commands - used to check for empty group in erase function, as an indication to remove the group - group_pointer_type - erasures_list_next_group; // The next group in the intrusive singly-linked list of groups with erasures i.e. with active erased-element free lists - size_type - group_number; // Used for comparison (> < >= <=) iterator operators (used by distance function and user) - - explicit group( const skipfield_type elements_per_group, - group_pointer_type const previous = nullptr ): - last_endpoint( reinterpret_cast( COLONY_ALLOCATE_INITIALIZATION( - uchar_allocator_type, ( ( elements_per_group * ( sizeof( aligned_element_type ) ) ) + ( ( - elements_per_group + 1u ) * sizeof( skipfield_type ) ) ), - ( previous == nullptr ) ? nullptr : - previous->elements ) ) ), /* allocating to here purely because it is first in the struct sequence - actual pointer is elements, last_endpoint is only initialized to element's base value initially, then incremented by one below */ - next_group( nullptr ), - elements( last_endpoint++ ), - skipfield( reinterpret_cast( elements + elements_per_group ) ), - previous_group( previous ), - free_list_head( std::numeric_limits::max() ), - capacity( elements_per_group ), - number_of_elements( 1 ), - erasures_list_next_group( nullptr ), - group_number( ( previous == nullptr ) ? 0 : previous->group_number + 1u ) { - // Static casts to unsigned int from short not necessary as C++ automatically promotes lesser types for arithmetic purposes. - std::memset( &*skipfield, 0, sizeof( skipfield_type ) * ( elements_per_group + - 1u ) ); // &* to avoid problems with non-trivial pointers - } - - ~group() noexcept { - // Null check not necessary (for copied group as above) as delete will also perform a null check. - COLONY_DEALLOCATE( uchar_allocator_type, ( *this ), - reinterpret_cast( elements ), - ( capacity * sizeof( aligned_element_type ) ) + ( ( capacity + 1u ) * sizeof( skipfield_type ) ) ); - } - }; - - // Implement const/non-const iterator switching pattern: - template struct choose; - - template struct choose { - using type = is_true; - }; - - template struct choose { - using type = is_false; - }; - - public: - - // Iterators: - template class colony_iterator - { - private: - group_pointer_type group_pointer; - aligned_pointer_type element_pointer; - skipfield_pointer_type skipfield_pointer; - - public: - using iterator_category = std::bidirectional_iterator_tag; - using value_type = typename colony::value_type; - using difference_type = typename colony::difference_type; - using pointer = typename - choose::type; - using reference = typename - choose::type; - - friend class colony; - friend class colony_reverse_iterator; - friend class colony_reverse_iterator; - - inline colony_iterator &operator=( const colony_iterator &source ) noexcept { - group_pointer = source.group_pointer; - element_pointer = source.element_pointer; - skipfield_pointer = source.skipfield_pointer; - return *this; - } - - inline colony_iterator &operator=( const colony_iterator < !is_const > &source ) noexcept { - group_pointer = source.group_pointer; - element_pointer = source.element_pointer; - skipfield_pointer = source.skipfield_pointer; - return *this; - } - - // Move assignment - only really necessary if the allocator uses non-standard i.e. smart pointers - inline colony_iterator &operator=( colony_iterator &&source ) - noexcept { // Move is a copy in this scenario - cata_assert( &source != this ); - group_pointer = std::move( source.group_pointer ); - element_pointer = std::move( source.element_pointer ); - skipfield_pointer = std::move( source.skipfield_pointer ); - return *this; - } - - inline colony_iterator &operator=( colony_iterator < !is_const > &&source ) noexcept { - cata_assert( &source != this ); - group_pointer = std::move( source.group_pointer ); - element_pointer = std::move( source.element_pointer ); - skipfield_pointer = std::move( source.skipfield_pointer ); - return *this; - } - - inline COLONY_FORCE_INLINE bool operator==( const colony_iterator &rh ) const noexcept { - return element_pointer == rh.element_pointer; - } - - inline COLONY_FORCE_INLINE bool operator==( const colony_iterator < !is_const > &rh ) const - noexcept { - return element_pointer == rh.element_pointer; - } - - inline COLONY_FORCE_INLINE bool operator!=( const colony_iterator &rh ) const noexcept { - return element_pointer != rh.element_pointer; - } - - inline COLONY_FORCE_INLINE bool operator!=( const colony_iterator < !is_const > &rh ) const - noexcept { - return element_pointer != rh.element_pointer; - } - - // may cause exception with uninitialized iterator - inline COLONY_FORCE_INLINE reference operator*() const { - return *( reinterpret_cast( element_pointer ) ); - } - - inline COLONY_FORCE_INLINE pointer operator->() const noexcept { - return reinterpret_cast( element_pointer ); - } - - colony_iterator &operator++() { - // covers uninitialized colony_iterator - cata_assert( group_pointer != nullptr ); - // Assert that iterator is not already at end() - cata_assert( !( element_pointer == group_pointer->last_endpoint && - group_pointer->next_group != nullptr ) ); - - skipfield_type skip = *( ++skipfield_pointer ); - - // ie. beyond end of available data - if( ( element_pointer += skip + 1 ) == group_pointer->last_endpoint && - group_pointer->next_group != nullptr ) { - group_pointer = group_pointer->next_group; - skip = *( group_pointer->skipfield ); - element_pointer = group_pointer->elements + skip; - skipfield_pointer = group_pointer->skipfield; - } - - skipfield_pointer += skip; - return *this; - } - - inline colony_iterator operator++( int ) { - colony_iterator copy( *this ); - ++*this; - return copy; - } - - private: - inline COLONY_FORCE_INLINE void check_for_end_of_group_and_progress() { // used by erase - if( element_pointer == group_pointer->last_endpoint && group_pointer->next_group != nullptr ) { - group_pointer = group_pointer->next_group; - skipfield_pointer = group_pointer->skipfield; - element_pointer = group_pointer->elements + *skipfield_pointer; - skipfield_pointer += *skipfield_pointer; - } - } - - public: - - colony_iterator &operator--() { - cata_assert( group_pointer != nullptr ); - cata_assert( !( element_pointer == group_pointer->elements && - group_pointer->previous_group == - nullptr ) ); // Assert that we are not already at begin() - this is not required to be tested in the code below as we don't need a special condition to progress to begin(), like we do with end() in operator ++ - - if( element_pointer != group_pointer->elements ) { // i.e. not already at beginning of group - const skipfield_type skip = *( --skipfield_pointer ); - skipfield_pointer -= skip; - - if( ( element_pointer -= skip + 1 ) != group_pointer->elements - - 1 ) { // i.e. iterator was not already at beginning of colony (with some previous consecutive deleted elements), and skipfield does not takes us into the previous group) - return *this; - } - } - - group_pointer = group_pointer->previous_group; - skipfield_pointer = group_pointer->skipfield + group_pointer->capacity - 1; - element_pointer = ( reinterpret_cast( group_pointer->skipfield ) - 1 ) - - *skipfield_pointer; - skipfield_pointer -= *skipfield_pointer; - - return *this; - } - - inline colony_iterator operator--( int ) { - const colony_iterator copy( *this ); - --*this; - return copy; - } - - inline bool operator>( const colony_iterator &rh ) const noexcept { - return ( ( group_pointer == rh.group_pointer ) & ( element_pointer > rh.element_pointer ) ) || - ( group_pointer != rh.group_pointer && - group_pointer->group_number > rh.group_pointer->group_number ); - } - - inline bool operator<( const colony_iterator &rh ) const noexcept { - return rh > *this; - } - - inline bool operator>=( const colony_iterator &rh ) const noexcept { - return !( rh > *this ); - } - - inline bool operator<=( const colony_iterator &rh ) const noexcept { - return !( *this > rh ); - } - - inline bool operator>( const colony_iterator < !is_const > &rh ) const noexcept { - return ( ( group_pointer == rh.group_pointer ) & ( element_pointer > rh.element_pointer ) ) || - ( group_pointer != rh.group_pointer && - group_pointer->group_number > rh.group_pointer->group_number ); - } - - inline bool operator<( const colony_iterator < !is_const > &rh ) const noexcept { - return rh > *this; - } - - inline bool operator>=( const colony_iterator < !is_const > &rh ) const noexcept { - return !( rh > *this ); - } - - inline bool operator<=( const colony_iterator < !is_const > &rh ) const noexcept { - return !( *this > rh ); - } - - colony_iterator() noexcept: group_pointer( nullptr ), element_pointer( nullptr ), - skipfield_pointer( nullptr ) {} - - private: - // Used by cend(), erase() etc: - colony_iterator( const group_pointer_type group_p, const aligned_pointer_type element_p, - const skipfield_pointer_type skipfield_p ) noexcept: group_pointer( group_p ), - element_pointer( element_p ), skipfield_pointer( skipfield_p ) {} - - public: - - inline colony_iterator( const colony_iterator &source ) noexcept: - group_pointer( source.group_pointer ), - element_pointer( source.element_pointer ), - skipfield_pointer( source.skipfield_pointer ) {} - - // NOLINTNEXTLINE(google-explicit-constructor) - inline colony_iterator( const colony_iterator < !is_const > &source ) noexcept: - group_pointer( source.group_pointer ), - element_pointer( source.element_pointer ), - skipfield_pointer( source.skipfield_pointer ) {} - - // move constructor - inline colony_iterator( colony_iterator &&source ) noexcept: - group_pointer( std::move( source.group_pointer ) ), - element_pointer( std::move( source.element_pointer ) ), - skipfield_pointer( std::move( source.skipfield_pointer ) ) {} - - // NOLINTNEXTLINE(google-explicit-constructor) - inline colony_iterator( colony_iterator < !is_const > &&source ) noexcept: - group_pointer( std::move( source.group_pointer ) ), - element_pointer( std::move( source.element_pointer ) ), - skipfield_pointer( std::move( source.skipfield_pointer ) ) {} - }; // colony_iterator - - // Reverse iterators: - template class colony_reverse_iterator - { - private: - iterator it; - - public: - using iterator_category = std::bidirectional_iterator_tag; - using value_type = typename colony::value_type; - using difference_type = typename colony::difference_type; - using pointer = typename - choose::type; - using reference = typename - choose::type; - - friend class colony; - - inline colony_reverse_iterator &operator=( const colony_reverse_iterator &source ) noexcept { - it = source.it; - return *this; - } - - // move assignment - inline colony_reverse_iterator &operator=( colony_reverse_iterator &&source ) noexcept { - it = std::move( source.it ); - return *this; - } - - inline COLONY_FORCE_INLINE bool operator==( const colony_reverse_iterator &rh ) const noexcept { - return it == rh.it; - } - - inline COLONY_FORCE_INLINE bool operator!=( const colony_reverse_iterator &rh ) const noexcept { - return it != rh.it; - } - - inline COLONY_FORCE_INLINE reference operator*() const noexcept { - return *( reinterpret_cast( it.element_pointer ) ); - } - - inline COLONY_FORCE_INLINE pointer *operator->() const noexcept { - return reinterpret_cast( it.element_pointer ); - } - - // In this case we have to redefine the algorithm, rather than using the internal iterator's -- operator, in order for the reverse_iterator to be allowed to reach rend() ie. begin_iterator - 1 - colony_reverse_iterator &operator++() { - colony::group_pointer_type &group_pointer = it.group_pointer; - colony::aligned_pointer_type &element_pointer = it.element_pointer; - colony::skipfield_pointer_type &skipfield_pointer = it.skipfield_pointer; - - cata_assert( group_pointer != nullptr ); - cata_assert( !( element_pointer == group_pointer->elements - 1 && - group_pointer->previous_group == nullptr ) ); // Assert that we are not already at rend() - - if( element_pointer != group_pointer->elements ) { // ie. not already at beginning of group - element_pointer -= *( --skipfield_pointer ) + 1; - skipfield_pointer -= *skipfield_pointer; - - if( !( element_pointer == group_pointer->elements - 1 && - group_pointer->previous_group == nullptr ) ) { // i.e. iterator is not == rend() - return *this; - } - } - - if( group_pointer->previous_group != nullptr ) { // i.e. not first group in colony - group_pointer = group_pointer->previous_group; - skipfield_pointer = group_pointer->skipfield + group_pointer->capacity - 1; - element_pointer = ( reinterpret_cast( group_pointer->skipfield ) - 1 ) - - *skipfield_pointer; - skipfield_pointer -= *skipfield_pointer; - } else { // necessary so that reverse_iterator can end up == rend(), if we were already at first element in colony - --element_pointer; - --skipfield_pointer; - } - - return *this; - } - - inline colony_reverse_iterator operator++( int ) { - const colony_reverse_iterator copy( *this ); - ++*this; - return copy; - } - - inline COLONY_FORCE_INLINE colony_reverse_iterator &operator--() { - // i.e. Check that we are not already at rbegin() - cata_assert( !( it.element_pointer == it.group_pointer->last_endpoint - 1 && - it.group_pointer->next_group == nullptr ) ); - ++it; - return *this; - } - - inline colony_reverse_iterator operator--( int ) { - const colony_reverse_iterator copy( *this ); - --*this; - return copy; - } - - inline typename colony::iterator base() const { - return ++( typename colony::iterator( it ) ); - } - - inline bool operator>( const colony_reverse_iterator &rh ) const noexcept { - return rh.it > it; - } - - inline bool operator<( const colony_reverse_iterator &rh ) const noexcept { - return it > rh.it; - } - - inline bool operator>=( const colony_reverse_iterator &rh ) const noexcept { - return !( it > rh.it ); - } - - inline bool operator<=( const colony_reverse_iterator &rh ) const noexcept { - return !( rh.it > it ); - } - - inline COLONY_FORCE_INLINE bool operator==( const colony_reverse_iterator < !r_is_const > &rh ) - const noexcept { - return it == rh.it; - } - - inline COLONY_FORCE_INLINE bool operator!=( const colony_reverse_iterator < !r_is_const > &rh ) - const noexcept { - return it != rh.it; - } - - inline bool operator>( const colony_reverse_iterator < !r_is_const > &rh ) const noexcept { - return rh.it > it; - } - - inline bool operator<( const colony_reverse_iterator < !r_is_const > &rh ) const noexcept { - return it > rh.it; - } - - inline bool operator>=( const colony_reverse_iterator < !r_is_const > &rh ) const noexcept { - return !( it > rh.it ); - } - - inline bool operator<=( const colony_reverse_iterator < !r_is_const > &rh ) const noexcept { - return !( rh.it > it ); - } - - colony_reverse_iterator() noexcept = default; - - colony_reverse_iterator( const colony_reverse_iterator &source ) noexcept: - it( source.it ) {} - - // NOLINTNEXTLINE(google-explicit-constructor) - colony_reverse_iterator( const typename colony::iterator &source ) noexcept: - it( source ) {} - - private: - // Used by rend(), etc: - colony_reverse_iterator( const group_pointer_type group_p, const aligned_pointer_type element_p, - const skipfield_pointer_type skipfield_p ) noexcept: - it( group_p, element_p, skipfield_p ) {} - - public: - - // move constructors - colony_reverse_iterator( colony_reverse_iterator &&source ) noexcept: - it( std::move( source.it ) ) {} - - // NOLINTNEXTLINE(google-explicit-constructor) - colony_reverse_iterator( typename colony::iterator &&source ) noexcept: - it( std::move( source ) ) {} - - }; // colony_reverse_iterator - - private: - - // Used to prevent fill-insert/constructor calls being mistakenly resolved to range-insert/constructor calls - template - struct enable_if_c { - using type = T; - }; - - template - struct enable_if_c { - }; - - iterator end_iterator, begin_iterator; - - // Head of a singly-linked intrusive list of groups which have erased-element memory locations available for reuse - group_pointer_type groups_with_erasures_list_head; - size_type total_number_of_elements, total_capacity; - - // Packaging the element pointer allocator with a lesser-used member variable, for empty-base-class optimization - struct ebco_pair2 : pointer_allocator_type { - skipfield_type min_elements_per_group; - explicit ebco_pair2( const skipfield_type min_elements ) noexcept: - min_elements_per_group( min_elements ) {} - } pointer_allocator_pair; - - struct ebco_pair : group_allocator_type { - skipfield_type max_elements_per_group; - explicit ebco_pair( const skipfield_type max_elements ) noexcept: - max_elements_per_group( max_elements ) {} - } group_allocator_pair; - - public: - - /** - * Default constructor: - * default minimum group size is 8, default maximum group size is - * std::numeric_limits::max() (typically 65535). You cannot set the group - * sizes from the constructor in this scenario, but you can call the change_group_sizes() - * member function after construction has occurred. - */ - colony() noexcept: - element_allocator_type( element_allocator_type() ), - groups_with_erasures_list_head( nullptr ), - total_number_of_elements( 0 ), - total_capacity( 0 ), - pointer_allocator_pair( ( sizeof( aligned_element_type ) * 8 > ( sizeof( *this ) + sizeof( - group ) ) * 2 ) ? 8 : ( ( ( sizeof( *this ) + sizeof( group ) ) * 2 ) / sizeof( - aligned_element_type ) ) ), - group_allocator_pair( std::numeric_limits::max() ) { - // skipfield type must be of unsigned integer type (uchar, ushort, uint etc) - cata_assert( std::numeric_limits::is_integer & - !std::numeric_limits::is_signed ); - } - - /** - * Default constructor, but using a custom memory allocator eg. something other than - * std::allocator. - */ - explicit colony( const element_allocator_type &alloc ): - element_allocator_type( alloc ), - groups_with_erasures_list_head( NULL ), - total_number_of_elements( 0 ), - total_capacity( 0 ), - pointer_allocator_pair( ( sizeof( aligned_element_type ) * 8 > ( sizeof( *this ) + sizeof( - group ) ) * 2 ) ? 8 : ( ( ( sizeof( *this ) + sizeof( group ) ) * 2 ) / sizeof( - aligned_element_type ) ) ), - group_allocator_pair( std::numeric_limits::max() ) { - cata_assert( std::numeric_limits::is_integer & - !std::numeric_limits::is_signed ); - } - - /** - * Copy constructor: - * Copy all contents from source colony, removes any empty (erased) element locations in the - * process. Size of groups created is either the total size of the source colony, or the - * maximum group size of the source colony, whichever is the smaller. - */ - colony( const colony &source ): - element_allocator_type( source ), - groups_with_erasures_list_head( nullptr ), - total_number_of_elements( 0 ), - total_capacity( 0 ), - // Make the first colony group capacity the greater of min_elements_per_group or total_number_of_elements, so long as total_number_of_elements isn't larger than max_elements_per_group - pointer_allocator_pair( static_cast( ( - source.pointer_allocator_pair.min_elements_per_group > source.total_number_of_elements ) ? - source.pointer_allocator_pair.min_elements_per_group : ( ( source.total_number_of_elements > - source.group_allocator_pair.max_elements_per_group ) ? - source.group_allocator_pair.max_elements_per_group : - source.total_number_of_elements ) ) ), - group_allocator_pair( source.group_allocator_pair.max_elements_per_group ) { - insert( source.begin_iterator, source.end_iterator ); - // reset to correct value for future clear() or erasures - pointer_allocator_pair.min_elements_per_group = - source.pointer_allocator_pair.min_elements_per_group; - } - - // Copy constructor (allocator-extended): - colony( const colony &source, const allocator_type &alloc ): - element_allocator_type( alloc ), - groups_with_erasures_list_head( nullptr ), - total_number_of_elements( 0 ), - total_capacity( 0 ), - pointer_allocator_pair( static_cast( ( - source.pointer_allocator_pair.min_elements_per_group > source.total_number_of_elements ) ? - source.pointer_allocator_pair.min_elements_per_group : ( ( source.total_number_of_elements > - source.group_allocator_pair.max_elements_per_group ) ? - source.group_allocator_pair.max_elements_per_group : source.total_number_of_elements ) ) ), - group_allocator_pair( source.group_allocator_pair.max_elements_per_group ) { - insert( source.begin_iterator, source.end_iterator ); - pointer_allocator_pair.min_elements_per_group = - source.pointer_allocator_pair.min_elements_per_group; - } - - private: - - inline void blank() noexcept { - // if all pointer types are trivial, we can just nuke it from orbit with memset (NULL is always 0 in C++): - if COLONY_CONSTEXPR( std::is_trivial::value && - std::is_trivial::value && - std::is_trivial::value ) { - std::memset( static_cast( this ), 0, offsetof( colony, pointer_allocator_pair ) ); - } else { - end_iterator.group_pointer = nullptr; - end_iterator.element_pointer = nullptr; - end_iterator.skipfield_pointer = nullptr; - begin_iterator.group_pointer = nullptr; - begin_iterator.element_pointer = nullptr; - begin_iterator.skipfield_pointer = nullptr; - groups_with_erasures_list_head = nullptr; - total_number_of_elements = 0; - total_capacity = 0; - } - } - - public: - - /** - * Move constructor: - * Move all contents from source colony, does not remove any erased element locations or - * alter any of the source group sizes. Source colony is now empty and can be safely - * destructed or otherwise used. - */ - colony( colony &&source ) noexcept: - element_allocator_type( source ), - end_iterator( std::move( source.end_iterator ) ), - begin_iterator( std::move( source.begin_iterator ) ), - groups_with_erasures_list_head( std::move( source.groups_with_erasures_list_head ) ), - total_number_of_elements( source.total_number_of_elements ), - total_capacity( source.total_capacity ), - pointer_allocator_pair( source.pointer_allocator_pair.min_elements_per_group ), - group_allocator_pair( source.group_allocator_pair.max_elements_per_group ) { - source.blank(); - } - - // Move constructor (allocator-extended): - colony( colony &&source, const allocator_type &alloc ): - element_allocator_type( alloc ), - end_iterator( std::move( source.end_iterator ) ), - begin_iterator( std::move( source.begin_iterator ) ), - groups_with_erasures_list_head( std::move( source.groups_with_erasures_list_head ) ), - total_number_of_elements( source.total_number_of_elements ), - total_capacity( source.total_capacity ), - pointer_allocator_pair( source.pointer_allocator_pair.min_elements_per_group ), - group_allocator_pair( source.group_allocator_pair.max_elements_per_group ) { - source.blank(); - } - - /** - * Fill constructor with value_type unspecified, so the value_type's default constructor is - * used. n specifies the number of elements to create upon construction. If n is larger than - * min_group_size, the size of the groups created will either be n and max_group_size, - * depending on which is smaller. min_group_size (i.e. the smallest possible number of - * elements which can be stored in a colony group) can be defined, as can the max_group_size. - * Setting the group sizes can be a performance advantage if you know in advance roughly how - * many objects are likely to be stored in your colony long-term - or at least the rough - * scale of storage. If that case, using this can stop many small initial groups being - * allocated (reserve() will achieve a similar result, but structurally at the moment is - * limited to allocating one group). - */ - colony( const size_type fill_number, const element_type &element, - const skipfield_type min_allocation_amount = 0, - const skipfield_type max_allocation_amount = std::numeric_limits::max(), - const element_allocator_type &alloc = element_allocator_type() ): - element_allocator_type( alloc ), - groups_with_erasures_list_head( nullptr ), - total_number_of_elements( 0 ), - total_capacity( 0 ), - pointer_allocator_pair( ( min_allocation_amount != 0 ) ? min_allocation_amount : - ( fill_number > max_allocation_amount ) ? max_allocation_amount : - ( fill_number > 8 ) ? static_cast( fill_number ) : 8 ), - group_allocator_pair( max_allocation_amount ) { - cata_assert( std::numeric_limits::is_integer & - !std::numeric_limits::is_signed ); - cata_assert( ( pointer_allocator_pair.min_elements_per_group > 2 ) & - ( pointer_allocator_pair.min_elements_per_group <= group_allocator_pair.max_elements_per_group ) ); - - insert( fill_number, element ); - } - - // Range constructor: - template - colony( const typename enable_if_c < !std::numeric_limits::is_integer, - iterator_type >::type &first, const iterator_type &last, - const skipfield_type min_allocation_amount = 8, - const skipfield_type max_allocation_amount = std::numeric_limits::max(), - const element_allocator_type &alloc = element_allocator_type() ): - element_allocator_type( alloc ), - groups_with_erasures_list_head( nullptr ), - total_number_of_elements( 0 ), - total_capacity( 0 ), - pointer_allocator_pair( min_allocation_amount ), - group_allocator_pair( max_allocation_amount ) { - cata_assert( std::numeric_limits::is_integer & - !std::numeric_limits::is_signed ); - cata_assert( ( pointer_allocator_pair.min_elements_per_group > 2 ) & - ( pointer_allocator_pair.min_elements_per_group <= group_allocator_pair.max_elements_per_group ) ); - - insert( first, last ); - } - - // Initializer-list constructor: - colony( const std::initializer_list &element_list, - const skipfield_type min_allocation_amount = 0, - const skipfield_type max_allocation_amount = std::numeric_limits::max(), - const element_allocator_type &alloc = element_allocator_type() ): - element_allocator_type( alloc ), - groups_with_erasures_list_head( nullptr ), - total_number_of_elements( 0 ), - total_capacity( 0 ), - pointer_allocator_pair( ( min_allocation_amount != 0 ) ? min_allocation_amount : - ( element_list.size() > max_allocation_amount ) ? max_allocation_amount : - ( element_list.size() > 8 ) ? static_cast( element_list.size() ) : 8 ), - group_allocator_pair( max_allocation_amount ) { - cata_assert( std::numeric_limits::is_integer & - !std::numeric_limits::is_signed ); - cata_assert( ( pointer_allocator_pair.min_elements_per_group > 2 ) & - ( pointer_allocator_pair.min_elements_per_group <= group_allocator_pair.max_elements_per_group ) ); - - insert( element_list ); - } - - inline COLONY_FORCE_INLINE iterator begin() noexcept { - return begin_iterator; - } - - inline COLONY_FORCE_INLINE const iterator &begin() const - noexcept { // To allow for functions which only take const colony & as a source eg. copy constructor - return begin_iterator; - } - - inline COLONY_FORCE_INLINE iterator end() noexcept { - return end_iterator; - } - - inline COLONY_FORCE_INLINE const iterator &end() const noexcept { - return end_iterator; - } - - inline const_iterator cbegin() const noexcept { - return const_iterator( begin_iterator.group_pointer, begin_iterator.element_pointer, - begin_iterator.skipfield_pointer ); - } - - inline const_iterator cend() const noexcept { - return const_iterator( end_iterator.group_pointer, end_iterator.element_pointer, - end_iterator.skipfield_pointer ); - } - - inline reverse_iterator rbegin() - const { // May throw exception if colony is empty so end_iterator is uninitialized - return ++reverse_iterator( end_iterator ); - } - - inline reverse_iterator rend() const noexcept { - return reverse_iterator( begin_iterator.group_pointer, begin_iterator.element_pointer - 1, - begin_iterator.skipfield_pointer - 1 ); - } - - inline const_reverse_iterator crbegin() const { - return ++const_reverse_iterator( end_iterator ); - } - - inline const_reverse_iterator crend() const noexcept { - return const_reverse_iterator( begin_iterator.group_pointer, begin_iterator.element_pointer - 1, - begin_iterator.skipfield_pointer - 1 ); - } - - ~colony() noexcept { - destroy_all_data(); - } - - private: - - void destroy_all_data() noexcept { - // Amusingly enough, these changes from && to logical & actually do make a significant difference in debug mode - if( ( total_number_of_elements != 0 ) & !std::is_trivially_destructible::value ) { - total_number_of_elements = 0; // to avoid double-destruction - - while( true ) { - const aligned_pointer_type end_pointer = begin_iterator.group_pointer->last_endpoint; - - do { - COLONY_DESTROY( element_allocator_type, ( *this ), - reinterpret_cast( begin_iterator.element_pointer ) ); - ++begin_iterator.skipfield_pointer; - begin_iterator.element_pointer += *begin_iterator.skipfield_pointer + 1; - begin_iterator.skipfield_pointer += *begin_iterator.skipfield_pointer; - } while( begin_iterator.element_pointer != end_pointer ); // ie. beyond end of available data - - const group_pointer_type next_group = begin_iterator.group_pointer->next_group; - COLONY_DESTROY( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, 1 ); - begin_iterator.group_pointer = - next_group; // required to be before if statement in order for first_group to be NULL and avoid potential double-destruction in future - - if( next_group == nullptr ) { - return; - } - - begin_iterator.element_pointer = next_group->elements + *( next_group->skipfield ); - begin_iterator.skipfield_pointer = next_group->skipfield + *( next_group->skipfield ); - } - } else { // Avoid iteration for both empty groups and trivially-destructible types eg. POD, structs, classes with empty destructors - // Technically under a type-traits-supporting compiler total_number_of_elements could be non-zero at this point, but since begin_iterator.group_pointer would already be NULL in the case of double-destruction, it's unnecessary to zero total_number_of_elements - while( begin_iterator.group_pointer != nullptr ) { - const group_pointer_type next_group = begin_iterator.group_pointer->next_group; - COLONY_DESTROY( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, 1 ); - begin_iterator.group_pointer = next_group; - } - } - } - - void initialize( const skipfield_type first_group_size ) { - begin_iterator.group_pointer = COLONY_ALLOCATE( group_allocator_type, group_allocator_pair, 1, - nullptr ); - - try { - COLONY_CONSTRUCT( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, - first_group_size ); - } catch( ... ) { - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, 1 ); - begin_iterator.group_pointer = nullptr; - throw; - } - - end_iterator.group_pointer = begin_iterator.group_pointer; - end_iterator.element_pointer = begin_iterator.element_pointer = - begin_iterator.group_pointer->elements; - end_iterator.skipfield_pointer = begin_iterator.skipfield_pointer = - begin_iterator.group_pointer->skipfield; - total_capacity = first_group_size; - } - - public: - - /** - * Inserts the element supplied to the colony, using the object's copy-constructor. Will - * insert the element into a previously erased element slot if one exists, otherwise will - * insert to back of colony. Returns iterator to location of inserted element. - */ - iterator insert( const element_type &element ) { - if( end_iterator.element_pointer != nullptr ) { - switch( ( ( groups_with_erasures_list_head != nullptr ) << 1 ) | ( end_iterator.element_pointer == - reinterpret_cast( end_iterator.group_pointer->skipfield ) ) ) { - case 0: { // ie. there are no erased elements and end_iterator is not at end of current final group - // Make copy for return before modifying end_iterator - iterator return_iterator = end_iterator; - - if COLONY_CONSTEXPR( std::is_nothrow_copy_constructible::value ) { - // For no good reason this compiles to faster code under GCC: - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer++ ), element ); - end_iterator.group_pointer->last_endpoint = end_iterator.element_pointer; - } else { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer ), element ); - // Shift the addition to the second operation, avoiding problems if an exception is thrown during construction - end_iterator.group_pointer->last_endpoint = ++end_iterator.element_pointer; - } - - ++( end_iterator.group_pointer->number_of_elements ); - ++end_iterator.skipfield_pointer; - ++total_number_of_elements; - - return return_iterator; // return value before incrementation - } - case 1: { // ie. there are no erased elements and end_iterator is at end of current final group - ie. colony is full - create new group - end_iterator.group_pointer->next_group = COLONY_ALLOCATE( group_allocator_type, - group_allocator_pair, 1, end_iterator.group_pointer ); - group &next_group = *( end_iterator.group_pointer->next_group ); - const skipfield_type new_group_size = ( total_number_of_elements < static_cast - ( group_allocator_pair.max_elements_per_group ) ) ? static_cast - ( total_number_of_elements ) : group_allocator_pair.max_elements_per_group; - - try { - COLONY_CONSTRUCT( group_allocator_type, group_allocator_pair, &next_group, new_group_size, - end_iterator.group_pointer ); - } catch( ... ) { - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, &next_group, 1 ); - end_iterator.group_pointer->next_group = nullptr; - throw; - } - - if COLONY_CONSTEXPR( std::is_nothrow_copy_constructible::value ) { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( next_group.elements ), element ); - } else { - try { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( next_group.elements ), element ); - } catch( ... ) { - COLONY_DESTROY( group_allocator_type, group_allocator_pair, &next_group ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, &next_group, 1 ); - end_iterator.group_pointer->next_group = nullptr; - throw; - } - } - - end_iterator.group_pointer = &next_group; - end_iterator.element_pointer = next_group.last_endpoint; - end_iterator.skipfield_pointer = next_group.skipfield + 1; - ++total_number_of_elements; - total_capacity += new_group_size; - - // returns value before incrementation - return iterator( end_iterator.group_pointer, next_group.elements, next_group.skipfield ); - } - default: { // ie. there are erased elements, reuse previous-erased element locations - iterator new_location; - new_location.group_pointer = groups_with_erasures_list_head; - new_location.element_pointer = groups_with_erasures_list_head->elements + - groups_with_erasures_list_head->free_list_head; - new_location.skipfield_pointer = groups_with_erasures_list_head->skipfield + - groups_with_erasures_list_head->free_list_head; - - // always at start of skipblock, update skipblock: - const skipfield_type prev_free_list_index = *( reinterpret_cast - ( new_location.element_pointer ) ); - - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( new_location.element_pointer ), element ); - - // Update skipblock: - const skipfield_type new_value = *( new_location.skipfield_pointer ) - 1; - - // ie. skipfield was not 1, ie. a single-node skipblock, with no additional nodes to update - if( new_value != 0 ) { - // set (new) start and (original) end of skipblock to new value: - *( new_location.skipfield_pointer + new_value ) = *( new_location.skipfield_pointer + 1 ) = - new_value; - - // transfer free list node to new start node: - ++( groups_with_erasures_list_head->free_list_head ); - - // i.e. not the tail free list node - if( prev_free_list_index != std::numeric_limits::max() ) { - *( reinterpret_cast( new_location.group_pointer->elements + - prev_free_list_index ) + 1 ) = groups_with_erasures_list_head->free_list_head; - } - - *( reinterpret_cast( new_location.element_pointer + 1 ) ) = - prev_free_list_index; - *( reinterpret_cast( new_location.element_pointer + 1 ) + 1 ) = - std::numeric_limits::max(); - } else { - groups_with_erasures_list_head->free_list_head = prev_free_list_index; - - // i.e. not the last free list node - if( prev_free_list_index != std::numeric_limits::max() ) { - *( reinterpret_cast( new_location.group_pointer->elements + - prev_free_list_index ) + 1 ) = std::numeric_limits::max(); - } else { - groups_with_erasures_list_head = groups_with_erasures_list_head->erasures_list_next_group; - } - } - - *( new_location.skipfield_pointer ) = 0; - ++( new_location.group_pointer->number_of_elements ); - - if( new_location.group_pointer == begin_iterator.group_pointer && - new_location.element_pointer < begin_iterator.element_pointer ) { - // i.e. begin_iterator was moved forwards as the result of an erasure at some point, this erased element is before the current begin, hence, set current begin iterator to this element - begin_iterator = new_location; - } - - ++total_number_of_elements; - return new_location; - } - } - } else { // i.e. newly-constructed colony, no insertions yet and no groups - initialize( pointer_allocator_pair.min_elements_per_group ); - - if COLONY_CONSTEXPR( std::is_nothrow_copy_constructible::value ) { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer++ ), element ); - } else { - try { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer++ ), element ); - } catch( ... ) { - clear(); - throw; - } - } - - ++end_iterator.skipfield_pointer; - total_number_of_elements = 1; - return begin_iterator; - } - } - - /** - * Moves the element supplied to the colony, using the object's move-constructor. Will - * insert the element in a previously erased element slot if one exists, otherwise will - * insert to back of colony. Returns iterator to location of inserted element. - * - * The move-insert function is near-identical to the regular insert function, with the - * exception of the element construction method and is_nothrow tests. - */ - iterator insert( element_type &&element ) { - if( end_iterator.element_pointer != nullptr ) { - switch( ( ( groups_with_erasures_list_head != nullptr ) << 1 ) | ( end_iterator.element_pointer == - reinterpret_cast( end_iterator.group_pointer->skipfield ) ) ) { - case 0: { - iterator return_iterator = end_iterator; - - if COLONY_CONSTEXPR( std::is_nothrow_move_constructible::value ) { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer++ ), std::move( element ) ); - end_iterator.group_pointer->last_endpoint = end_iterator.element_pointer; - } else { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer ), std::move( element ) ); - end_iterator.group_pointer->last_endpoint = ++end_iterator.element_pointer; - } - - ++( end_iterator.group_pointer->number_of_elements ); - ++end_iterator.skipfield_pointer; - ++total_number_of_elements; - - return return_iterator; - } - case 1: { - end_iterator.group_pointer->next_group = COLONY_ALLOCATE( group_allocator_type, - group_allocator_pair, 1, end_iterator.group_pointer ); - group &next_group = *( end_iterator.group_pointer->next_group ); - const skipfield_type new_group_size = ( total_number_of_elements < static_cast - ( group_allocator_pair.max_elements_per_group ) ) ? static_cast - ( total_number_of_elements ) : group_allocator_pair.max_elements_per_group; - - try { - COLONY_CONSTRUCT( group_allocator_type, group_allocator_pair, &next_group, new_group_size, - end_iterator.group_pointer ); - } catch( ... ) { - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, &next_group, 1 ); - end_iterator.group_pointer->next_group = nullptr; - throw; - } - - if COLONY_CONSTEXPR( std::is_nothrow_move_constructible::value ) { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( next_group.elements ), std::move( element ) ); - } else { - try { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( next_group.elements ), std::move( element ) ); - } catch( ... ) { - COLONY_DESTROY( group_allocator_type, group_allocator_pair, &next_group ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, &next_group, 1 ); - end_iterator.group_pointer->next_group = nullptr; - throw; - } - } - - end_iterator.group_pointer = &next_group; - end_iterator.element_pointer = next_group.last_endpoint; - end_iterator.skipfield_pointer = next_group.skipfield + 1; - ++total_number_of_elements; - total_capacity += new_group_size; - - return iterator( end_iterator.group_pointer, next_group.elements, next_group.skipfield ); - } - default: { - iterator new_location; - new_location.group_pointer = groups_with_erasures_list_head; - new_location.element_pointer = groups_with_erasures_list_head->elements + - groups_with_erasures_list_head->free_list_head; - new_location.skipfield_pointer = groups_with_erasures_list_head->skipfield + - groups_with_erasures_list_head->free_list_head; - - const skipfield_type prev_free_list_index = *( reinterpret_cast - ( new_location.element_pointer ) ); - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( new_location.element_pointer ), std::move( element ) ); - - const skipfield_type new_value = *( new_location.skipfield_pointer ) - 1; - - if( new_value != 0 ) { - *( new_location.skipfield_pointer + new_value ) = *( new_location.skipfield_pointer + 1 ) = - new_value; - ++( groups_with_erasures_list_head->free_list_head ); - - if( prev_free_list_index != std::numeric_limits::max() ) { - *( reinterpret_cast( new_location.group_pointer->elements + - prev_free_list_index ) + 1 ) = groups_with_erasures_list_head->free_list_head; - } - - *( reinterpret_cast( new_location.element_pointer + 1 ) ) = - prev_free_list_index; - *( reinterpret_cast( new_location.element_pointer + 1 ) + 1 ) = - std::numeric_limits::max(); - } else { - groups_with_erasures_list_head->free_list_head = prev_free_list_index; - - if( prev_free_list_index != std::numeric_limits::max() ) { - *( reinterpret_cast( new_location.group_pointer->elements + - prev_free_list_index ) + 1 ) = std::numeric_limits::max(); - } else { - groups_with_erasures_list_head = groups_with_erasures_list_head->erasures_list_next_group; - } - } - - *( new_location.skipfield_pointer ) = 0; - ++( new_location.group_pointer->number_of_elements ); - - if( new_location.group_pointer == begin_iterator.group_pointer && - new_location.element_pointer < begin_iterator.element_pointer ) { - begin_iterator = new_location; - } - - ++total_number_of_elements; - - return new_location; - } - } - } else { - initialize( pointer_allocator_pair.min_elements_per_group ); - - if COLONY_CONSTEXPR( std::is_nothrow_move_constructible::value ) { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer++ ), std::move( element ) ); - } else { - try { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer++ ), std::move( element ) ); - } catch( ... ) { - clear(); - throw; - } - } - - ++end_iterator.skipfield_pointer; - total_number_of_elements = 1; - return begin_iterator; - } - } - - /** - * Constructs new element directly within colony. Will insert the element in a previously - * erased element slot if one exists, otherwise will insert to back of colony. Returns - * iterator to location of inserted element. "...parameters" are whatever parameters are - * required by the object's constructor. - * - * The emplace function is near-identical to the regular insert function, with the exception - * of the element construction method and change to is_nothrow tests. - */ - template - iterator emplace( arguments &&... parameters ) { - if( end_iterator.element_pointer != nullptr ) { - switch( ( ( groups_with_erasures_list_head != nullptr ) << 1 ) | ( end_iterator.element_pointer == - reinterpret_cast( end_iterator.group_pointer->skipfield ) ) ) { - case 0: { - iterator return_iterator = end_iterator; - - if COLONY_CONSTEXPR( std::is_nothrow_constructible::value ) { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer++ ), - std::forward( parameters )... ); - end_iterator.group_pointer->last_endpoint = end_iterator.element_pointer; - } else { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer ), - std::forward( parameters )... ); - end_iterator.group_pointer->last_endpoint = ++end_iterator.element_pointer; - } - - ++( end_iterator.group_pointer->number_of_elements ); - ++end_iterator.skipfield_pointer; - ++total_number_of_elements; - - return return_iterator; - } - case 1: { - end_iterator.group_pointer->next_group = COLONY_ALLOCATE( group_allocator_type, - group_allocator_pair, 1, end_iterator.group_pointer ); - group &next_group = *( end_iterator.group_pointer->next_group ); - const skipfield_type new_group_size = ( total_number_of_elements < static_cast - ( group_allocator_pair.max_elements_per_group ) ) ? static_cast - ( total_number_of_elements ) : group_allocator_pair.max_elements_per_group; - - try { - COLONY_CONSTRUCT( group_allocator_type, group_allocator_pair, &next_group, new_group_size, - end_iterator.group_pointer ); - } catch( ... ) { - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, &next_group, 1 ); - end_iterator.group_pointer->next_group = nullptr; - throw; - } - - if COLONY_CONSTEXPR( std::is_nothrow_constructible::value ) { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( next_group.elements ), std::forward( parameters )... ); - } else { - try { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( next_group.elements ), std::forward( parameters )... ); - } catch( ... ) { - COLONY_DESTROY( group_allocator_type, group_allocator_pair, &next_group ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, &next_group, 1 ); - end_iterator.group_pointer->next_group = nullptr; - throw; - } - } - - end_iterator.group_pointer = &next_group; - end_iterator.element_pointer = next_group.last_endpoint; - end_iterator.skipfield_pointer = next_group.skipfield + 1; - total_capacity += new_group_size; - ++total_number_of_elements; - - return iterator( end_iterator.group_pointer, next_group.elements, next_group.skipfield ); - } - default: { - iterator new_location; - new_location.group_pointer = groups_with_erasures_list_head; - new_location.element_pointer = groups_with_erasures_list_head->elements + - groups_with_erasures_list_head->free_list_head; - new_location.skipfield_pointer = groups_with_erasures_list_head->skipfield + - groups_with_erasures_list_head->free_list_head; - - const skipfield_type prev_free_list_index = *( reinterpret_cast - ( new_location.element_pointer ) ); - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( new_location.element_pointer ), - std::forward( parameters ) ... ); - const skipfield_type new_value = *( new_location.skipfield_pointer ) - 1; - - if( new_value != 0 ) { - *( new_location.skipfield_pointer + new_value ) = *( new_location.skipfield_pointer + 1 ) = - new_value; - ++( groups_with_erasures_list_head->free_list_head ); - - if( prev_free_list_index != std::numeric_limits::max() ) { - *( reinterpret_cast( new_location.group_pointer->elements + - prev_free_list_index ) + 1 ) = groups_with_erasures_list_head->free_list_head; - } - - *( reinterpret_cast( new_location.element_pointer + 1 ) ) = - prev_free_list_index; - *( reinterpret_cast( new_location.element_pointer + 1 ) + 1 ) = - std::numeric_limits::max(); - } else { - groups_with_erasures_list_head->free_list_head = prev_free_list_index; - - if( prev_free_list_index != std::numeric_limits::max() ) { - *( reinterpret_cast( new_location.group_pointer->elements + - prev_free_list_index ) + 1 ) = std::numeric_limits::max(); - } else { - groups_with_erasures_list_head = groups_with_erasures_list_head->erasures_list_next_group; - } - } - - *( new_location.skipfield_pointer ) = 0; - ++( new_location.group_pointer->number_of_elements ); - - if( new_location.group_pointer == begin_iterator.group_pointer && - new_location.element_pointer < begin_iterator.element_pointer ) { - begin_iterator = new_location; - } - - ++total_number_of_elements; - - return new_location; - } - } - } else { - initialize( pointer_allocator_pair.min_elements_per_group ); - - if COLONY_CONSTEXPR( std::is_nothrow_constructible::value ) { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer++ ), - std::forward( parameters ) ... ); - } else { - try { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer++ ), - std::forward( parameters ) ... ); - } catch( ... ) { - clear(); - throw; - } - } - - ++end_iterator.skipfield_pointer; - total_number_of_elements = 1; - return begin_iterator; - } - } - - private: - - // Internal functions for fill insert: - void group_create( const skipfield_type number_of_elements ) { - const group_pointer_type next_group = end_iterator.group_pointer->next_group = COLONY_ALLOCATE( - group_allocator_type, group_allocator_pair, 1, end_iterator.group_pointer ); - - try { - COLONY_CONSTRUCT( group_allocator_type, group_allocator_pair, next_group, number_of_elements, - end_iterator.group_pointer ); - } catch( ... ) { - COLONY_DESTROY( group_allocator_type, group_allocator_pair, next_group ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, next_group, 1 ); - end_iterator.group_pointer->next_group = nullptr; - throw; - } - - end_iterator.group_pointer = next_group; - end_iterator.element_pointer = next_group->elements; - // group constructor sets this to 1 by default to allow for faster insertion during insertion/emplace in other cases - next_group->number_of_elements = 0; - total_capacity += number_of_elements; - } - - void group_fill( const element_type &element, const skipfield_type number_of_elements ) { - // ie. we can get away with using the cheaper fill_n here if there is no chance of an exception being thrown: - if COLONY_CONSTEXPR( std::is_trivially_copyable::value && - std::is_trivially_copy_constructible::value && - std::is_nothrow_copy_constructible::value ) { - if COLONY_CONSTEXPR( sizeof( aligned_element_type ) == sizeof( element_type ) ) { - std::fill_n( reinterpret_cast( end_iterator.element_pointer ), number_of_elements, - element ); - } else { - // to avoid potentially violating memory boundaries in line below, create an initial copy object of same (but aligned) type - alignas( sizeof( aligned_element_type ) ) element_type aligned_copy = element; - std::fill_n( end_iterator.element_pointer, number_of_elements, - *( reinterpret_cast( &aligned_copy ) ) ); - } - - end_iterator.element_pointer += number_of_elements; - } else { - const aligned_pointer_type fill_end = end_iterator.element_pointer + number_of_elements; - - do { - try { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), - reinterpret_cast( end_iterator.element_pointer++ ), element ); - } catch( ... ) { - end_iterator.group_pointer->last_endpoint = --end_iterator.element_pointer; - const skipfield_type elements_constructed_before_exception = static_cast - ( end_iterator.element_pointer - end_iterator.group_pointer->elements ); - end_iterator.group_pointer->number_of_elements = elements_constructed_before_exception; - end_iterator.skipfield_pointer = end_iterator.group_pointer->skipfield + - elements_constructed_before_exception; - throw; - } - } while( end_iterator.element_pointer != fill_end ); - } - - end_iterator.group_pointer->last_endpoint = end_iterator.element_pointer; - end_iterator.group_pointer->number_of_elements += number_of_elements; - } - - void fill_skipblock( const element_type &element, aligned_pointer_type const location, - skipfield_pointer_type const skipfield_pointer, const skipfield_type number_of_elements ) { - // ie. we can get away with using the cheaper fill_n here if there is no chance of an exception being thrown: - if COLONY_CONSTEXPR( std::is_trivially_copyable::value && - std::is_trivially_copy_constructible::value && - std::is_nothrow_copy_constructible::value ) { - if COLONY_CONSTEXPR( sizeof( aligned_element_type ) == sizeof( element_type ) ) { - std::fill_n( reinterpret_cast( location ), number_of_elements, element ); - } else { - // to avoid potentially violating memory boundaries in line below, create an initial copy object of same (but aligned) type - alignas( sizeof( aligned_element_type ) ) element_type aligned_copy = element; - std::fill_n( location, number_of_elements, - *( reinterpret_cast( &aligned_copy ) ) ); - } - } else { - // in case of exception, grabbing indexes before free_list node is reused - const skipfield_type prev_free_list_node = *( reinterpret_cast - ( location ) ); - const aligned_pointer_type fill_end = location + number_of_elements; - - for( aligned_pointer_type current_location = location; current_location != fill_end; - ++current_location ) { - try { - COLONY_CONSTRUCT( element_allocator_type, ( *this ), reinterpret_cast( current_location ), - element ); - } catch( ... ) { - // Reconstruct existing skipblock and free-list indexes to reflect partially-reused skipblock: - const skipfield_type elements_constructed_before_exception = static_cast( ( - current_location - 1 ) - location ); - groups_with_erasures_list_head->number_of_elements += elements_constructed_before_exception; - total_number_of_elements += elements_constructed_before_exception; - - std::memset( skipfield_pointer, 0, - elements_constructed_before_exception * sizeof( skipfield_type ) ); - - *( reinterpret_cast( location + elements_constructed_before_exception ) ) = - prev_free_list_node; - *( reinterpret_cast( location + elements_constructed_before_exception ) + - 1 ) = std::numeric_limits::max(); - - const skipfield_type new_skipblock_head_index = static_cast - ( location - groups_with_erasures_list_head->elements ) + elements_constructed_before_exception; - groups_with_erasures_list_head->free_list_head = new_skipblock_head_index; - - if( prev_free_list_node != std::numeric_limits::max() ) { - *( reinterpret_cast( groups_with_erasures_list_head->elements + - prev_free_list_node ) + 1 ) = new_skipblock_head_index; - } - - throw; - } - } - } - - // reset skipfield nodes within skipblock to 0 - std::memset( skipfield_pointer, 0, number_of_elements * sizeof( skipfield_type ) ); - groups_with_erasures_list_head->number_of_elements += number_of_elements; - total_number_of_elements += number_of_elements; - } - - public: - - /** - * Fill insert: - * Inserts n copies of val into the colony. Will insert the element into a previously erased - * element slot if one exists, otherwise will insert to back of colony. - */ - void insert( size_type number_of_elements, const element_type &element ) { - if( number_of_elements == 0 ) { - return; - } else if( number_of_elements == 1 ) { - insert( element ); - return; - } - - if( begin_iterator.group_pointer == nullptr ) { // Empty colony, no groups created yet - initialize( ( number_of_elements > group_allocator_pair.max_elements_per_group ) ? - group_allocator_pair.max_elements_per_group : ( number_of_elements < - pointer_allocator_pair.min_elements_per_group ) ? pointer_allocator_pair.min_elements_per_group : - static_cast( number_of_elements ) ); // Construct first group - begin_iterator.group_pointer->number_of_elements = 0; - } - - // ie. not an uninitialized colony or a situation where reserve has been called - if( total_number_of_elements != 0 ) { - // Use up erased locations if available: - if( groups_with_erasures_list_head != nullptr ) { - do { // skipblock loop: breaks when group is exhausted of reusable skipblocks, or returns if number_of_elements == 0 - aligned_pointer_type const element_pointer = groups_with_erasures_list_head->elements + - groups_with_erasures_list_head->free_list_head; - skipfield_pointer_type const skipfield_pointer = groups_with_erasures_list_head->skipfield + - groups_with_erasures_list_head->free_list_head; - const skipfield_type skipblock_size = *skipfield_pointer; - - if( groups_with_erasures_list_head == begin_iterator.group_pointer && - element_pointer < begin_iterator.element_pointer ) { - begin_iterator.element_pointer = element_pointer; - begin_iterator.skipfield_pointer = skipfield_pointer; - } - - if( skipblock_size <= number_of_elements ) { - // set free list head to previous free list node - groups_with_erasures_list_head->free_list_head = *( reinterpret_cast - ( element_pointer ) ); - fill_skipblock( element, element_pointer, skipfield_pointer, skipblock_size ); - number_of_elements -= skipblock_size; - - if( groups_with_erasures_list_head->free_list_head != std::numeric_limits::max() ) { - // set 'next' index of new free list head to 'end' (numeric max) - *( reinterpret_cast( groups_with_erasures_list_head->elements + - groups_with_erasures_list_head->free_list_head ) + 1 ) = std::numeric_limits::max(); - } else { - // change groups - groups_with_erasures_list_head = groups_with_erasures_list_head->erasures_list_next_group; - - if( groups_with_erasures_list_head == nullptr ) { - break; - } - } - } else { // skipblock is larger than remaining number of elements - // save before element location is overwritten - const skipfield_type prev_index = *( reinterpret_cast( element_pointer ) ); - fill_skipblock( element, element_pointer, skipfield_pointer, - static_cast( number_of_elements ) ); - const skipfield_type new_skipblock_size = static_cast( skipblock_size - - number_of_elements ); - - // Update skipfield (earlier nodes already memset'd in fill_skipblock function): - *( skipfield_pointer + number_of_elements ) = new_skipblock_size; - *( skipfield_pointer + skipblock_size - 1 ) = new_skipblock_size; - // set free list head to new start node - groups_with_erasures_list_head->free_list_head += static_cast( number_of_elements ); - - // Update free list with new head: - *( reinterpret_cast( element_pointer + number_of_elements ) ) = prev_index; - *( reinterpret_cast( element_pointer + number_of_elements ) + 1 ) = - std::numeric_limits::max(); - - if( prev_index != std::numeric_limits::max() ) { - // set 'next' index of previous skipblock to new start of skipblock - *( reinterpret_cast( groups_with_erasures_list_head->elements + prev_index ) - + 1 ) = groups_with_erasures_list_head->free_list_head; - } - - return; - } - } while( number_of_elements != 0 ); - } - - // Use up remaining available element locations in end group: - const skipfield_type group_remainder = ( static_cast - ( reinterpret_cast( end_iterator.group_pointer->skipfield ) - - end_iterator.element_pointer ) > number_of_elements ) ? static_cast - ( number_of_elements ) : static_cast( reinterpret_cast - ( end_iterator.group_pointer->skipfield ) - end_iterator.element_pointer ); - - if( group_remainder != 0 ) { - group_fill( element, group_remainder ); - total_number_of_elements += group_remainder; - number_of_elements -= group_remainder; - } - } else if( end_iterator.group_pointer->capacity >= number_of_elements ) { - group_fill( element, static_cast( number_of_elements ) ); - end_iterator.skipfield_pointer = end_iterator.group_pointer->skipfield + number_of_elements; - total_number_of_elements = number_of_elements; - return; - } else { - group_fill( element, end_iterator.group_pointer->capacity ); - total_number_of_elements = end_iterator.group_pointer->capacity; - number_of_elements -= end_iterator.group_pointer->capacity; - } - - // If there's some elements left that need to be created, create new groups and fill: - if( number_of_elements > group_allocator_pair.max_elements_per_group ) { - size_type multiples = number_of_elements / static_cast - ( group_allocator_pair.max_elements_per_group ); - const skipfield_type element_remainder = static_cast( number_of_elements - - ( multiples * static_cast( group_allocator_pair.max_elements_per_group ) ) ); - - while( multiples-- != 0 ) { - group_create( group_allocator_pair.max_elements_per_group ); - group_fill( element, group_allocator_pair.max_elements_per_group ); - } - - if( element_remainder != 0 ) { - group_create( group_allocator_pair.max_elements_per_group ); - group_fill( element, element_remainder ); - } - } else if( number_of_elements != 0 ) { - group_create( static_cast( ( number_of_elements > total_number_of_elements ) ? - number_of_elements : total_number_of_elements ) ); - group_fill( element, static_cast( number_of_elements ) ); - } - - // Adds the remainder from the last if-block - the insert functions in the first if/else block will already have incremented total_number_of_elements - total_number_of_elements += number_of_elements; - end_iterator.skipfield_pointer = end_iterator.group_pointer->skipfield + - ( end_iterator.element_pointer - end_iterator.group_pointer->elements ); - } - - /** - * Range insert: - * Inserts a series of value_type elements from an external source into a colony holding the - * same value_type (eg. int, float, a particular class, etcetera). Stops inserting once it - * reaches last. - */ - template - inline void insert( typename enable_if_c < !std::numeric_limits::is_integer, - iterator_type >::type first, const iterator_type last ) { - while( first != last ) { - insert( *first++ ); - } - } - - /** - * Initializer-list insert: - * Copies elements from an initializer list into the colony. Will insert the element in a - * previously erased element slot if one exists, otherwise will insert to back of colony. - */ - inline void insert( const std::initializer_list &element_list ) { - // use range insert: - insert( element_list.begin(), element_list.end() ); - } - - private: - - inline COLONY_FORCE_INLINE void update_subsequent_group_numbers( group_pointer_type current_group ) - noexcept { - do { - --( current_group->group_number ); - current_group = current_group->next_group; - } while( current_group != nullptr ); - } - - // get all elements contiguous in memory and shrink to fit, remove erasures and erasure free lists - inline COLONY_FORCE_INLINE void consolidate() { - colony temp; - // Make first allocated group as large total number of elements, where possible - temp.change_group_sizes( static_cast( ( - pointer_allocator_pair.min_elements_per_group > total_number_of_elements ) ? - pointer_allocator_pair.min_elements_per_group : ( ( total_number_of_elements > - group_allocator_pair.max_elements_per_group ) ? group_allocator_pair.max_elements_per_group : - total_number_of_elements ) ), - group_allocator_pair.max_elements_per_group ); - - if COLONY_CONSTEXPR( std::is_move_assignable::value && - std::is_move_constructible::value ) { - temp.insert( std::make_move_iterator( begin_iterator ), std::make_move_iterator( end_iterator ) ); - } else { - temp.insert( begin_iterator, end_iterator ); - } - // reset to correct value for future clear() or erasures - temp.pointer_allocator_pair.min_elements_per_group = pointer_allocator_pair.min_elements_per_group; - // Avoid generating 2nd temporary - *this = std::move( temp ); - } - - void remove_from_groups_with_erasures_list( const group_pointer_type group_to_remove ) noexcept { - if( group_to_remove == groups_with_erasures_list_head ) { - groups_with_erasures_list_head = groups_with_erasures_list_head->erasures_list_next_group; - return; - } - - group_pointer_type previous_group = groups_with_erasures_list_head; - group_pointer_type current_group = groups_with_erasures_list_head->erasures_list_next_group; - - while( group_to_remove != current_group ) { - previous_group = current_group; - current_group = current_group->erasures_list_next_group; - } - - previous_group->erasures_list_next_group = current_group->erasures_list_next_group; - } - - public: - - /** - * Removes the element pointed to by the supplied iterator, from the colony. Returns an - * iterator pointing to the next non-erased element in the colony (or to end() if no more - * elements are available). This must return an iterator because if a colony group becomes - * entirely empty, it will be removed from the colony, invalidating the existing iterator. - * Attempting to erase a previously-erased element results in undefined behavior (this is - * checked for via an assert in debug mode). - * - * must return iterator to subsequent non-erased element (or end()), in case the group - * containing the element which the iterator points to becomes empty after the erasure, and - * is thereafter removed from the colony chain, making the current iterator invalid and - * unusable in a ++ operation: - * - * if uninitialized/invalid iterator supplied, function could generate an exception - */ - iterator erase( const const_iterator &it ) { - cata_assert( !empty() ); - const group_pointer_type group_pointer = it.group_pointer; - // not uninitialized iterator - cata_assert( group_pointer != nullptr ); - // != end() - cata_assert( it.element_pointer != group_pointer->last_endpoint ); - // element pointed to by iterator has not been erased previously - cata_assert( *( it.skipfield_pointer ) == 0 ); - - // This if-statement should be removed by the compiler on resolution of element_type. For some optimizing compilers this step won't be necessary (for MSVC 2013 it makes a difference) - if COLONY_CONSTEXPR( !std::is_trivially_destructible::value ) { - COLONY_DESTROY( element_allocator_type, ( *this ), - reinterpret_cast( it.element_pointer ) ); // Destruct element - } - - --total_number_of_elements; - - // ie. non-empty group at this point in time, don't consolidate - optimization note: GCC optimizes postfix + 1 comparison better than prefix + 1 comparison in many cases. - if( group_pointer->number_of_elements-- != 1 ) { - // Code logic for following section: - // --------------------------------- - // If current skipfield node has no skipped node on either side, continue as usual - // If node only has skipped node on left, set current node and start node of the skipblock to left node value + 1. - // If node only has skipped node on right, make this node the start node of the skipblock and update end node - // If node has skipped nodes on left and right, set start node of left skipblock and end node of right skipblock to the values of the left + right nodes + 1 - - // Optimization explanation: - // The contextual logic below is the same as that in the insert() functions but in this case the value of the current skipfield node will always be - // zero (since it is not yet erased), meaning no additional manipulations are necessary for the previous skipfield node comparison - we only have to check against zero - const char prev_skipfield = *( it.skipfield_pointer - ( it.skipfield_pointer != - group_pointer->skipfield ) ) != 0; - // NOTE: boundary test (checking against end-of-elements) is able to be skipped due to the extra skipfield node (compared to element field) - which is present to enable faster iterator operator ++ operations - const char after_skipfield = *( it.skipfield_pointer + 1 ) != 0; - skipfield_type update_value = 1; - - switch( ( after_skipfield << 1 ) | prev_skipfield ) { - case 0: { // no consecutive erased elements - *it.skipfield_pointer = 1; // solo skipped node - const skipfield_type index = static_cast( it.element_pointer - - group_pointer->elements ); - - // ie. if this group already has some erased elements - if( group_pointer->free_list_head != std::numeric_limits::max() ) { - // set prev free list head's 'next index' number to the index of the current element - *( reinterpret_cast( group_pointer->elements + - group_pointer->free_list_head ) + 1 ) = index; - } else { - // add it to the groups-with-erasures free list - group_pointer->erasures_list_next_group = groups_with_erasures_list_head; - groups_with_erasures_list_head = group_pointer; - } - - *( reinterpret_cast( it.element_pointer ) ) = group_pointer->free_list_head; - *( reinterpret_cast( it.element_pointer ) + 1 ) = - std::numeric_limits::max(); - group_pointer->free_list_head = index; - break; - } - case 1: { // previous erased consecutive elements, none following - *( it.skipfield_pointer - * ( it.skipfield_pointer - 1 ) ) = *it.skipfield_pointer = * - ( it.skipfield_pointer - 1 ) + 1; - break; - } - case 2: { // following erased consecutive elements, none preceding - const skipfield_type following_value = *( it.skipfield_pointer + 1 ) + 1; - *( it.skipfield_pointer + following_value - 1 ) = *( it.skipfield_pointer ) = following_value; - - const skipfield_type following_previous = *( reinterpret_cast - ( it.element_pointer + 1 ) ); - const skipfield_type following_next = *( reinterpret_cast - ( it.element_pointer + 1 ) + 1 ); - *( reinterpret_cast( it.element_pointer ) ) = following_previous; - *( reinterpret_cast( it.element_pointer ) + 1 ) = following_next; - - const skipfield_type index = static_cast( it.element_pointer - - group_pointer->elements ); - - if( following_previous != std::numeric_limits::max() ) { - // Set next index of previous free list node to this node's 'next' index - *( reinterpret_cast( group_pointer->elements + following_previous ) + 1 ) = - index; - } - - if( following_next != std::numeric_limits::max() ) { - // Set previous index of next free list node to this node's 'previous' index - *( reinterpret_cast( group_pointer->elements + following_next ) ) = index; - } else { - group_pointer->free_list_head = index; - } - - update_value = following_value; - break; - } - case 3: { // both preceding and following consecutive erased elements - const skipfield_type preceding_value = *( it.skipfield_pointer - 1 ); - const skipfield_type following_value = *( it.skipfield_pointer + 1 ) + 1; - - // Join the skipblocks - *( it.skipfield_pointer - preceding_value ) = *( it.skipfield_pointer + following_value - 1 ) = - preceding_value + following_value; - - // Remove the following skipblock's entry from the free list - const skipfield_type following_previous = *( reinterpret_cast - ( it.element_pointer + 1 ) ); - const skipfield_type following_next = *( reinterpret_cast - ( it.element_pointer + 1 ) + 1 ); - - if( following_previous != std::numeric_limits::max() ) { - // Set next index of previous free list node to this node's 'next' index - *( reinterpret_cast( group_pointer->elements + following_previous ) + 1 ) = - following_next; - } - - if( following_next != std::numeric_limits::max() ) { - // Set previous index of next free list node to this node's 'previous' index - *( reinterpret_cast( group_pointer->elements + following_next ) ) = - following_previous; - } else { - group_pointer->free_list_head = following_previous; - } - - update_value = following_value; - break; - } - } - - iterator return_iterator( it.group_pointer, it.element_pointer + update_value, - it.skipfield_pointer + update_value ); - return_iterator.check_for_end_of_group_and_progress(); - - // If original iterator was first element in colony, update it's value with the next non-erased element: - if( it.element_pointer == begin_iterator.element_pointer ) { - begin_iterator = return_iterator; - } - - return return_iterator; - } - - // else: group is empty, consolidate groups - switch( ( group_pointer->next_group != nullptr ) | ( ( group_pointer != - begin_iterator.group_pointer ) - << 1 ) ) { - case 0: { // ie. group_pointer == begin_iterator.group_pointer && group_pointer->next_group == NULL; only group in colony - // Reset skipfield and free list rather than clearing - leads to fewer allocations/deallocations: - // &* to avoid problems with non-trivial pointers. Although there is one more skipfield than group_pointer->capacity, capacity + 1 is not necessary here as the end skipfield is never written to after initialization - std::memset( &*( group_pointer->skipfield ), 0, - sizeof( skipfield_type ) * group_pointer->capacity ); - group_pointer->free_list_head = std::numeric_limits::max(); - groups_with_erasures_list_head = nullptr; - - // Reset begin and end iterators: - end_iterator.element_pointer = begin_iterator.element_pointer = group_pointer->last_endpoint = - group_pointer->elements; - end_iterator.skipfield_pointer = begin_iterator.skipfield_pointer = group_pointer->skipfield; - - return end_iterator; - } - case 1: { // ie. group_pointer == begin_iterator.group_pointer && group_pointer->next_group != NULL. Remove first group, change first group to next group - group_pointer->next_group->previous_group = nullptr; // Cut off this group from the chain - begin_iterator.group_pointer = group_pointer->next_group; // Make the next group the first group - - update_subsequent_group_numbers( begin_iterator.group_pointer ); - - // Erasures present within the group, ie. was part of the intrusive list of groups with erasures. - if( group_pointer->free_list_head != std::numeric_limits::max() ) { - remove_from_groups_with_erasures_list( group_pointer ); - } - - total_capacity -= group_pointer->capacity; - COLONY_DESTROY( group_allocator_type, group_allocator_pair, group_pointer ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, group_pointer, 1 ); - - // note: end iterator only needs to be changed if the deleted group was the final group in the chain ie. not in this case - // If the beginning index has been erased (ie. skipfield != 0), skip to next non-erased element - begin_iterator.element_pointer = begin_iterator.group_pointer->elements + * - ( begin_iterator.group_pointer->skipfield ); - begin_iterator.skipfield_pointer = begin_iterator.group_pointer->skipfield + * - ( begin_iterator.group_pointer->skipfield ); - - return begin_iterator; - } - case 3: { // this is a non-first group but not final group in chain: delete the group, then link previous group to the next group in the chain: - group_pointer->next_group->previous_group = group_pointer->previous_group; - // close the chain, removing this group from it - const group_pointer_type return_group = group_pointer->previous_group->next_group = - group_pointer->next_group; - - update_subsequent_group_numbers( return_group ); - - if( group_pointer->free_list_head != std::numeric_limits::max() ) { - remove_from_groups_with_erasures_list( group_pointer ); - } - - total_capacity -= group_pointer->capacity; - COLONY_DESTROY( group_allocator_type, group_allocator_pair, group_pointer ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, group_pointer, 1 ); - - // Return next group's first non-erased element: - return iterator( return_group, return_group->elements + * ( return_group->skipfield ), - return_group->skipfield + * ( return_group->skipfield ) ); - } - default: { // this is a non-first group and the final group in the chain - if( group_pointer->free_list_head != std::numeric_limits::max() ) { - remove_from_groups_with_erasures_list( group_pointer ); - } - - group_pointer->previous_group->next_group = nullptr; - // end iterator needs to be changed as element supplied was the back element of the colony - end_iterator.group_pointer = group_pointer->previous_group; - end_iterator.element_pointer = reinterpret_cast - ( end_iterator.group_pointer->skipfield ); - end_iterator.skipfield_pointer = end_iterator.group_pointer->skipfield + - end_iterator.group_pointer->capacity; - - total_capacity -= group_pointer->capacity; - COLONY_DESTROY( group_allocator_type, group_allocator_pair, group_pointer ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, group_pointer, 1 ); - - return end_iterator; - } - } - } - - /** - * Range erase: - * Erases all elements of a given colony from first to the element before the last iterator. - * This function is optimized for multiple consecutive erasures and will always be faster - * than sequential single-element erase calls in that scenario. - */ - void erase( const const_iterator &iterator1, const const_iterator &iterator2 ) { - // if uninitialized/invalid iterators supplied, function could generate an exception. If iterator1 > iterator2, behavior is undefined. - cata_assert( iterator1 <= iterator2 ); - - iterator current = iterator1; - - if( current.group_pointer != iterator2.group_pointer ) { - if( current.element_pointer != current.group_pointer->elements + * - ( current.group_pointer->skipfield ) ) { - // if iterator1 is not the first non-erased element in it's group - most common case - size_type number_of_group_erasures = 0; - - // Now update skipfield: - const aligned_pointer_type end = iterator1.group_pointer->last_endpoint; - - // Schema: first erase all non-erased elements until end of group & remove all skipblocks post-iterator1 from the free_list. Then, either update preceding skipblock or create new one: - - // if trivially-destructible, and no erasures in group, skip while loop below and just jump straight to the location - if( std::is_trivially_destructible::value & - ( current.group_pointer->free_list_head == std::numeric_limits::max() ) ) { - number_of_group_erasures += static_cast( end - current.element_pointer ); - } else { - while( current.element_pointer != end ) { - if( *current.skipfield_pointer == 0 ) { - if COLONY_CONSTEXPR( !std::is_trivially_destructible::value ) { - COLONY_DESTROY( element_allocator_type, ( *this ), - reinterpret_cast( current.element_pointer ) ); // Destruct element - } - - ++number_of_group_erasures; - ++current.element_pointer; - ++current.skipfield_pointer; - } else { // remove skipblock from group: - const skipfield_type prev_free_list_index = *( reinterpret_cast - ( current.element_pointer ) ); - const skipfield_type next_free_list_index = *( reinterpret_cast - ( current.element_pointer ) + 1 ); - - current.element_pointer += *( current.skipfield_pointer ); - current.skipfield_pointer += *( current.skipfield_pointer ); - - // if this is the last skipblock in the free list - if( next_free_list_index == std::numeric_limits::max() && - prev_free_list_index == std::numeric_limits::max() ) { - // remove group from list of free-list groups - will be added back in down below, but not worth optimizing for - remove_from_groups_with_erasures_list( iterator1.group_pointer ); - iterator1.group_pointer->free_list_head = std::numeric_limits::max(); - number_of_group_erasures += static_cast( end - current.element_pointer ); - - if COLONY_CONSTEXPR( !std::is_trivially_destructible::value ) { - // miniloop - avoid checking skipfield for rest of elements in group, as there are no more skipped elements now - while( current.element_pointer != end ) { - COLONY_DESTROY( element_allocator_type, ( *this ), - reinterpret_cast( current.element_pointer++ ) ); // Destruct element - } - } - - break; // end overall while loop - } else if( next_free_list_index == std::numeric_limits::max() ) { - // if this is the head of the free list - // make free list head equal to next free list node - current.group_pointer->free_list_head = prev_free_list_index; - *( reinterpret_cast( current.group_pointer->elements + - prev_free_list_index ) + 1 ) = std::numeric_limits::max(); - } else { // either a tail or middle free list node - *( reinterpret_cast( current.group_pointer->elements + - next_free_list_index ) ) = prev_free_list_index; - - if( prev_free_list_index != - std::numeric_limits::max() ) { // ie. not the tail free list node - *( reinterpret_cast( current.group_pointer->elements + - prev_free_list_index ) + 1 ) = next_free_list_index; - } - } - } - } - } - - const skipfield_type previous_node_value = *( iterator1.skipfield_pointer - 1 ); - const skipfield_type distance_to_end = static_cast( end - - iterator1.element_pointer ); - - if( previous_node_value == 0 ) { // no previous skipblock - *iterator1.skipfield_pointer = distance_to_end; - *( iterator1.skipfield_pointer + distance_to_end - 1 ) = distance_to_end; - - const skipfield_type index = static_cast( iterator1.element_pointer - - iterator1.group_pointer->elements ); - - if( iterator1.group_pointer->free_list_head != std::numeric_limits::max() ) { - // if this group already has some erased elements - // set prev free list head's 'next index' number to the index of the iterator1 element - *( reinterpret_cast( iterator1.group_pointer->elements + - iterator1.group_pointer->free_list_head ) + 1 ) = index; - } else { - // add it to the groups-with-erasures free list - iterator1.group_pointer->erasures_list_next_group = groups_with_erasures_list_head; - groups_with_erasures_list_head = iterator1.group_pointer; - } - - *( reinterpret_cast( iterator1.element_pointer ) ) = - iterator1.group_pointer->free_list_head; - *( reinterpret_cast( iterator1.element_pointer ) + 1 ) = - std::numeric_limits::max(); - iterator1.group_pointer->free_list_head = index; - } else { - // update previous skipblock, no need to update free list: - *( iterator1.skipfield_pointer - previous_node_value ) = *( iterator1.skipfield_pointer + - distance_to_end - 1 ) = previous_node_value + distance_to_end; - } - - iterator1.group_pointer->number_of_elements -= static_cast - ( number_of_group_erasures ); - total_number_of_elements -= number_of_group_erasures; - - current.group_pointer = current.group_pointer->next_group; - } - - // Intermediate groups: - const group_pointer_type previous_group = current.group_pointer->previous_group; - - while( current.group_pointer != iterator2.group_pointer ) { - if COLONY_CONSTEXPR( !std::is_trivially_destructible::value ) { - current.element_pointer = current.group_pointer->elements + *( current.group_pointer->skipfield ); - current.skipfield_pointer = current.group_pointer->skipfield + * - ( current.group_pointer->skipfield ); - const aligned_pointer_type end = current.group_pointer->last_endpoint; - - do { - COLONY_DESTROY( element_allocator_type, ( *this ), - reinterpret_cast( current.element_pointer ) ); // Destruct element - ++current.skipfield_pointer; - current.element_pointer += *current.skipfield_pointer + 1; - current.skipfield_pointer += *current.skipfield_pointer; - } while( current.element_pointer != end ); - } - - if( current.group_pointer->free_list_head != std::numeric_limits::max() ) { - remove_from_groups_with_erasures_list( current.group_pointer ); - } - - total_number_of_elements -= current.group_pointer->number_of_elements; - const group_pointer_type current_group = current.group_pointer; - current.group_pointer = current.group_pointer->next_group; - - total_capacity -= current_group->capacity; - COLONY_DESTROY( group_allocator_type, group_allocator_pair, current_group ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, current_group, 1 ); - } - - current.element_pointer = current.group_pointer->elements + *( current.group_pointer->skipfield ); - current.skipfield_pointer = current.group_pointer->skipfield + * - ( current.group_pointer->skipfield ); - current.group_pointer->previous_group = previous_group; - - if( previous_group != nullptr ) { - previous_group->next_group = current.group_pointer; - } else { - // This line is included here primarily to avoid a secondary if statement within the if block below - it will not be needed in any other situation - begin_iterator = iterator2; - } - } - - // in case iterator2 was at beginning of it's group - also covers empty range case (first == last) - if( current.element_pointer == iterator2.element_pointer ) { - return; - } - - // Final group: - // Code explanation: - // If not erasing entire final group, 1. Destruct elements (if non-trivial destructor) and add locations to group free list. 2. process skipfield. - // If erasing entire group, 1. Destruct elements (if non-trivial destructor), 2. if no elements left in colony, clear() 3. otherwise reset end_iterator and remove group from groups-with-erasures list (if free list of erasures present) - if( iterator2.element_pointer != end_iterator.element_pointer || - current.element_pointer != current.group_pointer->elements + * - ( current.group_pointer->skipfield ) ) { // ie. not erasing entire group - size_type number_of_group_erasures = 0; - // Schema: first erased all non-erased elements until end of group & remove all skipblocks post-iterator2 from the free_list. Then, either update preceding skipblock or create new one: - - const iterator current_saved = current; - - // if trivially-destructible, and no erasures in group, skip while loop below and just jump straight to the location - if( std::is_trivially_destructible::value & - ( current.group_pointer->free_list_head == std::numeric_limits::max() ) ) { - number_of_group_erasures += static_cast( iterator2.element_pointer - - current.element_pointer ); - } else { - while( current.element_pointer != iterator2.element_pointer ) { - if( *current.skipfield_pointer == 0 ) { - if COLONY_CONSTEXPR( !std::is_trivially_destructible::value ) { - COLONY_DESTROY( element_allocator_type, ( *this ), - reinterpret_cast( current.element_pointer ) ); // Destruct element - } - - ++number_of_group_erasures; - ++current.element_pointer; - ++current.skipfield_pointer; - } else { // remove skipblock from group: - const skipfield_type prev_free_list_index = *( reinterpret_cast - ( current.element_pointer ) ); - const skipfield_type next_free_list_index = *( reinterpret_cast - ( current.element_pointer ) + 1 ); - - current.element_pointer += *( current.skipfield_pointer ); - current.skipfield_pointer += *( current.skipfield_pointer ); - - if( next_free_list_index == std::numeric_limits::max() && - prev_free_list_index == std::numeric_limits::max() ) { - // if this is the last skipblock in the free list - // remove group from list of free-list groups - will be added back in down below, but not worth optimizing for - remove_from_groups_with_erasures_list( iterator2.group_pointer ); - iterator2.group_pointer->free_list_head = std::numeric_limits::max(); - number_of_group_erasures += static_cast( iterator2.element_pointer - - current.element_pointer ); - - if COLONY_CONSTEXPR( !std::is_trivially_destructible::value ) { - while( current.element_pointer != iterator2.element_pointer ) { - COLONY_DESTROY( element_allocator_type, ( *this ), - reinterpret_cast( current.element_pointer++ ) ); // Destruct element - } - } - - break; // end overall while loop - } else if( next_free_list_index == - std::numeric_limits::max() ) { // if this is the head of the free list - current.group_pointer->free_list_head = prev_free_list_index; - *( reinterpret_cast( current.group_pointer->elements + - prev_free_list_index ) + 1 ) = std::numeric_limits::max(); - } else { - *( reinterpret_cast( current.group_pointer->elements + - next_free_list_index ) ) = prev_free_list_index; - - if( prev_free_list_index != - std::numeric_limits::max() ) { // ie. not the tail free list node - *( reinterpret_cast( current.group_pointer->elements + - prev_free_list_index ) + 1 ) = next_free_list_index; - } - } - } - } - } - - const skipfield_type distance_to_iterator2 = static_cast - ( iterator2.element_pointer - current_saved.element_pointer ); - const skipfield_type index = static_cast( current_saved.element_pointer - - iterator2.group_pointer->elements ); - - if( index == 0 || *( current_saved.skipfield_pointer - 1 ) == 0 ) { - // element is either at start of group or previous skipfield node is 0 - *( current_saved.skipfield_pointer ) = distance_to_iterator2; - *( iterator2.skipfield_pointer - 1 ) = distance_to_iterator2; - - if( iterator2.group_pointer->free_list_head != std::numeric_limits::max() ) { - // if this group already has some erased elements - *( reinterpret_cast( iterator2.group_pointer->elements + - iterator2.group_pointer->free_list_head ) + 1 ) = index; - } else { - // add it to the groups-with-erasures free list - iterator2.group_pointer->erasures_list_next_group = groups_with_erasures_list_head; - groups_with_erasures_list_head = iterator2.group_pointer; - } - - *( reinterpret_cast( current_saved.element_pointer ) ) = - iterator2.group_pointer->free_list_head; - *( reinterpret_cast( current_saved.element_pointer ) + 1 ) = - std::numeric_limits::max(); - iterator2.group_pointer->free_list_head = index; - } else { // If iterator 1 & 2 are in same group, but iterator 1 was not at start of group, and previous skipfield node is an end node in a skipblock: - // Just update existing skipblock, no need to create new free list node: - const skipfield_type prev_node_value = *( current_saved.skipfield_pointer - 1 ); - *( current_saved.skipfield_pointer - prev_node_value ) = prev_node_value + distance_to_iterator2; - *( iterator2.skipfield_pointer - 1 ) = prev_node_value + distance_to_iterator2; - } - - if( iterator1.element_pointer == begin_iterator.element_pointer ) { - begin_iterator = iterator2; - } - - iterator2.group_pointer->number_of_elements -= static_cast - ( number_of_group_erasures ); - total_number_of_elements -= number_of_group_erasures; - } else { // ie. full group erasure - if COLONY_CONSTEXPR( !std::is_trivially_destructible::value ) { - while( current.element_pointer != iterator2.element_pointer ) { - COLONY_DESTROY( element_allocator_type, ( *this ), - reinterpret_cast( current.element_pointer ) ); - ++current.skipfield_pointer; - current.element_pointer += 1 + *current.skipfield_pointer; - current.skipfield_pointer += *current.skipfield_pointer; - } - } - - if( ( total_number_of_elements -= current.group_pointer->number_of_elements ) != 0 ) { - // ie. previous_group != NULL - current.group_pointer->previous_group->next_group = current.group_pointer->next_group; - end_iterator.group_pointer = current.group_pointer->previous_group; - end_iterator.element_pointer = current.group_pointer->previous_group->last_endpoint; - end_iterator.skipfield_pointer = current.group_pointer->previous_group->skipfield + - current.group_pointer->previous_group->capacity; - total_capacity -= current.group_pointer->capacity; - - if( current.group_pointer->free_list_head != std::numeric_limits::max() ) { - remove_from_groups_with_erasures_list( current.group_pointer ); - } - } else { // ie. colony is now empty - blank(); - } - - COLONY_DESTROY( group_allocator_type, group_allocator_pair, current.group_pointer ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, current.group_pointer, 1 ); - } - } - - /** Returns a boolean indicating whether the colony is currently empty of elements. */ - inline COLONY_FORCE_INLINE bool empty() const noexcept { - return total_number_of_elements == 0; - } - - /** Returns total number of elements currently stored in container. */ - inline size_type size() const noexcept { - return total_number_of_elements; - } - - /** Returns the maximum number of elements that the allocator can store in the container. */ - inline size_type max_size() const noexcept { - return std::allocator_traits::max_size( *this ); - } - - /** Returns total number of elements currently able to be stored in container without expansion. */ - inline size_type capacity() const noexcept { - return total_capacity; - } - - /** - * Returns the approximate memory use of the container plus it's elements. Will be - * inaccurate if the elements themselves dynamically-allocate data. Utility function - * primarily for benchmarking. - */ - inline size_type approximate_memory_use() const noexcept { - return - sizeof( *this ) + // sizeof colony basic structure - ( total_capacity * ( sizeof( aligned_element_type ) + sizeof( skipfield_type ) ) ) - + // sizeof current colony data capacity + skipfields - ( ( end_iterator.group_pointer == NULL ) ? 0 : ( ( end_iterator.group_pointer->group_number + 1 ) - * ( sizeof( group ) + sizeof( - skipfield_type ) ) ) ); // if colony not empty, add the memory usage of the group structures themselves, adding the extra skipfield node - } - - /** - * Changes the minimum and maximum internal group sizes, in terms of number of elements - * stored per group. If the colony is not empty and either min_group_size is larger than the - * smallest group in the colony, or max_group_size is smaller than the largest group in the - * colony, the colony will be internally copy-constructed into a new colony which uses the - * new group sizes, invalidating all pointers/iterators/references. If trying to change - * group sizes with a colony storing a non-copyable/movable type, please use the - * reinitialize function instead. - */ - void change_group_sizes( const skipfield_type min_allocation_amount, - const skipfield_type max_allocation_amount ) { - cata_assert( ( min_allocation_amount > 2 ) & ( min_allocation_amount <= max_allocation_amount ) ); - - pointer_allocator_pair.min_elements_per_group = min_allocation_amount; - group_allocator_pair.max_elements_per_group = max_allocation_amount; - - if( begin_iterator.group_pointer != nullptr && - ( begin_iterator.group_pointer->capacity < min_allocation_amount || - end_iterator.group_pointer->capacity > max_allocation_amount ) ) { - consolidate(); - } - } - - /** - * Changes the minimum internal group size only, in terms of minimum number of elements - * stored per group. If the colony is not empty and min_group_size is larger than the - * smallest group in the colony, the colony will be internally move-constructed (if possible) - * or copy-constructed into a new colony which uses the new minimum group size, invalidating - * all pointers/iterators/references. If trying to change group sizes with a colony storing - * a non-copyable/movable type, please use the reinitialize function instead. - */ - inline void change_minimum_group_size( const skipfield_type min_allocation_amount ) { - change_group_sizes( min_allocation_amount, group_allocator_pair.max_elements_per_group ); - } - - /** - * Changes the maximum internal group size only, in terms of maximum number of elements - * stored per group. If the colony is not empty and either max_group_size is smaller than - * the largest group in the colony, the colony will be internally move-constructed (if - * possible) or copy-constructed into a new colony which uses the new maximum group size, - * invalidating all pointers/iterators/references. If trying to change group sizes with a - * colony storing a non-copyable/movable type, please use the reinitialize function instead. - */ - inline void change_maximum_group_size( const skipfield_type max_allocation_amount ) { - change_group_sizes( pointer_allocator_pair.min_elements_per_group, max_allocation_amount ); - } - - /** - * Sets @param minimum_group_size and @param maximum_group_size to the minimum and maximum - * group size respectively - */ - inline void get_group_sizes( skipfield_type &minimum_group_size, - skipfield_type &maximum_group_size ) const noexcept { - minimum_group_size = pointer_allocator_pair.min_elements_per_group; - maximum_group_size = group_allocator_pair.max_elements_per_group; - } - - /** - * Semantics of this function are the same as - * clear(); - * change_group_sizes(min_group_size, max_group_size); - * but without the move/copy-construction code of the change_group_sizes() function - this - * means it can be used with element types which are non-copy-constructible and - * non-move-constructible, unlike change_group_sizes(). - */ - inline void reinitialize( const skipfield_type min_allocation_amount, - const skipfield_type max_allocation_amount ) noexcept { - cata_assert( ( min_allocation_amount > 2 ) & ( min_allocation_amount <= max_allocation_amount ) ); - clear(); - pointer_allocator_pair.min_elements_per_group = min_allocation_amount; - group_allocator_pair.max_elements_per_group = max_allocation_amount; - } - - /** - * Empties the colony and removes all elements and groups. - */ - inline COLONY_FORCE_INLINE void clear() noexcept { - destroy_all_data(); - blank(); - } - - /** - * Copy assignment: - * Copy the elements from another colony to this colony, clearing this colony of existing - * elements first. - */ - inline colony &operator=( const colony &source ) { - cata_assert( &source != this ); - - destroy_all_data(); - colony temp( source ); - // Avoid generating 2nd temporary - *this = std::move( temp ); - - return *this; - } - - /** - * Move assignment: - * Move the elements from another colony to this colony, clearing this colony of existing - * elements first. Source colony is now empty and in a valid state (same as a new colony - * without any insertions), can be safely destructed or used in any regular way without - * problems. - */ - colony &operator=( colony &&source ) COLONY_NOEXCEPT_MOVE_ASSIGNMENT( allocator_type ) { - cata_assert( &source != this ); - destroy_all_data(); - - if COLONY_CONSTEXPR( std::is_trivial::value && - std::is_trivial::value && std::is_trivial::value ) { - // NOLINTNEXTLINE(bugprone-undefined-memory-manipulation) - std::memcpy( static_cast( this ), &source, sizeof( colony ) ); - } else { - end_iterator = std::move( source.end_iterator ); - begin_iterator = std::move( source.begin_iterator ); - groups_with_erasures_list_head = source.groups_with_erasures_list_head; - total_number_of_elements = source.total_number_of_elements; - total_capacity = source.total_capacity; - pointer_allocator_pair.min_elements_per_group = - source.pointer_allocator_pair.min_elements_per_group; - group_allocator_pair.max_elements_per_group = source.group_allocator_pair.max_elements_per_group; - } - - source.blank(); - return *this; - } - - /** Compare contents of another colony to this colony. Returns true if they are equal. */ - bool operator==( const colony &rh ) const noexcept { - cata_assert( this != &rh ); - - if( total_number_of_elements != rh.total_number_of_elements ) { - return false; - } - - for( iterator lh_iterator = begin_iterator, rh_iterator = rh.begin_iterator; - lh_iterator != end_iterator; ) { - if( *rh_iterator++ != *lh_iterator++ ) { - return false; - } - } - - return true; - } - - /** Compare contents of another colony to this colony. Returns true if they are unequal. */ - inline bool operator!=( const colony &rh ) const noexcept { - return !( *this == rh ); - } - - /** - * Reduces container capacity to the amount necessary to store all currently stored - * elements, consolidates elements and removes any erased locations. If the total number of - * elements is larger than the maximum group size, the resultant capacity will be equal to - * ((total_elements / max_group_size) + 1) * max_group_size (rounding down at division). - * Invalidates all pointers, iterators and references to elements within the container. - */ - void shrink_to_fit() { - if( total_number_of_elements == total_capacity ) { - return; - } else if( total_number_of_elements == 0 ) { // Edge case - clear(); - return; - } - - consolidate(); - } - - /** - * Preallocates memory space sufficient to store the number of elements indicated by - * reserve_amount. The maximum size for this number is currently limited to the maximum - * group size of the colony and will be rounded down if necessary. The default maximum group - * size is 65535 on the majority of platforms. The default minimum reserve amount is the - * same as the current minimum group size, and will be rounded up silently if necessary. - * This function is useful from a performance perspective when the user is inserting - * elements singly, but the overall number of insertions is known in advance. By reserving, - * colony can forgo creating many smaller memory block allocations (due to colony's growth - * factor) and reserve a single memory block instead. Alternatively one could simply change - * the default group sizes. - */ - void reserve( const size_type original_reserve_amount ) { - if( original_reserve_amount == 0 || original_reserve_amount <= total_capacity ) { - // Already have enough space allocated - return; - } - - skipfield_type reserve_amount; - - if( original_reserve_amount > static_cast - ( group_allocator_pair.max_elements_per_group ) ) { - reserve_amount = group_allocator_pair.max_elements_per_group; - } else if( original_reserve_amount < static_cast - ( pointer_allocator_pair.min_elements_per_group ) ) { - reserve_amount = pointer_allocator_pair.min_elements_per_group; - } else if( original_reserve_amount > max_size() ) { - reserve_amount = static_cast( max_size() ); - } else { - reserve_amount = static_cast( original_reserve_amount ); - } - - if( total_number_of_elements == 0 ) { // Most common scenario - empty colony - if( begin_iterator.group_pointer != nullptr ) { - // Edge case - empty colony but first group is initialized ie. had some insertions but all elements got subsequently erased - COLONY_DESTROY( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer ); - COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, 1 ); - } // else: Empty colony, no insertions yet, time to allocate - - initialize( reserve_amount ); - // last_endpoint initially == elements + 1 via default constructor - begin_iterator.group_pointer->last_endpoint = begin_iterator.group_pointer->elements; - begin_iterator.group_pointer->number_of_elements = 0; // 1 by default - } else { // Non-empty colony, don't have enough space allocated - const skipfield_type original_min_elements = pointer_allocator_pair.min_elements_per_group; - // Make sure all groups are at maximum appropriate capacity (this amount already rounded down to a skipfield type earlier in function) - pointer_allocator_pair.min_elements_per_group = static_cast( reserve_amount ); - consolidate(); - pointer_allocator_pair.min_elements_per_group = original_min_elements; - } - } - - /** - * Increments/decrements the iterator supplied by the positive or negative amount indicated - * by distance. Speed of incrementation will almost always be faster than using the ++ - * operator on the iterator for increments greater than 1. In some cases it may approximate - * O(1). The iterator_type can be an iterator, const_iterator, reverse_iterator or - * const_reverse_iterator. - */ - // Advance implementation for iterator and const_iterator: - // Cannot be noexcept due to the possibility of an uninitialized iterator - template - void advance( colony_iterator &it, difference_type distance ) const { - // For code simplicity - should hopefully be optimized out by compiler: - group_pointer_type &group_pointer = it.group_pointer; - aligned_pointer_type &element_pointer = it.element_pointer; - skipfield_pointer_type &skipfield_pointer = it.skipfield_pointer; - - // covers uninitialized colony_iterator && empty group - cata_assert( group_pointer != nullptr ); - - // Now, run code based on the nature of the distance type - negative, positive or zero: - if( distance > 0 ) { // ie. += - // Code explanation: - // For the initial state of the iterator, we don't know how what elements have been erased before that element in that group. - // So for the first group, we follow the following logic: - // 1. If no elements have been erased in the group, we do simple addition to progress either to within the group (if the distance is small enough) or the end of the group and subtract from distance accordingly. - // 2. If any of the first group elements have been erased, we manually iterate, as we don't know whether the erased elements occur before or after the initial iterator position, and we subtract 1 from the distance amount each time. Iteration continues until either distance becomes zero, or we reach the end of the group. - - // For all subsequent groups, we follow this logic: - // 1. If distance is larger than the total number of non-erased elements in a group, we skip that group and subtract the number of elements in that group from distance - // 2. If distance is smaller than the total number of non-erased elements in a group, then: - // a. if there are no erased elements in the group we simply add distance to group->elements to find the new location for the iterator - // b. if there are erased elements in the group, we manually iterate and subtract 1 from distance on each iteration, until the new iterator location is found ie. distance = 0 - - // Note: incrementing element_pointer is avoided until necessary to avoid needless calculations - - // Check that we're not already at end() - cata_assert( !( element_pointer == group_pointer->last_endpoint && - group_pointer->next_group == nullptr ) ); - - // Special case for initial element pointer and initial group (we don't know how far into the group the element pointer is) - if( element_pointer != group_pointer->elements + * ( group_pointer->skipfield ) ) { - // ie. != first non-erased element in group - const difference_type distance_from_end = static_cast - ( group_pointer->last_endpoint - element_pointer ); - - if( group_pointer->number_of_elements == static_cast( distance_from_end ) ) { - // ie. if there are no erasures in the group (using endpoint - elements_start to determine number of elements in group just in case this is the last group of the colony, in which case group->last_endpoint != group->elements + group->capacity) - if( distance < distance_from_end ) { - element_pointer += distance; - skipfield_pointer += distance; - return; - } else if( group_pointer->next_group == nullptr ) { - // either we've reached end() or gone beyond it, so bound to end() - element_pointer = group_pointer->last_endpoint; - skipfield_pointer += distance_from_end; - return; - } else { - distance -= distance_from_end; - } - } else { - const skipfield_pointer_type endpoint = skipfield_pointer + distance_from_end; - - while( true ) { - ++skipfield_pointer; - skipfield_pointer += *skipfield_pointer; - --distance; - - if( skipfield_pointer == endpoint ) { - break; - } else if( distance == 0 ) { - element_pointer = group_pointer->elements + ( skipfield_pointer - group_pointer->skipfield ); - return; - } - } - - if( group_pointer->next_group == nullptr ) { - // either we've reached end() or gone beyond it, so bound to end() - element_pointer = group_pointer->last_endpoint; - return; - } - } - - group_pointer = group_pointer->next_group; - - if( distance == 0 ) { - element_pointer = group_pointer->elements + *( group_pointer->skipfield ); - skipfield_pointer = group_pointer->skipfield + *( group_pointer->skipfield ); - return; - } - } - - // Intermediary groups - at the start of this code block and the subsequent block, the position of the iterator is assumed to be the first non-erased element in the current group: - while( static_cast( group_pointer->number_of_elements ) <= distance ) { - if( group_pointer->next_group == nullptr ) { - // either we've reached end() or gone beyond it, so bound to end() - element_pointer = group_pointer->last_endpoint; - skipfield_pointer = group_pointer->skipfield + ( group_pointer->last_endpoint - - group_pointer->elements ); - return; - } else if( ( distance -= group_pointer->number_of_elements ) == 0 ) { - group_pointer = group_pointer->next_group; - element_pointer = group_pointer->elements + *( group_pointer->skipfield ); - skipfield_pointer = group_pointer->skipfield + *( group_pointer->skipfield ); - return; - } else { - group_pointer = group_pointer->next_group; - } - } - - // Final group (if not already reached): - if( group_pointer->free_list_head == std::numeric_limits::max() ) { - // No erasures in this group, use straight pointer addition - element_pointer = group_pointer->elements + distance; - skipfield_pointer = group_pointer->skipfield + distance; - return; - } else { // ie. number_of_elements > distance - safe to ignore endpoint check condition while incrementing: - skipfield_pointer = group_pointer->skipfield + *( group_pointer->skipfield ); - - do { - ++skipfield_pointer; - skipfield_pointer += *skipfield_pointer; - } while( --distance != 0 ); - - element_pointer = group_pointer->elements + ( skipfield_pointer - group_pointer->skipfield ); - return; - } - - return; - } else if( distance < 0 ) { // for negative change - // Code logic is very similar to += above - - // check that we're not already at begin() - cata_assert( !( ( element_pointer == group_pointer->elements + * ( group_pointer->skipfield ) ) && - group_pointer->previous_group == nullptr ) ); - distance = -distance; - - // Special case for initial element pointer and initial group (we don't know how far into the group the element pointer is) - if( element_pointer != group_pointer->last_endpoint ) { // ie. != end() - if( group_pointer->free_list_head == std::numeric_limits::max() ) { - // ie. no prior erasures have occurred in this group - const difference_type distance_from_beginning = static_cast - ( element_pointer - group_pointer->elements ); - - if( distance <= distance_from_beginning ) { - element_pointer -= distance; - skipfield_pointer -= distance; - return; - } else if( group_pointer->previous_group == nullptr ) { - // ie. we've gone before begin(), so bound to begin() - element_pointer = group_pointer->elements; - skipfield_pointer = group_pointer->skipfield; - return; - } else { - distance -= distance_from_beginning; - } - } else { - const skipfield_pointer_type beginning_point = group_pointer->skipfield + * - ( group_pointer->skipfield ); - - while( skipfield_pointer != beginning_point ) { - --skipfield_pointer; - skipfield_pointer -= *skipfield_pointer; - - if( --distance == 0 ) { - element_pointer = group_pointer->elements + ( skipfield_pointer - group_pointer->skipfield ); - return; - } - } - - if( group_pointer->previous_group == nullptr ) { - // This is first group, so bound to begin() (just in case final decrement took us before begin()) - element_pointer = group_pointer->elements + *( group_pointer->skipfield ); - skipfield_pointer = group_pointer->skipfield + *( group_pointer->skipfield ); - return; - } - } - - group_pointer = group_pointer->previous_group; - } - - // Intermediary groups - at the start of this code block and the subsequent block, the position of the iterator is assumed to be either the first non-erased element in the next group over, or end(): - while( static_cast( group_pointer->number_of_elements ) < distance ) { - if( group_pointer->previous_group == nullptr ) { - // we've gone beyond begin(), so bound to it - element_pointer = group_pointer->elements + *( group_pointer->skipfield ); - skipfield_pointer = group_pointer->skipfield + *( group_pointer->skipfield ); - return; - } - - distance -= group_pointer->number_of_elements; - group_pointer = group_pointer->previous_group; - } - - // Final group (if not already reached): - if( static_cast( group_pointer->number_of_elements ) == distance ) { - element_pointer = group_pointer->elements + *( group_pointer->skipfield ); - skipfield_pointer = group_pointer->skipfield + *( group_pointer->skipfield ); - return; - } else if( group_pointer->free_list_head == std::numeric_limits::max() ) { - // ie. no erased elements in this group - element_pointer = reinterpret_cast( group_pointer->skipfield ) - distance; - skipfield_pointer = ( group_pointer->skipfield + group_pointer->capacity ) - distance; - return; - } else { // ie. no more groups to traverse but there are erased elements in this group - skipfield_pointer = group_pointer->skipfield + group_pointer->capacity; - - do { - --skipfield_pointer; - skipfield_pointer -= *skipfield_pointer; - } while( --distance != 0 ); - - element_pointer = group_pointer->elements + ( skipfield_pointer - group_pointer->skipfield ); - return; - } - } - // Only distance == 0 reaches here - } - - // Advance for reverse_iterator and const_reverse_iterator - this needs to be implemented slightly differently to forward-iterator's advance, as it needs to be able to reach rend() (ie. begin() - 1) and to be bounded by rbegin(): - template - void advance( colony_reverse_iterator &reverse_it, - difference_type distance ) const { // could cause exception if iterator is uninitialized - group_pointer_type &group_pointer = reverse_it.it.group_pointer; - aligned_pointer_type &element_pointer = reverse_it.it.element_pointer; - skipfield_pointer_type &skipfield_pointer = reverse_it.it.skipfield_pointer; - - cata_assert( element_pointer != nullptr ); - - if( distance > 0 ) { - // Check that we're not already at rend() - cata_assert( !( element_pointer == group_pointer->elements - 1 && - group_pointer->previous_group == nullptr ) ); - // Special case for initial element pointer and initial group (we don't know how far into the group the element pointer is) - // Since a reverse_iterator cannot == last_endpoint (ie. before rbegin()) we don't need to check for that like with iterator - if( group_pointer->free_list_head == std::numeric_limits::max() ) { - difference_type distance_from_beginning = static_cast - ( element_pointer - group_pointer->elements ); - - if( distance <= distance_from_beginning ) { - element_pointer -= distance; - skipfield_pointer -= distance; - return; - } else if( group_pointer->previous_group == nullptr ) { - // Either we've reached rend() or gone beyond it, so bound to rend() - element_pointer = group_pointer->elements - 1; - skipfield_pointer = group_pointer->skipfield - 1; - return; - } else { - distance -= distance_from_beginning; - } - } else { - const skipfield_pointer_type beginning_point = group_pointer->skipfield + * - ( group_pointer->skipfield ); - - while( skipfield_pointer != beginning_point ) { - --skipfield_pointer; - skipfield_pointer -= *skipfield_pointer; - - if( --distance == 0 ) { - element_pointer = group_pointer->elements + ( skipfield_pointer - group_pointer->skipfield ); - return; - } - } - - if( group_pointer->previous_group == nullptr ) { - // If we've reached rend(), bound to that - element_pointer = group_pointer->elements - 1; - skipfield_pointer = group_pointer->skipfield - 1; - return; - } - } - - group_pointer = group_pointer->previous_group; - - // Intermediary groups - at the start of this code block and the subsequent block, the position of the iterator is assumed to be the first non-erased element in the next group: - while( static_cast( group_pointer->number_of_elements ) < distance ) { - if( group_pointer->previous_group == nullptr ) { // bound to rend() - element_pointer = group_pointer->elements - 1; - skipfield_pointer = group_pointer->skipfield - 1; - return; - } - - distance -= static_cast( group_pointer->number_of_elements ); - group_pointer = group_pointer->previous_group; - } - - // Final group (if not already reached) - if( static_cast( group_pointer->number_of_elements ) == distance ) { - element_pointer = group_pointer->elements + *( group_pointer->skipfield ); - skipfield_pointer = group_pointer->skipfield + *( group_pointer->skipfield ); - return; - } else if( group_pointer->free_list_head == std::numeric_limits::max() ) { - element_pointer = reinterpret_cast( group_pointer->skipfield ) - distance; - skipfield_pointer = ( group_pointer->skipfield + group_pointer->capacity ) - distance; - return; - } else { - skipfield_pointer = group_pointer->skipfield + group_pointer->capacity; - - do { - --skipfield_pointer; - skipfield_pointer -= *skipfield_pointer; - } while( --distance != 0 ); - - element_pointer = group_pointer->elements + ( skipfield_pointer - group_pointer->skipfield ); - return; - } - } else if( distance < 0 ) { - // Check that we're not already at rbegin() - cata_assert( !( ( element_pointer == ( group_pointer->last_endpoint - 1 ) - * - ( group_pointer->skipfield + ( group_pointer->last_endpoint - group_pointer->elements ) - 1 ) ) && - group_pointer->next_group == nullptr ) ); - - if( element_pointer != group_pointer->elements + * ( group_pointer->skipfield ) ) { - // ie. != first non-erased element in group - if( group_pointer->free_list_head == std::numeric_limits::max() ) { - // ie. if there are no erasures in the group - const difference_type distance_from_end = static_cast - ( group_pointer->last_endpoint - element_pointer ); - - if( distance < distance_from_end ) { - element_pointer += distance; - skipfield_pointer += distance; - return; - } else if( group_pointer->next_group == nullptr ) { // bound to rbegin() - // no erasures so we don't have to subtract skipfield value as we do below - element_pointer = group_pointer->last_endpoint - 1; - skipfield_pointer += distance_from_end - 1; - return; - } else { - distance -= distance_from_end; - } - } else { - const skipfield_pointer_type endpoint = skipfield_pointer + ( group_pointer->last_endpoint - - element_pointer ); - - while( true ) { - ++skipfield_pointer; - skipfield_pointer += *skipfield_pointer; - --distance; - - if( skipfield_pointer == endpoint ) { - break; - } else if( distance == 0 ) { - element_pointer = group_pointer->elements + ( skipfield_pointer - group_pointer->skipfield ); - return; - } - } - - if( group_pointer->next_group == nullptr ) { // bound to rbegin() - --skipfield_pointer; - element_pointer = ( group_pointer->last_endpoint - 1 ) - *skipfield_pointer; - skipfield_pointer -= *skipfield_pointer; - return; - } - } - - group_pointer = group_pointer->next_group; - - if( distance == 0 ) { - element_pointer = group_pointer->elements + *( group_pointer->skipfield ); - skipfield_pointer = group_pointer->skipfield + *( group_pointer->skipfield ); - return; - } - } - - // Intermediary groups - at the start of this code block and the subsequent block, the position of the iterator is assumed to be the first non-erased element in the current group, as a result of the previous code blocks: - while( static_cast( group_pointer->number_of_elements ) <= distance ) { - if( group_pointer->next_group == nullptr ) { // bound to rbegin() - skipfield_pointer = group_pointer->skipfield + ( group_pointer->last_endpoint - - group_pointer->elements ) - 1; - --skipfield_pointer; - element_pointer = ( group_pointer->last_endpoint - 1 ) - *skipfield_pointer; - skipfield_pointer -= *skipfield_pointer; - return; - } else if( ( distance -= group_pointer->number_of_elements ) == 0 ) { - group_pointer = group_pointer->next_group; - element_pointer = group_pointer->elements + *( group_pointer->skipfield ); - skipfield_pointer = group_pointer->skipfield + *( group_pointer->skipfield ); - return; - } else { - group_pointer = group_pointer->next_group; - } - } - - // Final group (if not already reached): - if( group_pointer->free_list_head == std::numeric_limits::max() ) { - // No erasures in this group, use straight pointer addition - element_pointer = group_pointer->elements + distance; - skipfield_pointer = group_pointer->skipfield + distance; - return; - } else { // ie. number_of_elements > distance - safe to ignore endpoint check condition while incrementing: - skipfield_pointer = group_pointer->skipfield + *( group_pointer->skipfield ); - - do { - ++skipfield_pointer; - skipfield_pointer += *skipfield_pointer; - } while( --distance != 0 ); - - element_pointer = group_pointer->elements + ( skipfield_pointer - group_pointer->skipfield ); - return; - } - - return; - } - } - - /** - * Creates a copy of the iterator supplied, then increments/decrements this iterator by - * the positive or negative amount indicated by distance. - */ - template - inline colony_iterator next( const colony_iterator &it, - const typename colony_iterator::difference_type distance = 1 ) const { - colony_iterator return_iterator( it ); - advance( return_iterator, distance ); - return return_iterator; - } - - /** - * Creates a copy of the iterator supplied, then increments/decrements this iterator by - * the positive or negative amount indicated by distance. - */ - template - inline colony_reverse_iterator next( const colony_reverse_iterator &it, - const typename colony_reverse_iterator::difference_type distance = 1 ) const { - colony_reverse_iterator return_iterator( it ); - advance( return_iterator, distance ); - return return_iterator; - } - - /** - * Creates a copy of the iterator supplied, then decrements/increments this iterator by the - * positive or negative amount indicated by distance. - */ - template - inline colony_iterator prev( const colony_iterator &it, - const typename colony_iterator::difference_type distance = 1 ) const { - colony_iterator return_iterator( it ); - advance( return_iterator, -distance ); - return return_iterator; - } - - /** - * Creates a copy of the iterator supplied, then decrements/increments this iterator by the - * positive or negative amount indicated by distance. - */ - template - inline colony_reverse_iterator prev( const colony_reverse_iterator &it, - const typename colony_reverse_iterator::difference_type distance = 1 ) const { - colony_reverse_iterator return_iterator( it ); - advance( return_iterator, -distance ); - return return_iterator; - } - - /** - * Measures the distance between two iterators, returning the result, which will be negative - * if the second iterator supplied is before the first iterator supplied in terms of its - * location in the colony. - */ - template - typename colony_iterator::difference_type distance( const colony_iterator - &first, const colony_iterator &last ) const { - // Code logic: - // If iterators are the same, return 0 - // Otherwise, find which iterator is later in colony, copy that to iterator2. Copy the lower to iterator1. - // If they are not pointing to elements in the same group, process the intermediate groups and add distances, - // skipping manual incrementation in all but the initial and final groups. - // In the initial and final groups, manual incrementation must be used to calculate distance, if there have been no prior erasures in those groups. - // If there are no prior erasures in either of those groups, we can use pointer arithmetic to calculate the distances for those groups. - - cata_assert( !( first.group_pointer == nullptr ) && - !( last.group_pointer == nullptr ) ); // Check that they are initialized - - if( last.element_pointer == first.element_pointer ) { - return 0; - } - - using iterator_type = colony_iterator; - using diff_type = typename iterator_type::difference_type; - diff_type distance = 0; - - iterator_type iterator1 = first; - iterator_type iterator2 = last; - const bool swap = first > last; - - if( swap ) { // Less common case - iterator1 = last; - iterator2 = first; - } - - if( iterator1.group_pointer != iterator2.group_pointer ) { - // if not in same group, process intermediate groups - - // Process initial group: - if( iterator1.group_pointer->free_list_head == std::numeric_limits::max() ) { - // If no prior erasures have occurred in this group we can do simple addition - distance += static_cast( iterator1.group_pointer->last_endpoint - - iterator1.element_pointer ); - } else if( iterator1.element_pointer == iterator1.group_pointer->elements ) { - // ie. element is at start of group - rare case - distance += static_cast( iterator1.group_pointer->number_of_elements ); - } else { - const skipfield_pointer_type endpoint = iterator1.skipfield_pointer + - ( iterator1.group_pointer->last_endpoint - iterator1.element_pointer ); - - while( iterator1.skipfield_pointer != endpoint ) { - iterator1.skipfield_pointer += *( ++iterator1.skipfield_pointer ); - ++distance; - } - } - - // Process all other intermediate groups: - iterator1.group_pointer = iterator1.group_pointer->next_group; - - while( iterator1.group_pointer != iterator2.group_pointer ) { - distance += static_cast( iterator1.group_pointer->number_of_elements ); - iterator1.group_pointer = iterator1.group_pointer->next_group; - } - - iterator1.skipfield_pointer = iterator1.group_pointer->skipfield; - } - - if( iterator1.group_pointer->free_list_head == std::numeric_limits::max() ) { - // ie. no erasures in this group, direct subtraction is possible - distance += static_cast( iterator2.skipfield_pointer - iterator1.skipfield_pointer ); - } else if( iterator1.group_pointer->last_endpoint - 1 >= iterator2.element_pointer ) { - // ie. if iterator2 is .end() or 1 before - distance += static_cast( iterator1.group_pointer->number_of_elements - - ( iterator1.group_pointer->last_endpoint - iterator2.element_pointer ) ); - } else { - while( iterator1.skipfield_pointer != iterator2.skipfield_pointer ) { - iterator1.skipfield_pointer += *( ++iterator1.skipfield_pointer ); - ++distance; - } - } - - if( swap ) { - distance = -distance; - } - - return distance; - } - - template - inline typename colony_reverse_iterator::difference_type distance( - const colony_reverse_iterator &first, - const colony_reverse_iterator &last ) const { - return distance( last.it, first.it ); - } - - // Type-changing functions: - - /** - * Getting a pointer from an iterator is simple - simply dereference it then grab the - * address ie. "&(*the_iterator);". Getting an iterator from a pointer is typically not so - * simple. This function enables the user to do exactly that. This is expected to be useful - * in the use-case where external containers are storing pointers to colony elements instead - * of iterators (as iterators for colonies have 3 times the size of an element pointer) and - * the program wants to erase the element being pointed to or possibly change the element - * being pointed to. Converting a pointer to an iterator using this method and then erasing, - * is about 20% slower on average than erasing when you already have the iterator. This is - * less dramatic than it sounds, as it is still faster than all std:: container erasure - * times which it is roughly equal to. However this is generally a slower, lookup-based - * operation. If the lookup doesn't find a non-erased element based on that pointer, it - * returns end(). Otherwise it returns an iterator pointing to the element in question. - * - * Cannot be noexcept as colony could be empty or pointer invalid - */ - iterator get_iterator_from_pointer( const pointer element_pointer ) const { - cata_assert( !empty() ); - cata_assert( element_pointer != nullptr ); - - // Start with last group first, as will be the largest group - group_pointer_type current_group = end_iterator.group_pointer; - - while( current_group != nullptr ) { - if( reinterpret_cast( element_pointer ) >= current_group->elements && - reinterpret_cast( element_pointer ) < reinterpret_cast - ( current_group->skipfield ) ) { - const skipfield_pointer_type skipfield_pointer = current_group->skipfield + - ( reinterpret_cast( element_pointer ) - current_group->elements ); - return ( *skipfield_pointer == 0 ) ? iterator( current_group, - reinterpret_cast( element_pointer ), - skipfield_pointer ) : end_iterator; // If element has been erased, return end() - } - - current_group = current_group->previous_group; - } - - return end_iterator; - } - - /** - * While colony is a container with unordered insertion (and is therefore unordered), it - * still has a (transitory) order which changes upon any erasure or insertion. Temporary - * index numbers are therefore obtainable. These can be useful, for example, when creating - * a save file in a computer game, where certain elements in a container may need to be - * re-linked to other elements in other container upon reloading the save file. - * - * may throw exception if iterator is invalid/uninitialized - */ - template - size_type get_index_from_iterator( const colony_iterator &it ) const { - cata_assert( !empty() ); - cata_assert( it.group_pointer != nullptr ); - - // This is essentially a simplified version of distance() optimized for counting from begin() - size_type index = 0; - group_pointer_type group_pointer = begin_iterator.group_pointer; - - // For all prior groups, add group sizes - while( group_pointer != it.group_pointer ) { - index += static_cast( group_pointer->number_of_elements ); - group_pointer = group_pointer->next_group; - } - - if( group_pointer->free_list_head == std::numeric_limits::max() ) { - // If no erased elements in group exist, do straight pointer arithmetic to get distance to start for first element - index += static_cast( it.element_pointer - group_pointer->elements ); - } else { - // Otherwise do manual ++ loop - count from beginning of group until location is reached - skipfield_pointer_type skipfield_pointer = group_pointer->skipfield + *( group_pointer->skipfield ); - - while( skipfield_pointer != it.skipfield_pointer ) { - ++skipfield_pointer; - skipfield_pointer += *skipfield_pointer; - ++index; - } - } - - return index; - } - - /** - * The same as get_index_from_iterator, but for reverse_iterators and const_reverse_iterators. - * Index is from front of colony (same as iterator), not back of colony. - */ - template - inline size_type get_index_from_reverse_iterator( const colony_reverse_iterator - &rev_iterator ) const { - return get_index_from_iterator( rev_iterator.it ); - } - - /** - * As described above, there may be situations where obtaining iterators to specific - * elements based on an index can be useful, for example, when reloading save files. This - * function is basically a shorthand to avoid typing - * iterator it = colony.begin(); - * colony.advance(it, index); - * - * Cannot be noexcept as colony could be empty - */ - template - iterator get_iterator_from_index( const index_type index ) const { - cata_assert( !empty() ); - cata_assert( std::numeric_limits::is_integer ); - - iterator it( begin_iterator ); - advance( it, static_cast( index ) ); - return it; - } - - /** Returns a copy of the allocator used by the colony instance. */ - inline allocator_type get_allocator() const noexcept { - return element_allocator_type(); - } - - private: - - struct less { - bool operator()( const element_type &a, const element_type &b ) const noexcept { - return a < b; - } - }; - - // Function-object, used to redirect the sort function to compare element pointers by the elements they point to, and sort the element pointers instead of the elements: - template - struct sort_dereferencer { - comparison_function stored_instance; - - explicit sort_dereferencer( const comparison_function &function_instance ): - stored_instance( function_instance ) - {} - - sort_dereferencer() noexcept = default; - - bool operator()( const pointer first, const pointer second ) { - return stored_instance( *first, *second ); - } - }; - - public: - - /** - * Sort the content of the colony. By default this compares the colony content using a - * less-than operator, unless the user supplies a comparison function (ie. same conditions - * as std::list's sort function). - * - * Uses std::sort internally. - */ - template - void sort( comparison_function compare ) { - if( total_number_of_elements < 2 ) { - return; - } - - pointer *const element_pointers = COLONY_ALLOCATE( pointer_allocator_type, pointer_allocator_pair, - total_number_of_elements, nullptr ); - pointer *element_pointer = element_pointers; - - // Construct pointers to all elements in the colony in sequence: - for( iterator current_element = begin_iterator; current_element != end_iterator; - ++current_element ) { - COLONY_CONSTRUCT( pointer_allocator_type, pointer_allocator_pair, element_pointer++, - &*current_element ); - } - - // Now, sort the pointers by the values they point to: - std::sort( element_pointers, element_pointers + total_number_of_elements, - sort_dereferencer( compare ) ); - - // Create a new colony and copy the elements from the old one to the new one in the order of the sorted pointers: - colony new_location; - new_location.change_group_sizes( pointer_allocator_pair.min_elements_per_group, - group_allocator_pair.max_elements_per_group ); - - try { - new_location.reserve( static_cast( ( total_number_of_elements > - std::numeric_limits::max() ) ? std::numeric_limits::max() : - total_number_of_elements ) ); - - if COLONY_CONSTEXPR( std::is_move_constructible::value ) { - for( pointer *current_element_pointer = element_pointers; - current_element_pointer != element_pointer; ++current_element_pointer ) { - new_location.insert( std::move( *reinterpret_cast( *current_element_pointer ) ) ); - } - } else { - for( pointer *current_element_pointer = element_pointers; - current_element_pointer != element_pointer; ++current_element_pointer ) { - new_location.insert( *reinterpret_cast( *current_element_pointer ) ); - } - } - } catch( ... ) { - if COLONY_CONSTEXPR( !std::is_trivially_destructible::value ) { - for( pointer *current_element_pointer = element_pointers; - current_element_pointer != element_pointer; ++current_element_pointer ) { - COLONY_DESTROY( pointer_allocator_type, pointer_allocator_pair, current_element_pointer ); - } - } - - COLONY_DEALLOCATE( pointer_allocator_type, pointer_allocator_pair, element_pointers, - total_number_of_elements ); - throw; - } - - // Make the old colony the new one, destroy the old one's data/sequence: - *this = std::move( new_location ); // avoid generating temporary - - if COLONY_CONSTEXPR( !std::is_trivially_destructible::value ) { - for( pointer *current_element_pointer = element_pointers; - current_element_pointer != element_pointer; ++current_element_pointer ) { - COLONY_DESTROY( pointer_allocator_type, pointer_allocator_pair, current_element_pointer ); - } - } - - COLONY_DEALLOCATE( pointer_allocator_type, pointer_allocator_pair, element_pointers, - total_number_of_elements ); - } - - inline void sort() { - sort( less() ); - } - - /** - * Transfer all elements from source colony into destination colony without invalidating - * pointers/iterators to either colony's elements (in other words the destination takes - * ownership of the source's memory blocks). After the splice, the source colony is empty. - * Splicing is much faster than range-moving or copying all elements from one colony to - * another. Colony does not guarantee a particular order of elements after splicing, for - * performance reasons; the insertion location of source elements in the destination colony - * is chosen based on the most positive performance outcome for subsequent iterations/ - * insertions. For example if the destination colony is {1, 2, 3, 4} and the source colony - * is {5, 6, 7, 8} the destination colony post-splice could be {1, 2, 3, 4, 5, 6, 7, 8} or - * {5, 6, 7, 8, 1, 2, 3, 4}, depending on internal state of both colonies and prior - * insertions/erasures. - */ - void splice( colony &source ) COLONY_NOEXCEPT_SWAP( allocator_type ) { - // Process: if there are unused memory spaces at the end of the current back group of the chain, convert them - // to skipped elements and add the locations to the group's free list. - // Then link the destination's groups to the source's groups and nullify the source. - // If the source has more unused memory spaces in the back group than the destination, swap them before processing to reduce the number of locations added to a free list and also subsequent jumps during iteration. - - cata_assert( &source != this ); - - if( source.total_number_of_elements == 0 ) { - return; - } else if( total_number_of_elements == 0 ) { - *this = std::move( source ); - return; - } - - // If there's more unused element locations at end of destination than source, swap with source to reduce number of skipped elements and size of free-list: - if( ( reinterpret_cast( end_iterator.group_pointer->skipfield ) - - end_iterator.element_pointer ) > ( reinterpret_cast - ( source.end_iterator.group_pointer->skipfield ) - source.end_iterator.element_pointer ) ) { - swap( source ); - } - - // Correct group sizes if necessary: - if( source.pointer_allocator_pair.min_elements_per_group < - pointer_allocator_pair.min_elements_per_group ) { - pointer_allocator_pair.min_elements_per_group = - source.pointer_allocator_pair.min_elements_per_group; - } - - if( source.group_allocator_pair.max_elements_per_group > - group_allocator_pair.max_elements_per_group ) { - group_allocator_pair.max_elements_per_group = source.group_allocator_pair.max_elements_per_group; - } - - // Add source list of groups-with-erasures to destination list of groups-with-erasures: - if( source.groups_with_erasures_list_head != nullptr ) { - if( groups_with_erasures_list_head != nullptr ) { - group_pointer_type tail_group = groups_with_erasures_list_head; - - while( tail_group->erasures_list_next_group != nullptr ) { - tail_group = tail_group->erasures_list_next_group; - } - - tail_group->erasures_list_next_group = source.groups_with_erasures_list_head; - } else { - groups_with_erasures_list_head = source.groups_with_erasures_list_head; - } - } - - const skipfield_type distance_to_end = static_cast - ( reinterpret_cast( end_iterator.group_pointer->skipfield ) - - end_iterator.element_pointer ); - - if( distance_to_end != 0 ) { // 0 == edge case - // Mark unused element memory locations from back group as skipped/erased: - - // Update skipfield: - const skipfield_type previous_node_value = *( end_iterator.skipfield_pointer - 1 ); - end_iterator.group_pointer->last_endpoint = reinterpret_cast - ( end_iterator.group_pointer->skipfield ); - - if( previous_node_value == 0 ) { // no previous skipblock - *end_iterator.skipfield_pointer = distance_to_end; - *( end_iterator.skipfield_pointer + distance_to_end - 1 ) = distance_to_end; - - const skipfield_type index = static_cast( end_iterator.element_pointer - - end_iterator.group_pointer->elements ); - - if( end_iterator.group_pointer->free_list_head != std::numeric_limits::max() ) { - // ie. if this group already has some erased elements - // set prev free list head's 'next index' number to the index of the current element - *( reinterpret_cast( end_iterator.group_pointer->elements + - end_iterator.group_pointer->free_list_head ) + 1 ) = index; - } else { - // add it to the groups-with-erasures free list - end_iterator.group_pointer->erasures_list_next_group = groups_with_erasures_list_head; - groups_with_erasures_list_head = end_iterator.group_pointer; - } - - *( reinterpret_cast( end_iterator.element_pointer ) ) = - end_iterator.group_pointer->free_list_head; - *( reinterpret_cast( end_iterator.element_pointer ) + 1 ) = - std::numeric_limits::max(); - end_iterator.group_pointer->free_list_head = index; - } else { - // update previous skipblock, no need to update free list: - *( end_iterator.skipfield_pointer - previous_node_value ) = *( end_iterator.skipfield_pointer + - distance_to_end - 1 ) = previous_node_value + distance_to_end; - } - } - - // Update subsequent group numbers: - group_pointer_type current_group = source.begin_iterator.group_pointer; - size_type current_group_number = end_iterator.group_pointer->group_number; - - do { - current_group->group_number = ++current_group_number; - current_group = current_group->next_group; - } while( current_group != nullptr ); - - // Join the destination and source group chains: - end_iterator.group_pointer->next_group = source.begin_iterator.group_pointer; - source.begin_iterator.group_pointer->previous_group = end_iterator.group_pointer; - end_iterator = source.end_iterator; - total_number_of_elements += source.total_number_of_elements; - source.blank(); - } - - /** Swaps the colony's contents with that of source. */ - void swap( colony &source ) COLONY_NOEXCEPT_SWAP( allocator_type ) { - cata_assert( &source != this ); - - // if all pointer types are trivial we can just copy using memcpy - avoids constructors/destructors etc and is faster - if COLONY_CONSTEXPR( std::is_trivial::value && - std::is_trivial::value && - std::is_trivial::value ) { - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - char temp[sizeof( colony )]; - std::memcpy( &temp, static_cast( this ), sizeof( colony ) ); - std::memcpy( static_cast( this ), static_cast( &source ), sizeof( colony ) ); - std::memcpy( static_cast( &source ), &temp, sizeof( colony ) ); - - // If pointer types are not trivial, moving them is probably going to be more efficient than copying them below - } else if COLONY_CONSTEXPR( std::is_move_assignable::value && - std::is_move_assignable::value && - std::is_move_assignable::value && - std::is_move_constructible::value && - std::is_move_constructible::value && - std::is_move_constructible::value ) { - colony temp( std::move( source ) ); - source = std::move( *this ); - *this = std::move( temp ); - } else { - const iterator swap_end_iterator = end_iterator; - const iterator swap_begin_iterator = begin_iterator; - const group_pointer_type swap_groups_with_erasures_list_head = groups_with_erasures_list_head; - const size_type swap_total_number_of_elements = total_number_of_elements; - const size_type swap_total_capacity = total_capacity; - const skipfield_type swap_min_elements_per_group = pointer_allocator_pair.min_elements_per_group; - const skipfield_type swap_max_elements_per_group = group_allocator_pair.max_elements_per_group; - - end_iterator = source.end_iterator; - begin_iterator = source.begin_iterator; - groups_with_erasures_list_head = source.groups_with_erasures_list_head; - total_number_of_elements = source.total_number_of_elements; - total_capacity = source.total_capacity; - pointer_allocator_pair.min_elements_per_group = - source.pointer_allocator_pair.min_elements_per_group; - group_allocator_pair.max_elements_per_group = source.group_allocator_pair.max_elements_per_group; - - source.end_iterator = swap_end_iterator; - source.begin_iterator = swap_begin_iterator; - source.groups_with_erasures_list_head = swap_groups_with_erasures_list_head; - source.total_number_of_elements = swap_total_number_of_elements; - source.total_capacity = swap_total_capacity; - source.pointer_allocator_pair.min_elements_per_group = swap_min_elements_per_group; - source.group_allocator_pair.max_elements_per_group = swap_max_elements_per_group; - } - } - -}; // colony - -/** - * Swaps colony A's contents with that of colony B. - * Assumes both colonies have same element type, allocator type, etc. - */ -template -inline void swap( colony &a, - colony &b ) COLONY_NOEXCEPT_SWAP( - element_allocator_type ) -{ - a.swap( b ); -} - +template> +using colony = plf::colony; } // namespace cata -#undef COLONY_FORCE_INLINE - -#undef COLONY_NOEXCEPT_SWAP -#undef COLONY_NOEXCEPT_MOVE_ASSIGNMENT -#undef COLONY_CONSTEXPR - -#undef COLONY_CONSTRUCT -#undef COLONY_DESTROY -#undef COLONY_ALLOCATE -#undef COLONY_ALLOCATE_INITIALIZATION -#undef COLONY_DEALLOCATE - #endif // CATA_SRC_COLONY_H diff --git a/src/item_stack.h b/src/item_stack.h index f41809f2ecb..cc081245fc0 100644 --- a/src/item_stack.h +++ b/src/item_stack.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "colony.h" #include "coords_fwd.h" diff --git a/src/list.h b/src/list.h index 570eef0cfb9..56976fb527b 100644 --- a/src/list.h +++ b/src/list.h @@ -1,2479 +1,15 @@ -// This software is a modified version of the original. -// The original license is as follows: - -// Copyright (c) 2019, Matthew Bentley (mattreecebentley@gmail.com) www.plflib.org - -// zLib license (https://www.zlib.net/zlib_license.html): -// This software is provided 'as-is', without any express or implied -// warranty. In no event will the authors be held liable for any damages -// arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it -// freely, subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; you must not -// claim that you wrote the original software. If you use this software -// in a product, an acknowledgement in the product documentation would be -// appreciated but is not required. -// 2. Altered source versions must be plainly marked as such, and must not be -// misrepresented as being the original software. -// 3. This notice may not be removed or altered from any source distribution. - #pragma once #ifndef CATA_SRC_LIST_H #define CATA_SRC_LIST_H -#define LIST_BLOCK_MIN static_cast((sizeof(node) * 8 > (sizeof(*this) + sizeof(group)) * 2) ? 8 : (((sizeof(*this) + sizeof(group)) * 2) / sizeof(node)) + 1) -#define LIST_BLOCK_MAX 2048 +#include -#define LIST_CONSTEXPR -#define LIST_NOEXCEPT_SWAP(the_allocator) noexcept -#define LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept - -// TODO: Switch to these when we move to C++17 -// #define LIST_CONSTEXPR constexpr -// #define LIST_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits::propagate_on_container_swap::value) -// #define LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) - -// Note: GCC creates faster code without forcing inline -#if defined(_MSC_VER) -#define LIST_FORCE_INLINE __forceinline -#else -#define LIST_FORCE_INLINE -#endif - -// TODO: get rid of these defines -#define LIST_CONSTRUCT(the_allocator, allocator_instance, location, ...) std::allocator_traits::construct(allocator_instance, location, __VA_ARGS__) -#define LIST_DESTROY(the_allocator, allocator_instance, location) std::allocator_traits::destroy(allocator_instance, location) -#define LIST_ALLOCATE(the_allocator, allocator_instance, size, hint) std::allocator_traits::allocate(allocator_instance, size, hint) -#define LIST_ALLOCATE_INITIALIZATION(the_allocator, size, hint) std::allocator_traits::allocate(*this, size, hint) -#define LIST_DEALLOCATE(the_allocator, allocator_instance, location, size) std::allocator_traits::deallocate(allocator_instance, location, size) - -#include // std::sort -#include // memmove, memcpy -#include -#include // std::bidirectional_iterator_tag -#include // std::numeric_limits -#include // std::uninitialized_copy, std::allocator -#include // std::is_trivially_destructible, etc -#include // std::move - -#include "cata_assert.h" +#include // IWYU pragma: export namespace cata { - -template > class - list : private element_allocator_type -{ - public: - // Standard container typedefs: - using value_type = element_type; - using allocator_type = element_allocator_type; - using group_size_type = unsigned short; - - using size_type = typename std::allocator_traits::size_type; - using difference_type = typename std::allocator_traits::difference_type; - using reference = element_type&; - using const_reference = const element_type&; - using pointer = typename std::allocator_traits::pointer; - using const_pointer = typename std::allocator_traits::const_pointer; - - // Iterator declarations: - template class list_iterator; - using iterator = list_iterator; - using const_iterator = list_iterator; - friend class list_iterator; // Using 'iterator' typedef name here is illegal under C++03 - friend class list_iterator; - - template class list_reverse_iterator; - using reverse_iterator = list_reverse_iterator; - using const_reverse_iterator = list_reverse_iterator; - friend class list_reverse_iterator; - friend class list_reverse_iterator; - - private: - struct group; // forward declarations for typedefs below - struct node; - - using group_allocator_type = typename std::allocator_traits::template - rebind_alloc; - using node_allocator_type = typename std::allocator_traits::template - rebind_alloc; - using group_pointer_type = typename std::allocator_traits::pointer; - using node_pointer_type = typename std::allocator_traits::pointer; - using node_pointer_allocator_type = typename std::allocator_traits::template - rebind_alloc; - - struct node_base { - node_pointer_type next, previous; - - node_base() = default; - - node_base( const node_pointer_type &n, const node_pointer_type &p ): - next( n ), - previous( p ) - {} - - node_base( node_pointer_type &&n, node_pointer_type &&p ) noexcept: - next( std::move( n ) ), - previous( std::move( p ) ) - {} - }; - - struct node : public node_base { - element_type element; - - node( const node_pointer_type next, const node_pointer_type previous, const element_type &source ): - node_base( next, previous ), - element( source ) - {} - - node( node_pointer_type &&next, node_pointer_type &&previous, element_type &&source ) noexcept: - node_base( std::move( next ), std::move( previous ) ), - element( std::move( source ) ) - {} - - template - node( node_pointer_type const next, node_pointer_type const previous, arguments &&... parameters ): - node_base( next, previous ), - element( std::forward( parameters ) ... ) - {} - }; - - struct group : public node_allocator_type { - node_pointer_type nodes; - node_pointer_type free_list_head; - node_pointer_type beyond_end; - group_size_type number_of_elements; - - group() noexcept: - nodes( nullptr ), - free_list_head( nullptr ), - beyond_end( nullptr ), - number_of_elements( 0 ) - {} - - explicit group( const group_size_type group_size, node_pointer_type const previous = nullptr ): - nodes( LIST_ALLOCATE_INITIALIZATION( node_allocator_type, group_size, previous ) ), - free_list_head( nullptr ), - beyond_end( nodes + group_size ), - number_of_elements( 0 ) - {} - - // Actually a move operator, used by c++03 in group_vector's remove, expand_capacity and append - group &operator=( const group &source ) noexcept { - nodes = source.nodes; - free_list_head = source.free_list_head; - beyond_end = source.beyond_end; - number_of_elements = source.number_of_elements; - return *this; - } - - group( group &&source ) noexcept: - node_allocator_type( source ), - nodes( std::move( source.nodes ) ), - free_list_head( std::move( source.free_list_head ) ), - beyond_end( std::move( source.beyond_end ) ), - number_of_elements( source.number_of_elements ) { - source.nodes = nullptr; - source.beyond_end = nullptr; - } - - group &operator=( group &&source ) noexcept { - nodes = std::move( source.nodes ); - free_list_head = std::move( source.free_list_head ); - beyond_end = std::move( source.beyond_end ); - number_of_elements = std::move( source.number_of_elements ); - source.nodes = nullptr; - source.beyond_end = nullptr; - return *this; - } - - ~group() noexcept { - LIST_DEALLOCATE( node_allocator_type, ( *this ), nodes, - static_cast( beyond_end - nodes ) ); - } - }; - - class group_vector : private node_pointer_allocator_type - { - public: - // last_endpoint_group is the last -active- group in the block. Other -inactive- (previously used, now empty of elements) groups may be stored after this group for future usage (to reduce deallocation/reallocation of nodes). block_pointer + size - 1 == the last group in the block, regardless of whether or not the group is active. - group_pointer_type last_endpoint_group, block_pointer, last_searched_group; - size_type size; - - struct ebco_pair2 : allocator_type { // empty-base-class optimization - size_type capacity; // Total element capacity of all initialized groups - explicit ebco_pair2( const size_type number_of_elements ) noexcept: capacity( - number_of_elements ) {} - } element_allocator_pair; - - struct ebco_pair : group_allocator_type { - size_type capacity; // Total group capacity - explicit ebco_pair( const size_type number_of_groups ) noexcept: capacity( number_of_groups ) {} - } group_allocator_pair; - - group_vector() noexcept: - node_pointer_allocator_type( node_pointer_allocator_type() ), - last_endpoint_group( nullptr ), - block_pointer( nullptr ), - last_searched_group( nullptr ), - size( 0 ), - element_allocator_pair( 0 ), - group_allocator_pair( 0 ) - {} - - inline LIST_FORCE_INLINE void blank() noexcept { - if LIST_CONSTEXPR( std::is_trivial::value ) { - std::memset( static_cast( this ), 0, sizeof( group_vector ) ); - } else { - last_endpoint_group = nullptr; - block_pointer = nullptr; - last_searched_group = nullptr; - size = 0; - element_allocator_pair.capacity = 0; - group_allocator_pair.capacity = 0; - } - } - - group_vector( group_vector &&source ) noexcept: - last_endpoint_group( std::move( source.last_endpoint_group ) ), - block_pointer( std::move( source.block_pointer ) ), - last_searched_group( std::move( source.last_searched_group ) ), - size( source.size ), - element_allocator_pair( source.element_allocator_pair.capacity ), - group_allocator_pair( source.group_allocator_pair.capacity ) { - source.blank(); - } - - group_vector &operator = ( group_vector &&source ) noexcept { - if LIST_CONSTEXPR( std::is_trivial::value ) { - // NOLINTNEXTLINE(bugprone-undefined-memory-manipulation) - std::memcpy( static_cast( this ), &source, sizeof( group_vector ) ); - } else { - last_endpoint_group = std::move( source.last_endpoint_group ); - block_pointer = std::move( source.block_pointer ); - last_searched_group = std::move( source.last_searched_group ); - size = source.size; - element_allocator_pair.capacity = source.element_allocator_pair.capacity; - group_allocator_pair.capacity = source.group_allocator_pair.capacity; - } - - source.blank(); - return *this; - } - - ~group_vector() noexcept = default; - - void destroy_all_data( const node_pointer_type last_endpoint_node ) noexcept { - if( block_pointer == nullptr ) { - return; - } - - if LIST_CONSTEXPR( !std::is_trivially_destructible::value || - !std::is_trivially_destructible::value ) { - clear( last_endpoint_node ); // If clear has already been called, last_endpoint_node will already be == block_pointer->nodes, so no work will occur - } - - const group_pointer_type end_group = block_pointer + size; - for( group_pointer_type current_group = block_pointer; current_group != end_group; - ++current_group ) { - LIST_DESTROY( group_allocator_type, group_allocator_pair, current_group ); - } - - LIST_DEALLOCATE( group_allocator_type, group_allocator_pair, block_pointer, - group_allocator_pair.capacity ); - blank(); - } - - void clear( const node_pointer_type last_endpoint_node ) noexcept { - for( group_pointer_type current_group = block_pointer; current_group != last_endpoint_group; - ++current_group ) { - if LIST_CONSTEXPR( !std::is_trivially_destructible::value || - !std::is_trivially_destructible::value ) { - const node_pointer_type end = current_group->beyond_end; - - if( ( end - current_group->nodes ) != current_group->number_of_elements ) { - // If there are erased nodes present in the group - for( node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node ) { - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - if( current_node->next != nullptr ) { // ie. is not part of free list - LIST_DESTROY( element_allocator_type, element_allocator_pair, &( current_node->element ) ); - } - } - - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->next ) ); - LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->previous ) ); - } - } - } else { - for( node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node ) { - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - LIST_DESTROY( element_allocator_type, element_allocator_pair, &( current_node->element ) ); - } - - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->next ) ); - LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->previous ) ); - } - } - } - } - - current_group->free_list_head = nullptr; - current_group->number_of_elements = 0; - } - - if LIST_CONSTEXPR( !std::is_trivially_destructible::value || - !std::is_trivially_destructible::value ) { - if( ( last_endpoint_node - last_endpoint_group->nodes ) != - last_endpoint_group->number_of_elements ) { - // If there are erased nodes present in the group - for( node_pointer_type current_node = last_endpoint_group->nodes; - current_node != last_endpoint_node; ++current_node ) { - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - if( current_node->next != nullptr ) { - // is not part of free list ie. element has not already had it's destructor called - LIST_DESTROY( element_allocator_type, element_allocator_pair, &( current_node->element ) ); - } - } - - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->next ) ); - LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->previous ) ); - } - } - } else { - for( node_pointer_type current_node = last_endpoint_group->nodes; - current_node != last_endpoint_node; ++current_node ) { - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - LIST_DESTROY( element_allocator_type, element_allocator_pair, &( current_node->element ) ); - } - - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->next ) ); - LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->previous ) ); - } - } - } - } - - last_endpoint_group->free_list_head = nullptr; - last_endpoint_group->number_of_elements = 0; - last_searched_group = last_endpoint_group = block_pointer; - } - - void expand_capacity( const size_type new_capacity ) { // used by add_new and append - group_pointer_type const old_block = block_pointer; - block_pointer = LIST_ALLOCATE( group_allocator_type, group_allocator_pair, new_capacity, nullptr ); - - if LIST_CONSTEXPR( std::is_trivially_copyable::value && - std::is_trivially_destructible::value ) { - // Dereferencing here in order to deal with smart pointer situations i.e. obtaining the raw pointer from the smart pointer - // reinterpret_cast necessary to deal with GCC 8 warnings - std::memcpy( static_cast( &*block_pointer ), static_cast( &*old_block ), - sizeof( group ) * size ); - } else if LIST_CONSTEXPR( std::is_move_constructible::value ) { - std::uninitialized_copy( std::make_move_iterator( old_block ), - std::make_move_iterator( old_block + size ), block_pointer ); - } else { - // If allocator supplies non-trivial pointers it becomes necessary to destroy the group. uninitialized_copy will not work in this context as the copy constructor for "group" is overridden in C++03/98. The = operator for "group" has been overridden to make the following work: - const group_pointer_type beyond_end = old_block + size; - group_pointer_type current_new_group = block_pointer; - - for( group_pointer_type current_group = old_block; current_group != beyond_end; ++current_group ) { - *( current_new_group++ ) = *current_group; - - current_group->nodes = nullptr; - current_group->beyond_end = nullptr; - LIST_DESTROY( group_allocator_type, group_allocator_pair, current_group ); - } - } - - // correct pointer post-reallocation - last_searched_group = block_pointer + ( last_searched_group - old_block ); - LIST_DEALLOCATE( group_allocator_type, group_allocator_pair, old_block, - group_allocator_pair.capacity ); - group_allocator_pair.capacity = new_capacity; - } - - void add_new( const group_size_type group_size ) { - if( group_allocator_pair.capacity == size ) { - expand_capacity( group_allocator_pair.capacity * 2 ); - } - - last_endpoint_group = block_pointer + size - 1; - - LIST_CONSTRUCT( group_allocator_type, group_allocator_pair, last_endpoint_group + 1, group_size, - last_endpoint_group->nodes ); - - ++last_endpoint_group; // Doing this here instead of pre-construct to avoid need for a try-catch block - element_allocator_pair.capacity += group_size; - ++size; - } - - // For adding first group *only* when group vector is completely empty and block_pointer is NULL - void initialize( const group_size_type group_size ) { - last_endpoint_group = block_pointer = last_searched_group = LIST_ALLOCATE( group_allocator_type, - group_allocator_pair, 1, nullptr ); - group_allocator_pair.capacity = 1; - - LIST_CONSTRUCT( group_allocator_type, group_allocator_pair, last_endpoint_group, group_size ); - - size = 1; // Doing these here instead of pre-construct to avoid need for a try-catch block - element_allocator_pair.capacity = group_size; - } - - void remove( group_pointer_type const group_to_erase ) noexcept { - if( last_searched_group >= group_to_erase && last_searched_group != block_pointer ) { - --last_searched_group; - } - - element_allocator_pair.capacity -= static_cast( group_to_erase->beyond_end - - group_to_erase->nodes ); - - LIST_DESTROY( group_allocator_type, group_allocator_pair, group_to_erase ); - - if LIST_CONSTEXPR( std::is_trivially_copyable::value && - std::is_trivially_destructible::value ) { - // Dereferencing here in order to deal with smart pointer situations ie. obtaining the raw pointer from the smart pointer - std::memmove( static_cast( &*group_to_erase ), static_cast( &*group_to_erase + 1 ), - sizeof( group ) * ( --size - static_cast( &*group_to_erase - &*block_pointer ) ) ); - } else if LIST_CONSTEXPR( std::is_move_constructible::value ) { - std::move( group_to_erase + 1, block_pointer + size--, group_to_erase ); - } else { - group_pointer_type back = block_pointer + size--; - std::copy( group_to_erase + 1, back--, group_to_erase ); - - back->nodes = nullptr; - back->beyond_end = nullptr; - LIST_DESTROY( group_allocator_type, group_allocator_pair, back ); - } - } - - void move_to_back( group_pointer_type const group_to_erase ) { - if( last_searched_group >= group_to_erase && last_searched_group != block_pointer ) { - --last_searched_group; - } - - group *temp_group = LIST_ALLOCATE( group_allocator_type, group_allocator_pair, 1, nullptr ); - - if LIST_CONSTEXPR( std::is_trivially_copyable::value && - std::is_trivially_destructible::value ) { - std::memcpy( static_cast( &*temp_group ), static_cast( &*group_to_erase ), - sizeof( group ) ); - std::memmove( static_cast( &*group_to_erase ), static_cast( &*group_to_erase + 1 ), - sizeof( group ) * ( ( size - 1 ) - static_cast( &*group_to_erase - &*block_pointer ) ) ); - std::memcpy( static_cast( &*( block_pointer + size - 1 ) ), - static_cast( &*temp_group ), sizeof( group ) ); - } else if LIST_CONSTEXPR( std::is_move_constructible::value ) { - LIST_CONSTRUCT( group_allocator_type, group_allocator_pair, temp_group, - std::move( *group_to_erase ) ); - std::move( group_to_erase + 1, block_pointer + size, group_to_erase ); - *( block_pointer + size - 1 ) = std::move( *temp_group ); - - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - LIST_DESTROY( group_allocator_type, group_allocator_pair, temp_group ); - } - } else { - LIST_CONSTRUCT( group_allocator_type, group_allocator_pair, temp_group, group() ); - - *temp_group = *group_to_erase; - std::copy( group_to_erase + 1, block_pointer + size, group_to_erase ); - *( block_pointer + --size ) = *temp_group; - - temp_group->nodes = nullptr; - LIST_DESTROY( group_allocator_type, group_allocator_pair, temp_group ); - } - - LIST_DEALLOCATE( group_allocator_type, group_allocator_pair, temp_group, 1 ); - } - - // In working implementation this cannot throw - group_pointer_type get_nearest_freelist_group( const node_pointer_type location_node ) noexcept { - const group_pointer_type beyond_end_group = last_endpoint_group + 1; - group_pointer_type left = last_searched_group - 1; - group_pointer_type right = last_searched_group + 1; - group_pointer_type freelist_group = nullptr; - bool right_not_beyond_back = right < beyond_end_group; - bool left_not_beyond_front = left >= block_pointer; - - // ie. location is within last_search_group - if( location_node >= last_searched_group->nodes && - location_node < last_searched_group->beyond_end ) { - // if last_searched_group has previously-erased nodes - if( last_searched_group->free_list_head != nullptr ) { - return last_searched_group; - } - } else { // search for the node group which location_node is located within, using last_searched_group as a starting point and searching left and right. Try and find the closest node group with reusable erased-element locations along the way: - group_pointer_type closest_freelist_left = ( last_searched_group->free_list_head == nullptr ) ? - nullptr : - last_searched_group; - group_pointer_type closest_freelist_right = ( last_searched_group->free_list_head == nullptr ) ? - nullptr : last_searched_group; - - while( true ) { - if( right_not_beyond_back ) { - // location_node's group is found - if( ( location_node < right->beyond_end ) && ( location_node >= right->nodes ) ) { - // group has erased nodes, reuse them: - if( right->free_list_head != nullptr ) { - last_searched_group = right; - return right; - } - - difference_type left_distance; - - if( closest_freelist_right != nullptr ) { - last_searched_group = right; - left_distance = right - closest_freelist_right; - - if( left_distance <= 2 ) { // ie. this group is close enough to location_node's group - return closest_freelist_right; - } - - freelist_group = closest_freelist_right; - } else { - last_searched_group = right; - left_distance = right - left; - } - - // Otherwise find closest group with freelist - check an equal distance on the right to the distance we've checked on the left: - const group_pointer_type end_group = ( ( right + left_distance ) > beyond_end_group ) ? - beyond_end_group : ( right + left_distance - 1 ); - - while( ++right != end_group ) { - if( right->free_list_head != nullptr ) { - return right; - } - } - - if( freelist_group != nullptr ) { - return freelist_group; - } - - right_not_beyond_back = ( right < beyond_end_group ); - break; // group with reusable erased nodes not found yet, continue searching in loop below - } - - // location_node's group not found, but a reusable location found - if( right->free_list_head != nullptr ) { - if( ( closest_freelist_right == nullptr ) & ( closest_freelist_left == nullptr ) ) { - closest_freelist_left = right; - } - - closest_freelist_right = right; - } - - right_not_beyond_back = ( ++right < beyond_end_group ); - } - - if( left_not_beyond_front ) { - if( ( location_node >= left->nodes ) && ( location_node < left->beyond_end ) ) { - if( left->free_list_head != nullptr ) { - last_searched_group = left; - return left; - } - - difference_type right_distance; - - if( closest_freelist_left != nullptr ) { - last_searched_group = left; - right_distance = closest_freelist_left - left; - - if( right_distance <= 2 ) { - return closest_freelist_left; - } - - freelist_group = closest_freelist_left; - } else { - last_searched_group = left; - right_distance = right - left; - } - - // Otherwise find closest group with freelist: - const group_pointer_type end_group = ( ( left - right_distance ) < block_pointer ) ? block_pointer - - 1 : ( left - right_distance ) + 1; - - while( --left != end_group ) { - if( left->free_list_head != nullptr ) { - return left; - } - } - - if( freelist_group != nullptr ) { - return freelist_group; - } - - left_not_beyond_front = ( left >= block_pointer ); - break; - } - - if( left->free_list_head != nullptr ) { - if( ( closest_freelist_left == nullptr ) & ( closest_freelist_right == nullptr ) ) { - closest_freelist_right = left; - } - - closest_freelist_left = left; - } - - left_not_beyond_front = ( --left >= block_pointer ); - } - } - } - - // The node group which location_node is located within, is known at this point. Continue searching outwards from this group until a group is found with a reusable location: - while( true ) { - if( right_not_beyond_back ) { - if( right->free_list_head != nullptr ) { - return right; - } - - right_not_beyond_back = ( ++right < beyond_end_group ); - } - - if( left_not_beyond_front ) { - if( left->free_list_head != nullptr ) { - return left; - } - - left_not_beyond_front = ( --left >= block_pointer ); - } - } - - // Will never reach here on functioning implementations - } - - void swap( group_vector &source ) LIST_NOEXCEPT_SWAP( group_allocator_type ) { - if LIST_CONSTEXPR( - std::is_trivial::value ) { // if all pointer types are trivial we can just copy using memcpy - faster - avoids constructors/destructors etc - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - char temp[sizeof( group_vector )]; - std::memcpy( static_cast( &temp ), static_cast( this ), sizeof( group_vector ) ); - std::memcpy( static_cast( this ), static_cast( &source ), sizeof( group_vector ) ); - std::memcpy( static_cast( &source ), static_cast( &temp ), sizeof( group_vector ) ); - } else { - const group_pointer_type swap_last_endpoint_group = last_endpoint_group; - const group_pointer_type swap_block_pointer = block_pointer; - const group_pointer_type swap_last_searched_group = last_searched_group; - const size_type swap_size = size; - const size_type swap_element_capacity = element_allocator_pair.capacity; - const size_type swap_capacity = group_allocator_pair.capacity; - - last_endpoint_group = source.last_endpoint_group; - block_pointer = source.block_pointer; - last_searched_group = source.last_searched_group; - size = source.size; - element_allocator_pair.capacity = source.element_allocator_pair.capacity; - group_allocator_pair.capacity = source.group_allocator_pair.capacity; - - source.last_endpoint_group = swap_last_endpoint_group; - source.block_pointer = swap_block_pointer; - source.last_searched_group = swap_last_searched_group; - source.size = swap_size; - source.element_allocator_pair.capacity = swap_element_capacity; - source.group_allocator_pair.capacity = swap_capacity; - } - } - - void trim_trailing_groups() noexcept { - const group_pointer_type beyond_last = block_pointer + size; - - for( group_pointer_type current_group = last_endpoint_group + 1; current_group != beyond_last; - ++current_group ) { - element_allocator_pair.capacity -= static_cast( current_group->beyond_end - - current_group->nodes ); - LIST_DESTROY( group_allocator_type, group_allocator_pair, current_group ); - } - - size -= static_cast( beyond_last - ( last_endpoint_group + 1 ) ); - } - - void append( group_vector &source ) { - source.trim_trailing_groups(); - trim_trailing_groups(); - - if( size + source.size > group_allocator_pair.capacity ) { - expand_capacity( size + source.size ); - } - - if LIST_CONSTEXPR( std::is_trivially_copyable::value && - std::is_trivially_destructible::value ) { - // &* in order to deal with smart pointer situations ie. obtaining the raw pointer from the smart pointer - std::memcpy( static_cast( &*block_pointer + size ), - static_cast( &*source.block_pointer ), sizeof( group ) * source.size ); - } else if LIST_CONSTEXPR( std::is_move_constructible::value ) { - std::uninitialized_copy( std::make_move_iterator( source.block_pointer ), - std::make_move_iterator( source.block_pointer + source.size ), block_pointer + size ); - } else { - group_pointer_type current_new_group = block_pointer + size; - const group_pointer_type beyond_end_source = source.block_pointer + source.size; - - for( group_pointer_type current_group = source.block_pointer; current_group != beyond_end_source; - ++current_group ) { - *( current_new_group++ ) = *current_group; - - current_group->nodes = nullptr; - current_group->beyond_end = nullptr; - LIST_DESTROY( group_allocator_type, source.group_allocator_pair, current_group ); - } - } - - LIST_DEALLOCATE( group_allocator_type, source.group_allocator_pair, source.block_pointer, - source.group_allocator_pair.capacity ); - size += source.size; - last_endpoint_group = block_pointer + size - 1; - element_allocator_pair.capacity += source.element_allocator_pair.capacity; - source.blank(); - } - }; - - // Implement const/non-const iterator switching pattern: - template struct choose; - - template struct choose { - using type = IsTrue; - }; - - template struct choose { - using type = IsFalse; - }; - - public: - - template class list_iterator - { - private: - node_pointer_type node_pointer; - - public: - using iterator_category = std::bidirectional_iterator_tag; - using value_type = typename list::value_type; - using difference_type = typename list::difference_type; - using pointer = typename - choose::type; - using reference = typename - choose::type; - - friend class list; - - inline LIST_FORCE_INLINE bool operator==( const list_iterator rh ) const noexcept { - return node_pointer == rh.node_pointer; - } - - inline LIST_FORCE_INLINE bool operator==( const list_iterator < !is_const > rh ) const - noexcept { - return node_pointer == rh.node_pointer; - } - - inline LIST_FORCE_INLINE bool operator!=( const list_iterator rh ) const noexcept { - return node_pointer != rh.node_pointer; - } - - inline LIST_FORCE_INLINE bool operator!=( const list_iterator < !is_const > rh ) const - noexcept { - return node_pointer != rh.node_pointer; - } - - inline LIST_FORCE_INLINE reference operator*() const { - return node_pointer->element; - } - - inline LIST_FORCE_INLINE pointer operator->() const { - return &( node_pointer->element ); - } - - inline LIST_FORCE_INLINE list_iterator &operator++() noexcept { - cata_assert( node_pointer != nullptr ); // covers uninitialized list_iterator - node_pointer = node_pointer->next; - return *this; - } - - inline list_iterator operator++( int ) noexcept { - const list_iterator copy( *this ); - ++*this; - return copy; - } - - inline LIST_FORCE_INLINE list_iterator &operator--() noexcept { - cata_assert( node_pointer != nullptr ); // covers uninitialized list_iterator - node_pointer = node_pointer->previous; - return *this; - } - - inline list_iterator operator--( int ) noexcept { - const list_iterator copy( *this ); - --*this; - return copy; - } - - inline list_iterator &operator=( const list_iterator &rh ) noexcept { - node_pointer = rh.node_pointer; - return *this; - } - - inline list_iterator &operator=( const list_iterator < !is_const > &rh ) noexcept { - node_pointer = rh.node_pointer; - return *this; - } - - inline list_iterator &operator=( list_iterator &&rh ) noexcept { - node_pointer = std::move( rh.node_pointer ); - return *this; - } - - inline list_iterator &operator=( const list_iterator < !is_const > &&rh ) noexcept { - node_pointer = std::move( rh.node_pointer ); - return *this; - } - - list_iterator() noexcept: node_pointer( NULL ) {} - - list_iterator( const list_iterator &source ) noexcept: node_pointer( source.node_pointer ) {} - - // NOLINTNEXTLINE(google-explicit-constructor) - list_iterator( const list_iterator < !is_const > &source ) noexcept: node_pointer( - source.node_pointer ) {} - - list_iterator( const list_iterator &&source ) noexcept: node_pointer( std::move( - source.node_pointer ) ) {} - - // NOLINTNEXTLINE(google-explicit-constructor) - list_iterator( const list_iterator < !is_const > && - source ) noexcept: node_pointer( std::move( source.node_pointer ) ) {} - - private: - - // NOLINTNEXTLINE(google-explicit-constructor) - list_iterator( const node_pointer_type node_p ) noexcept: node_pointer( node_p ) {} - }; - - template class list_reverse_iterator - { - private: - node_pointer_type node_pointer; - - public: - using iterator_category = std::bidirectional_iterator_tag; - using value_type = typename list::value_type; - using difference_type = typename list::difference_type; - using pointer = typename - choose::type; - using reference = typename - choose::type; - - friend class list; - - inline LIST_FORCE_INLINE bool operator==( const list_reverse_iterator rh ) const noexcept { - return node_pointer == rh.node_pointer; - } - - inline LIST_FORCE_INLINE bool operator==( const list_reverse_iterator < !is_const > rh ) const - noexcept { - return node_pointer == rh.node_pointer; - } - - inline LIST_FORCE_INLINE bool operator!=( const list_reverse_iterator rh ) const noexcept { - return node_pointer != rh.node_pointer; - } - - inline LIST_FORCE_INLINE bool operator!=( const list_reverse_iterator < !is_const > rh ) const - noexcept { - return node_pointer != rh.node_pointer; - } - - inline LIST_FORCE_INLINE reference operator*() const { - return node_pointer->element; - } - - inline LIST_FORCE_INLINE pointer operator->() const { - return &( node_pointer->element ); - } - - inline LIST_FORCE_INLINE list_reverse_iterator &operator++() noexcept { - cata_assert( node_pointer != nullptr ); // covers uninitialized list_reverse_iterator - node_pointer = node_pointer->previous; - return *this; - } - - inline list_reverse_iterator operator++( int ) noexcept { - const list_reverse_iterator copy( *this ); - ++*this; - return copy; - } - - inline LIST_FORCE_INLINE list_reverse_iterator &operator--() noexcept { - cata_assert( node_pointer != nullptr ); - node_pointer = node_pointer->next; - return *this; - } - - inline list_reverse_iterator operator--( int ) noexcept { - const list_reverse_iterator copy( *this ); - --*this; - return copy; - } - - inline list_reverse_iterator &operator=( const list_reverse_iterator &rh ) noexcept { - node_pointer = rh.node_pointer; - return *this; - } - - inline list_reverse_iterator &operator=( const list_reverse_iterator < !is_const > &rh ) - noexcept { - node_pointer = rh.node_pointer; - return *this; - } - - inline list_reverse_iterator &operator=( list_reverse_iterator &&rh ) noexcept { - node_pointer = std::move( rh.node_pointer ); - return *this; - } - - inline list_reverse_iterator &operator=( const list_reverse_iterator < !is_const > && - rh ) noexcept { - node_pointer = std::move( rh.node_pointer ); - return *this; - } - - inline typename list::iterator base() const noexcept { - return typename list::iterator( node_pointer->next ); - } - - list_reverse_iterator() noexcept: node_pointer( NULL ) {} - - list_reverse_iterator( const list_reverse_iterator &source ) noexcept: node_pointer( - source.node_pointer ) {} - - list_reverse_iterator( const list_reverse_iterator &&source ) noexcept: node_pointer( std::move( - source.node_pointer ) ) {} - - private: - - explicit list_reverse_iterator( const node_pointer_type node_p ) noexcept: node_pointer( node_p ) {} - }; - - private: - - // Used by range-insert and range-constructor to prevent fill-insert and fill-constructor function calls mistakenly resolving to the range insert/constructor - template - struct plf_enable_if_c { - using type = T; - }; - - template - struct plf_enable_if_c { - }; - - group_vector groups; - node_base end_node; - node_pointer_type - last_endpoint; // last_endpoint being NULL means no elements have been constructed, but there may still be groups available due to clear() or reserve() - iterator end_iterator; // end_iterator is always the last entry point in last group in list (or one past the end of group) - iterator begin_iterator; - - // Packaging the group allocator with least-used member variables, for empty-base-class optimization - struct ebco_pair1 : node_pointer_allocator_type { - size_type total_number_of_elements; - explicit ebco_pair1( const size_type total_num_elements ) noexcept: total_number_of_elements( - total_num_elements ) {} - } node_pointer_allocator_pair; - - struct ebco_pair2 : node_allocator_type { - size_type number_of_erased_nodes; - explicit ebco_pair2( const size_type num_erased_nodes ) noexcept: number_of_erased_nodes( - num_erased_nodes ) {} - } node_allocator_pair; - - public: - - // Default constructor: - list() noexcept: - element_allocator_type( element_allocator_type() ), - end_node( reinterpret_cast( &end_node ), - reinterpret_cast( &end_node ) ), - last_endpoint( nullptr ), - end_iterator( reinterpret_cast( &end_node ) ), - begin_iterator( reinterpret_cast( &end_node ) ), - node_pointer_allocator_pair( 0 ), - node_allocator_pair( 0 ) - {} - - // Allocator-extended constructor: - explicit list( const element_allocator_type &alloc ): - element_allocator_type( alloc ), - end_node( reinterpret_cast( &end_node ), - reinterpret_cast( &end_node ) ), - last_endpoint( NULL ), - end_iterator( reinterpret_cast( &end_node ) ), - begin_iterator( reinterpret_cast( &end_node ) ), - node_pointer_allocator_pair( 0 ), - node_allocator_pair( 0 ) - {} - - // Copy constructor: - list( const list &source ): - element_allocator_type( source ), - end_node( reinterpret_cast( &end_node ), - reinterpret_cast( &end_node ) ), - last_endpoint( nullptr ), - end_iterator( reinterpret_cast( &end_node ) ), - begin_iterator( reinterpret_cast( &end_node ) ), - node_pointer_allocator_pair( 0 ), - node_allocator_pair( 0 ) { - reserve( source.node_pointer_allocator_pair.total_number_of_elements ); - insert( end_iterator, source.begin_iterator, source.end_iterator ); - } - - // Allocator-extended copy constructor: - list( const list &source, const allocator_type &alloc ): - element_allocator_type( alloc ), - end_node( reinterpret_cast( &end_node ), - reinterpret_cast( &end_node ) ), - last_endpoint( nullptr ), - end_iterator( reinterpret_cast( &end_node ) ), - begin_iterator( reinterpret_cast( &end_node ) ), - node_pointer_allocator_pair( 0 ), - node_allocator_pair( 0 ) { - reserve( source.node_pointer_allocator_pair.total_number_of_elements ); - insert( end_iterator, source.begin_iterator, source.end_iterator ); - } - - // Move constructor: - list( list &&source ) noexcept: - element_allocator_type( source ), - groups( std::move( source.groups ) ), - end_node( std::move( source.end_node ) ), - last_endpoint( std::move( source.last_endpoint ) ), - end_iterator( reinterpret_cast( &end_node ) ), - begin_iterator( ( source.begin_iterator.node_pointer == source.end_iterator.node_pointer ) ? - reinterpret_cast( &end_node ) : std::move( source.begin_iterator ) ), - node_pointer_allocator_pair( source.node_pointer_allocator_pair.total_number_of_elements ), - node_allocator_pair( source.node_allocator_pair.number_of_erased_nodes ) { - end_node.previous->next = begin_iterator.node_pointer->previous = end_iterator.node_pointer; - source.groups.blank(); - source.reset(); - } - - // Allocator-extended move constructor: - list( list &&source, const allocator_type &alloc ): - element_allocator_type( alloc ), - groups( std::move( source.groups ) ), - end_node( std::move( source.end_node ) ), - last_endpoint( std::move( source.last_endpoint ) ), - end_iterator( reinterpret_cast( &end_node ) ), - begin_iterator( ( source.begin_iterator.node_pointer == source.end_iterator.node_pointer ) ? - reinterpret_cast( &end_node ) : std::move( source.begin_iterator ) ), - node_pointer_allocator_pair( source.node_pointer_allocator_pair.total_number_of_elements ), - node_allocator_pair( source.node_allocator_pair.number_of_erased_nodes ) { - end_node.previous->next = begin_iterator.node_pointer->previous = end_iterator.node_pointer; - source.groups.blank(); - source.reset(); - } - - // Fill constructor: - list( const size_type fill_number, const element_type &element, - const element_allocator_type &alloc = element_allocator_type() ): - element_allocator_type( alloc ), - end_node( reinterpret_cast( &end_node ), - reinterpret_cast( &end_node ) ), - last_endpoint( nullptr ), - end_iterator( reinterpret_cast( &end_node ) ), - begin_iterator( reinterpret_cast( &end_node ) ), - node_pointer_allocator_pair( 0 ), - node_allocator_pair( 0 ) { - reserve( fill_number ); - insert( end_iterator, fill_number, element ); - } - - // Range constructor: - template - list( const typename plf_enable_if_c < !std::numeric_limits::is_integer, - iterator_type >::type &first, const iterator_type &last, - const element_allocator_type &alloc = element_allocator_type() ): - element_allocator_type( alloc ), - end_node( reinterpret_cast( &end_node ), - reinterpret_cast( &end_node ) ), - last_endpoint( nullptr ), - end_iterator( reinterpret_cast( &end_node ) ), - begin_iterator( reinterpret_cast( &end_node ) ), - node_pointer_allocator_pair( 0 ), - node_allocator_pair( 0 ) { - insert( end_iterator, first, last ); - } - - // Initializer-list constructor: - list( const std::initializer_list &element_list, - const element_allocator_type &alloc = element_allocator_type() ): - element_allocator_type( alloc ), - end_node( reinterpret_cast( &end_node ), - reinterpret_cast( &end_node ) ), - last_endpoint( nullptr ), - end_iterator( reinterpret_cast( &end_node ) ), - begin_iterator( reinterpret_cast( &end_node ) ), - node_pointer_allocator_pair( 0 ), - node_allocator_pair( 0 ) { - reserve( element_list.size() ); - insert( end_iterator, element_list ); - } - - ~list() noexcept { - groups.destroy_all_data( last_endpoint ); - } - - inline iterator begin() noexcept { - return begin_iterator; - } - - inline const_iterator begin() const noexcept { - return begin_iterator; - } - - inline iterator end() noexcept { - return end_iterator; - } - - inline const_iterator end() const noexcept { - return end_iterator; - } - - inline const_iterator cbegin() const noexcept { - return const_iterator( begin_iterator.node_pointer ); - } - - inline const_iterator cend() const noexcept { - return const_iterator( end_iterator.node_pointer ); - } - - inline reverse_iterator rbegin() const noexcept { - return reverse_iterator( end_node.previous ); - } - - inline reverse_iterator rend() const noexcept { - return reverse_iterator( end_iterator.node_pointer ); - } - - inline const_reverse_iterator crbegin() const noexcept { - return const_reverse_iterator( end_node.previous ); - } - - inline const_reverse_iterator crend() const noexcept { - return const_reverse_iterator( end_iterator.node_pointer ); - } - - inline reference front() { - cata_assert( begin_iterator.node_pointer != &end_node ); - return begin_iterator.node_pointer->element; - } - - inline const_reference front() const { - cata_assert( begin_iterator.node_pointer != &end_node ); - return begin_iterator.node_pointer->element; - } - - inline reference back() { - cata_assert( end_node.previous != &end_node ); - return end_node.previous->element; - } - - inline const_reference back() const { - cata_assert( end_node.previous != &end_node ); - return end_node.previous->element; - } - - void clear() noexcept { - if( last_endpoint == nullptr ) { - return; - } - - if( node_pointer_allocator_pair.total_number_of_elements != 0 ) { - groups.clear( last_endpoint ); - } - - end_node.next = reinterpret_cast( &end_node ); - end_node.previous = reinterpret_cast( &end_node ); - last_endpoint = groups.block_pointer->nodes; - begin_iterator.node_pointer = end_iterator.node_pointer; - node_pointer_allocator_pair.total_number_of_elements = 0; - node_allocator_pair.number_of_erased_nodes = 0; - } - - private: - - void reset() noexcept { - groups.destroy_all_data( last_endpoint ); - last_endpoint = nullptr; - end_node.next = reinterpret_cast( &end_node ); - end_node.previous = reinterpret_cast( &end_node ); - begin_iterator.node_pointer = end_iterator.node_pointer; - node_pointer_allocator_pair.total_number_of_elements = 0; - node_allocator_pair.number_of_erased_nodes = 0; - } - - public: - - iterator insert( const iterator it, const element_type &element ) { - // ie. list is not empty - if( last_endpoint != nullptr ) { - // No erased nodes available for reuse - if( node_allocator_pair.number_of_erased_nodes == 0 ) { - // last_endpoint is beyond the end of a group - if( last_endpoint == groups.last_endpoint_group->beyond_end ) { - // ie. there are no reusable groups available at the back of group vector - if( static_cast( groups.last_endpoint_group - groups.block_pointer ) == groups.size - - 1 ) { - groups.add_new( ( node_pointer_allocator_pair.total_number_of_elements < LIST_BLOCK_MAX ) ? - static_cast( node_pointer_allocator_pair.total_number_of_elements ) : - LIST_BLOCK_MAX ); - } else { - ++groups.last_endpoint_group; - } - - last_endpoint = groups.last_endpoint_group->nodes; - } - - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, last_endpoint, it.node_pointer, - it.node_pointer->previous, element ); - - ++( groups.last_endpoint_group->number_of_elements ); - ++node_pointer_allocator_pair.total_number_of_elements; - - if( it.node_pointer == begin_iterator.node_pointer ) { - begin_iterator.node_pointer = last_endpoint; - } - - it.node_pointer->previous->next = last_endpoint; - it.node_pointer->previous = last_endpoint; - - return iterator( last_endpoint++ ); - } else { - group_pointer_type const node_group = groups.get_nearest_freelist_group( ( - it.node_pointer != end_iterator.node_pointer ) ? it.node_pointer : end_node.previous ); - node_pointer_type const selected_node = node_group->free_list_head; - const node_pointer_type previous = node_group->free_list_head->previous; - - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, selected_node, it.node_pointer, - it.node_pointer->previous, element ); - - node_group->free_list_head = previous; - ++( node_group->number_of_elements ); - ++node_pointer_allocator_pair.total_number_of_elements; - --node_allocator_pair.number_of_erased_nodes; - - it.node_pointer->previous->next = selected_node; - it.node_pointer->previous = selected_node; - - if( it.node_pointer == begin_iterator.node_pointer ) { - begin_iterator.node_pointer = selected_node; - } - - return iterator( selected_node ); - } - } else { // list is empty - // In case of prior reserve/clear call as opposed to being uninitialized - if( groups.block_pointer == nullptr ) { - groups.initialize( LIST_BLOCK_MIN ); - } - - groups.last_endpoint_group->number_of_elements = 1; - end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = - groups.last_endpoint_group->nodes; - node_pointer_allocator_pair.total_number_of_elements = 1; - - if LIST_CONSTEXPR( - std::is_nothrow_copy_constructible::value ) { // Avoid try-catch code generation - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, last_endpoint++, - end_iterator.node_pointer, end_iterator.node_pointer, element ); - } else { - try { - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, last_endpoint++, - end_iterator.node_pointer, end_iterator.node_pointer, element ); - } catch( ... ) { - reset(); - throw; - } - } - - return begin_iterator; - } - } - - inline LIST_FORCE_INLINE void push_back( const element_type &element ) { - insert( end_iterator, element ); - } - - inline LIST_FORCE_INLINE void push_front( const element_type &element ) { - insert( begin_iterator, element ); - } - - // This is almost identical to the insert implementation above with the only change being std::move of the element - iterator insert( const iterator it, element_type &&element ) { - if( last_endpoint != nullptr ) { - if( node_allocator_pair.number_of_erased_nodes == 0 ) { - if( last_endpoint == groups.last_endpoint_group->beyond_end ) { - if( static_cast( groups.last_endpoint_group - groups.block_pointer ) == groups.size - - 1 ) { - groups.add_new( ( node_pointer_allocator_pair.total_number_of_elements < LIST_BLOCK_MAX ) ? - static_cast( node_pointer_allocator_pair.total_number_of_elements ) : - LIST_BLOCK_MAX ); - } else { - ++groups.last_endpoint_group; - } - - last_endpoint = groups.last_endpoint_group->nodes; - } - - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, last_endpoint, it.node_pointer, - it.node_pointer->previous, std::move( element ) ); - - ++( groups.last_endpoint_group->number_of_elements ); - ++node_pointer_allocator_pair.total_number_of_elements; - - if( it.node_pointer == begin_iterator.node_pointer ) { - begin_iterator.node_pointer = last_endpoint; - } - - it.node_pointer->previous->next = last_endpoint; - it.node_pointer->previous = last_endpoint; - - return iterator( last_endpoint++ ); - } else { - group_pointer_type const node_group = groups.get_nearest_freelist_group( ( - it.node_pointer != end_iterator.node_pointer ) ? it.node_pointer : end_node.previous ); - node_pointer_type const selected_node = node_group->free_list_head; - const node_pointer_type previous = node_group->free_list_head->previous; - - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, selected_node, it.node_pointer, - it.node_pointer->previous, std::move( element ) ); - - node_group->free_list_head = previous; - ++( node_group->number_of_elements ); - ++node_pointer_allocator_pair.total_number_of_elements; - --node_allocator_pair.number_of_erased_nodes; - - it.node_pointer->previous->next = selected_node; - it.node_pointer->previous = selected_node; - - if( it.node_pointer == begin_iterator.node_pointer ) { - begin_iterator.node_pointer = selected_node; - } - - return iterator( selected_node ); - } - } else { - if( groups.block_pointer == nullptr ) { - groups.initialize( LIST_BLOCK_MIN ); - } - - groups.last_endpoint_group->number_of_elements = 1; - end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = - groups.last_endpoint_group->nodes; - node_pointer_allocator_pair.total_number_of_elements = 1; - - if LIST_CONSTEXPR( std::is_nothrow_move_constructible::value ) { - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, last_endpoint++, - end_iterator.node_pointer, end_iterator.node_pointer, std::move( element ) ); - - } else { - try { - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, last_endpoint++, - end_iterator.node_pointer, end_iterator.node_pointer, std::move( element ) ); - } catch( ... ) { - reset(); - throw; - } - } - - return begin_iterator; - } - } - - inline LIST_FORCE_INLINE void push_back( element_type &&element ) { - insert( end_iterator, std::move( element ) ); - } - - inline LIST_FORCE_INLINE void push_front( element_type &&element ) { - insert( begin_iterator, std::move( element ) ); - } - - // This is almost identical to the insert implementations above with the only changes being std::forward of element parameters - template - iterator emplace( const iterator it, arguments &&... parameters ) { - if( last_endpoint != nullptr ) { - if( node_allocator_pair.number_of_erased_nodes == 0 ) { - if( last_endpoint == groups.last_endpoint_group->beyond_end ) { - if( static_cast( groups.last_endpoint_group - groups.block_pointer ) == groups.size - - 1 ) { - groups.add_new( ( node_pointer_allocator_pair.total_number_of_elements < LIST_BLOCK_MAX ) ? - static_cast( node_pointer_allocator_pair.total_number_of_elements ) : - LIST_BLOCK_MAX ); - } else { - ++groups.last_endpoint_group; - } - - last_endpoint = groups.last_endpoint_group->nodes; - } - - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, last_endpoint, it.node_pointer, - it.node_pointer->previous, std::forward( parameters )... ); - - ++( groups.last_endpoint_group->number_of_elements ); - ++node_pointer_allocator_pair.total_number_of_elements; - - if( it.node_pointer == begin_iterator.node_pointer ) { - begin_iterator.node_pointer = last_endpoint; - } - - it.node_pointer->previous->next = last_endpoint; - it.node_pointer->previous = last_endpoint; - - return iterator( last_endpoint++ ); - } else { - group_pointer_type const node_group = groups.get_nearest_freelist_group( ( - it.node_pointer != end_iterator.node_pointer ) ? it.node_pointer : end_node.previous ); - node_pointer_type const selected_node = node_group->free_list_head; - const node_pointer_type previous = node_group->free_list_head->previous; - - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, selected_node, it.node_pointer, - it.node_pointer->previous, std::forward( parameters )... ); - - node_group->free_list_head = previous; - ++( node_group->number_of_elements ); - ++node_pointer_allocator_pair.total_number_of_elements; - --node_allocator_pair.number_of_erased_nodes; - - it.node_pointer->previous->next = selected_node; - it.node_pointer->previous = selected_node; - - if( it.node_pointer == begin_iterator.node_pointer ) { - begin_iterator.node_pointer = selected_node; - } - - return iterator( selected_node ); - } - } else { - if( groups.block_pointer == nullptr ) { - groups.initialize( LIST_BLOCK_MIN ); - } - - groups.last_endpoint_group->number_of_elements = 1; - end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = - groups.last_endpoint_group->nodes; - node_pointer_allocator_pair.total_number_of_elements = 1; - - if LIST_CONSTEXPR( std::is_nothrow_constructible::value ) { - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, last_endpoint++, - end_iterator.node_pointer, end_iterator.node_pointer, std::forward( parameters )... ); - } else { - try { - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, last_endpoint++, - end_iterator.node_pointer, end_iterator.node_pointer, std::forward( parameters )... ); - } catch( ... ) { - reset(); - throw; - } - } - - return begin_iterator; - } - } - - template - inline LIST_FORCE_INLINE reference emplace_back( arguments &&... parameters ) { - return emplace( end_iterator, std::forward( parameters )... ).node_pointer->element; - } - - template - inline LIST_FORCE_INLINE reference emplace_front( arguments &&... parameters ) { - return emplace( begin_iterator, - std::forward( parameters )... ).node_pointer->element; - } - - private: - - void group_fill_position( const element_type &element, group_size_type number_of_elements, - node_pointer_type const position ) { - position->previous->next = last_endpoint; - groups.last_endpoint_group->number_of_elements += number_of_elements; - node_pointer_type previous = position->previous; - - do { - if LIST_CONSTEXPR( std::is_nothrow_copy_constructible::value ) { - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, last_endpoint, last_endpoint + 1, - previous, element ); - } else { - try { - LIST_CONSTRUCT( node_allocator_type, node_allocator_pair, last_endpoint, last_endpoint + 1, - previous, element ); - } catch( ... ) { - previous->next = position; - position->previous = --previous; - groups.last_endpoint_group->number_of_elements -= static_cast - ( number_of_elements - ( last_endpoint - position ) ); - throw; - } - } - - previous = last_endpoint++; - } while( --number_of_elements != 0 ); - - previous->next = position; - position->previous = previous; - } - - public: - - // Fill insert - iterator insert( iterator position, const size_type number_of_elements, - const element_type &element ) { - if( number_of_elements == 0 ) { - return end_iterator; - } else if( number_of_elements == 1 ) { - return insert( position, element ); - } - - if( node_pointer_allocator_pair.total_number_of_elements == 0 && last_endpoint != nullptr && - ( static_cast( groups.block_pointer->beyond_end - groups.block_pointer->nodes ) < - number_of_elements ) && - ( static_cast( groups.block_pointer->beyond_end - groups.block_pointer->nodes ) < - LIST_BLOCK_MAX ) ) { - reset(); - } - - if( groups.block_pointer == nullptr ) { // ie. Uninitialized list - if( number_of_elements > LIST_BLOCK_MAX ) { - size_type multiples = number_of_elements / LIST_BLOCK_MAX; - const group_size_type remainder = static_cast( number_of_elements - - ( multiples++ * LIST_BLOCK_MAX ) ); // ++ to aid while loop below - - // Create and fill first group: - if( remainder != 0 ) { // make sure smallest block is first - if( remainder >= LIST_BLOCK_MIN ) { - groups.initialize( remainder ); - end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = - groups.last_endpoint_group->nodes; - group_fill_position( element, remainder, end_iterator.node_pointer ); - } else { - // Create first group as BLOCK_MIN size then subtract difference between BLOCK_MIN and remainder from next group: - groups.initialize( LIST_BLOCK_MIN ); - end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = - groups.last_endpoint_group->nodes; - group_fill_position( element, LIST_BLOCK_MIN, end_iterator.node_pointer ); - - groups.add_new( LIST_BLOCK_MAX - ( LIST_BLOCK_MIN - remainder ) ); - end_node.previous = last_endpoint = groups.last_endpoint_group->nodes; - group_fill_position( element, LIST_BLOCK_MAX - ( LIST_BLOCK_MIN - remainder ), - end_iterator.node_pointer ); - --multiples; - } - } else { - groups.initialize( LIST_BLOCK_MAX ); - end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = - groups.last_endpoint_group->nodes; - group_fill_position( element, LIST_BLOCK_MAX, end_iterator.node_pointer ); - --multiples; - } - - while( --multiples != 0 ) { - groups.add_new( LIST_BLOCK_MAX ); - end_node.previous = last_endpoint = groups.last_endpoint_group->nodes; - group_fill_position( element, LIST_BLOCK_MAX, end_iterator.node_pointer ); - } - - } else { - groups.initialize( ( number_of_elements < LIST_BLOCK_MIN ) ? LIST_BLOCK_MIN : - static_cast( number_of_elements ) ); // Construct first group - end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = - groups.last_endpoint_group->nodes; - group_fill_position( element, static_cast( number_of_elements ), - end_iterator.node_pointer ); - } - - node_pointer_allocator_pair.total_number_of_elements = number_of_elements; - return begin_iterator; - } else { - // Insert first element, then use up any erased nodes: - size_type remainder = number_of_elements - 1; - const iterator return_iterator = insert( position, element ); - - while( node_allocator_pair.number_of_erased_nodes != 0 ) { - insert( position, element ); - --node_allocator_pair.number_of_erased_nodes; - - if( --remainder == 0 ) { - return return_iterator; - } - } - - node_pointer_allocator_pair.total_number_of_elements += remainder; - - // then use up remainder of last_endpoint_group: - const group_size_type remaining_nodes_in_group = static_cast - ( groups.last_endpoint_group->beyond_end - last_endpoint ); - - if( remaining_nodes_in_group != 0 ) { - if( remaining_nodes_in_group < remainder ) { - group_fill_position( element, remaining_nodes_in_group, position.node_pointer ); - remainder -= remaining_nodes_in_group; - } else { - group_fill_position( element, static_cast( remainder ), position.node_pointer ); - return return_iterator; - } - } - - // use up trailing groups: - while( ( groups.last_endpoint_group != ( groups.block_pointer + groups.size - 1 ) ) & - ( remainder != 0 ) ) { - last_endpoint = ( ++groups.last_endpoint_group )->nodes; - const group_size_type group_size = static_cast - ( groups.last_endpoint_group->beyond_end - groups.last_endpoint_group->nodes ); - - if( group_size < remainder ) { - group_fill_position( element, group_size, position.node_pointer ); - remainder -= group_size; - } else { - group_fill_position( element, static_cast( remainder ), position.node_pointer ); - return return_iterator; - } - } - - size_type multiples = remainder / static_cast( LIST_BLOCK_MAX ); - remainder -= multiples * LIST_BLOCK_MAX; - - while( multiples-- != 0 ) { - groups.add_new( LIST_BLOCK_MAX ); - last_endpoint = groups.last_endpoint_group->nodes; - group_fill_position( element, LIST_BLOCK_MAX, position.node_pointer ); - } - - if( remainder != - 0 ) { // Bit annoying to create a large block to house a lower number of elements, but beats the alternatives - groups.add_new( LIST_BLOCK_MAX ); - last_endpoint = groups.last_endpoint_group->nodes; - group_fill_position( element, static_cast( remainder ), position.node_pointer ); - } - - return return_iterator; - } - } - - // Range insert - template - iterator insert( const iterator it, - typename plf_enable_if_c < !std::numeric_limits::is_integer, - iterator_type >::type first, const iterator_type last ) { - if( first == last ) { - return end_iterator; - } - - const iterator return_iterator = insert( it, *first ); - - while( ++first != last ) { - insert( it, *first ); - } - - return return_iterator; - } - - // Initializer-list insert - inline iterator insert( const iterator it, - const std::initializer_list &element_list ) { - // use range insert: - return insert( it, element_list.begin(), element_list.end() ); - } - - private: - - inline LIST_FORCE_INLINE void destroy_all_node_pointers( group_pointer_type const - group_to_process, const node_pointer_type beyond_end_node ) noexcept { - for( node_pointer_type current_node = group_to_process->nodes; current_node != beyond_end_node; - ++current_node ) { - LIST_DESTROY( node_pointer_allocator_type, node_pointer_allocator_pair, - &( current_node->next ) ); // Destruct element - LIST_DESTROY( node_pointer_allocator_type, node_pointer_allocator_pair, - &( current_node->previous ) ); // Destruct element - } - } - - public: - - // Single erase: - // if uninitialized/invalid iterator supplied, function could generate an exception, hence no noexcept - iterator erase( const const_iterator it ) { - cata_assert( node_pointer_allocator_pair.total_number_of_elements != 0 ); - cata_assert( it.node_pointer != nullptr ); - cata_assert( it.node_pointer != end_iterator.node_pointer ); - - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - LIST_DESTROY( element_allocator_type, ( *this ), - &( it.node_pointer->element ) ); // Destruct element - } - - --node_pointer_allocator_pair.total_number_of_elements; - ++node_allocator_pair.number_of_erased_nodes; - - group_pointer_type node_group = groups.last_searched_group; - - // find nearest group with reusable (erased element) memory location: - if( ( it.node_pointer < node_group->nodes ) || ( it.node_pointer >= node_group->beyond_end ) ) { - // Search left and right: - const group_pointer_type beyond_end_group = groups.last_endpoint_group + 1; - group_pointer_type left = node_group - 1; - bool right_not_beyond_back = ++node_group < beyond_end_group; - bool left_not_beyond_front = left >= groups.block_pointer; - - while( true ) { - if( right_not_beyond_back ) { - if( ( it.node_pointer < node_group->beyond_end ) && - ( it.node_pointer >= node_group->nodes ) ) { // usable location found - break; - } - - right_not_beyond_back = ( ++node_group < beyond_end_group ); - } - - if( left_not_beyond_front ) { - if( ( it.node_pointer >= left->nodes ) && - ( it.node_pointer < left->beyond_end ) ) { // usable location found - node_group = left; - break; - } - - left_not_beyond_front = ( --left >= groups.block_pointer ); - } - } - - groups.last_searched_group = node_group; - } - - it.node_pointer->next->previous = it.node_pointer->previous; - it.node_pointer->previous->next = it.node_pointer->next; - - if( it.node_pointer == begin_iterator.node_pointer ) { - begin_iterator.node_pointer = it.node_pointer->next; - } - - const iterator return_iterator( it.node_pointer->next ); - - // ie. group is not empty yet, add node to free list - if( --( node_group->number_of_elements ) != 0 ) { - // next == NULL so that destructor can detect the free list item as opposed to non-free-list item - it.node_pointer->next = nullptr; - it.node_pointer->previous = node_group->free_list_head; - node_group->free_list_head = it.node_pointer; - return return_iterator; - } else if( node_group != groups.last_endpoint_group-- ) { - // remove group (and decrement active back group) - const group_size_type group_size = static_cast( node_group->beyond_end - - node_group->nodes ); - node_allocator_pair.number_of_erased_nodes -= group_size; - - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - destroy_all_node_pointers( node_group, node_group->beyond_end ); - } - - node_group->free_list_head = nullptr; - - // Preserve only last (active) group or second/third-to-last group - seems to be best for performance under high-modification benchmarks - if( ( group_size == LIST_BLOCK_MAX ) | ( node_group >= groups.last_endpoint_group - 1 ) ) { - groups.move_to_back( node_group ); - } else { - groups.remove( node_group ); - } - - return return_iterator; - } else { // clear back group, leave trailing - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - destroy_all_node_pointers( node_group, last_endpoint ); - } - - node_group->free_list_head = nullptr; - - if( node_pointer_allocator_pair.total_number_of_elements != 0 ) { - node_allocator_pair.number_of_erased_nodes -= static_cast - ( last_endpoint - node_group->nodes ); - last_endpoint = groups.last_endpoint_group->beyond_end; - } else { - // If number of elements is zero, it indicates that this was the first group in the vector. In which case the last_endpoint_group would be invalid at this point due to the decrement in the above else-if statement. So it needs to be reset, as it will not be reset in the function call below. - groups.last_endpoint_group = groups.block_pointer; - clear(); - } - - return return_iterator; - } - } - - // Range-erase: - // if uninitialized/invalid iterator supplied, function could generate an exception - inline void erase( const const_iterator iterator1, const const_iterator iterator2 ) { - for( const_iterator current = iterator1; current != iterator2; ) { - current = erase( current ); - } - } - - inline void pop_back() { // Exception will occur on empty list - erase( iterator( end_node.previous ) ); - } - - inline void pop_front() { // Exception will occur on empty list - erase( begin_iterator ); - } - - inline list &operator=( const list &source ) { - cata_assert( &source != this ); - - clear(); - reserve( source.node_pointer_allocator_pair.total_number_of_elements ); - insert( end_iterator, source.begin_iterator, source.end_iterator ); - - return *this; - } - - // Move assignment - list &operator=( list &&source ) LIST_NOEXCEPT_MOVE_ASSIGNMENT( allocator_type ) { - cata_assert( &source != this ); - - // Move source values across: - groups.destroy_all_data( last_endpoint ); - - groups = std::move( source.groups ); - end_node = std::move( source.end_node ); - last_endpoint = std::move( source.last_endpoint ); - begin_iterator.node_pointer = ( source.begin_iterator.node_pointer == - source.end_iterator.node_pointer ) ? end_iterator.node_pointer : std::move( - source.begin_iterator.node_pointer ); - node_pointer_allocator_pair.total_number_of_elements = - source.node_pointer_allocator_pair.total_number_of_elements; - node_allocator_pair.number_of_erased_nodes = source.node_allocator_pair.number_of_erased_nodes; - - end_node.previous->next = begin_iterator.node_pointer->previous = end_iterator.node_pointer; - - source.groups.blank(); - source.reset(); - return *this; - } - - bool operator==( const list &rh ) const noexcept { - cata_assert( this != &rh ); - - if( node_pointer_allocator_pair.total_number_of_elements != - rh.node_pointer_allocator_pair.total_number_of_elements ) { - return false; - } - - iterator rh_iterator = rh.begin_iterator; - - for( iterator lh_iterator = begin_iterator; lh_iterator != end_iterator; ) { - if( *rh_iterator++ != *lh_iterator++ ) { - return false; - } - } - - return true; - } - - inline bool operator!=( const list &rh ) const noexcept { - return !( *this == rh ); - } - - inline bool empty() const noexcept { - return node_pointer_allocator_pair.total_number_of_elements == 0; - } - - inline size_type size() const noexcept { - return node_pointer_allocator_pair.total_number_of_elements; - } - - inline size_type max_size() const noexcept { - return std::allocator_traits::max_size( *this ); - } - - inline size_type capacity() const noexcept { - return groups.element_allocator_pair.capacity; - } - - inline size_type approximate_memory_use() const noexcept { - return static_cast( sizeof( *this ) + ( groups.element_allocator_pair.capacity * sizeof( - node ) ) + ( sizeof( group ) * groups.group_allocator_pair.capacity ) ); - } - - private: - - struct less { - inline bool operator()( const element_type &a, const element_type &b ) const noexcept { - return a < b; - } - }; - - // Function-object to redirect the sort function to sort pointers by the elements they point to, not the pointer value - template - struct sort_dereferencer { - comparison_function stored_instance; - - explicit sort_dereferencer( const comparison_function &function_instance ): - stored_instance( function_instance ) - {} - - sort_dereferencer() noexcept = default; - - inline bool operator()( const node_pointer_type first, const node_pointer_type second ) { - return stored_instance( first->element, second->element ); - } - }; - - public: - - template - void sort( comparison_function compare ) { - if( node_pointer_allocator_pair.total_number_of_elements < 2 ) { - return; - } - - node_pointer_type *const node_pointers = LIST_ALLOCATE( node_pointer_allocator_type, - node_pointer_allocator_pair, node_pointer_allocator_pair.total_number_of_elements, nullptr ); - node_pointer_type *node_pointer = node_pointers; - - // According to the C++ standard, construction of a pointer (of any type) may not trigger an exception - hence, no try-catch blocks are necessary for constructing the pointers: - for( group_pointer_type current_group = groups.block_pointer; - current_group != groups.last_endpoint_group; ++current_group ) { - const node_pointer_type end = current_group->beyond_end; - - if( ( end - current_group->nodes ) != - current_group->number_of_elements ) { // If there are erased nodes present in the group - for( node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node ) { - if( current_node->next != nullptr ) { // is not free list node - LIST_CONSTRUCT( node_pointer_allocator_type, node_pointer_allocator_pair, node_pointer++, - current_node ); - } - } - } else { - for( node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node ) { - LIST_CONSTRUCT( node_pointer_allocator_type, node_pointer_allocator_pair, node_pointer++, - current_node ); - } - } - } - - if( ( last_endpoint - groups.last_endpoint_group->nodes ) != - groups.last_endpoint_group->number_of_elements ) { // If there are erased nodes present in the group - for( node_pointer_type current_node = groups.last_endpoint_group->nodes; - current_node != last_endpoint; ++current_node ) { - if( current_node->next != nullptr ) { - LIST_CONSTRUCT( node_pointer_allocator_type, node_pointer_allocator_pair, node_pointer++, - current_node ); - } - } - } else { - for( node_pointer_type current_node = groups.last_endpoint_group->nodes; - current_node != last_endpoint; ++current_node ) { - LIST_CONSTRUCT( node_pointer_allocator_type, node_pointer_allocator_pair, node_pointer++, - current_node ); - } - } - - std::sort( node_pointers, node_pointers + node_pointer_allocator_pair.total_number_of_elements, - sort_dereferencer( compare ) ); - - begin_iterator.node_pointer = node_pointers[0]; - begin_iterator.node_pointer->next = node_pointers[1]; - begin_iterator.node_pointer->previous = end_iterator.node_pointer; - - end_node.next = node_pointers[0]; - end_node.previous = node_pointers[node_pointer_allocator_pair.total_number_of_elements - 1]; - end_node.previous->next = end_iterator.node_pointer; - end_node.previous->previous = node_pointers[node_pointer_allocator_pair.total_number_of_elements - - 2]; - - node_pointer_type *const back = node_pointers + - node_pointer_allocator_pair.total_number_of_elements - 1; - - for( node_pointer = node_pointers + 1; node_pointer != back; ++node_pointer ) { - ( *node_pointer )->next = *( node_pointer + 1 ); - ( *node_pointer )->previous = *( node_pointer - 1 ); - - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - LIST_DESTROY( node_pointer_allocator_type, node_pointer_allocator_pair, node_pointer - 1 ); - } - } - - if LIST_CONSTEXPR( !std::is_trivially_destructible::value ) { - LIST_DESTROY( node_pointer_allocator_type, node_pointer_allocator_pair, back ); - } - - LIST_DEALLOCATE( node_pointer_allocator_type, node_pointer_allocator_pair, node_pointers, - node_pointer_allocator_pair.total_number_of_elements ); - } - - inline void sort() { - sort( less() ); - } - - void reorder( const iterator position, const iterator first, const iterator last ) noexcept { - last.node_pointer->next->previous = first.node_pointer->previous; - first.node_pointer->previous->next = last.node_pointer->next; - - last.node_pointer->next = position.node_pointer; - first.node_pointer->previous = position.node_pointer->previous; - - position.node_pointer->previous->next = first.node_pointer; - position.node_pointer->previous = last.node_pointer; - - if( begin_iterator == position ) { - begin_iterator = first; - } - } - - inline void reorder( const iterator position, const iterator location ) noexcept { - reorder( position, location, location ); - } - - void reserve( size_type reserve_amount ) { - if( reserve_amount == 0 || reserve_amount <= groups.element_allocator_pair.capacity ) { - return; - } else if( reserve_amount < LIST_BLOCK_MIN ) { - reserve_amount = LIST_BLOCK_MIN; - } else if( reserve_amount > max_size() ) { - reserve_amount = max_size(); - } - - if( groups.block_pointer != nullptr && node_pointer_allocator_pair.total_number_of_elements == 0 ) { - // edge case: has been filled with elements then clear()'d - some groups may be smaller than would be desired, should be replaced - group_size_type end_group_size = static_cast( ( groups.block_pointer + groups.size - - 1 )->beyond_end - ( groups.block_pointer + groups.size - 1 )->nodes ); - - // if last group isn't large enough, remove all groups - if( reserve_amount > end_group_size && end_group_size != LIST_BLOCK_MAX ) { - reset(); - } else { - size_type number_of_full_groups_needed = reserve_amount / LIST_BLOCK_MAX; - group_size_type remainder = static_cast( reserve_amount - - ( number_of_full_groups_needed * LIST_BLOCK_MAX ) ); - - // Remove any max_size groups which're not needed and any groups that're smaller than remainder: - for( group_pointer_type current_group = groups.block_pointer; - current_group < groups.block_pointer + groups.size; ) { - const group_size_type current_group_size = static_cast - ( groups.block_pointer->beyond_end - groups.block_pointer->nodes ); - - if( number_of_full_groups_needed != 0 && current_group_size == LIST_BLOCK_MAX ) { - --number_of_full_groups_needed; - ++current_group; - } else if( remainder != 0 && current_group_size >= remainder ) { - remainder = 0; - ++current_group; - } else { - groups.remove( current_group ); - } - } - - last_endpoint = groups.block_pointer->nodes; - } - } - - reserve_amount -= groups.element_allocator_pair.capacity; - - // To correct from possible reallocation caused by add_new: - const difference_type last_endpoint_group_number = groups.last_endpoint_group - - groups.block_pointer; - - size_type number_of_full_groups = reserve_amount / LIST_BLOCK_MAX; - reserve_amount -= ( number_of_full_groups++ * LIST_BLOCK_MAX ); // ++ to aid while loop below - - // Previously uninitialized list or reset in above if statement; most common scenario - if( groups.block_pointer == nullptr ) { - if( reserve_amount != 0 ) { - groups.initialize( static_cast( ( ( reserve_amount < LIST_BLOCK_MIN ) ? - LIST_BLOCK_MIN : reserve_amount ) ) ); - } else { - groups.initialize( LIST_BLOCK_MAX ); - --number_of_full_groups; - } - } else if( reserve_amount != 0 ) { - // Create a group at least as large as the last group - may allocate more than necessary, but better solution than creating a very small group in the middle of the group vector, I think: - const group_size_type last_endpoint_group_capacity = static_cast - ( groups.last_endpoint_group->beyond_end - groups.last_endpoint_group->nodes ); - groups.add_new( static_cast( ( reserve_amount < last_endpoint_group_capacity ) ? - last_endpoint_group_capacity : reserve_amount ) ); - } - - while( --number_of_full_groups != 0 ) { - groups.add_new( LIST_BLOCK_MAX ); - } - - groups.last_endpoint_group = groups.block_pointer + last_endpoint_group_number; - } - - inline LIST_FORCE_INLINE void free_unused_memory() noexcept { - groups.trim_trailing_groups(); - } - - void shrink_to_fit() { - if( ( last_endpoint == nullptr ) | ( node_pointer_allocator_pair.total_number_of_elements == - groups.element_allocator_pair.capacity ) ) { // uninitialized list or full - return; - } else if( node_pointer_allocator_pair.total_number_of_elements == 0 ) { // Edge case - reset(); - return; - } else if( node_allocator_pair.number_of_erased_nodes == 0 && - last_endpoint == - groups.last_endpoint_group->beyond_end ) { //edge case - currently no waste except for possible trailing groups - groups.trim_trailing_groups(); - return; - } - - list temp; - temp.reserve( node_pointer_allocator_pair.total_number_of_elements ); - - if LIST_CONSTEXPR( std::is_move_assignable::value && - std::is_move_constructible::value ) { // move elements if possible, otherwise copy them - temp.insert( temp.end_iterator, std::make_move_iterator( begin_iterator ), - std::make_move_iterator( end_iterator ) ); - } else { - temp.insert( temp.end_iterator, begin_iterator, end_iterator ); - } - - *this = std::move( temp ); - } - - private: - - void append_process( list &source ) { // used by merge and splice - if( last_endpoint != groups.last_endpoint_group->beyond_end ) { - // Add unused nodes to group's free list - const node_pointer_type back_node = last_endpoint - 1; - for( node_pointer_type current_node = groups.last_endpoint_group->beyond_end - 1; - current_node != back_node; --current_node ) { - current_node->next = nullptr; - current_node->previous = groups.last_endpoint_group->free_list_head; - groups.last_endpoint_group->free_list_head = current_node; - } - - node_allocator_pair.number_of_erased_nodes += static_cast - ( groups.last_endpoint_group->beyond_end - last_endpoint ); - } - - groups.append( source.groups ); - last_endpoint = source.last_endpoint; - node_pointer_allocator_pair.total_number_of_elements += - source.node_pointer_allocator_pair.total_number_of_elements; - source.reset(); - } - - public: - - void splice( iterator position, list &source ) { - cata_assert( &source != this ); - - if( source.node_pointer_allocator_pair.total_number_of_elements == 0 ) { - return; - } else if( node_pointer_allocator_pair.total_number_of_elements == 0 ) { - *this = std::move( source ); - return; - } - - if( position.node_pointer == - begin_iterator.node_pointer ) { // put source groups at front rather than back - swap( source ); - position.node_pointer = end_iterator.node_pointer; - } - - position.node_pointer->previous->next = source.begin_iterator.node_pointer; - source.begin_iterator.node_pointer->previous = position.node_pointer->previous; - position.node_pointer->previous = source.end_node.previous; - source.end_node.previous->next = position.node_pointer; - - append_process( source ); - } - - template - void merge( list &source, comparison_function compare ) { - cata_assert( &source != this ); - splice( ( source.node_pointer_allocator_pair.total_number_of_elements >= - node_pointer_allocator_pair.total_number_of_elements ) ? end_iterator : begin_iterator, source ); - sort( compare ); - } - - void merge( list &source ) { - cata_assert( &source != this ); - - if( source.node_pointer_allocator_pair.total_number_of_elements == 0 ) { - return; - } else if( node_pointer_allocator_pair.total_number_of_elements == 0 ) { - *this = std::move( source ); - return; - } - - node_pointer_type current1 = begin_iterator.node_pointer->next; - node_pointer_type current2 = source.begin_iterator.node_pointer->next; - node_pointer_type previous = source.begin_iterator.node_pointer; - const node_pointer_type source_end = source.end_iterator.node_pointer; - const node_pointer_type this_end = end_iterator.node_pointer; - - begin_iterator.node_pointer->next = source.begin_iterator.node_pointer; - source.begin_iterator.node_pointer->previous = begin_iterator.node_pointer; - - while( ( current1 != this_end ) & ( current2 != source_end ) ) { - previous->next = current1; - current1->previous = previous; - previous = current1; - current1 = current1->next; - - previous->next = current2; - current2->previous = previous; - previous = current2; - current2 = current2->next; - } - - if( current1 != this_end ) { - previous->next = current1; - current1->previous = previous; - } else { - end_node.previous = source.end_node.previous; - source.end_node.previous->next = end_iterator.node_pointer; - } - - append_process( source ); - } - - void reverse() noexcept { - if( node_pointer_allocator_pair.total_number_of_elements > 1 ) { - for( group_pointer_type current_group = groups.block_pointer; - current_group != groups.last_endpoint_group; ++current_group ) { - const node_pointer_type end = current_group->beyond_end; - - for( node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node ) { - if( current_node->next != nullptr ) { // is not free list node - // swap the pointers: - const node_pointer_type temp = current_node->next; - current_node->next = current_node->previous; - current_node->previous = temp; - } - } - } - - for( node_pointer_type current_node = groups.last_endpoint_group->nodes; - current_node != last_endpoint; ++current_node ) { - if( current_node->next != nullptr ) { - const node_pointer_type temp = current_node->next; - current_node->next = current_node->previous; - current_node->previous = temp; - } - } - - const node_pointer_type temp = end_node.previous; - end_node.previous = begin_iterator.node_pointer; - begin_iterator.node_pointer = temp; - - end_node.previous->next = end_iterator.node_pointer; - begin_iterator.node_pointer->previous = end_iterator.node_pointer; - } - } - - private: - - // Used by unique() - struct eq { - inline bool operator()( const element_type &a, const element_type &b ) const noexcept { - return a == b; - } - }; - - // Used by remove() - struct eq_to { - const element_type value; - - explicit eq_to( const element_type store_value ): - value( store_value ) - {} - - eq_to() noexcept = default; - - inline bool operator()( const element_type compare_value ) const noexcept { - return value == compare_value; - } - }; - - public: - - template - size_type unique( comparison_function compare ) { - const size_type original_number_of_elements = node_pointer_allocator_pair.total_number_of_elements; - - if( original_number_of_elements > 1 ) { - element_type *previous = &( begin_iterator.node_pointer->element ); - - for( iterator current = ++iterator( begin_iterator ); current != end_iterator; ) { - if( compare( *current, *previous ) ) { - current = erase( current ); - } else { - previous = &( current++.node_pointer->element ); - } - } - } - - return original_number_of_elements - node_pointer_allocator_pair.total_number_of_elements; - } - - inline size_type unique() { - return unique( eq() ); - } - - template - size_type remove_if( predicate_function predicate ) { - const size_type original_number_of_elements = node_pointer_allocator_pair.total_number_of_elements; - - if( original_number_of_elements != 0 ) { - for( group_pointer_type current_group = groups.block_pointer; - current_group != groups.last_endpoint_group; ++current_group ) { - group_size_type num_elements = current_group->number_of_elements; - const node_pointer_type end = current_group->beyond_end; - - // If there are erased nodes present in the group - if( end - current_group->nodes != num_elements ) { - for( node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node ) { - // is not free list node and validates predicate - if( current_node->next != nullptr && predicate( current_node->element ) ) { - erase( current_node ); - - // i.e. group will be empty (and removed) now - nothing left to iterate over - if( --num_elements == 0 ) { - // As current group has been removed, subsequent groups have already shifted back by one, hence, the ++ to the current group in the for loop is unnecessary, and negated here - --current_group; - break; - } - } - } - } else { // No erased nodes in group - for( node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node ) { - if( predicate( current_node->element ) ) { - erase( current_node ); - - if( --num_elements == 0 ) { - --current_group; - break; - } - } - } - } - } - - group_size_type num_elements = groups.last_endpoint_group->number_of_elements; - - // If there are erased nodes present in the group - if( last_endpoint - groups.last_endpoint_group->nodes != num_elements ) { - for( node_pointer_type current_node = groups.last_endpoint_group->nodes; - current_node != last_endpoint; ++current_node ) { - if( current_node->next != nullptr && predicate( current_node->element ) ) { - erase( current_node ); - - if( --num_elements == 0 ) { - break; - } - } - } - } else { - for( node_pointer_type current_node = groups.last_endpoint_group->nodes; - current_node != last_endpoint; ++current_node ) { - if( predicate( current_node->element ) ) { - erase( current_node ); - - if( --num_elements == 0 ) { - break; - } - } - } - } - } - - return original_number_of_elements - node_pointer_allocator_pair.total_number_of_elements; - } - - inline size_type remove( const element_type &value ) { - return remove_if( eq_to( value ) ); - } - - void resize( const size_type number_of_elements, const element_type &value = element_type() ) { - if( node_pointer_allocator_pair.total_number_of_elements == number_of_elements ) { - return; - } else if( number_of_elements == 0 ) { - clear(); - return; - } else if( node_pointer_allocator_pair.total_number_of_elements < number_of_elements ) { - insert( end_iterator, number_of_elements - node_pointer_allocator_pair.total_number_of_elements, - value ); - } else { // ie. node_pointer_allocator_pair.total_number_of_elements > number_of_elements - iterator current( end_node.previous ); - - for( size_type number_to_remove = node_pointer_allocator_pair.total_number_of_elements - - number_of_elements; number_to_remove != 0; --number_to_remove ) { - const node_pointer_type temp = current.node_pointer->previous; - erase( current ); - current.node_pointer = temp; - } - } - } - - // Range assign: - template - inline void assign( const typename plf_enable_if_c < - !std::numeric_limits::is_integer, iterator_type >::type first, - const iterator_type last ) { - clear(); - insert( end_iterator, first, last ); - groups.trim_trailing_groups(); - } - - // Fill assign: - inline void assign( const size_type number_of_elements, const element_type &value ) { - clear(); - reserve( number_of_elements ); // Will return anyway if capacity already > number_of_elements - insert( end_iterator, number_of_elements, value ); - } - - // Initializer-list assign: - inline void assign( const std::initializer_list &element_list ) { - clear(); - reserve( element_list.size() ); - insert( end_iterator, element_list ); - } - - inline allocator_type get_allocator() const noexcept { - return element_allocator_type(); - } - - void swap( list &source ) LIST_NOEXCEPT_SWAP( allocator_type ) { - list temp( std::move( source ) ); - source = std::move( *this ); - *this = std::move( temp ); - } -}; - -template -inline void swap( list &a, - list &b ) LIST_NOEXCEPT_SWAP( - swap_element_allocator_type ) -{ - a.swap( b ); -} - +template> +using list = plf::list; } // namespace cata -#undef LIST_BLOCK_MAX -#undef LIST_BLOCK_MIN - -#undef LIST_FORCE_INLINE - -#undef LIST_CONSTEXPR -#undef LIST_NOEXCEPT_SWAP -#undef LIST_NOEXCEPT_MOVE_ASSIGNMENT - -#undef LIST_CONSTRUCT -#undef LIST_DESTROY -#undef LIST_ALLOCATE -#undef LIST_ALLOCATE_INITIALIZATION -#undef LIST_DEALLOCATE - #endif // CATA_SRC_LIST_H diff --git a/src/third-party/plf/LICENSE b/src/third-party/plf/LICENSE new file mode 100644 index 00000000000..03d83887636 --- /dev/null +++ b/src/third-party/plf/LICENSE @@ -0,0 +1,19 @@ +zlib License + +(C) 2019 mattreecebentley + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. diff --git a/src/third-party/plf/colony.h b/src/third-party/plf/colony.h new file mode 100644 index 00000000000..c827967fe87 --- /dev/null +++ b/src/third-party/plf/colony.h @@ -0,0 +1,4082 @@ +// Copyright (c) 2019, Matthew Bentley (mattreecebentley@gmail.com) www.plflib.org + +// zLib license (https://www.zlib.net/zlib_license.html): +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgement in the product documentation would be +// appreciated but is not required. +// 2. Altered source versions must be plainly marked as such, and must not be +// misrepresented as being the original software. +// 3. This notice may not be removed or altered from any source distribution. + + +#ifndef PLF_COLONY_H +#define PLF_COLONY_H + + +// Compiler-specific defines used by colony: + +#if defined(_MSC_VER) + #define PLF_COLONY_FORCE_INLINE __forceinline + + #if _MSC_VER < 1600 + #define PLF_COLONY_NOEXCEPT throw() + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) throw() + #elif _MSC_VER == 1600 + #define PLF_COLONY_MOVE_SEMANTICS_SUPPORT + #define PLF_COLONY_NOEXCEPT throw() + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) throw() + #elif _MSC_VER == 1700 + #define PLF_COLONY_TYPE_TRAITS_SUPPORT + #define PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + #define PLF_COLONY_MOVE_SEMANTICS_SUPPORT + #define PLF_COLONY_NOEXCEPT throw() + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) throw() + #elif _MSC_VER == 1800 + #define PLF_COLONY_TYPE_TRAITS_SUPPORT + #define PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + #define PLF_COLONY_VARIADICS_SUPPORT // Variadics, in this context, means both variadic templates and variadic macros are supported + #define PLF_COLONY_MOVE_SEMANTICS_SUPPORT + #define PLF_COLONY_NOEXCEPT throw() + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) throw() + #define PLF_COLONY_INITIALIZER_LIST_SUPPORT + #elif _MSC_VER >= 1900 + #define PLF_COLONY_ALIGNMENT_SUPPORT + #define PLF_COLONY_TYPE_TRAITS_SUPPORT + #define PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + #define PLF_COLONY_VARIADICS_SUPPORT + #define PLF_COLONY_MOVE_SEMANTICS_SUPPORT + #define PLF_COLONY_NOEXCEPT noexcept + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits::propagate_on_container_swap::value) + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) + #define PLF_COLONY_INITIALIZER_LIST_SUPPORT + #endif + + #if defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L) + #define PLF_COLONY_CONSTEXPR constexpr + #else + #define PLF_COLONY_CONSTEXPR + #endif + +#elif defined(__cplusplus) && __cplusplus >= 201103L // C++11 support, at least + #define PLF_COLONY_FORCE_INLINE // note: GCC creates faster code without forcing inline + + #if defined(__GNUC__) && defined(__GNUC_MINOR__) && !defined(__clang__) // If compiler is GCC/G++ + #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4 // 4.2 and below do not support variadic templates + #define PLF_COLONY_VARIADICS_SUPPORT + #endif + #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ > 4 // 4.3 and below do not support initializer lists + #define PLF_COLONY_INITIALIZER_LIST_SUPPORT + #endif + #if (__GNUC__ == 4 && __GNUC_MINOR__ < 6) || __GNUC__ < 4 + #define PLF_COLONY_NOEXCEPT throw() + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) + #elif __GNUC__ < 6 + #define PLF_COLONY_NOEXCEPT noexcept + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) noexcept + #else // C++17 support + #define PLF_COLONY_NOEXCEPT noexcept + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits::propagate_on_container_swap::value) + #endif + #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) || __GNUC__ > 4 + #define PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + #endif + #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4 + #define PLF_COLONY_ALIGNMENT_SUPPORT + #endif + #if __GNUC__ >= 5 // GCC v4.9 and below do not support std::is_trivially_copyable + #define PLF_COLONY_TYPE_TRAITS_SUPPORT + #endif + #elif defined(__GLIBCXX__) // Using another compiler type with libstdc++ - we are assuming full c++11 compliance for compiler - which may not be true + #if __GLIBCXX__ >= 20080606 // libstdc++ 4.2 and below do not support variadic templates + #define PLF_COLONY_VARIADICS_SUPPORT + #endif + #if __GLIBCXX__ >= 20090421 // libstdc++ 4.3 and below do not support initializer lists + #define PLF_COLONY_INITIALIZER_LIST_SUPPORT + #endif + #if __GLIBCXX__ >= 20160111 + #define PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + #define PLF_COLONY_NOEXCEPT noexcept + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits::propagate_on_container_swap::value) + #elif __GLIBCXX__ >= 20120322 + #define PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + #define PLF_COLONY_NOEXCEPT noexcept + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) noexcept + #else + #define PLF_COLONY_NOEXCEPT throw() + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) + #endif + #if __GLIBCXX__ >= 20130322 + #define PLF_COLONY_ALIGNMENT_SUPPORT + #endif + #if __GLIBCXX__ >= 20150422 // libstdc++ v4.9 and below do not support std::is_trivially_copyable + #define PLF_COLONY_TYPE_TRAITS_SUPPORT + #endif + #elif defined(_LIBCPP_VERSION) + #define PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + #define PLF_COLONY_VARIADICS_SUPPORT + #define PLF_COLONY_INITIALIZER_LIST_SUPPORT + #define PLF_COLONY_ALIGNMENT_SUPPORT + #define PLF_COLONY_NOEXCEPT noexcept + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) noexcept + + #if !(defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)) + #define PLF_COLONY_TYPE_TRAITS_SUPPORT + #endif + #else // Assume type traits and initializer support for other compilers and standard libraries + #define PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + #define PLF_COLONY_ALIGNMENT_SUPPORT + #define PLF_COLONY_VARIADICS_SUPPORT + #define PLF_COLONY_INITIALIZER_LIST_SUPPORT + #define PLF_COLONY_TYPE_TRAITS_SUPPORT + #define PLF_COLONY_NOEXCEPT noexcept + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) noexcept + #endif + + #if __cplusplus >= 201703L + #if defined(__clang__) && ((__clang_major__ == 3 && __clang_minor__ == 9) || __clang_major__ > 3) + #define PLF_COLONY_CONSTEXPR constexpr + #elif defined(__GNUC__) && __GNUC__ >= 7 + #define PLF_COLONY_CONSTEXPR constexpr + #elif !defined(__clang__) && !defined(__GNUC__) + #define PLF_COLONY_CONSTEXPR constexpr // assume correct C++17 implementation for other compilers + #else + #define PLF_COLONY_CONSTEXPR + #endif + #else + #define PLF_COLONY_CONSTEXPR + #endif + + #define PLF_COLONY_MOVE_SEMANTICS_SUPPORT +#else + #define PLF_COLONY_FORCE_INLINE + #define PLF_COLONY_NOEXCEPT throw() + #define PLF_COLONY_NOEXCEPT_SWAP(the_allocator) + #define PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) + #define PLF_COLONY_CONSTEXPR +#endif + + + + + +#ifdef PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + #ifdef PLF_COLONY_VARIADICS_SUPPORT + #define PLF_COLONY_CONSTRUCT(the_allocator, allocator_instance, location, ...) std::allocator_traits::construct(allocator_instance, location, __VA_ARGS__) + #else + #define PLF_COLONY_CONSTRUCT(the_allocator, allocator_instance, location, data) std::allocator_traits::construct(allocator_instance, location, data) + #endif + + #define PLF_COLONY_DESTROY(the_allocator, allocator_instance, location) std::allocator_traits::destroy(allocator_instance, location) + #define PLF_COLONY_ALLOCATE(the_allocator, allocator_instance, size, hint) std::allocator_traits::allocate(allocator_instance, size, hint) + #define PLF_COLONY_ALLOCATE_INITIALIZATION(the_allocator, size, hint) std::allocator_traits::allocate(*this, size, hint) + #define PLF_COLONY_DEALLOCATE(the_allocator, allocator_instance, location, size) std::allocator_traits::deallocate(allocator_instance, location, size) +#else + #ifdef PLF_COLONY_VARIADICS_SUPPORT + #define PLF_COLONY_CONSTRUCT(the_allocator, allocator_instance, location, ...) allocator_instance.construct(location, __VA_ARGS__) + #else + #define PLF_COLONY_CONSTRUCT(the_allocator, allocator_instance, location, data) allocator_instance.construct(location, data) + #endif + + #define PLF_COLONY_DESTROY(the_allocator, allocator_instance, location) allocator_instance.destroy(location) + #define PLF_COLONY_ALLOCATE(the_allocator, allocator_instance, size, hint) allocator_instance.allocate(size, hint) + #define PLF_COLONY_ALLOCATE_INITIALIZATION(the_allocator, size, hint) the_allocator::allocate(size, hint) + #define PLF_COLONY_DEALLOCATE(the_allocator, allocator_instance, location, size) allocator_instance.deallocate(location, size) +#endif + + + +#include // std::sort and std::fill_n + +#include // memset, memcpy +#include // assert +#include // std::numeric_limits +#include // std::allocator +#include // std::bidirectional_iterator_tag + + +#ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + #include // offsetof, used in blank() + #include // std::is_trivially_destructible, etc +#endif + +#ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + #include // std::move +#endif + +#ifdef PLF_COLONY_INITIALIZER_LIST_SUPPORT + #include +#endif + + + +namespace plf +{ + + +template , typename element_skipfield_type = unsigned short > class colony : private element_allocator_type // Empty base class optimisation - inheriting allocator functions +// Note: unsigned short is equivalent to uint_least16_t ie. Using 16-bit unsigned integer in best-case scenario, greater-than-16-bit unsigned integer where platform doesn't support 16-bit types +{ +public: + // Standard container typedefs: + typedef element_type value_type; + typedef element_allocator_type allocator_type; + typedef element_skipfield_type skipfield_type; + + #ifdef PLF_COLONY_ALIGNMENT_SUPPORT + typedef typename std::aligned_storage (sizeof(element_skipfield_type) * 2)) ? alignof(element_type) : (sizeof(element_skipfield_type) * 2)>::type aligned_element_type; + #else + typedef element_type aligned_element_type; + #endif + + #ifdef PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + typedef typename std::allocator_traits::size_type size_type; + typedef typename std::allocator_traits::difference_type difference_type; + typedef element_type & reference; + typedef const element_type & const_reference; + typedef typename std::allocator_traits::pointer pointer; + typedef typename std::allocator_traits::const_pointer const_pointer; + #else + typedef typename element_allocator_type::size_type size_type; + typedef typename element_allocator_type::difference_type difference_type; + typedef typename element_allocator_type::reference reference; + typedef typename element_allocator_type::const_reference const_reference; + typedef typename element_allocator_type::pointer pointer; + typedef typename element_allocator_type::const_pointer const_pointer; + #endif + + + // Iterator declarations: + template class colony_iterator; + typedef colony_iterator iterator; + typedef colony_iterator const_iterator; + friend class colony_iterator; // Using above typedef name here is illegal under C++03 + friend class colony_iterator; + + template class colony_reverse_iterator; + typedef colony_reverse_iterator reverse_iterator; + typedef colony_reverse_iterator const_reverse_iterator; + friend class colony_reverse_iterator; + friend class colony_reverse_iterator; + + +private: + + + struct group; // forward declaration for typedefs below + + #ifdef PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + typedef typename std::allocator_traits::template rebind_alloc aligned_element_allocator_type; + typedef typename std::allocator_traits::template rebind_alloc group_allocator_type; + typedef typename std::allocator_traits::template rebind_alloc skipfield_allocator_type; + typedef typename std::allocator_traits::template rebind_alloc uchar_allocator_type; // Using uchar as the generic allocator type, as sizeof is always guaranteed to be 1 byte regardless of the number of bits in a byte on given computer, whereas for example, uint8_t would fail on machines where there are more than 8 bits in a byte eg. Texas Instruments C54x DSPs. + + typedef typename std::allocator_traits::pointer aligned_pointer_type; // Different typedef to 'pointer' - this is a pointer to the overaligned element type, not the original element type + typedef typename std::allocator_traits::pointer group_pointer_type; + typedef typename std::allocator_traits::pointer skipfield_pointer_type; + typedef typename std::allocator_traits::pointer uchar_pointer_type; + + typedef typename std::allocator_traits::template rebind_alloc pointer_allocator_type; + #else + typedef typename element_allocator_type::template rebind::other aligned_element_allocator_type; // In case compiler supports alignment but not allocator_traits + typedef typename element_allocator_type::template rebind::other group_allocator_type; + typedef typename element_allocator_type::template rebind::other skipfield_allocator_type; + typedef typename element_allocator_type::template rebind::other uchar_allocator_type; + + typedef typename aligned_element_allocator_type::pointer aligned_pointer_type; + typedef typename group_allocator_type::pointer group_pointer_type; + typedef typename skipfield_allocator_type::pointer skipfield_pointer_type; + typedef typename uchar_allocator_type::pointer uchar_pointer_type; + + typedef typename element_allocator_type::template rebind::other pointer_allocator_type; + #endif + + + + // Colony groups: + struct group : private uchar_allocator_type // Empty base class optimisation (EBCO) - inheriting allocator functions + { + aligned_pointer_type last_endpoint; // The address that is one past the highest cell number that's been used so far in this group - does not change with erase command but may change with insert (if no previously-erased locations are available) - is necessary because an iterator cannot access the colony's end_iterator. Most-used variable in colony use (operator ++, --) so first in struct + group_pointer_type next_group; // Next group in the intrusive list of all groups. NULL if no next group + const aligned_pointer_type elements; // Element storage + const skipfield_pointer_type skipfield; // Skipfield storage. The element and skipfield arrays are allocated contiguously, hence the skipfield pointer also functions as a 'one-past-end' pointer for the elements array. There will always be one additional skipfield node allocated compared to the number of elements. This is to ensure a faster ++ iterator operation (fewer checks are required when this is present). The extra node is unused and always zero, but checked, and not having it will result in out-of-bounds memory errors. + group_pointer_type previous_group; // previous group in the intrusive list of all groups. NULL if no preceding group + skipfield_type free_list_head; // The index of the last erased element in the group. The last erased element will, in turn, contain the number of the index of the next erased element, and so on. If this is == maximum skipfield_type value then free_list is empty ie. no erasures have occurred in the group (or if they have, the erased locations have then been reused via insert()). + const skipfield_type capacity; // The element capacity of this particular group + skipfield_type number_of_elements; // indicates total number of active elements in group - changes with insert and erase commands - used to check for empty group in erase function, as an indication to remove the group + group_pointer_type erasures_list_next_group; // The next group in the intrusive singly-linked list of groups with erasures ie. with active erased-element free lists + size_type group_number; // Used for comparison (> < >= <=) iterator operators (used by distance function and user) + + + #ifdef PLF_COLONY_VARIADICS_SUPPORT + group(const skipfield_type elements_per_group, group_pointer_type const previous = NULL): + last_endpoint(reinterpret_cast(PLF_COLONY_ALLOCATE_INITIALIZATION(uchar_allocator_type, ((elements_per_group * (sizeof(aligned_element_type))) + ((elements_per_group + 1u) * sizeof(skipfield_type))), (previous == NULL) ? 0 : previous->elements))), /* allocating to here purely because it is first in the struct sequence - actual pointer is elements, last_endpoint is only initialised to element's base value initially, then incremented by one below */ + next_group(NULL), + elements(last_endpoint++), + skipfield(reinterpret_cast(elements + elements_per_group)), + previous_group(previous), + free_list_head(std::numeric_limits::max()), + capacity(elements_per_group), + number_of_elements(1), + erasures_list_next_group(NULL), + group_number((previous == NULL) ? 0 : previous->group_number + 1u) + { + // Static casts to unsigned int from short not necessary as C++ automatically promotes lesser types for arithmetic purposes. + std::memset(&*skipfield, 0, sizeof(skipfield_type) * (elements_per_group + 1u)); // &* to avoid problems with non-trivial pointers + } + + #else + // This is a hack around the fact that element_allocator_type::construct only supports copy construction in C++03 and copy elision does not occur on the vast majority of compilers in this circumstance. And to avoid running out of memory (and losing performance) from allocating the same block twice, we're allocating in this constructor and moving data in the copy constructor. + group(const skipfield_type elements_per_group, group_pointer_type const previous = NULL): + last_endpoint(reinterpret_cast(PLF_COLONY_ALLOCATE_INITIALIZATION(uchar_allocator_type, ((elements_per_group * (sizeof(aligned_element_type))) + ((elements_per_group + 1) * sizeof(skipfield_type))), (previous == NULL) ? 0 : previous->elements))), + elements(NULL), + skipfield(reinterpret_cast(last_endpoint + elements_per_group)), + previous_group(previous), + capacity(elements_per_group) + { + std::memset(&*skipfield, 0, sizeof(skipfield_type) * (elements_per_group + 1u)); + } + + + + // Not a real copy constructor ie. actually a move constructor. Only used for allocator.construct in C++03 for reasons stated above: + group(const group &source) PLF_COLONY_NOEXCEPT: + uchar_allocator_type(source), + last_endpoint(source.last_endpoint + 1), + next_group(NULL), + elements(source.last_endpoint), + skipfield(source.skipfield), + previous_group(source.previous_group), + free_list_head(std::numeric_limits::max()), + capacity(source.capacity), + number_of_elements(1), + erasures_list_next_group(NULL), + group_number((source.previous_group == NULL) ? 0 : source.previous_group->group_number + 1u) + {} + #endif + + + + ~group() PLF_COLONY_NOEXCEPT + { + // Null check not necessary (for copied group as above) as delete will also perform a null check. + PLF_COLONY_DEALLOCATE(uchar_allocator_type, (*this), reinterpret_cast(elements), (capacity * sizeof(aligned_element_type)) + ((capacity + 1u) * sizeof(skipfield_type))); + } + }; + + + + + // Implement const/non-const iterator switching pattern: + template struct choose; + + template struct choose + { + typedef is_true type; + }; + + template struct choose + { + typedef is_false type; + }; + + +public: + + + // Iterators: + template class colony_iterator + { + private: + group_pointer_type group_pointer; + aligned_pointer_type element_pointer; + skipfield_pointer_type skipfield_pointer; + + public: + typedef std::bidirectional_iterator_tag iterator_category; + typedef typename colony::value_type value_type; + typedef typename colony::difference_type difference_type; + typedef typename choose::type pointer; + typedef typename choose::type reference; + + friend class colony; + friend class colony_reverse_iterator; + friend class colony_reverse_iterator; + + + + inline colony_iterator & operator = (const colony_iterator &source) PLF_COLONY_NOEXCEPT + { + group_pointer = source.group_pointer; + element_pointer = source.element_pointer; + skipfield_pointer = source.skipfield_pointer; + return *this; + } + + + + inline colony_iterator & operator = (const colony_iterator &source) PLF_COLONY_NOEXCEPT + { + group_pointer = source.group_pointer; + element_pointer = source.element_pointer; + skipfield_pointer = source.skipfield_pointer; + return *this; + } + + + + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + // Move assignment - only really necessary if the allocator uses non-standard ie. smart pointers + inline colony_iterator & operator = (colony_iterator &&source) PLF_COLONY_NOEXCEPT // Move is a copy in this scenario + { + assert (&source != this); + group_pointer = std::move(source.group_pointer); + element_pointer = std::move(source.element_pointer); + skipfield_pointer = std::move(source.skipfield_pointer); + return *this; + } + + + + inline colony_iterator & operator = (colony_iterator &&source) PLF_COLONY_NOEXCEPT + { + assert (&source != this); + group_pointer = std::move(source.group_pointer); + element_pointer = std::move(source.element_pointer); + skipfield_pointer = std::move(source.skipfield_pointer); + return *this; + } + #endif + + + + inline PLF_COLONY_FORCE_INLINE bool operator == (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (element_pointer == rh.element_pointer); + } + + + + inline PLF_COLONY_FORCE_INLINE bool operator == (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (element_pointer == rh.element_pointer); + } + + + + inline PLF_COLONY_FORCE_INLINE bool operator != (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (element_pointer != rh.element_pointer); + } + + + + inline PLF_COLONY_FORCE_INLINE bool operator != (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (element_pointer != rh.element_pointer); + } + + + + inline PLF_COLONY_FORCE_INLINE reference operator * () const // may cause exception with uninitialized iterator + { + return *(reinterpret_cast(element_pointer)); + } + + + + inline PLF_COLONY_FORCE_INLINE pointer operator -> () const PLF_COLONY_NOEXCEPT + { + return reinterpret_cast(element_pointer); + } + + + +#if defined(_MSC_VER) && _MSC_VER <= 1600 // MSVC 2010 needs a bit of a helping hand when it comes to optimizing + inline PLF_COLONY_FORCE_INLINE colony_iterator & operator ++ () +#else + colony_iterator & operator ++ () +#endif + { + assert(group_pointer != NULL); // covers uninitialised colony_iterator + assert(!(element_pointer == group_pointer->last_endpoint && group_pointer->next_group != NULL)); // Assert that iterator is not already at end() + + skipfield_type skip = *(++skipfield_pointer); + + if ((element_pointer += skip + 1) == group_pointer->last_endpoint && group_pointer->next_group != NULL) // ie. beyond end of available data + { + group_pointer = group_pointer->next_group; + skip = *(group_pointer->skipfield); + element_pointer = group_pointer->elements + skip; + skipfield_pointer = group_pointer->skipfield; + } + + skipfield_pointer += skip; + return *this; + } + + + + inline colony_iterator operator ++(int) + { + const colony_iterator copy(*this); + ++*this; + return copy; + } + + + + private: + inline PLF_COLONY_FORCE_INLINE void check_for_end_of_group_and_progress() // used by erase + { + if (element_pointer == group_pointer->last_endpoint && group_pointer->next_group != NULL) + { + group_pointer = group_pointer->next_group; + skipfield_pointer = group_pointer->skipfield; + element_pointer = group_pointer->elements + *skipfield_pointer; + skipfield_pointer += *skipfield_pointer; + } + } + + + + public: + + colony_iterator & operator -- () + { + assert(group_pointer != NULL); + assert(!(element_pointer == group_pointer->elements && group_pointer->previous_group == NULL)); // Assert that we are not already at begin() - this is not required to be tested in the code below as we don't need a special condition to progress to begin(), like we do with end() in operator ++ + + if (element_pointer != group_pointer->elements) // ie. not already at beginning of group + { + const skipfield_type skip = *(--skipfield_pointer); + skipfield_pointer -= skip; + + if ((element_pointer -= skip + 1) != group_pointer->elements - 1) // ie. iterator was not already at beginning of colony (with some previous consecutive deleted elements), and skipfield does not takes us into the previous group) + { + return *this; + } + } + + group_pointer = group_pointer->previous_group; + skipfield_pointer = group_pointer->skipfield + group_pointer->capacity - 1; + element_pointer = (reinterpret_cast(group_pointer->skipfield) - 1) - *skipfield_pointer; + skipfield_pointer -= *skipfield_pointer; + + return *this; + } + + + + inline colony_iterator operator -- (int) + { + const colony_iterator copy(*this); + --*this; + return copy; + } + + + + inline bool operator > (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return ((group_pointer == rh.group_pointer) & (element_pointer > rh.element_pointer)) || (group_pointer != rh.group_pointer && group_pointer->group_number > rh.group_pointer->group_number); + } + + + + inline bool operator < (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return rh > *this; + } + + + + inline bool operator >= (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return !(rh > *this); + } + + + + inline bool operator <= (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return !(*this > rh); + } + + + + inline bool operator > (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return ((group_pointer == rh.group_pointer) & (element_pointer > rh.element_pointer)) || (group_pointer != rh.group_pointer && group_pointer->group_number > rh.group_pointer->group_number); + } + + + + inline bool operator < (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return rh > *this; + } + + + + inline bool operator >= (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return !(rh > *this); + } + + + + inline bool operator <= (const colony_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return !(*this > rh); + } + + + colony_iterator() PLF_COLONY_NOEXCEPT: group_pointer(NULL), element_pointer(NULL), skipfield_pointer(NULL) {} + + + + private: + // Used by cend(), erase() etc: + colony_iterator(const group_pointer_type group_p, const aligned_pointer_type element_p, const skipfield_pointer_type skipfield_p) PLF_COLONY_NOEXCEPT: group_pointer(group_p), element_pointer(element_p), skipfield_pointer(skipfield_p) {} + + + + public: + + inline colony_iterator (const colony_iterator &source) PLF_COLONY_NOEXCEPT: + group_pointer(source.group_pointer), + element_pointer(source.element_pointer), + skipfield_pointer(source.skipfield_pointer) + {} + + + inline colony_iterator(const colony_iterator &source) PLF_COLONY_NOEXCEPT: + group_pointer(source.group_pointer), + element_pointer(source.element_pointer), + skipfield_pointer(source.skipfield_pointer) + {} + + + + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + // move constructor + inline colony_iterator(colony_iterator &&source) PLF_COLONY_NOEXCEPT: + group_pointer(std::move(source.group_pointer)), + element_pointer(std::move(source.element_pointer)), + skipfield_pointer(std::move(source.skipfield_pointer)) + {} + + + inline colony_iterator(colony_iterator &&source) PLF_COLONY_NOEXCEPT: + group_pointer(std::move(source.group_pointer)), + element_pointer(std::move(source.element_pointer)), + skipfield_pointer(std::move(source.skipfield_pointer)) + {} + #endif + + + }; // colony_iterator + + + + + + // Reverse iterators: + + template class colony_reverse_iterator + { + private: + iterator it; + + public: + typedef std::bidirectional_iterator_tag iterator_category; + typedef typename colony::value_type value_type; + typedef typename colony::difference_type difference_type; + typedef typename choose::type pointer; + typedef typename choose::type reference; + + friend class colony; + + + inline colony_reverse_iterator& operator = (const colony_reverse_iterator &source) PLF_COLONY_NOEXCEPT + { + it = source.it; + return *this; + } + + + + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + // move assignment + inline colony_reverse_iterator& operator = (colony_reverse_iterator &&source) PLF_COLONY_NOEXCEPT + { + it = std::move(source.it); + return *this; + } + #endif + + + + inline PLF_COLONY_FORCE_INLINE bool operator == (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (it == rh.it); + } + + + + inline PLF_COLONY_FORCE_INLINE bool operator != (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (it != rh.it); + } + + + + inline PLF_COLONY_FORCE_INLINE reference operator * () const PLF_COLONY_NOEXCEPT + { + return *(reinterpret_cast(it.element_pointer)); + } + + + + inline PLF_COLONY_FORCE_INLINE pointer * operator -> () const PLF_COLONY_NOEXCEPT + { + return reinterpret_cast(it.element_pointer); + } + + + + // In this case we have to redefine the algorithm, rather than using the internal iterator's -- operator, in order for the reverse_iterator to be allowed to reach rend() ie. begin_iterator - 1 + colony_reverse_iterator & operator ++ () + { + colony::group_pointer_type &group_pointer = it.group_pointer; + colony::aligned_pointer_type &element_pointer = it.element_pointer; + colony::skipfield_pointer_type &skipfield_pointer = it.skipfield_pointer; + + assert(group_pointer != NULL); + assert(!(element_pointer == group_pointer->elements - 1 && group_pointer->previous_group == NULL)); // Assert that we are not already at rend() + + if (element_pointer != group_pointer->elements) // ie. not already at beginning of group + { + element_pointer -= *(--skipfield_pointer) + 1; + skipfield_pointer -= *skipfield_pointer; + + if (!(element_pointer == group_pointer->elements - 1 && group_pointer->previous_group == NULL)) // ie. iterator is not == rend() + { + return *this; + } + } + + if (group_pointer->previous_group != NULL) // ie. not first group in colony + { + group_pointer = group_pointer->previous_group; + skipfield_pointer = group_pointer->skipfield + group_pointer->capacity - 1; + element_pointer = (reinterpret_cast(group_pointer->skipfield) - 1) - *skipfield_pointer; + skipfield_pointer -= *skipfield_pointer; + } + else // necessary so that reverse_iterator can end up == rend(), if we were already at first element in colony + { + --element_pointer; + --skipfield_pointer; + } + + return *this; + } + + + + inline colony_reverse_iterator operator ++ (int) + { + const colony_reverse_iterator copy(*this); + ++*this; + return copy; + } + + + + inline PLF_COLONY_FORCE_INLINE colony_reverse_iterator & operator -- () + { + assert(!(it.element_pointer == it.group_pointer->last_endpoint - 1 && it.group_pointer->next_group == NULL)); // ie. Check that we are not already at rbegin() + ++it; + return *this; + } + + + + inline colony_reverse_iterator operator -- (int) + { + const colony_reverse_iterator copy(*this); + --*this; + return copy; + } + + + + inline typename colony::iterator base() const + { + return ++(typename colony::iterator(it)); + } + + + + inline bool operator > (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (rh.it > it); + } + + + + inline bool operator < (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (it > rh.it); + } + + + + inline bool operator >= (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return !(it > rh.it); + } + + + + inline bool operator <= (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return !(rh.it > it); + } + + + + inline PLF_COLONY_FORCE_INLINE bool operator == (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (it == rh.it); + } + + + + inline PLF_COLONY_FORCE_INLINE bool operator != (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (it != rh.it); + } + + + + inline bool operator > (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (rh.it > it); + } + + + + inline bool operator < (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return (it > rh.it); + } + + + + inline bool operator >= (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return !(it > rh.it); + } + + + + inline bool operator <= (const colony_reverse_iterator &rh) const PLF_COLONY_NOEXCEPT + { + return !(rh.it > it); + } + + + + colony_reverse_iterator () PLF_COLONY_NOEXCEPT + {} + + + + colony_reverse_iterator (const colony_reverse_iterator &source) PLF_COLONY_NOEXCEPT: + it(source.it) + {} + + + + colony_reverse_iterator (const typename colony::iterator &source) PLF_COLONY_NOEXCEPT: + it(source) + {} + + + + private: + // Used by rend(), etc: + colony_reverse_iterator(const group_pointer_type group_p, const aligned_pointer_type element_p, const skipfield_pointer_type skipfield_p) PLF_COLONY_NOEXCEPT: it(group_p, element_p, skipfield_p) {} + + + + public: + + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + // move constructors + colony_reverse_iterator (colony_reverse_iterator &&source) PLF_COLONY_NOEXCEPT: + it(std::move(source.it)) + {} + + colony_reverse_iterator (typename colony::iterator &&source) PLF_COLONY_NOEXCEPT: + it(std::move(source)) + {} + #endif + + }; // colony_reverse_iterator + + + + +private: + + // Used to prevent fill-insert/constructor calls being mistakenly resolved to range-insert/constructor calls + template + struct plf_enable_if_c + { + typedef T type; + }; + + template + struct plf_enable_if_c + {}; + + + iterator end_iterator, begin_iterator; + group_pointer_type groups_with_erasures_list_head; // Head of a singly-linked intrusive list of groups which have erased-element memory locations available for reuse + size_type total_number_of_elements, total_capacity; + + struct ebco_pair2 : pointer_allocator_type // Packaging the element pointer allocator with a lesser-used member variable, for empty-base-class optimisation + { + skipfield_type min_elements_per_group; + explicit ebco_pair2(const skipfield_type min_elements) PLF_COLONY_NOEXCEPT: min_elements_per_group(min_elements) {} + } pointer_allocator_pair; + + struct ebco_pair : group_allocator_type + { + skipfield_type max_elements_per_group; + explicit ebco_pair(const skipfield_type max_elements) PLF_COLONY_NOEXCEPT: max_elements_per_group(max_elements) {} + } group_allocator_pair; + + +public: + + // Default constuctor: + + colony() PLF_COLONY_NOEXCEPT: + element_allocator_type(element_allocator_type()), + groups_with_erasures_list_head(NULL), + total_number_of_elements(0), + total_capacity(0), + pointer_allocator_pair((sizeof(aligned_element_type) * 8 > (sizeof(*this) + sizeof(group)) * 2) ? 8 : (((sizeof(*this) + sizeof(group)) * 2) / sizeof(aligned_element_type))), + group_allocator_pair(std::numeric_limits::max()) + { + assert(std::numeric_limits::is_integer & !std::numeric_limits::is_signed); // skipfield type must be of unsigned integer type (uchar, ushort, uint etc) + + #ifndef PLF_COLONY_ALIGNMENT_SUPPORT + assert(sizeof(element_type) >= sizeof(skipfield_type) * 2); // eg. under C++03, aligned_storage is not available, so sizeof(skipfield type) * 2 must be larger or equal to sizeof(element_type), otherwise the doubly-linked free lists of erased element indexes will not work correctly. So if you're storing chars, for example, and using the default skipfield type (unsigned short), the compiler will flag you with this assert. You cannot store char or unsigned char in colony under C++03, and if storing short or unsigned short you must change your skipfield type to unsigned char. Or just use C++11 and above. + #endif + } + + + + // Default constuctor (allocator-extended): + + explicit colony(const element_allocator_type &alloc): + element_allocator_type(alloc), + groups_with_erasures_list_head(NULL), + total_number_of_elements(0), + total_capacity(0), + pointer_allocator_pair((sizeof(aligned_element_type) * 8 > (sizeof(*this) + sizeof(group)) * 2) ? 8 : (((sizeof(*this) + sizeof(group)) * 2) / sizeof(aligned_element_type))), + group_allocator_pair(std::numeric_limits::max()) + { + assert(std::numeric_limits::is_integer & !std::numeric_limits::is_signed); + + #ifndef PLF_COLONY_ALIGNMENT_SUPPORT + assert(sizeof(element_type) >= sizeof(skipfield_type) * 2); // see default constructor explanation + #endif + } + + + + // Copy constructor: + + colony(const colony &source): + element_allocator_type(source), + groups_with_erasures_list_head(NULL), + total_number_of_elements(0), + total_capacity(0), + pointer_allocator_pair(static_cast((source.pointer_allocator_pair.min_elements_per_group > source.total_number_of_elements) ? source.pointer_allocator_pair.min_elements_per_group : ((source.total_number_of_elements > source.group_allocator_pair.max_elements_per_group) ? source.group_allocator_pair.max_elements_per_group : source.total_number_of_elements))), // Make the first colony group capacity the greater of min_elements_per_group or total_number_of_elements, so long as total_number_of_elements isn't larger than max_elements_per_group + group_allocator_pair(source.group_allocator_pair.max_elements_per_group) + { + insert(source.begin_iterator, source.end_iterator); + pointer_allocator_pair.min_elements_per_group = source.pointer_allocator_pair.min_elements_per_group; // reset to correct value for future clear() or erasures + } + + + + // Copy constructor (allocator-extended): + + colony(const colony &source, const allocator_type &alloc): + element_allocator_type(alloc), + groups_with_erasures_list_head(NULL), + total_number_of_elements(0), + total_capacity(0), + pointer_allocator_pair(static_cast((source.pointer_allocator_pair.min_elements_per_group > source.total_number_of_elements) ? source.pointer_allocator_pair.min_elements_per_group : ((source.total_number_of_elements > source.group_allocator_pair.max_elements_per_group) ? source.group_allocator_pair.max_elements_per_group : source.total_number_of_elements))), + group_allocator_pair(source.group_allocator_pair.max_elements_per_group) + { + insert(source.begin_iterator, source.end_iterator); + pointer_allocator_pair.min_elements_per_group = source.pointer_allocator_pair.min_elements_per_group; + } + + + + +private: + + inline void blank() PLF_COLONY_NOEXCEPT + { + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_trivial::value && std::is_trivial::value && std::is_trivial::value) // if all pointer types are trivial, we can just nuke it from orbit with memset (NULL is always 0 in C++): + { + std::memset(static_cast(this), 0, offsetof(colony, pointer_allocator_pair)); + } + else + #endif + { + end_iterator.group_pointer = NULL; + end_iterator.element_pointer = NULL; + end_iterator.skipfield_pointer = NULL; + begin_iterator.group_pointer = NULL; + begin_iterator.element_pointer = NULL; + begin_iterator.skipfield_pointer = NULL; + groups_with_erasures_list_head = NULL; + total_number_of_elements = 0; + total_capacity = 0; + } + } + + + +public: + + + + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + // Move constructor: + + colony(colony &&source) PLF_COLONY_NOEXCEPT: + element_allocator_type(source), + end_iterator(std::move(source.end_iterator)), + begin_iterator(std::move(source.begin_iterator)), + groups_with_erasures_list_head(std::move(source.groups_with_erasures_list_head)), + total_number_of_elements(source.total_number_of_elements), + total_capacity(source.total_capacity), + pointer_allocator_pair(source.pointer_allocator_pair.min_elements_per_group), + group_allocator_pair(source.group_allocator_pair.max_elements_per_group) + { + source.blank(); + } + + + // Move constructor (allocator-extended): + + colony(colony &&source, const allocator_type &alloc): + element_allocator_type(alloc), + end_iterator(std::move(source.end_iterator)), + begin_iterator(std::move(source.begin_iterator)), + groups_with_erasures_list_head(std::move(source.groups_with_erasures_list_head)), + total_number_of_elements(source.total_number_of_elements), + total_capacity(source.total_capacity), + pointer_allocator_pair(source.pointer_allocator_pair.min_elements_per_group), + group_allocator_pair(source.group_allocator_pair.max_elements_per_group) + { + source.blank(); + } + #endif + + + + // Fill constructor: + + colony(const size_type fill_number, const element_type &element, const skipfield_type min_allocation_amount = 0, const skipfield_type max_allocation_amount = std::numeric_limits::max(), const element_allocator_type &alloc = element_allocator_type()): + element_allocator_type(alloc), + groups_with_erasures_list_head(NULL), + total_number_of_elements(0), + total_capacity(0), + pointer_allocator_pair((min_allocation_amount != 0) ? min_allocation_amount : + (fill_number > max_allocation_amount) ? max_allocation_amount : + (fill_number > 8) ? static_cast(fill_number) : 8), + group_allocator_pair(max_allocation_amount) + { + assert(std::numeric_limits::is_integer & !std::numeric_limits::is_signed); + assert((pointer_allocator_pair.min_elements_per_group > 2) & (pointer_allocator_pair.min_elements_per_group <= group_allocator_pair.max_elements_per_group)); + + #ifndef PLF_COLONY_ALIGNMENT_SUPPORT + assert(sizeof(element_type) >= sizeof(skipfield_type) * 2); // see default constructor explanation + #endif + + insert(fill_number, element); + } + + + + // Range constructor: + + template + colony(const typename plf_enable_if_c::is_integer, iterator_type>::type &first, const iterator_type &last, const skipfield_type min_allocation_amount = 8, const skipfield_type max_allocation_amount = std::numeric_limits::max(), const element_allocator_type &alloc = element_allocator_type()): + element_allocator_type(alloc), + groups_with_erasures_list_head(NULL), + total_number_of_elements(0), + total_capacity(0), + pointer_allocator_pair(min_allocation_amount), + group_allocator_pair(max_allocation_amount) + { + assert(std::numeric_limits::is_integer & !std::numeric_limits::is_signed); + assert((pointer_allocator_pair.min_elements_per_group > 2) & (pointer_allocator_pair.min_elements_per_group <= group_allocator_pair.max_elements_per_group)); + + #ifndef PLF_COLONY_ALIGNMENT_SUPPORT + assert(sizeof(element_type) >= sizeof(skipfield_type) * 2); // see default constructor explanation + #endif + + insert(first, last); + } + + + + // Initializer-list constructor: + + #ifdef PLF_COLONY_INITIALIZER_LIST_SUPPORT + colony(const std::initializer_list &element_list, const skipfield_type min_allocation_amount = 0, const skipfield_type max_allocation_amount = std::numeric_limits::max(), const element_allocator_type &alloc = element_allocator_type()): + element_allocator_type(alloc), + groups_with_erasures_list_head(NULL), + total_number_of_elements(0), + total_capacity(0), + pointer_allocator_pair((min_allocation_amount != 0) ? min_allocation_amount : + (element_list.size() > max_allocation_amount) ? max_allocation_amount : + (element_list.size() > 8) ? static_cast(element_list.size()) : 8), + group_allocator_pair(max_allocation_amount) + { + assert(std::numeric_limits::is_integer & !std::numeric_limits::is_signed); + assert((pointer_allocator_pair.min_elements_per_group > 2) & (pointer_allocator_pair.min_elements_per_group <= group_allocator_pair.max_elements_per_group)); + + #ifndef PLF_COLONY_ALIGNMENT_SUPPORT + assert(sizeof(element_type) >= sizeof(skipfield_type) * 2); // see default constructor explanation + #endif + + insert(element_list); + } + + #endif + + + + inline PLF_COLONY_FORCE_INLINE iterator begin() PLF_COLONY_NOEXCEPT + { + return begin_iterator; + } + + + + inline PLF_COLONY_FORCE_INLINE const iterator & begin() const PLF_COLONY_NOEXCEPT // To allow for functions which only take const colony & as a source eg. copy constructor + { + return begin_iterator; + } + + + + inline PLF_COLONY_FORCE_INLINE iterator end() PLF_COLONY_NOEXCEPT + { + return end_iterator; + } + + + + inline PLF_COLONY_FORCE_INLINE const iterator & end() const PLF_COLONY_NOEXCEPT + { + return end_iterator; + } + + + + inline const_iterator cbegin() const PLF_COLONY_NOEXCEPT + { + return const_iterator(begin_iterator.group_pointer, begin_iterator.element_pointer, begin_iterator.skipfield_pointer); + } + + + + inline const_iterator cend() const PLF_COLONY_NOEXCEPT + { + return const_iterator(end_iterator.group_pointer, end_iterator.element_pointer, end_iterator.skipfield_pointer); + } + + + + inline reverse_iterator rbegin() const // May throw exception if colony is empty so end_iterator is uninitialized + { + return ++reverse_iterator(end_iterator); + } + + + + inline reverse_iterator rend() const PLF_COLONY_NOEXCEPT + { + return reverse_iterator(begin_iterator.group_pointer, begin_iterator.element_pointer - 1, begin_iterator.skipfield_pointer - 1); + } + + + + inline const_reverse_iterator crbegin() const + { + return ++const_reverse_iterator(end_iterator); + } + + + + inline const_reverse_iterator crend() const PLF_COLONY_NOEXCEPT + { + return const_reverse_iterator(begin_iterator.group_pointer, begin_iterator.element_pointer - 1, begin_iterator.skipfield_pointer - 1); + } + + + + ~colony() PLF_COLONY_NOEXCEPT + { + destroy_all_data(); + } + + + +private: + + void destroy_all_data() PLF_COLONY_NOEXCEPT + { + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if ((total_number_of_elements != 0) & !(std::is_trivially_destructible::value)) // Amusingly enough, these changes from && to logical & actually do make a significant difference in debug mode + #else // If compiler doesn't support traits, iterate regardless - trivial destructors will not be called, hopefully compiler will optimise the 'destruct' loop out for POD types + if (total_number_of_elements != 0) + #endif + { + total_number_of_elements = 0; // to avoid double-destruction + + while (true) + { + const aligned_pointer_type end_pointer = begin_iterator.group_pointer->last_endpoint; + + do + { + PLF_COLONY_DESTROY(element_allocator_type, (*this), reinterpret_cast(begin_iterator.element_pointer)); + ++begin_iterator.skipfield_pointer; + begin_iterator.element_pointer += *begin_iterator.skipfield_pointer + 1; + begin_iterator.skipfield_pointer += *begin_iterator.skipfield_pointer; + } while(begin_iterator.element_pointer != end_pointer); // ie. beyond end of available data + + const group_pointer_type next_group = begin_iterator.group_pointer->next_group; + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, begin_iterator.group_pointer); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, 1); + begin_iterator.group_pointer = next_group; // required to be before if statement in order for first_group to be NULL and avoid potential double-destruction in future + + if (next_group == NULL) + { + return; + } + + begin_iterator.element_pointer = next_group->elements + *(next_group->skipfield); + begin_iterator.skipfield_pointer = next_group->skipfield + *(next_group->skipfield); + } + } + else // Avoid iteration for both empty groups and trivially-destructible types eg. POD, structs, classes with empty destructors + { + // Technically under a type-traits-supporting compiler total_number_of_elements could be non-zero at this point, but since begin_iterator.group_pointer would already be NULL in the case of double-destruction, it's unnecessary to zero total_number_of_elements + while (begin_iterator.group_pointer != NULL) + { + const group_pointer_type next_group = begin_iterator.group_pointer->next_group; + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, begin_iterator.group_pointer); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, 1); + begin_iterator.group_pointer = next_group; + } + } + } + + + + void initialize(const skipfield_type first_group_size) + { + begin_iterator.group_pointer = PLF_COLONY_ALLOCATE(group_allocator_type, group_allocator_pair, 1, 0); + + try + { + #ifdef PLF_COLONY_VARIADICS_SUPPORT + PLF_COLONY_CONSTRUCT(group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, first_group_size); + #else + PLF_COLONY_CONSTRUCT(group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, group(first_group_size)); + #endif + } + catch (...) + { + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, 1); + begin_iterator.group_pointer = NULL; + throw; + } + + end_iterator.group_pointer = begin_iterator.group_pointer; + end_iterator.element_pointer = begin_iterator.element_pointer = begin_iterator.group_pointer->elements; + end_iterator.skipfield_pointer = begin_iterator.skipfield_pointer = begin_iterator.group_pointer->skipfield; + total_capacity = first_group_size; + } + + + +public: + + + iterator insert(const element_type &element) + { + if (end_iterator.element_pointer != NULL) + { + switch(((groups_with_erasures_list_head != NULL) << 1) | (end_iterator.element_pointer == reinterpret_cast(end_iterator.group_pointer->skipfield))) + { + case 0: // ie. there are no erased elements and end_iterator is not at end of current final group + { + const iterator return_iterator = end_iterator; /* Make copy for return before modifying end_iterator */ + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_nothrow_copy_constructible::value) + { // For no good reason this compiles to faster code under GCC: + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer++), element); + end_iterator.group_pointer->last_endpoint = end_iterator.element_pointer; + } + else + #endif + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer), element); + end_iterator.group_pointer->last_endpoint = ++end_iterator.element_pointer; // Shift the addition to the second operation, avoiding problems if an exception is thrown during construction + } + + ++(end_iterator.group_pointer->number_of_elements); + ++end_iterator.skipfield_pointer; + ++total_number_of_elements; + + return return_iterator; // return value before incrementation + } + case 1: // ie. there are no erased elements and end_iterator is at end of current final group - ie. colony is full - create new group + { + end_iterator.group_pointer->next_group = PLF_COLONY_ALLOCATE(group_allocator_type, group_allocator_pair, 1, end_iterator.group_pointer); + group &next_group = *(end_iterator.group_pointer->next_group); + const skipfield_type new_group_size = (total_number_of_elements < static_cast(group_allocator_pair.max_elements_per_group)) ? static_cast(total_number_of_elements) : group_allocator_pair.max_elements_per_group; + + try + { + #ifdef PLF_COLONY_VARIADICS_SUPPORT + PLF_COLONY_CONSTRUCT(group_allocator_type, group_allocator_pair, &next_group, new_group_size, end_iterator.group_pointer); + #else + PLF_COLONY_CONSTRUCT(group_allocator_type, group_allocator_pair, &next_group, group(new_group_size, end_iterator.group_pointer)); + #endif + } + catch (...) + { + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, &next_group, 1); + end_iterator.group_pointer->next_group = NULL; + throw; + } + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_nothrow_copy_constructible::value) + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(next_group.elements), element); + } + else + #endif + { + try + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(next_group.elements), element); + } + catch (...) + { + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, &next_group); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, &next_group, 1); + end_iterator.group_pointer->next_group = NULL; + throw; + } + } + + end_iterator.group_pointer = &next_group; + end_iterator.element_pointer = next_group.last_endpoint; + end_iterator.skipfield_pointer = next_group.skipfield + 1; + ++total_number_of_elements; + total_capacity += new_group_size; + + return iterator(end_iterator.group_pointer, next_group.elements, next_group.skipfield); /* returns value before incrementation */ + } + default: // ie. there are erased elements, reuse previous-erased element locations + { + iterator new_location; + new_location.group_pointer = groups_with_erasures_list_head; + new_location.element_pointer = groups_with_erasures_list_head->elements + groups_with_erasures_list_head->free_list_head; + new_location.skipfield_pointer = groups_with_erasures_list_head->skipfield + groups_with_erasures_list_head->free_list_head; + + // always at start of skipblock, update skipblock: + const skipfield_type prev_free_list_index = *(reinterpret_cast(new_location.element_pointer)); + + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(new_location.element_pointer), element); + + // Update skipblock: + const skipfield_type new_value = *(new_location.skipfield_pointer) - 1; + + if (new_value != 0) // ie. skipfield was not 1, ie. a single-node skipblock, with no additional nodes to update + { + // set (new) start and (original) end of skipblock to new value: + *(new_location.skipfield_pointer + new_value) = *(new_location.skipfield_pointer + 1) = new_value; + + // transfer free list node to new start node: + ++(groups_with_erasures_list_head->free_list_head); + + if (prev_free_list_index != std::numeric_limits::max()) // ie. not the tail free list node + { + *(reinterpret_cast(new_location.group_pointer->elements + prev_free_list_index) + 1) = groups_with_erasures_list_head->free_list_head; + } + + *(reinterpret_cast(new_location.element_pointer + 1)) = prev_free_list_index; + *(reinterpret_cast(new_location.element_pointer + 1) + 1) = std::numeric_limits::max(); + } + else + { + groups_with_erasures_list_head->free_list_head = prev_free_list_index; + + if (prev_free_list_index != std::numeric_limits::max()) // ie. not the last free list node + { + *(reinterpret_cast(new_location.group_pointer->elements + prev_free_list_index) + 1) = std::numeric_limits::max(); + } + else + { + groups_with_erasures_list_head = groups_with_erasures_list_head->erasures_list_next_group; + } + } + + *(new_location.skipfield_pointer) = 0; + ++(new_location.group_pointer->number_of_elements); + + if (new_location.group_pointer == begin_iterator.group_pointer && new_location.element_pointer < begin_iterator.element_pointer) + { /* ie. begin_iterator was moved forwards as the result of an erasure at some point, this erased element is before the current begin, hence, set current begin iterator to this element */ + begin_iterator = new_location; + } + + ++total_number_of_elements; + return new_location; + } + } + } + else // ie. newly-constructed colony, no insertions yet and no groups + { + initialize(pointer_allocator_pair.min_elements_per_group); + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_nothrow_copy_constructible::value) + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer++), element); + } + else + #endif + { + try + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer++), element); + } + catch (...) + { + clear(); + throw; + } + } + + ++end_iterator.skipfield_pointer; + total_number_of_elements = 1; + return begin_iterator; + } + } + + + + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + iterator insert(element_type &&element) // The move-insert function is near-identical to the regular insert function, with the exception of the element construction method and is_nothrow tests. + { + if (end_iterator.element_pointer != NULL) + { + switch(((groups_with_erasures_list_head != NULL) << 1) | (end_iterator.element_pointer == reinterpret_cast(end_iterator.group_pointer->skipfield))) + { + case 0: + { + const iterator return_iterator = end_iterator; + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_nothrow_move_constructible::value) + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer++), std::move(element)); + end_iterator.group_pointer->last_endpoint = end_iterator.element_pointer; + } + else + #endif + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer), std::move(element)); + end_iterator.group_pointer->last_endpoint = ++end_iterator.element_pointer; + } + + ++(end_iterator.group_pointer->number_of_elements); + ++end_iterator.skipfield_pointer; + ++total_number_of_elements; + + return return_iterator; + } + case 1: + { + end_iterator.group_pointer->next_group = PLF_COLONY_ALLOCATE(group_allocator_type, group_allocator_pair, 1, end_iterator.group_pointer); + group &next_group = *(end_iterator.group_pointer->next_group); + const skipfield_type new_group_size = (total_number_of_elements < static_cast(group_allocator_pair.max_elements_per_group)) ? static_cast(total_number_of_elements) : group_allocator_pair.max_elements_per_group; + + try + { + #ifdef PLF_COLONY_VARIADICS_SUPPORT + PLF_COLONY_CONSTRUCT(group_allocator_type, group_allocator_pair, &next_group, new_group_size, end_iterator.group_pointer); + #else + PLF_COLONY_CONSTRUCT(group_allocator_type, group_allocator_pair, &next_group, group(new_group_size, end_iterator.group_pointer)); + #endif + } + catch (...) + { + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, &next_group, 1); + end_iterator.group_pointer->next_group = NULL; + throw; + } + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_nothrow_move_constructible::value) + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(next_group.elements), std::move(element)); + } + else + #endif + { + try + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(next_group.elements), std::move(element)); + } + catch (...) + { + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, &next_group); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, &next_group, 1); + end_iterator.group_pointer->next_group = NULL; + throw; + } + } + + end_iterator.group_pointer = &next_group; + end_iterator.element_pointer = next_group.last_endpoint; + end_iterator.skipfield_pointer = next_group.skipfield + 1; + ++total_number_of_elements; + total_capacity += new_group_size; + + return iterator(end_iterator.group_pointer, next_group.elements, next_group.skipfield); + } + default: + { + iterator new_location; + new_location.group_pointer = groups_with_erasures_list_head; + new_location.element_pointer = groups_with_erasures_list_head->elements + groups_with_erasures_list_head->free_list_head; + new_location.skipfield_pointer = groups_with_erasures_list_head->skipfield + groups_with_erasures_list_head->free_list_head; + + const skipfield_type prev_free_list_index = *(reinterpret_cast(new_location.element_pointer)); + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(new_location.element_pointer), std::move(element)); + + const skipfield_type new_value = *(new_location.skipfield_pointer) - 1; + + if (new_value != 0) + { + *(new_location.skipfield_pointer + new_value) = *(new_location.skipfield_pointer + 1) = new_value; + ++(groups_with_erasures_list_head->free_list_head); + + if (prev_free_list_index != std::numeric_limits::max()) + { + *(reinterpret_cast(new_location.group_pointer->elements + prev_free_list_index) + 1) = groups_with_erasures_list_head->free_list_head; + } + + *(reinterpret_cast(new_location.element_pointer + 1)) = prev_free_list_index; + *(reinterpret_cast(new_location.element_pointer + 1) + 1) = std::numeric_limits::max(); + } + else + { + groups_with_erasures_list_head->free_list_head = prev_free_list_index; + + if (prev_free_list_index != std::numeric_limits::max()) + { + *(reinterpret_cast(new_location.group_pointer->elements + prev_free_list_index) + 1) = std::numeric_limits::max(); + } + else + { + groups_with_erasures_list_head = groups_with_erasures_list_head->erasures_list_next_group; + } + } + + *(new_location.skipfield_pointer) = 0; + ++(new_location.group_pointer->number_of_elements); + + if (new_location.group_pointer == begin_iterator.group_pointer && new_location.element_pointer < begin_iterator.element_pointer) + { + begin_iterator = new_location; + } + + ++total_number_of_elements; + + return new_location; + } + } + } + else + { + initialize(pointer_allocator_pair.min_elements_per_group); + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_nothrow_move_constructible::value) + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer++), std::move(element)); + } + else + #endif + { + try + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer++), std::move(element)); + } + catch (...) + { + clear(); + throw; + } + } + + ++end_iterator.skipfield_pointer; + total_number_of_elements = 1; + return begin_iterator; + } + } + #endif + + + + + #ifdef PLF_COLONY_VARIADICS_SUPPORT + template + iterator emplace(arguments &&... parameters) // The emplace function is near-identical to the regular insert function, with the exception of the element construction method, removal of internal VARIADICS support checks, and change to is_nothrow tests. + { + if (end_iterator.element_pointer != NULL) + { + switch(((groups_with_erasures_list_head != NULL) << 1) | (end_iterator.element_pointer == reinterpret_cast(end_iterator.group_pointer->skipfield))) + { + case 0: + { + const iterator return_iterator = end_iterator; + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_nothrow_constructible::value) + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer++), std::forward(parameters)...); + end_iterator.group_pointer->last_endpoint = end_iterator.element_pointer; + } + else + #endif + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer), std::forward(parameters)...); + end_iterator.group_pointer->last_endpoint = ++end_iterator.element_pointer; + } + + ++(end_iterator.group_pointer->number_of_elements); + ++end_iterator.skipfield_pointer; + ++total_number_of_elements; + + return return_iterator; + } + case 1: + { + end_iterator.group_pointer->next_group = PLF_COLONY_ALLOCATE(group_allocator_type, group_allocator_pair, 1, end_iterator.group_pointer); + group &next_group = *(end_iterator.group_pointer->next_group); + const skipfield_type new_group_size = (total_number_of_elements < static_cast(group_allocator_pair.max_elements_per_group)) ? static_cast(total_number_of_elements) : group_allocator_pair.max_elements_per_group; + + try + { + PLF_COLONY_CONSTRUCT(group_allocator_type, group_allocator_pair, &next_group, new_group_size, end_iterator.group_pointer); + } + catch (...) + { + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, &next_group, 1); + end_iterator.group_pointer->next_group = NULL; + throw; + } + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_nothrow_constructible::value) + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(next_group.elements), std::forward(parameters)...); + } + else + #endif + { + try + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(next_group.elements), std::forward(parameters)...); + } + catch (...) + { + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, &next_group); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, &next_group, 1); + end_iterator.group_pointer->next_group = NULL; + throw; + } + } + + end_iterator.group_pointer = &next_group; + end_iterator.element_pointer = next_group.last_endpoint; + end_iterator.skipfield_pointer = next_group.skipfield + 1; + total_capacity += new_group_size; + ++total_number_of_elements; + + return iterator(end_iterator.group_pointer, next_group.elements, next_group.skipfield); + } + default: + { + iterator new_location; + new_location.group_pointer = groups_with_erasures_list_head; + new_location.element_pointer = groups_with_erasures_list_head->elements + groups_with_erasures_list_head->free_list_head; + new_location.skipfield_pointer = groups_with_erasures_list_head->skipfield + groups_with_erasures_list_head->free_list_head; + + const skipfield_type prev_free_list_index = *(reinterpret_cast(new_location.element_pointer)); + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(new_location.element_pointer), std::forward(parameters) ...); + const skipfield_type new_value = *(new_location.skipfield_pointer) - 1; + + if (new_value != 0) + { + *(new_location.skipfield_pointer + new_value) = *(new_location.skipfield_pointer + 1) = new_value; + ++(groups_with_erasures_list_head->free_list_head); + + if (prev_free_list_index != std::numeric_limits::max()) + { + *(reinterpret_cast(new_location.group_pointer->elements + prev_free_list_index) + 1) = groups_with_erasures_list_head->free_list_head; + } + + *(reinterpret_cast(new_location.element_pointer + 1)) = prev_free_list_index; + *(reinterpret_cast(new_location.element_pointer + 1) + 1) = std::numeric_limits::max(); + } + else + { + groups_with_erasures_list_head->free_list_head = prev_free_list_index; + + if (prev_free_list_index != std::numeric_limits::max()) + { + *(reinterpret_cast(new_location.group_pointer->elements + prev_free_list_index) + 1) = std::numeric_limits::max(); + } + else + { + groups_with_erasures_list_head = groups_with_erasures_list_head->erasures_list_next_group; + } + } + + *(new_location.skipfield_pointer) = 0; + ++(new_location.group_pointer->number_of_elements); + + if (new_location.group_pointer == begin_iterator.group_pointer && new_location.element_pointer < begin_iterator.element_pointer) + { + begin_iterator = new_location; + } + + ++total_number_of_elements; + + return new_location; + } + } + } + else + { + initialize(pointer_allocator_pair.min_elements_per_group); + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_nothrow_constructible::value) + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer++), std::forward(parameters) ...); + } + else + #endif + { + try + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer++), std::forward(parameters) ...); + } + catch (...) + { + clear(); + throw; + } + } + + ++end_iterator.skipfield_pointer; + total_number_of_elements = 1; + return begin_iterator; + } + } + #endif + + + + +private: + + // Internal functions for fill insert: + + void group_create(const skipfield_type number_of_elements) + { + const group_pointer_type next_group = end_iterator.group_pointer->next_group = PLF_COLONY_ALLOCATE(group_allocator_type, group_allocator_pair, 1, end_iterator.group_pointer); + + try + { + #ifdef PLF_COLONY_VARIADICS_SUPPORT + PLF_COLONY_CONSTRUCT(group_allocator_type, group_allocator_pair, next_group, number_of_elements, end_iterator.group_pointer); + #else + PLF_COLONY_CONSTRUCT(group_allocator_type, group_allocator_pair, next_group, group(number_of_elements, end_iterator.group_pointer)); + #endif + } + catch (...) + { + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, next_group); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, next_group, 1); + end_iterator.group_pointer->next_group = NULL; + throw; + } + + end_iterator.group_pointer = next_group; + end_iterator.element_pointer = next_group->elements; + next_group->number_of_elements = 0; // group constructor sets this to 1 by default to allow for faster insertion during insertion/emplace in other cases + total_capacity += number_of_elements; + } + + + + void group_fill(const element_type &element, const skipfield_type number_of_elements) + { + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_trivially_copyable::value && std::is_trivially_copy_constructible::value && std::is_nothrow_copy_constructible::value) // ie. we can get away with using the cheaper fill_n here if there is no chance of an exception being thrown: + { + #ifdef PLF_COLONY_ALIGNMENT_SUPPORT + if PLF_COLONY_CONSTEXPR (sizeof(aligned_element_type) == sizeof(element_type)) + { + std::fill_n(reinterpret_cast(end_iterator.element_pointer), number_of_elements, element); + } + else + { + alignas (sizeof(aligned_element_type)) element_type aligned_copy = element; // to avoid potentially violating memory boundaries in line below, create an initial copy object of same (but aligned) type + std::fill_n(end_iterator.element_pointer, number_of_elements, *(reinterpret_cast(&aligned_copy))); + } + #else // type is not aligned to anything so is safe to use fill_n anyway: + std::fill_n(reinterpret_cast(end_iterator.element_pointer), number_of_elements, element); + #endif + + end_iterator.element_pointer += number_of_elements; + } + else + #endif + { + const aligned_pointer_type fill_end = end_iterator.element_pointer + number_of_elements; + + do + { + try + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(end_iterator.element_pointer++), element); + } + catch (...) + { + end_iterator.group_pointer->last_endpoint = --end_iterator.element_pointer; + const skipfield_type elements_constructed_before_exception = static_cast(end_iterator.element_pointer - end_iterator.group_pointer->elements); + end_iterator.group_pointer->number_of_elements = elements_constructed_before_exception; + end_iterator.skipfield_pointer = end_iterator.group_pointer->skipfield + elements_constructed_before_exception; + throw; + } + } while (end_iterator.element_pointer != fill_end); + } + + end_iterator.group_pointer->last_endpoint = end_iterator.element_pointer; + end_iterator.group_pointer->number_of_elements += number_of_elements; + } + + + + void fill_skipblock(const element_type &element, aligned_pointer_type const location, skipfield_pointer_type const skipfield_pointer, const skipfield_type number_of_elements) + { + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_trivially_copyable::value && std::is_trivially_copy_constructible::value && std::is_nothrow_copy_constructible::value) // ie. we can get away with using the cheaper fill_n here if there is no chance of an exception being thrown: + { + #ifdef PLF_COLONY_ALIGNMENT_SUPPORT + if PLF_COLONY_CONSTEXPR (sizeof(aligned_element_type) == sizeof(element_type)) + { + std::fill_n(reinterpret_cast(location), number_of_elements, element); + } + else + { + alignas (sizeof(aligned_element_type)) element_type aligned_copy = element; // to avoid potentially violating memory boundaries in line below, create an initial copy object of same (but aligned) type + std::fill_n(location, number_of_elements, *(reinterpret_cast(&aligned_copy))); + } + #else // type is not aligned to anything so is safe to use fill_n anyway: + std::fill_n(reinterpret_cast(location), number_of_elements, element); + #endif + } + else + #endif + { + const skipfield_type prev_free_list_node = *(reinterpret_cast(location)); // in case of exception, grabbing indexes before free_list node is reused + const aligned_pointer_type fill_end = location + number_of_elements; + + for (aligned_pointer_type current_location = location; current_location != fill_end; ++current_location) + { + try + { + PLF_COLONY_CONSTRUCT(element_allocator_type, (*this), reinterpret_cast(current_location), element); + } + catch (...) + { + // Reconstruct existing skipblock and free-list indexes to reflect partially-reused skipblock: + const skipfield_type elements_constructed_before_exception = static_cast((current_location - 1) - location); + groups_with_erasures_list_head->number_of_elements += elements_constructed_before_exception; + total_number_of_elements += elements_constructed_before_exception; + + std::memset(skipfield_pointer, 0, elements_constructed_before_exception * sizeof(skipfield_type)); + + *(reinterpret_cast(location + elements_constructed_before_exception)) = prev_free_list_node; + *(reinterpret_cast(location + elements_constructed_before_exception) + 1) = std::numeric_limits::max(); + + const skipfield_type new_skipblock_head_index = static_cast(location - groups_with_erasures_list_head->elements) + elements_constructed_before_exception; + groups_with_erasures_list_head->free_list_head = new_skipblock_head_index; + + if (prev_free_list_node != std::numeric_limits::max()) + { + *(reinterpret_cast(groups_with_erasures_list_head->elements + prev_free_list_node) + 1) = new_skipblock_head_index; + } + + throw; + } + } + } + + std::memset(skipfield_pointer, 0, number_of_elements * sizeof(skipfield_type)); // reset skipfield nodes within skipblock to 0 + groups_with_erasures_list_head->number_of_elements += number_of_elements; + total_number_of_elements += number_of_elements; + } + + + +public: + + // Fill insert + + void insert(size_type number_of_elements, const element_type &element) + { + if (number_of_elements == 0) + { + return; + } + else if (number_of_elements == 1) + { + insert(element); + return; + } + + if (begin_iterator.group_pointer == NULL) // Empty colony, no groups created yet + { + initialize((number_of_elements > group_allocator_pair.max_elements_per_group) ? group_allocator_pair.max_elements_per_group : (number_of_elements < pointer_allocator_pair.min_elements_per_group) ? pointer_allocator_pair.min_elements_per_group : static_cast(number_of_elements)); // Construct first group + begin_iterator.group_pointer->number_of_elements = 0; + } + + if (total_number_of_elements != 0) // ie. not an uninitialized colony or a situation where reserve has been called + { + // Use up erased locations if available: + if (groups_with_erasures_list_head != NULL) + { + do // skipblock loop: breaks when group is exhausted of reusable skipblocks, or returns if number_of_elements == 0 + { + aligned_pointer_type const element_pointer = groups_with_erasures_list_head->elements + groups_with_erasures_list_head->free_list_head; + skipfield_pointer_type const skipfield_pointer = groups_with_erasures_list_head->skipfield + groups_with_erasures_list_head->free_list_head; + const skipfield_type skipblock_size = *skipfield_pointer; + + if (groups_with_erasures_list_head == begin_iterator.group_pointer && element_pointer < begin_iterator.element_pointer) + { + begin_iterator.element_pointer = element_pointer; + begin_iterator.skipfield_pointer = skipfield_pointer; + } + + if (skipblock_size <= number_of_elements) + { + groups_with_erasures_list_head->free_list_head = *(reinterpret_cast(element_pointer)); // set free list head to previous free list node + fill_skipblock(element, element_pointer, skipfield_pointer, skipblock_size); + number_of_elements -= skipblock_size; + + if (groups_with_erasures_list_head->free_list_head != std::numeric_limits::max()) + { + *(reinterpret_cast(groups_with_erasures_list_head->elements + groups_with_erasures_list_head->free_list_head) + 1) = std::numeric_limits::max(); // set 'next' index of new free list head to 'end' (numeric max) + } + else + { + groups_with_erasures_list_head = groups_with_erasures_list_head->erasures_list_next_group; // change groups + + if (groups_with_erasures_list_head == NULL) + { + break; + } + } + } + else // skipblock is larger than remaining number of elements + { + const skipfield_type prev_index = *(reinterpret_cast(element_pointer)); // save before element location is overwritten + fill_skipblock(element, element_pointer, skipfield_pointer, static_cast(number_of_elements)); + const skipfield_type new_skipblock_size = static_cast(skipblock_size - number_of_elements); + + // Update skipfield (earlier nodes already memset'd in fill_skipblock function): + *(skipfield_pointer + number_of_elements) = new_skipblock_size; + *(skipfield_pointer + skipblock_size - 1) = new_skipblock_size; + groups_with_erasures_list_head->free_list_head += static_cast(number_of_elements); // set free list head to new start node + + // Update free list with new head: + *(reinterpret_cast(element_pointer + number_of_elements)) = prev_index; + *(reinterpret_cast(element_pointer + number_of_elements) + 1) = std::numeric_limits::max(); + + if (prev_index != std::numeric_limits::max()) + { + *(reinterpret_cast(groups_with_erasures_list_head->elements + prev_index) + 1) = groups_with_erasures_list_head->free_list_head; // set 'next' index of previous skipblock to new start of skipblock + } + + return; + } + } while(number_of_elements != 0); + } + + + // Use up remaining available element locations in end group: + const skipfield_type group_remainder = (static_cast(reinterpret_cast(end_iterator.group_pointer->skipfield) - end_iterator.element_pointer) > number_of_elements) ? static_cast(number_of_elements) : static_cast(reinterpret_cast(end_iterator.group_pointer->skipfield) - end_iterator.element_pointer); + + if (group_remainder != 0) + { + group_fill(element, group_remainder); + total_number_of_elements += group_remainder; + number_of_elements -= group_remainder; + } + } + else if (end_iterator.group_pointer->capacity >= number_of_elements) + { + group_fill(element, static_cast(number_of_elements)); + end_iterator.skipfield_pointer = end_iterator.group_pointer->skipfield + number_of_elements; + total_number_of_elements = number_of_elements; + return; + } + else + { + group_fill(element, end_iterator.group_pointer->capacity); + total_number_of_elements = end_iterator.group_pointer->capacity; + number_of_elements -= end_iterator.group_pointer->capacity; + } + + + // If there's some elements left that need to be created, create new groups and fill: + if (number_of_elements > group_allocator_pair.max_elements_per_group) + { + size_type multiples = (number_of_elements / static_cast(group_allocator_pair.max_elements_per_group)); + const skipfield_type element_remainder = static_cast(number_of_elements - (multiples * static_cast(group_allocator_pair.max_elements_per_group))); + + while (multiples-- != 0) + { + group_create(group_allocator_pair.max_elements_per_group); + group_fill(element, group_allocator_pair.max_elements_per_group); + } + + if (element_remainder != 0) + { + group_create(group_allocator_pair.max_elements_per_group); + group_fill(element, element_remainder); + } + } + else if (number_of_elements != 0) + { + group_create(static_cast((number_of_elements > total_number_of_elements) ? number_of_elements : total_number_of_elements)); + group_fill(element, static_cast(number_of_elements)); + } + + total_number_of_elements += number_of_elements; // Adds the remainder from the last if-block - the insert functions in the first if/else block will already have incremented total_number_of_elements + end_iterator.skipfield_pointer = end_iterator.group_pointer->skipfield + (end_iterator.element_pointer - end_iterator.group_pointer->elements); + } + + + + // Range insert + + template + inline void insert (typename plf_enable_if_c::is_integer, iterator_type>::type first, const iterator_type last) + { + while (first != last) + { + insert(*first++); + } + } + + + + // Initializer-list insert + + #ifdef PLF_COLONY_INITIALIZER_LIST_SUPPORT + inline void insert (const std::initializer_list &element_list) + { // use range insert: + insert(element_list.begin(), element_list.end()); + } + #endif + + + +private: + + inline PLF_COLONY_FORCE_INLINE void update_subsequent_group_numbers(group_pointer_type current_group) PLF_COLONY_NOEXCEPT + { + do + { + --(current_group->group_number); + current_group = current_group->next_group; + } while (current_group != NULL); + } + + + + inline PLF_COLONY_FORCE_INLINE void consolidate() // get all elements contiguous in memory and shrink to fit, remove erasures and erasure free lists + { + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + colony temp; + temp.change_group_sizes(static_cast((pointer_allocator_pair.min_elements_per_group > total_number_of_elements) ? pointer_allocator_pair.min_elements_per_group : ((total_number_of_elements > group_allocator_pair.max_elements_per_group) ? group_allocator_pair.max_elements_per_group : total_number_of_elements)), group_allocator_pair.max_elements_per_group); // Make first allocated group as large total number of elements, where possible + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_move_assignable::value && std::is_move_constructible::value) + { + temp.insert(std::make_move_iterator(begin_iterator), std::make_move_iterator(end_iterator)); + } + else + #endif + { + temp.insert(begin_iterator, end_iterator); + } + + temp.pointer_allocator_pair.min_elements_per_group = pointer_allocator_pair.min_elements_per_group; // reset to correct value for future clear() or erasures + *this = std::move(temp); // Avoid generating 2nd temporary + #else + colony temp(*this); + swap(temp); + #endif + } + + + + void remove_from_groups_with_erasures_list(const group_pointer_type group_to_remove) PLF_COLONY_NOEXCEPT + { + if (group_to_remove == groups_with_erasures_list_head) + { + groups_with_erasures_list_head = groups_with_erasures_list_head->erasures_list_next_group; + return; + } + + group_pointer_type previous_group = groups_with_erasures_list_head, current_group = groups_with_erasures_list_head->erasures_list_next_group; + + while (group_to_remove != current_group) + { + previous_group = current_group; + current_group = current_group->erasures_list_next_group; + } + + previous_group->erasures_list_next_group = current_group->erasures_list_next_group; + } + + + +public: + + // must return iterator to subsequent non-erased element (or end()), in case the group containing the element which the iterator points to becomes empty after the erasure, and is thereafter removed from the colony chain, making the current iterator invalid and unusable in a ++ operation: + iterator erase(const const_iterator &it) // if uninitialized/invalid iterator supplied, function could generate an exception + { + assert(!empty()); + const group_pointer_type group_pointer = it.group_pointer; + assert(group_pointer != NULL); // ie. not uninitialized iterator + assert(it.element_pointer != group_pointer->last_endpoint); // ie. != end() + assert(*(it.skipfield_pointer) == 0); // ie. element pointed to by iterator has not been erased previously + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (!(std::is_trivially_destructible::value)) // This if-statement should be removed by the compiler on resolution of element_type. For some optimizing compilers this step won't be necessary (for MSVC 2013 it makes a difference) + #endif + { + PLF_COLONY_DESTROY(element_allocator_type, (*this), reinterpret_cast(it.element_pointer)); // Destruct element + } + + --total_number_of_elements; + + if (group_pointer->number_of_elements-- != 1) // ie. non-empty group at this point in time, don't consolidate - optimization note: GCC optimizes postfix + 1 comparison better than prefix + 1 comparison in many cases. + { + // Code logic for following section: + // --------------------------------- + // If current skipfield node has no skipped node on either side, continue as usual + // If node only has skipped node on left, set current node and start node of the skipblock to left node value + 1. + // If node only has skipped node on right, make this node the start node of the skipblock and update end node + // If node has skipped nodes on left and right, set start node of left skipblock and end node of right skipblock to the values of the left + right nodes + 1 + + // Optimization explanation: + // The contextual logic below is the same as that in the insert() functions but in this case the value of the current skipfield node will always be + // zero (since it is not yet erased), meaning no additional manipulations are necessary for the previous skipfield node comparison - we only have to check against zero + const char prev_skipfield = *(it.skipfield_pointer - (it.skipfield_pointer != group_pointer->skipfield)) != 0; + const char after_skipfield = *(it.skipfield_pointer + 1) != 0; // NOTE: boundary test (checking against end-of-elements) is able to be skipped due to the extra skipfield node (compared to element field) - which is present to enable faster iterator operator ++ operations + skipfield_type update_value = 1; + + switch ((after_skipfield << 1) | prev_skipfield) + { + case 0: // no consecutive erased elements + { + *it.skipfield_pointer = 1; // solo skipped node + const skipfield_type index = static_cast(it.element_pointer - group_pointer->elements); + + if (group_pointer->free_list_head != std::numeric_limits::max()) // ie. if this group already has some erased elements + { + *(reinterpret_cast(group_pointer->elements + group_pointer->free_list_head) + 1) = index; // set prev free list head's 'next index' number to the index of the current element + } + else + { + group_pointer->erasures_list_next_group = groups_with_erasures_list_head; // add it to the groups-with-erasures free list + groups_with_erasures_list_head = group_pointer; + } + + *(reinterpret_cast(it.element_pointer)) = group_pointer->free_list_head; + *(reinterpret_cast(it.element_pointer) + 1) = std::numeric_limits::max(); + group_pointer->free_list_head = index; + break; + } + case 1: // previous erased consecutive elements, none following + { + *(it.skipfield_pointer - *(it.skipfield_pointer - 1)) = *it.skipfield_pointer = *(it.skipfield_pointer - 1) + 1; + break; + } + case 2: // following erased consecutive elements, none preceding + { + const skipfield_type following_value = *(it.skipfield_pointer + 1) + 1; + *(it.skipfield_pointer + following_value - 1) = *(it.skipfield_pointer) = following_value; + + const skipfield_type following_previous = *(reinterpret_cast(it.element_pointer + 1)); + const skipfield_type following_next = *(reinterpret_cast(it.element_pointer + 1) + 1); + *(reinterpret_cast(it.element_pointer)) = following_previous; + *(reinterpret_cast(it.element_pointer) + 1) = following_next; + + const skipfield_type index = static_cast(it.element_pointer - group_pointer->elements); + + if (following_previous != std::numeric_limits::max()) + { + *(reinterpret_cast(group_pointer->elements + following_previous) + 1) = index; // Set next index of previous free list node to this node's 'next' index + } + + if (following_next != std::numeric_limits::max()) + { + *(reinterpret_cast(group_pointer->elements + following_next)) = index; // Set previous index of next free list node to this node's 'previous' index + } + else + { + group_pointer->free_list_head = index; + } + + update_value = following_value; + break; + } + case 3: // both preceding and following consecutive erased elements + { + const skipfield_type preceding_value = *(it.skipfield_pointer - 1); + const skipfield_type following_value = *(it.skipfield_pointer + 1) + 1; + + // Join the skipblocks + *(it.skipfield_pointer - preceding_value) = *(it.skipfield_pointer + following_value - 1) = preceding_value + following_value; + + // Remove the following skipblock's entry from the free list + const skipfield_type following_previous = *(reinterpret_cast(it.element_pointer + 1)); + const skipfield_type following_next = *(reinterpret_cast(it.element_pointer + 1) + 1); + + if (following_previous != std::numeric_limits::max()) + { + *(reinterpret_cast(group_pointer->elements + following_previous) + 1) = following_next; // Set next index of previous free list node to this node's 'next' index + } + + if (following_next != std::numeric_limits::max()) + { + *(reinterpret_cast(group_pointer->elements + following_next)) = following_previous; // Set previous index of next free list node to this node's 'previous' index + } + else + { + group_pointer->free_list_head = following_previous; + } + + update_value = following_value; + break; + } + } + + iterator return_iterator(it.group_pointer, it.element_pointer + update_value, it.skipfield_pointer + update_value); + return_iterator.check_for_end_of_group_and_progress(); + + if (it.element_pointer == begin_iterator.element_pointer) // If original iterator was first element in colony, update it's value with the next non-erased element: + { + begin_iterator = return_iterator; + } + + return return_iterator; + } + + // else: group is empty, consolidate groups + switch((group_pointer->next_group != NULL) | ((group_pointer != begin_iterator.group_pointer) << 1)) + { + case 0: // ie. group_pointer == begin_iterator.group_pointer && group_pointer->next_group == NULL; only group in colony + { + // Reset skipfield and free list rather than clearing - leads to fewer allocations/deallocations: + std::memset(&*(group_pointer->skipfield), 0, sizeof(skipfield_type) * group_pointer->capacity); // &* to avoid problems with non-trivial pointers. Although there is one more skipfield than group_pointer->capacity, capacity + 1 is not necessary here as the end skipfield is never written to after initialization + group_pointer->free_list_head = std::numeric_limits::max(); + groups_with_erasures_list_head = NULL; + + // Reset begin and end iterators: + end_iterator.element_pointer = begin_iterator.element_pointer = group_pointer->last_endpoint = group_pointer->elements; + end_iterator.skipfield_pointer = begin_iterator.skipfield_pointer = group_pointer->skipfield; + + return end_iterator; + } + case 1: // ie. group_pointer == begin_iterator.group_pointer && group_pointer->next_group != NULL. Remove first group, change first group to next group + { + group_pointer->next_group->previous_group = NULL; // Cut off this group from the chain + begin_iterator.group_pointer = group_pointer->next_group; // Make the next group the first group + + update_subsequent_group_numbers(begin_iterator.group_pointer); + + if (group_pointer->free_list_head != std::numeric_limits::max()) // Erasures present within the group, ie. was part of the intrusive list of groups with erasures. + { + remove_from_groups_with_erasures_list(group_pointer); + } + + total_capacity -= group_pointer->capacity; + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, group_pointer); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, group_pointer, 1); + + // note: end iterator only needs to be changed if the deleted group was the final group in the chain ie. not in this case + begin_iterator.element_pointer = begin_iterator.group_pointer->elements + *(begin_iterator.group_pointer->skipfield); // If the beginning index has been erased (ie. skipfield != 0), skip to next non-erased element + begin_iterator.skipfield_pointer = begin_iterator.group_pointer->skipfield + *(begin_iterator.group_pointer->skipfield); + + return begin_iterator; + } + case 3: // this is a non-first group but not final group in chain: delete the group, then link previous group to the next group in the chain: + { + group_pointer->next_group->previous_group = group_pointer->previous_group; + const group_pointer_type return_group = group_pointer->previous_group->next_group = group_pointer->next_group; // close the chain, removing this group from it + + update_subsequent_group_numbers(return_group); + + if (group_pointer->free_list_head != std::numeric_limits::max()) + { + remove_from_groups_with_erasures_list(group_pointer); + } + + total_capacity -= group_pointer->capacity; + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, group_pointer); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, group_pointer, 1); + + // Return next group's first non-erased element: + return iterator(return_group, return_group->elements + *(return_group->skipfield), return_group->skipfield + *(return_group->skipfield)); + } + default: // this is a non-first group and the final group in the chain + { + if (group_pointer->free_list_head != std::numeric_limits::max()) + { + remove_from_groups_with_erasures_list(group_pointer); + } + + group_pointer->previous_group->next_group = NULL; + end_iterator.group_pointer = group_pointer->previous_group; // end iterator needs to be changed as element supplied was the back element of the colony + end_iterator.element_pointer = reinterpret_cast(end_iterator.group_pointer->skipfield); + end_iterator.skipfield_pointer = end_iterator.group_pointer->skipfield + end_iterator.group_pointer->capacity; + + total_capacity -= group_pointer->capacity; + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, group_pointer); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, group_pointer, 1); + + return end_iterator; + } + } + } + + + + // Range erase: + + void erase(const const_iterator &iterator1, const const_iterator &iterator2) // if uninitialized/invalid iterators supplied, function could generate an exception. If iterator1 > iterator2, behaviour is undefined. + { + assert(iterator1 <= iterator2); + + iterator current = iterator1; + + if (current.group_pointer != iterator2.group_pointer) + { + if (current.element_pointer != current.group_pointer->elements + *(current.group_pointer->skipfield)) // if iterator1 is not the first non-erased element in it's group - most common case + { + size_type number_of_group_erasures = 0; + + // Now update skipfield: + const aligned_pointer_type end = iterator1.group_pointer->last_endpoint; + + // Schema: first erase all non-erased elements until end of group & remove all skipblocks post-iterator1 from the free_list. Then, either update preceding skipblock or create new one: + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT // if trivially-destructible, and C++11 or higher, and no erasures in group, skip while loop below and just jump straight to the location + if ((std::is_trivially_destructible::value) & (current.group_pointer->free_list_head == std::numeric_limits::max())) + { + number_of_group_erasures += static_cast(end - current.element_pointer); + } + else + #endif + { + while (current.element_pointer != end) + { + if (*current.skipfield_pointer == 0) + { + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (!(std::is_trivially_destructible::value)) + #endif + { + PLF_COLONY_DESTROY(element_allocator_type, (*this), reinterpret_cast(current.element_pointer)); // Destruct element + } + + ++number_of_group_erasures; + ++current.element_pointer; + ++current.skipfield_pointer; + } + else // remove skipblock from group: + { + const skipfield_type prev_free_list_index = *(reinterpret_cast(current.element_pointer)); + const skipfield_type next_free_list_index = *(reinterpret_cast(current.element_pointer) + 1); + + current.element_pointer += *(current.skipfield_pointer); + current.skipfield_pointer += *(current.skipfield_pointer); + + if (next_free_list_index == std::numeric_limits::max() && prev_free_list_index == std::numeric_limits::max()) // if this is the last skipblock in the free list + { + remove_from_groups_with_erasures_list(iterator1.group_pointer); // remove group from list of free-list groups - will be added back in down below, but not worth optimizing for + iterator1.group_pointer->free_list_head = std::numeric_limits::max(); + number_of_group_erasures += static_cast(end - current.element_pointer); + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (!(std::is_trivially_destructible::value)) + #endif + { + while (current.element_pointer != end) // miniloop - avoid checking skipfield for rest of elements in group, as there are no more skipped elements now + { + PLF_COLONY_DESTROY(element_allocator_type, (*this), reinterpret_cast(current.element_pointer++)); // Destruct element + } + } + + break; // end overall while loop + } + else if (next_free_list_index == std::numeric_limits::max()) // if this is the head of the free list + { + current.group_pointer->free_list_head = prev_free_list_index; // make free list head equal to next free list node + *(reinterpret_cast(current.group_pointer->elements + prev_free_list_index) + 1) = std::numeric_limits::max(); + } + else // either a tail or middle free list node + { + *(reinterpret_cast(current.group_pointer->elements + next_free_list_index)) = prev_free_list_index; + + if (prev_free_list_index != std::numeric_limits::max()) // ie. not the tail free list node + { + *(reinterpret_cast(current.group_pointer->elements + prev_free_list_index) + 1) = next_free_list_index; + } + } + } + } + } + + + const skipfield_type previous_node_value = *(iterator1.skipfield_pointer - 1); + const skipfield_type distance_to_end = static_cast(end - iterator1.element_pointer); + + if (previous_node_value == 0) // no previous skipblock + { + *iterator1.skipfield_pointer = distance_to_end; + *(iterator1.skipfield_pointer + distance_to_end - 1) = distance_to_end; + + const skipfield_type index = static_cast(iterator1.element_pointer - iterator1.group_pointer->elements); + + if (iterator1.group_pointer->free_list_head != std::numeric_limits::max()) // ie. if this group already has some erased elements + { + *(reinterpret_cast(iterator1.group_pointer->elements + iterator1.group_pointer->free_list_head) + 1) = index; // set prev free list head's 'next index' number to the index of the iterator1 element + } + else + { + iterator1.group_pointer->erasures_list_next_group = groups_with_erasures_list_head; // add it to the groups-with-erasures free list + groups_with_erasures_list_head = iterator1.group_pointer; + } + + *(reinterpret_cast(iterator1.element_pointer)) = iterator1.group_pointer->free_list_head; + *(reinterpret_cast(iterator1.element_pointer) + 1) = std::numeric_limits::max(); + iterator1.group_pointer->free_list_head = index; + } + else + { // update previous skipblock, no need to update free list: + *(iterator1.skipfield_pointer - previous_node_value) = *(iterator1.skipfield_pointer + distance_to_end - 1) = previous_node_value + distance_to_end; + } + + iterator1.group_pointer->number_of_elements -= static_cast(number_of_group_erasures); + total_number_of_elements -= number_of_group_erasures; + + current.group_pointer = current.group_pointer->next_group; + } + + + // Intermediate groups: + const group_pointer_type previous_group = current.group_pointer->previous_group; + + while (current.group_pointer != iterator2.group_pointer) + { + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (!(std::is_trivially_destructible::value)) + #endif + { + current.element_pointer = current.group_pointer->elements + *(current.group_pointer->skipfield); + current.skipfield_pointer = current.group_pointer->skipfield + *(current.group_pointer->skipfield); + const aligned_pointer_type end = current.group_pointer->last_endpoint; + + do + { + PLF_COLONY_DESTROY(element_allocator_type, (*this), reinterpret_cast(current.element_pointer)); // Destruct element + ++current.skipfield_pointer; + current.element_pointer += *current.skipfield_pointer + 1; + current.skipfield_pointer += *current.skipfield_pointer; + } while (current.element_pointer != end); + } + + if (current.group_pointer->free_list_head != std::numeric_limits::max()) + { + remove_from_groups_with_erasures_list(current.group_pointer); + } + + total_number_of_elements -= current.group_pointer->number_of_elements; + const group_pointer_type current_group = current.group_pointer; + current.group_pointer = current.group_pointer->next_group; + + total_capacity -= current_group->capacity; + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, current_group); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, current_group, 1); + } + + current.element_pointer = current.group_pointer->elements + *(current.group_pointer->skipfield); + current.skipfield_pointer = current.group_pointer->skipfield + *(current.group_pointer->skipfield); + current.group_pointer->previous_group = previous_group; + + if (previous_group != NULL) + { + previous_group->next_group = current.group_pointer; + } + else + { + begin_iterator = iterator2; // This line is included here primarily to avoid a secondary if statement within the if block below - it will not be needed in any other situation + } + } + + if (current.element_pointer == iterator2.element_pointer) // in case iterator2 was at beginning of it's group - also covers empty range case (first == last) + { + return; + } + + // Final group: + // Code explanation: + // If not erasing entire final group, 1. Destruct elements (if non-trivial destructor) and add locations to group free list. 2. process skipfield. + // If erasing entire group, 1. Destruct elements (if non-trivial destructor), 2. if no elements left in colony, clear() 3. otherwise reset end_iterator and remove group from groups-with-erasures list (if free list of erasures present) + + if (iterator2.element_pointer != end_iterator.element_pointer || current.element_pointer != current.group_pointer->elements + *(current.group_pointer->skipfield)) // ie. not erasing entire group + { + size_type number_of_group_erasures = 0; + // Schema: first erased all non-erased elements until end of group & remove all skipblocks post-iterator2 from the free_list. Then, either update preceding skipblock or create new one: + + const iterator current_saved = current; + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT // if trivially-destructible, and C++11 or higher, and no erasures in group, skip while loop below and just jump straight to the location + if ((std::is_trivially_destructible::value) & (current.group_pointer->free_list_head == std::numeric_limits::max())) + { + number_of_group_erasures += static_cast(iterator2.element_pointer - current.element_pointer); + } + else + #endif + { + while (current.element_pointer != iterator2.element_pointer) + { + if (*current.skipfield_pointer == 0) + { + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (!(std::is_trivially_destructible::value)) + #endif + { + PLF_COLONY_DESTROY(element_allocator_type, (*this), reinterpret_cast(current.element_pointer)); // Destruct element + } + + ++number_of_group_erasures; + ++current.element_pointer; + ++current.skipfield_pointer; + } + else // remove skipblock from group: + { + const skipfield_type prev_free_list_index = *(reinterpret_cast(current.element_pointer)); + const skipfield_type next_free_list_index = *(reinterpret_cast(current.element_pointer) + 1); + + current.element_pointer += *(current.skipfield_pointer); + current.skipfield_pointer += *(current.skipfield_pointer); + + if (next_free_list_index == std::numeric_limits::max() && prev_free_list_index == std::numeric_limits::max()) // if this is the last skipblock in the free list + { + remove_from_groups_with_erasures_list(iterator2.group_pointer); // remove group from list of free-list groups - will be added back in down below, but not worth optimizing for + iterator2.group_pointer->free_list_head = std::numeric_limits::max(); + number_of_group_erasures += static_cast(iterator2.element_pointer - current.element_pointer); + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (!(std::is_trivially_destructible::value)) + #endif + { + while (current.element_pointer != iterator2.element_pointer) + { + PLF_COLONY_DESTROY(element_allocator_type, (*this), reinterpret_cast(current.element_pointer++)); // Destruct element + } + } + + break; // end overall while loop + } + else if (next_free_list_index == std::numeric_limits::max()) // if this is the head of the free list + { + current.group_pointer->free_list_head = prev_free_list_index; + *(reinterpret_cast(current.group_pointer->elements + prev_free_list_index) + 1) = std::numeric_limits::max(); + } + else + { + *(reinterpret_cast(current.group_pointer->elements + next_free_list_index)) = prev_free_list_index; + + if (prev_free_list_index != std::numeric_limits::max()) // ie. not the tail free list node + { + *(reinterpret_cast(current.group_pointer->elements + prev_free_list_index) + 1) = next_free_list_index; + } + } + } + } + } + + + const skipfield_type distance_to_iterator2 = static_cast(iterator2.element_pointer - current_saved.element_pointer); + const skipfield_type index = static_cast(current_saved.element_pointer - iterator2.group_pointer->elements); + + if (index == 0 || *(current_saved.skipfield_pointer - 1) == 0) // element is either at start of group or previous skipfield node is 0 + { + *(current_saved.skipfield_pointer) = distance_to_iterator2; + *(iterator2.skipfield_pointer - 1) = distance_to_iterator2; + + if (iterator2.group_pointer->free_list_head != std::numeric_limits::max()) // ie. if this group already has some erased elements + { + *(reinterpret_cast(iterator2.group_pointer->elements + iterator2.group_pointer->free_list_head) + 1) = index; + } + else + { + iterator2.group_pointer->erasures_list_next_group = groups_with_erasures_list_head; // add it to the groups-with-erasures free list + groups_with_erasures_list_head = iterator2.group_pointer; + } + + *(reinterpret_cast(current_saved.element_pointer)) = iterator2.group_pointer->free_list_head; + *(reinterpret_cast(current_saved.element_pointer) + 1) = std::numeric_limits::max(); + iterator2.group_pointer->free_list_head = index; + } + else // If iterator 1 & 2 are in same group, but iterator 1 was not at start of group, and previous skipfield node is an end node in a skipblock: + { + // Just update existing skipblock, no need to create new free list node: + const skipfield_type prev_node_value = *(current_saved.skipfield_pointer - 1); + *(current_saved.skipfield_pointer - prev_node_value) = prev_node_value + distance_to_iterator2; + *(iterator2.skipfield_pointer - 1) = prev_node_value + distance_to_iterator2; + } + + + if (iterator1.element_pointer == begin_iterator.element_pointer) + { + begin_iterator = iterator2; + } + + iterator2.group_pointer->number_of_elements -= static_cast(number_of_group_erasures); + total_number_of_elements -= number_of_group_erasures; + } + else // ie. full group erasure + { + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (!(std::is_trivially_destructible::value)) + #endif + { + while(current.element_pointer != iterator2.element_pointer) + { + PLF_COLONY_DESTROY(element_allocator_type, (*this), reinterpret_cast(current.element_pointer)); + ++current.skipfield_pointer; + current.element_pointer += 1 + *current.skipfield_pointer; + current.skipfield_pointer += *current.skipfield_pointer; + } + } + + + if ((total_number_of_elements -= current.group_pointer->number_of_elements) != 0) // ie. previous_group != NULL + { + current.group_pointer->previous_group->next_group = current.group_pointer->next_group; + end_iterator.group_pointer = current.group_pointer->previous_group; + end_iterator.element_pointer = current.group_pointer->previous_group->last_endpoint; + end_iterator.skipfield_pointer = current.group_pointer->previous_group->skipfield + current.group_pointer->previous_group->capacity; + total_capacity -= current.group_pointer->capacity; + + if (current.group_pointer->free_list_head != std::numeric_limits::max()) + { + remove_from_groups_with_erasures_list(current.group_pointer); + } + } + else // ie. colony is now empty + { + blank(); + } + + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, current.group_pointer); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, current.group_pointer, 1); + } + } + + + + inline PLF_COLONY_FORCE_INLINE bool empty() const PLF_COLONY_NOEXCEPT + { + return total_number_of_elements == 0; + } + + + + inline size_type size() const PLF_COLONY_NOEXCEPT + { + return total_number_of_elements; + } + + + + #ifdef PLF_COLONY_TEST_DEBUG // used for debugging during internal testing only: + inline size_type group_size_sum() const + { + group_pointer_type current = begin_iterator.group_pointer; + size_type temp = 0; + + while(current != NULL) + { + temp += current->number_of_elements; + current = current->next_group; + } + + return temp; + } + #endif + + + inline size_type max_size() const PLF_COLONY_NOEXCEPT + { + #ifdef PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT + return std::allocator_traits::max_size(*this); + #else + return element_allocator_type::max_size(); + #endif + } + + + + inline size_type capacity() const PLF_COLONY_NOEXCEPT + { + return total_capacity; + } + + + + inline size_type approximate_memory_use() const PLF_COLONY_NOEXCEPT + { + return + sizeof(*this) + // sizeof colony basic structure + (total_capacity * (sizeof(aligned_element_type) + sizeof(skipfield_type))) + // sizeof current colony data capacity + skipfields + ((end_iterator.group_pointer == NULL) ? 0 : ((end_iterator.group_pointer->group_number + 1) * (sizeof(group) + sizeof(skipfield_type)))); // if colony not empty, add the memory usage of the group structures themselves, adding the extra skipfield node + } + + + + void change_group_sizes(const skipfield_type min_allocation_amount, const skipfield_type max_allocation_amount) + { + assert((min_allocation_amount > 2) & (min_allocation_amount <= max_allocation_amount)); + + pointer_allocator_pair.min_elements_per_group = min_allocation_amount; + group_allocator_pair.max_elements_per_group = max_allocation_amount; + + if (begin_iterator.group_pointer != NULL && (begin_iterator.group_pointer->capacity < min_allocation_amount || end_iterator.group_pointer->capacity > max_allocation_amount)) + { + consolidate(); + } + } + + + + inline void change_minimum_group_size(const skipfield_type min_allocation_amount) + { + change_group_sizes(min_allocation_amount, group_allocator_pair.max_elements_per_group); + } + + + + inline void change_maximum_group_size(const skipfield_type max_allocation_amount) + { + change_group_sizes(pointer_allocator_pair.min_elements_per_group, max_allocation_amount); + } + + + + inline void get_group_sizes(skipfield_type &minimum_group_size, skipfield_type &maximum_group_size) const PLF_COLONY_NOEXCEPT + { + minimum_group_size = pointer_allocator_pair.min_elements_per_group; + maximum_group_size = group_allocator_pair.max_elements_per_group; + } + + + + inline void reinitialize(const skipfield_type min_allocation_amount, const skipfield_type max_allocation_amount) PLF_COLONY_NOEXCEPT + { + assert((min_allocation_amount > 2) & (min_allocation_amount <= max_allocation_amount)); + clear(); + pointer_allocator_pair.min_elements_per_group = min_allocation_amount; + group_allocator_pair.max_elements_per_group = max_allocation_amount; + } + + + + inline PLF_COLONY_FORCE_INLINE void clear() PLF_COLONY_NOEXCEPT + { + destroy_all_data(); + blank(); + } + + + + inline colony & operator = (const colony &source) + { + assert (&source != this); + + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + destroy_all_data(); + colony temp(source); + *this = std::move(temp); // Avoid generating 2nd temporary + #else + clear(); + colony temp(source); + swap(temp); + #endif + + return *this; + } + + + + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + // Move assignment + colony & operator = (colony &&source) PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT(allocator_type) + { + assert (&source != this); + destroy_all_data(); + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_trivial::value && std::is_trivial::value && std::is_trivial::value) + { + std::memcpy(static_cast(this), &source, sizeof(colony)); + } + else + #endif + { + end_iterator = std::move(source.end_iterator); + begin_iterator = std::move(source.begin_iterator); + groups_with_erasures_list_head = source.groups_with_erasures_list_head; + total_number_of_elements = source.total_number_of_elements; + total_capacity = source.total_capacity; + pointer_allocator_pair.min_elements_per_group = source.pointer_allocator_pair.min_elements_per_group; + group_allocator_pair.max_elements_per_group = source.group_allocator_pair.max_elements_per_group; + } + + source.blank(); + return *this; + } + #endif + + + + bool operator == (const colony &rh) const PLF_COLONY_NOEXCEPT + { + assert (this != &rh); + + if (total_number_of_elements != rh.total_number_of_elements) + { + return false; + } + + for (iterator lh_iterator = begin_iterator, rh_iterator = rh.begin_iterator; lh_iterator != end_iterator;) + { + if (*rh_iterator++ != *lh_iterator++) + { + return false; + } + } + + return true; + } + + + + inline bool operator != (const colony &rh) const PLF_COLONY_NOEXCEPT + { + return !(*this == rh); + } + + + + void shrink_to_fit() + { + if (total_number_of_elements == total_capacity) + { + return; + } + else if (total_number_of_elements == 0) // Edge case + { + clear(); + return; + } + + consolidate(); + } + + + + void reserve(const size_type original_reserve_amount) + { + if (original_reserve_amount == 0 || original_reserve_amount <= total_capacity) // Already have enough space allocated + { + return; + } + + skipfield_type reserve_amount; + + if (original_reserve_amount > static_cast(group_allocator_pair.max_elements_per_group)) + { + reserve_amount = group_allocator_pair.max_elements_per_group; + } + else if (original_reserve_amount < static_cast(pointer_allocator_pair.min_elements_per_group)) + { + reserve_amount = pointer_allocator_pair.min_elements_per_group; + } + else if (original_reserve_amount > max_size()) + { + reserve_amount = static_cast(max_size()); + } + else + { + reserve_amount = static_cast(original_reserve_amount); + } + + if (total_number_of_elements == 0) // Most common scenario - empty colony + { + if (begin_iterator.group_pointer != NULL) // Edge case - empty colony but first group is initialized ie. had some insertions but all elements got subsequently erased + { + PLF_COLONY_DESTROY(group_allocator_type, group_allocator_pair, begin_iterator.group_pointer); + PLF_COLONY_DEALLOCATE(group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, 1); + } // else: Empty colony, no insertions yet, time to allocate + + initialize(reserve_amount); + begin_iterator.group_pointer->last_endpoint = begin_iterator.group_pointer->elements; // last_endpoint initially == elements + 1 via default constructor + begin_iterator.group_pointer->number_of_elements = 0; // 1 by default + } + else // Non-empty colony, don't have enough space allocated + { + const skipfield_type original_min_elements = pointer_allocator_pair.min_elements_per_group; + pointer_allocator_pair.min_elements_per_group = static_cast(reserve_amount); // Make sure all groups are at maximum appropriate capacity (this amount already rounded down to a skipfield type earlier in function) + consolidate(); + pointer_allocator_pair.min_elements_per_group = original_min_elements; + } + } + + + + // Advance implementation for iterator and const_iterator: + template + void advance(colony_iterator &it, difference_type distance) const // Cannot be noexcept due to the possibility of an uninitialized iterator + { + // For code simplicity - should hopefully be optimized out by compiler: + group_pointer_type &group_pointer = it.group_pointer; + aligned_pointer_type &element_pointer = it.element_pointer; + skipfield_pointer_type &skipfield_pointer = it.skipfield_pointer; + + assert(group_pointer != NULL); // covers uninitialized colony_iterator && empty group + + // Now, run code based on the nature of the distance type - negative, positive or zero: + if (distance > 0) // ie. += + { + // Code explanation: + // For the initial state of the iterator, we don't know how what elements have been erased before that element in that group. + // So for the first group, we follow the following logic: + // 1. If no elements have been erased in the group, we do simple addition to progress either to within the group (if the distance is small enough) or the end of the group and subtract from distance accordingly. + // 2. If any of the first group elements have been erased, we manually iterate, as we don't know whether the erased elements occur before or after the initial iterator position, and we subtract 1 from the distance amount each time. Iteration continues until either distance becomes zero, or we reach the end of the group. + + // For all subsequent groups, we follow this logic: + // 1. If distance is larger than the total number of non-erased elements in a group, we skip that group and subtract the number of elements in that group from distance + // 2. If distance is smaller than the total number of non-erased elements in a group, then: + // a. if there're no erased elements in the group we simply add distance to group->elements to find the new location for the iterator + // b. if there are erased elements in the group, we manually iterate and subtract 1 from distance on each iteration, until the new iterator location is found ie. distance = 0 + + // Note: incrementing element_pointer is avoided until necessary to avoid needless calculations + + assert (!(element_pointer == group_pointer->last_endpoint && group_pointer->next_group == NULL)); // Check that we're not already at end() + + // Special case for initial element pointer and initial group (we don't know how far into the group the element pointer is) + if (element_pointer != group_pointer->elements + *(group_pointer->skipfield)) // ie. != first non-erased element in group + { + const difference_type distance_from_end = static_cast(group_pointer->last_endpoint - element_pointer); + + if (group_pointer->number_of_elements == static_cast(distance_from_end)) // ie. if there are no erasures in the group (using endpoint - elements_start to determine number of elements in group just in case this is the last group of the colony, in which case group->last_endpoint != group->elements + group->capacity) + { + if (distance < distance_from_end) + { + element_pointer += distance; + skipfield_pointer += distance; + return; + } + else if (group_pointer->next_group == NULL) // either we've reached end() or gone beyond it, so bound to end() + { + element_pointer = group_pointer->last_endpoint; + skipfield_pointer += distance_from_end; + return; + } + else + { + distance -= distance_from_end; + } + } + else + { + const skipfield_pointer_type endpoint = skipfield_pointer + distance_from_end; + + while(true) + { + ++skipfield_pointer; + skipfield_pointer += *skipfield_pointer; + --distance; + + if (skipfield_pointer == endpoint) + { + break; + } + else if (distance == 0) + { + element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield); + return; + } + } + + if (group_pointer->next_group == NULL) // either we've reached end() or gone beyond it, so bound to end() + { + element_pointer = group_pointer->last_endpoint; + return; + } + } + + group_pointer = group_pointer->next_group; + + if (distance == 0) + { + element_pointer = group_pointer->elements + *(group_pointer->skipfield); + skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield); + return; + } + } + + + // Intermediary groups - at the start of this code block and the subsequent block, the position of the iterator is assumed to be the first non-erased element in the current group: + while (static_cast(group_pointer->number_of_elements) <= distance) + { + if (group_pointer->next_group == NULL) // either we've reached end() or gone beyond it, so bound to end() + { + element_pointer = group_pointer->last_endpoint; + skipfield_pointer = group_pointer->skipfield + (group_pointer->last_endpoint - group_pointer->elements); + return; + } + else if ((distance -= group_pointer->number_of_elements) == 0) + { + group_pointer = group_pointer->next_group; + element_pointer = group_pointer->elements + *(group_pointer->skipfield); + skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield); + return; + } + else + { + group_pointer = group_pointer->next_group; + } + } + + + // Final group (if not already reached): + if (group_pointer->free_list_head == std::numeric_limits::max()) // No erasures in this group, use straight pointer addition + { + element_pointer = group_pointer->elements + distance; + skipfield_pointer = group_pointer->skipfield + distance; + return; + } + else // ie. number_of_elements > distance - safe to ignore endpoint check condition while incrementing: + { + skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield); + + do + { + ++skipfield_pointer; + skipfield_pointer += *skipfield_pointer; + } while(--distance != 0); + + element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield); + return; + } + + return; + } + else if (distance < 0) // for negative change + { + // Code logic is very similar to += above + assert(!((element_pointer == group_pointer->elements + *(group_pointer->skipfield)) && group_pointer->previous_group == NULL)); // check that we're not already at begin() + distance = -distance; + + // Special case for initial element pointer and initial group (we don't know how far into the group the element pointer is) + if (element_pointer != group_pointer->last_endpoint) // ie. != end() + { + if (group_pointer->free_list_head == std::numeric_limits::max()) // ie. no prior erasures have occurred in this group + { + const difference_type distance_from_beginning = static_cast(element_pointer - group_pointer->elements); + + if (distance <= distance_from_beginning) + { + element_pointer -= distance; + skipfield_pointer -= distance; + return; + } + else if (group_pointer->previous_group == NULL) // ie. we've gone before begin(), so bound to begin() + { + element_pointer = group_pointer->elements; + skipfield_pointer = group_pointer->skipfield; + return; + } + else + { + distance -= distance_from_beginning; + } + } + else + { + const skipfield_pointer_type beginning_point = group_pointer->skipfield + *(group_pointer->skipfield); + + while(skipfield_pointer != beginning_point) + { + --skipfield_pointer; + skipfield_pointer -= *skipfield_pointer; + + if (--distance == 0) + { + element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield); + return; + } + } + + if (group_pointer->previous_group == NULL) + { + element_pointer = group_pointer->elements + *(group_pointer->skipfield); // This is first group, so bound to begin() (just in case final decrement took us before begin()) + skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield); + return; + } + } + + group_pointer = group_pointer->previous_group; + } + + + // Intermediary groups - at the start of this code block and the subsequent block, the position of the iterator is assumed to be either the first non-erased element in the next group over, or end(): + while(static_cast(group_pointer->number_of_elements) < distance) + { + if (group_pointer->previous_group == NULL) // we've gone beyond begin(), so bound to it + { + element_pointer = group_pointer->elements + *(group_pointer->skipfield); + skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield); + return; + } + + distance -= group_pointer->number_of_elements; + group_pointer = group_pointer->previous_group; + } + + + // Final group (if not already reached): + if (static_cast(group_pointer->number_of_elements) == distance) + { + element_pointer = group_pointer->elements + *(group_pointer->skipfield); + skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield); + return; + } + else if (group_pointer->free_list_head == std::numeric_limits::max()) // ie. no erased elements in this group + { + element_pointer = reinterpret_cast(group_pointer->skipfield) - distance; + skipfield_pointer = (group_pointer->skipfield + group_pointer->capacity) - distance; + return; + } + else // ie. no more groups to traverse but there are erased elements in this group + { + skipfield_pointer = group_pointer->skipfield + group_pointer->capacity; + + do + { + --skipfield_pointer; + skipfield_pointer -= *skipfield_pointer; + } while(--distance != 0); + + element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield); + return; + } + } + + // Only distance == 0 reaches here + } + + + + + // Advance for reverse_iterator and const_reverse_iterator - this needs to be implemented slightly differently to forward-iterator's advance, as it needs to be able to reach rend() (ie. begin() - 1) and to be bounded by rbegin(): + template + void advance(colony_reverse_iterator &reverse_it, difference_type distance) const // could cause exception if iterator is uninitialized + { + group_pointer_type &group_pointer = reverse_it.it.group_pointer; + aligned_pointer_type &element_pointer = reverse_it.it.element_pointer; + skipfield_pointer_type &skipfield_pointer = reverse_it.it.skipfield_pointer; + + assert(element_pointer != NULL); + + if (distance > 0) + { + assert (!(element_pointer == group_pointer->elements - 1 && group_pointer->previous_group == NULL)); // Check that we're not already at rend() + // Special case for initial element pointer and initial group (we don't know how far into the group the element pointer is) + // Since a reverse_iterator cannot == last_endpoint (ie. before rbegin()) we don't need to check for that like with iterator + if (group_pointer->free_list_head == std::numeric_limits::max()) + { + difference_type distance_from_beginning = static_cast(element_pointer - group_pointer->elements); + + if (distance <= distance_from_beginning) + { + element_pointer -= distance; + skipfield_pointer -= distance; + return; + } + else if (group_pointer->previous_group == NULL) // Either we've reached rend() or gone beyond it, so bound to rend() + { + element_pointer = group_pointer->elements - 1; + skipfield_pointer = group_pointer->skipfield - 1; + return; + } + else + { + distance -= distance_from_beginning; + } + } + else + { + const skipfield_pointer_type beginning_point = group_pointer->skipfield + *(group_pointer->skipfield); + + while(skipfield_pointer != beginning_point) + { + --skipfield_pointer; + skipfield_pointer -= *skipfield_pointer; + + if (--distance == 0) + { + element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield); + return; + } + } + + if (group_pointer->previous_group == NULL) + { + element_pointer = group_pointer->elements - 1; // If we've reached rend(), bound to that + skipfield_pointer = group_pointer->skipfield - 1; + return; + } + } + + group_pointer = group_pointer->previous_group; + + + // Intermediary groups - at the start of this code block and the subsequent block, the position of the iterator is assumed to be the first non-erased element in the next group: + while(static_cast(group_pointer->number_of_elements) < distance) + { + if (group_pointer->previous_group == NULL) // bound to rend() + { + element_pointer = group_pointer->elements - 1; + skipfield_pointer = group_pointer->skipfield - 1; + return; + } + + distance -= static_cast(group_pointer->number_of_elements); + group_pointer = group_pointer->previous_group; + } + + + // Final group (if not already reached) + if (static_cast(group_pointer->number_of_elements) == distance) + { + element_pointer = group_pointer->elements + *(group_pointer->skipfield); + skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield); + return; + } + else if (group_pointer->free_list_head == std::numeric_limits::max()) + { + element_pointer = reinterpret_cast(group_pointer->skipfield) - distance; + skipfield_pointer = (group_pointer->skipfield + group_pointer->capacity) - distance; + return; + } + else + { + skipfield_pointer = group_pointer->skipfield + group_pointer->capacity; + + do + { + --skipfield_pointer; + skipfield_pointer -= *skipfield_pointer; + } while(--distance != 0); + + element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield); + return; + } + } + else if (distance < 0) + { + assert (!((element_pointer == (group_pointer->last_endpoint - 1) - *(group_pointer->skipfield + (group_pointer->last_endpoint - group_pointer->elements) - 1)) && group_pointer->next_group == NULL)); // Check that we're not already at rbegin() + + if (element_pointer != group_pointer->elements + *(group_pointer->skipfield)) // ie. != first non-erased element in group + { + if (group_pointer->free_list_head == std::numeric_limits::max()) // ie. if there are no erasures in the group + { + const difference_type distance_from_end = static_cast(group_pointer->last_endpoint - element_pointer); + + if (distance < distance_from_end) + { + element_pointer += distance; + skipfield_pointer += distance; + return; + } + else if (group_pointer->next_group == NULL) // bound to rbegin() + { + element_pointer = group_pointer->last_endpoint - 1; // no erasures so we don't have to subtract skipfield value as we do below + skipfield_pointer += distance_from_end - 1; + return; + } + else + { + distance -= distance_from_end; + } + } + else + { + const skipfield_pointer_type endpoint = skipfield_pointer + (group_pointer->last_endpoint - element_pointer); + + while(true) + { + ++skipfield_pointer; + skipfield_pointer += *skipfield_pointer; + --distance; + + if (skipfield_pointer == endpoint) + { + break; + } + else if (distance == 0) + { + element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield); + return; + } + } + + if (group_pointer->next_group == NULL) // bound to rbegin() + { + --skipfield_pointer; + element_pointer = (group_pointer->last_endpoint - 1) - *skipfield_pointer; + skipfield_pointer -= *skipfield_pointer; + return; + } + } + + group_pointer = group_pointer->next_group; + + if (distance == 0) + { + element_pointer = group_pointer->elements + *(group_pointer->skipfield); + skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield); + return; + } + } + + + // Intermediary groups - at the start of this code block and the subsequent block, the position of the iterator is assumed to be the first non-erased element in the current group, as a result of the previous code blocks: + while(static_cast(group_pointer->number_of_elements) <= distance) + { + if (group_pointer->next_group == NULL) // bound to rbegin() + { + skipfield_pointer = group_pointer->skipfield + (group_pointer->last_endpoint - group_pointer->elements) - 1; + --skipfield_pointer; + element_pointer = (group_pointer->last_endpoint - 1) - *skipfield_pointer; + skipfield_pointer -= *skipfield_pointer; + return; + } + else if ((distance -= group_pointer->number_of_elements) == 0) + { + group_pointer = group_pointer->next_group; + element_pointer = group_pointer->elements + *(group_pointer->skipfield); + skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield); + return; + } + else + { + group_pointer = group_pointer->next_group; + } + } + + + // Final group (if not already reached): + if (group_pointer->free_list_head == std::numeric_limits::max()) // No erasures in this group, use straight pointer addition + { + element_pointer = group_pointer->elements + distance; + skipfield_pointer = group_pointer->skipfield + distance; + return; + } + else // ie. number_of_elements > distance - safe to ignore endpoint check condition while incrementing: + { + skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield); + + do + { + ++skipfield_pointer; + skipfield_pointer += *skipfield_pointer; + } while(--distance != 0); + + element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield); + return; + } + + return; + } + } + + + + + // Next implementations: + template + inline colony_iterator next(const colony_iterator &it, const typename colony_iterator::difference_type distance = 1) const + { + colony_iterator return_iterator(it); + advance(return_iterator, distance); + return return_iterator; + } + + + + template + inline colony_reverse_iterator next(const colony_reverse_iterator &it, const typename colony_reverse_iterator::difference_type distance = 1) const + { + colony_reverse_iterator return_iterator(it); + advance(return_iterator, distance); + return return_iterator; + } + + + + // Prev implementations: + template + inline colony_iterator prev(const colony_iterator &it, const typename colony_iterator::difference_type distance = 1) const + { + colony_iterator return_iterator(it); + advance(return_iterator, -distance); + return return_iterator; + } + + + + template + inline colony_reverse_iterator prev(const colony_reverse_iterator &it, const typename colony_reverse_iterator::difference_type distance = 1) const + { + colony_reverse_iterator return_iterator(it); + advance(return_iterator, -distance); + return return_iterator; + } + + + + // distance implementation: + + template + typename colony_iterator::difference_type distance(const colony_iterator &first, const colony_iterator &last) const + { + // Code logic: + // If iterators are the same, return 0 + // Otherwise, find which iterator is later in colony, copy that to iterator2. Copy the lower to iterator1. + // If they are not pointing to elements in the same group, process the intermediate groups and add distances, + // skipping manual incrementation in all but the initial and final groups. + // In the initial and final groups, manual incrementation must be used to calculate distance, if there have been no prior erasures in those groups. + // If there are no prior erasures in either of those groups, we can use pointer arithmetic to calculate the distances for those groups. + + assert(!(first.group_pointer == NULL) && !(last.group_pointer == NULL)); // Check that they are initialized + + if (last.element_pointer == first.element_pointer) + { + return 0; + } + + typedef colony_iterator iterator_type; + typedef typename iterator_type::difference_type diff_type; + diff_type distance = 0; + + iterator_type iterator1 = first, iterator2 = last; + const bool swap = first > last; + + if (swap) // Less common case + { + iterator1 = last; + iterator2 = first; + } + + if (iterator1.group_pointer != iterator2.group_pointer) // if not in same group, process intermediate groups + { + // Process initial group: + if (iterator1.group_pointer->free_list_head == std::numeric_limits::max()) // If no prior erasures have occured in this group we can do simple addition + { + distance += static_cast(iterator1.group_pointer->last_endpoint - iterator1.element_pointer); + } + else if (iterator1.element_pointer == iterator1.group_pointer->elements) // ie. element is at start of group - rare case + { + distance += static_cast(iterator1.group_pointer->number_of_elements); + } + else + { + const skipfield_pointer_type endpoint = iterator1.skipfield_pointer + (iterator1.group_pointer->last_endpoint - iterator1.element_pointer); + + while (iterator1.skipfield_pointer != endpoint) + { + iterator1.skipfield_pointer += *(++iterator1.skipfield_pointer); + ++distance; + } + } + + // Process all other intermediate groups: + iterator1.group_pointer = iterator1.group_pointer->next_group; + + while (iterator1.group_pointer != iterator2.group_pointer) + { + distance += static_cast(iterator1.group_pointer->number_of_elements); + iterator1.group_pointer = iterator1.group_pointer->next_group; + } + + iterator1.skipfield_pointer = iterator1.group_pointer->skipfield; + } + + + if (iterator1.group_pointer->free_list_head == std::numeric_limits::max()) // ie. no erasures in this group, direct subtraction is possible + { + distance += static_cast(iterator2.skipfield_pointer - iterator1.skipfield_pointer); + } + else if (iterator1.group_pointer->last_endpoint - 1 >= iterator2.element_pointer) // ie. if iterator2 is .end() or 1 before + { + distance += static_cast(iterator1.group_pointer->number_of_elements - (iterator1.group_pointer->last_endpoint - iterator2.element_pointer)); + } + else + { + while (iterator1.skipfield_pointer != iterator2.skipfield_pointer) + { + iterator1.skipfield_pointer += *(++iterator1.skipfield_pointer); + ++distance; + } + } + + + if (swap) + { + distance = -distance; + } + + return distance; + } + + + + template + inline typename colony_reverse_iterator::difference_type distance(const colony_reverse_iterator &first, const colony_reverse_iterator &last) const + { + return distance(last.it, first.it); + } + + + + + // Type-changing functions: + + iterator get_iterator_from_pointer(const pointer element_pointer) const // Cannot be noexcept as colony could be empty or pointer invalid + { + assert(!empty()); + assert(element_pointer != NULL); + + group_pointer_type current_group = end_iterator.group_pointer; // Start with last group first, as will be the largest group + + while (current_group != NULL) + { + if (reinterpret_cast(element_pointer) >= current_group->elements && reinterpret_cast(element_pointer) < reinterpret_cast(current_group->skipfield)) + { + const skipfield_pointer_type skipfield_pointer = current_group->skipfield + (reinterpret_cast(element_pointer) - current_group->elements); + return (*skipfield_pointer == 0) ? iterator(current_group, reinterpret_cast(element_pointer), skipfield_pointer) : end_iterator; // If element has been erased, return end() + } + + current_group = current_group->previous_group; + } + + return end_iterator; + } + + + + template + size_type get_index_from_iterator(const colony_iterator &it) const // may throw exception if iterator is invalid/uninitialized + { + assert(!empty()); + assert(it.group_pointer != NULL); + + // This is essentially a simplified version of distance() optimized for counting from begin() + size_type index = 0; + group_pointer_type group_pointer = begin_iterator.group_pointer; + + // For all prior groups, add group sizes + while (group_pointer != it.group_pointer) + { + index += static_cast(group_pointer->number_of_elements); + group_pointer = group_pointer->next_group; + } + + if (group_pointer->free_list_head == std::numeric_limits::max()) + { + index += static_cast(it.element_pointer - group_pointer->elements); // If no erased elements in group exist, do straight pointer arithmetic to get distance to start for first element + } + else // Otherwise do manual ++ loop - count from beginning of group until location is reached + { + skipfield_pointer_type skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield); + + while(skipfield_pointer != it.skipfield_pointer) + { + ++skipfield_pointer; + skipfield_pointer += *skipfield_pointer; + ++index; + } + } + + return index; + } + + + + template + inline size_type get_index_from_reverse_iterator(const colony_reverse_iterator &rev_iterator) const + { + return get_index_from_iterator(rev_iterator.it); + } + + + + template + iterator get_iterator_from_index(const index_type index) const // Cannot be noexcept as colony could be empty + { + assert(!empty()); + assert(std::numeric_limits::is_integer); + + iterator it(begin_iterator); + advance(it, static_cast(index)); + return it; + } + + + + inline allocator_type get_allocator() const PLF_COLONY_NOEXCEPT + { + return element_allocator_type(); + } + + + + +private: + + struct less + { + bool operator() (const element_type &a, const element_type &b) const PLF_COLONY_NOEXCEPT + { + return a < b; + } + }; + + + // Function-object, used to redirect the sort function to compare element pointers by the elements they point to, and sort the element pointers instead of the elements: + template + struct sort_dereferencer + { + comparison_function stored_instance; + + explicit sort_dereferencer(const comparison_function &function_instance): + stored_instance(function_instance) + {} + + sort_dereferencer() PLF_COLONY_NOEXCEPT + {} + + bool operator() (const pointer first, const pointer second) + { + return stored_instance(*first, *second); + } + }; + + +public: + + + template + void sort(comparison_function compare) + { + if (total_number_of_elements < 2) + { + return; + } + + pointer * const element_pointers = PLF_COLONY_ALLOCATE(pointer_allocator_type, pointer_allocator_pair, total_number_of_elements, NULL); + pointer *element_pointer = element_pointers; + + // Construct pointers to all elements in the colony in sequence: + for (iterator current_element = begin_iterator; current_element != end_iterator; ++current_element) + { + PLF_COLONY_CONSTRUCT(pointer_allocator_type, pointer_allocator_pair, element_pointer++, &*current_element); + } + + // Now, sort the pointers by the values they point to: + #ifdef GFX_TIMSORT_HPP + gfx::timsort(element_pointers, element_pointers + total_number_of_elements, sort_dereferencer(compare)); + #else + std::sort(element_pointers, element_pointers + total_number_of_elements, sort_dereferencer(compare)); + #endif + + + // Create a new colony and copy the elements from the old one to the new one in the order of the sorted pointers: + colony new_location; + new_location.change_group_sizes(pointer_allocator_pair.min_elements_per_group, group_allocator_pair.max_elements_per_group); + + + try + { + new_location.reserve(static_cast((total_number_of_elements > std::numeric_limits::max()) ? std::numeric_limits::max() : total_number_of_elements)); + + #if defined(PLF_COLONY_TYPE_TRAITS_SUPPORT) && defined(PLF_COLONY_MOVE_SEMANTICS_SUPPORT) + if PLF_COLONY_CONSTEXPR (std::is_move_constructible::value) + { + for (pointer *current_element_pointer = element_pointers; current_element_pointer != element_pointer; ++current_element_pointer) + { + new_location.insert(std::move(*reinterpret_cast(*current_element_pointer))); + } + } + else + #endif + { + for (pointer *current_element_pointer = element_pointers; current_element_pointer != element_pointer; ++current_element_pointer) + { + new_location.insert(*reinterpret_cast(*current_element_pointer)); + } + } + } + catch (...) + { + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + for (pointer *current_element_pointer = element_pointers; current_element_pointer != element_pointer; ++current_element_pointer) + { + PLF_COLONY_DESTROY(pointer_allocator_type, pointer_allocator_pair, current_element_pointer); + } + } + + PLF_COLONY_DEALLOCATE(pointer_allocator_type, pointer_allocator_pair, element_pointers, total_number_of_elements); + throw; + } + + + // Make the old colony the new one, destroy the old one's data/sequence: + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + *this = std::move(new_location); // avoid generating temporary + #else + swap(new_location); + #endif + + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + for (pointer *current_element_pointer = element_pointers; current_element_pointer != element_pointer; ++current_element_pointer) + { + PLF_COLONY_DESTROY(pointer_allocator_type, pointer_allocator_pair, current_element_pointer); + } + } + + PLF_COLONY_DEALLOCATE(pointer_allocator_type, pointer_allocator_pair, element_pointers, total_number_of_elements); + } + + + + + inline void sort() + { + sort(less()); + } + + + + void splice(colony &source) PLF_COLONY_NOEXCEPT_SWAP(allocator_type) + { + // Process: if there are unused memory spaces at the end of the current back group of the chain, convert them + // to skipped elements and add the locations to the group's free list. + // Then link the destination's groups to the source's groups and nullify the source. + // If the source has more unused memory spaces in the back group than the destination, swap them before processing to reduce the number of locations added to a free list and also subsequent jumps during iteration. + + assert(&source != this); + + if (source.total_number_of_elements == 0) + { + return; + } + else if (total_number_of_elements == 0) + { + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT + *this = std::move(source); + #else + clear(); + swap(source); + #endif + + return; + } + + // If there's more unused element locations at end of destination than source, swap with source to reduce number of skipped elements and size of free-list: + if ((reinterpret_cast(end_iterator.group_pointer->skipfield) - end_iterator.element_pointer) > (reinterpret_cast(source.end_iterator.group_pointer->skipfield) - source.end_iterator.element_pointer)) + { + swap(source); + } + + + // Correct group sizes if necessary: + if (source.pointer_allocator_pair.min_elements_per_group < pointer_allocator_pair.min_elements_per_group) + { + pointer_allocator_pair.min_elements_per_group = source.pointer_allocator_pair.min_elements_per_group; + } + + if (source.group_allocator_pair.max_elements_per_group > group_allocator_pair.max_elements_per_group) + { + group_allocator_pair.max_elements_per_group = source.group_allocator_pair.max_elements_per_group; + } + + // Add source list of groups-with-erasures to destination list of groups-with-erasures: + if (source.groups_with_erasures_list_head != NULL) + { + if (groups_with_erasures_list_head != NULL) + { + group_pointer_type tail_group = groups_with_erasures_list_head; + + while (tail_group->erasures_list_next_group != NULL) + { + tail_group = tail_group->erasures_list_next_group; + } + + tail_group->erasures_list_next_group = source.groups_with_erasures_list_head; + } + else + { + groups_with_erasures_list_head = source.groups_with_erasures_list_head; + } + } + + + const skipfield_type distance_to_end = static_cast(reinterpret_cast(end_iterator.group_pointer->skipfield) - end_iterator.element_pointer); + + if (distance_to_end != 0) // 0 == edge case + { // Mark unused element memory locations from back group as skipped/erased: + + // Update skipfield: + const skipfield_type previous_node_value = *(end_iterator.skipfield_pointer - 1); + end_iterator.group_pointer->last_endpoint = reinterpret_cast(end_iterator.group_pointer->skipfield); + + if (previous_node_value == 0) // no previous skipblock + { + *end_iterator.skipfield_pointer = distance_to_end; + *(end_iterator.skipfield_pointer + distance_to_end - 1) = distance_to_end; + + const skipfield_type index = static_cast(end_iterator.element_pointer - end_iterator.group_pointer->elements); + + if (end_iterator.group_pointer->free_list_head != std::numeric_limits::max()) // ie. if this group already has some erased elements + { + *(reinterpret_cast(end_iterator.group_pointer->elements + end_iterator.group_pointer->free_list_head) + 1) = index; // set prev free list head's 'next index' number to the index of the current element + } + else + { + end_iterator.group_pointer->erasures_list_next_group = groups_with_erasures_list_head; // add it to the groups-with-erasures free list + groups_with_erasures_list_head = end_iterator.group_pointer; + } + + *(reinterpret_cast(end_iterator.element_pointer)) = end_iterator.group_pointer->free_list_head; + *(reinterpret_cast(end_iterator.element_pointer) + 1) = std::numeric_limits::max(); + end_iterator.group_pointer->free_list_head = index; + } + else + { // update previous skipblock, no need to update free list: + *(end_iterator.skipfield_pointer - previous_node_value) = *(end_iterator.skipfield_pointer + distance_to_end - 1) = previous_node_value + distance_to_end; + } + } + + + // Update subsequent group numbers: + group_pointer_type current_group = source.begin_iterator.group_pointer; + size_type current_group_number = end_iterator.group_pointer->group_number; + + do + { + current_group->group_number = ++current_group_number; + current_group = current_group->next_group; + } while (current_group != NULL); + + + // Join the destination and source group chains: + end_iterator.group_pointer->next_group = source.begin_iterator.group_pointer; + source.begin_iterator.group_pointer->previous_group = end_iterator.group_pointer; + end_iterator = source.end_iterator; + total_number_of_elements += source.total_number_of_elements; + source.blank(); + } + + + + void swap(colony &source) PLF_COLONY_NOEXCEPT_SWAP(allocator_type) + { + assert(&source != this); + + #ifdef PLF_COLONY_TYPE_TRAITS_SUPPORT + if PLF_COLONY_CONSTEXPR (std::is_trivial::value && std::is_trivial::value && std::is_trivial::value) // if all pointer types are trivial we can just copy using memcpy - avoids constructors/destructors etc and is faster + { + char temp[sizeof(colony)]; + std::memcpy(&temp, static_cast(this), sizeof(colony)); + std::memcpy(static_cast(this), static_cast(&source), sizeof(colony)); + std::memcpy(static_cast(&source), &temp, sizeof(colony)); + } + #ifdef PLF_COLONY_MOVE_SEMANTICS_SUPPORT // If pointer types are not trivial, moving them is probably going to be more efficient than copying them below + else if PLF_COLONY_CONSTEXPR (std::is_move_assignable::value && std::is_move_assignable::value && std::is_move_assignable::value && std::is_move_constructible::value && std::is_move_constructible::value && std::is_move_constructible::value) + { + colony temp(std::move(source)); + source = std::move(*this); + *this = std::move(temp); + } + #endif + else + #endif + { + const iterator swap_end_iterator = end_iterator, swap_begin_iterator = begin_iterator; + const group_pointer_type swap_groups_with_erasures_list_head = groups_with_erasures_list_head; + const size_type swap_total_number_of_elements = total_number_of_elements, swap_total_capacity = total_capacity; + const skipfield_type swap_min_elements_per_group = pointer_allocator_pair.min_elements_per_group, swap_max_elements_per_group = group_allocator_pair.max_elements_per_group; + + end_iterator = source.end_iterator; + begin_iterator = source.begin_iterator; + groups_with_erasures_list_head = source.groups_with_erasures_list_head; + total_number_of_elements = source.total_number_of_elements; + total_capacity = source.total_capacity; + pointer_allocator_pair.min_elements_per_group = source.pointer_allocator_pair.min_elements_per_group; + group_allocator_pair.max_elements_per_group = source.group_allocator_pair.max_elements_per_group; + + source.end_iterator = swap_end_iterator; + source.begin_iterator = swap_begin_iterator; + source.groups_with_erasures_list_head = swap_groups_with_erasures_list_head; + source.total_number_of_elements = swap_total_number_of_elements; + source.total_capacity = swap_total_capacity; + source.pointer_allocator_pair.min_elements_per_group = swap_min_elements_per_group; + source.group_allocator_pair.max_elements_per_group = swap_max_elements_per_group; + } + } + +}; // colony + + + + +template +inline void swap (colony &a, colony &b) PLF_COLONY_NOEXCEPT_SWAP(element_allocator_type) +{ + a.swap(b); +} + + + +} // plf namespace + + + + +#undef PLF_COLONY_FORCE_INLINE + +#undef PLF_COLONY_ALIGNMENT_SUPPORT +#undef PLF_COLONY_INITIALIZER_LIST_SUPPORT +#undef PLF_COLONY_TYPE_TRAITS_SUPPORT +#undef PLF_COLONY_ALLOCATOR_TRAITS_SUPPORT +#undef PLF_COLONY_VARIADICS_SUPPORT +#undef PLF_COLONY_MOVE_SEMANTICS_SUPPORT +#undef PLF_COLONY_NOEXCEPT +#undef PLF_COLONY_NOEXCEPT_SWAP +#undef PLF_COLONY_NOEXCEPT_MOVE_ASSIGNMENT +#undef PLF_COLONY_CONSTEXPR + +#undef PLF_COLONY_CONSTRUCT +#undef PLF_COLONY_DESTROY +#undef PLF_COLONY_ALLOCATE +#undef PLF_COLONY_ALLOCATE_INITIALIZATION +#undef PLF_COLONY_DEALLOCATE + + +#endif // PLF_COLONY_H diff --git a/src/third-party/plf/list.h b/src/third-party/plf/list.h new file mode 100644 index 00000000000..79c3d8d082c --- /dev/null +++ b/src/third-party/plf/list.h @@ -0,0 +1,3431 @@ +// Copyright (c) 2019, Matthew Bentley (mattreecebentley@gmail.com) www.plflib.org + +// zLib license (https://www.zlib.net/zlib_license.html): +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgement in the product documentation would be +// appreciated but is not required. +// 2. Altered source versions must be plainly marked as such, and must not be +// misrepresented as being the original software. +// 3. This notice may not be removed or altered from any source distribution. + + +#ifndef PLF_LIST_H +#define PLF_LIST_H + + +#define PLF_LIST_BLOCK_MIN static_cast((sizeof(node) * 8 > (sizeof(*this) + sizeof(group)) * 2) ? 8 : (((sizeof(*this) + sizeof(group)) * 2) / sizeof(node)) + 1) +#define PLF_LIST_BLOCK_MAX 2048 + + + +// Compiler-specific defines used by list: + +#if defined(_MSC_VER) + #define PLF_LIST_FORCE_INLINE __forceinline + + #if _MSC_VER < 1600 + #define PLF_LIST_NOEXCEPT throw() + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) throw() + #elif _MSC_VER == 1600 + #define PLF_LIST_MOVE_SEMANTICS_SUPPORT + #define PLF_LIST_NOEXCEPT throw() + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) throw() + #elif _MSC_VER == 1700 + #define PLF_LIST_TYPE_TRAITS_SUPPORT + #define PLF_LIST_ALLOCATOR_TRAITS_SUPPORT + #define PLF_LIST_MOVE_SEMANTICS_SUPPORT + #define PLF_LIST_NOEXCEPT throw() + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) throw() + #elif _MSC_VER == 1800 + #define PLF_LIST_TYPE_TRAITS_SUPPORT + #define PLF_LIST_ALLOCATOR_TRAITS_SUPPORT + #define PLF_LIST_VARIADICS_SUPPORT // Variadics, in this context, means both variadic templates and variadic macros are supported + #define PLF_LIST_MOVE_SEMANTICS_SUPPORT + #define PLF_LIST_NOEXCEPT throw() + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) throw() + #define PLF_LIST_INITIALIZER_LIST_SUPPORT + #elif _MSC_VER >= 1900 + #define PLF_LIST_ALIGNMENT_SUPPORT + #define PLF_LIST_TYPE_TRAITS_SUPPORT + #define PLF_LIST_ALLOCATOR_TRAITS_SUPPORT + #define PLF_LIST_VARIADICS_SUPPORT + #define PLF_LIST_MOVE_SEMANTICS_SUPPORT + #define PLF_LIST_NOEXCEPT noexcept + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits::propagate_on_container_swap::value) + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) + #define PLF_LIST_INITIALIZER_LIST_SUPPORT + #endif + + #if defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L) + #define PLF_LIST_CONSTEXPR constexpr + #else + #define PLF_LIST_CONSTEXPR + #endif + +#elif defined(__cplusplus) && __cplusplus >= 201103L // C++11 support, at least + #define PLF_LIST_FORCE_INLINE // note: GCC creates faster code without forcing inline + + #if defined(__GNUC__) && defined(__GNUC_MINOR__) && !defined(__clang__) // If compiler is GCC/G++ + #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4 // 4.2 and below do not support variadic templates + #define PLF_LIST_VARIADICS_SUPPORT + #endif + #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ > 4 // 4.3 and below do not support initializer lists + #define PLF_LIST_INITIALIZER_LIST_SUPPORT + #endif + #if (__GNUC__ == 4 && __GNUC_MINOR__ < 6) || __GNUC__ < 4 + #define PLF_LIST_NOEXCEPT throw() + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) + #elif __GNUC__ < 6 + #define PLF_LIST_NOEXCEPT noexcept + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) noexcept + #else // C++17 support + #define PLF_LIST_NOEXCEPT noexcept + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits::propagate_on_container_swap::value) + #endif + #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) || __GNUC__ > 4 + #define PLF_LIST_ALLOCATOR_TRAITS_SUPPORT + #endif + #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4 + #define PLF_LIST_ALIGNMENT_SUPPORT + #endif + #if __GNUC__ >= 5 // GCC v4.9 and below do not support std::is_trivially_copyable + #define PLF_LIST_TYPE_TRAITS_SUPPORT + #endif + #elif defined(__GLIBCXX__) // Using another compiler type with libstdc++ - we are assuming full c++11 compliance for compiler - which may not be true + #if __GLIBCXX__ >= 20080606 // libstdc++ 4.2 and below do not support variadic templates + #define PLF_LIST_VARIADICS_SUPPORT + #endif + #if __GLIBCXX__ >= 20090421 // libstdc++ 4.3 and below do not support initializer lists + #define PLF_LIST_INITIALIZER_LIST_SUPPORT + #endif + #if __GLIBCXX__ >= 20160111 + #define PLF_LIST_ALLOCATOR_TRAITS_SUPPORT + #define PLF_LIST_NOEXCEPT noexcept + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits::propagate_on_container_swap::value) + #elif __GLIBCXX__ >= 20120322 + #define PLF_LIST_ALLOCATOR_TRAITS_SUPPORT + #define PLF_LIST_NOEXCEPT noexcept + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) noexcept + #else + #define PLF_LIST_NOEXCEPT throw() + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) + #endif + #if __GLIBCXX__ >= 20130322 + #define PLF_LIST_ALIGNMENT_SUPPORT + #endif + #if __GLIBCXX__ >= 20150422 // libstdc++ v4.9 and below do not support std::is_trivially_copyable + #define PLF_LIST_TYPE_TRAITS_SUPPORT + #endif + #elif defined(_LIBCPP_VERSION) + #define PLF_LIST_ALLOCATOR_TRAITS_SUPPORT + #define PLF_LIST_VARIADICS_SUPPORT + #define PLF_LIST_INITIALIZER_LIST_SUPPORT + #define PLF_LIST_ALIGNMENT_SUPPORT + #define PLF_LIST_NOEXCEPT noexcept + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) noexcept + + #if !(defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)) + #define PLF_LIST_TYPE_TRAITS_SUPPORT + #endif + #else // Assume type traits and initializer support for other compilers and standard libraries + #define PLF_LIST_ALLOCATOR_TRAITS_SUPPORT + #define PLF_LIST_ALIGNMENT_SUPPORT + #define PLF_LIST_VARIADICS_SUPPORT + #define PLF_LIST_INITIALIZER_LIST_SUPPORT + #define PLF_LIST_TYPE_TRAITS_SUPPORT + #define PLF_LIST_NOEXCEPT noexcept + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits::is_always_equal::value) + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) noexcept + #endif + + #if __cplusplus >= 201703L + #if defined(__clang__) && ((__clang_major__ == 3 && __clang_minor__ == 9) || __clang_major__ > 3) + #define PLF_LIST_CONSTEXPR constexpr + #elif defined(__GNUC__) && __GNUC__ >= 7 + #define PLF_LIST_CONSTEXPR constexpr + #elif !defined(__clang__) && !defined(__GNUC__) + #define PLF_LIST_CONSTEXPR constexpr // assume correct C++17 implementation for other compilers + #else + #define PLF_LIST_CONSTEXPR + #endif + #else + #define PLF_LIST_CONSTEXPR + #endif + + #define PLF_LIST_MOVE_SEMANTICS_SUPPORT +#else + #define PLF_LIST_FORCE_INLINE + #define PLF_LIST_NOEXCEPT throw() + #define PLF_LIST_NOEXCEPT_SWAP(the_allocator) + #define PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) + #define PLF_LIST_CONSTEXPR +#endif + + + +#ifdef PLF_LIST_ALLOCATOR_TRAITS_SUPPORT + #ifdef PLF_LIST_VARIADICS_SUPPORT + #define PLF_LIST_CONSTRUCT(the_allocator, allocator_instance, location, ...) std::allocator_traits::construct(allocator_instance, location, __VA_ARGS__) + #else + #define PLF_LIST_CONSTRUCT(the_allocator, allocator_instance, location, data) std::allocator_traits::construct(allocator_instance, location, data) + #endif + + #define PLF_LIST_DESTROY(the_allocator, allocator_instance, location) std::allocator_traits::destroy(allocator_instance, location) + #define PLF_LIST_ALLOCATE(the_allocator, allocator_instance, size, hint) std::allocator_traits::allocate(allocator_instance, size, hint) + #define PLF_LIST_ALLOCATE_INITIALIZATION(the_allocator, size, hint) std::allocator_traits::allocate(*this, size, hint) + #define PLF_LIST_DEALLOCATE(the_allocator, allocator_instance, location, size) std::allocator_traits::deallocate(allocator_instance, location, size) +#else + #ifdef PLF_LIST_VARIADICS_SUPPORT + #define PLF_LIST_CONSTRUCT(the_allocator, allocator_instance, location, ...) allocator_instance.construct(location, __VA_ARGS__) + #else + #define PLF_LIST_CONSTRUCT(the_allocator, allocator_instance, location, data) allocator_instance.construct(location, data) + #endif + + #define PLF_LIST_DESTROY(the_allocator, allocator_instance, location) allocator_instance.destroy(location) + #define PLF_LIST_ALLOCATE(the_allocator, allocator_instance, size, hint) allocator_instance.allocate(size, hint) + #define PLF_LIST_ALLOCATE_INITIALIZATION(the_allocator, size, hint) the_allocator::allocate(size, hint) + #define PLF_LIST_DEALLOCATE(the_allocator, allocator_instance, location, size) allocator_instance.deallocate(location, size) +#endif + + + + +#include // memmove, memcpy +#include // assert +#include // std::numeric_limits +#include // std::uninitialized_copy, std::allocator +#include // std::bidirectional_iterator_tag + + +#ifndef GFX_TIMSORT_HPP + #include // std::sort +#endif + +#ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + #include // std::is_trivially_destructible, etc +#endif + +#ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + #include // std::move +#endif + +#ifdef PLF_LIST_INITIALIZER_LIST_SUPPORT + #include +#endif + + + + +namespace plf +{ + + + +template > class list : private element_allocator_type +{ +public: + // Standard container typedefs: + typedef element_type value_type; + typedef element_allocator_type allocator_type; + typedef unsigned short group_size_type; + + #ifdef PLF_LIST_ALLOCATOR_TRAITS_SUPPORT // C++11 + typedef typename std::allocator_traits::size_type size_type; + typedef typename std::allocator_traits::difference_type difference_type; + typedef element_type & reference; + typedef const element_type & const_reference; + typedef typename std::allocator_traits::pointer pointer; + typedef typename std::allocator_traits::const_pointer const_pointer; + #else + typedef typename element_allocator_type::size_type size_type; + typedef typename element_allocator_type::difference_type difference_type; + typedef typename element_allocator_type::reference reference; + typedef typename element_allocator_type::const_reference const_reference; + typedef typename element_allocator_type::pointer pointer; + typedef typename element_allocator_type::const_pointer const_pointer; + #endif + + + // Iterator declarations: + template class list_iterator; + typedef list_iterator iterator; + typedef list_iterator const_iterator; + friend class list_iterator; // Using 'iterator' typedef name here is illegal under C++03 + friend class list_iterator; + + template class list_reverse_iterator; + typedef list_reverse_iterator reverse_iterator; + typedef list_reverse_iterator const_reverse_iterator; + friend class list_reverse_iterator; + friend class list_reverse_iterator; + + +private: + struct group; // forward declarations for typedefs below + struct node; + + #ifdef PLF_LIST_ALLOCATOR_TRAITS_SUPPORT // C++11 + typedef typename std::allocator_traits::template rebind_alloc group_allocator_type; + typedef typename std::allocator_traits::template rebind_alloc node_allocator_type; + typedef typename std::allocator_traits::pointer group_pointer_type; + typedef typename std::allocator_traits::pointer node_pointer_type; + typedef typename std::allocator_traits::template rebind_alloc node_pointer_allocator_type; + #else + typedef typename element_allocator_type::template rebind::other group_allocator_type; + typedef typename element_allocator_type::template rebind::other node_allocator_type; + typedef typename group_allocator_type::pointer group_pointer_type; + typedef typename node_allocator_type::pointer node_pointer_type; + typedef typename element_allocator_type::template rebind::other node_pointer_allocator_type; + #endif + + + + struct node_base + { + node_pointer_type next, previous; + + node_base() + {} + + node_base(const node_pointer_type &n, const node_pointer_type &p): + next(n), + previous(p) + {} + + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + node_base(node_pointer_type &&n, node_pointer_type &&p) PLF_LIST_NOEXCEPT: + next(std::move(n)), + previous(std::move(p)) + {} + #endif + }; + + + + struct node : public node_base + { + element_type element; + + node(const node_pointer_type next, const node_pointer_type previous, const element_type &source): + node_base(next, previous), + element(source) + {} + + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + node(node_pointer_type &&next, node_pointer_type &&previous, element_type &&source) PLF_LIST_NOEXCEPT: + node_base(std::move(next), std::move(previous)), + element(std::move(source)) + {} + #endif + + + #ifdef PLF_LIST_VARIADICS_SUPPORT + template + node(node_pointer_type const next, node_pointer_type const previous, arguments&&... parameters): + node_base(next, previous), + element(std::forward(parameters) ...) + {} + #endif + }; + + + + struct group : public node_allocator_type + { + node_pointer_type nodes; + node_pointer_type free_list_head; + node_pointer_type beyond_end; + group_size_type number_of_elements; + + + group() PLF_LIST_NOEXCEPT: + nodes(NULL), + free_list_head(NULL), + beyond_end(NULL), + number_of_elements(0) + {} + + + #if defined(PLF_LIST_VARIADICS_SUPPORT) || defined(PLF_LIST_MOVE_SEMANTICS_SUPPORT) + group(const group_size_type group_size, node_pointer_type const previous = NULL): + nodes(PLF_LIST_ALLOCATE_INITIALIZATION(node_allocator_type, group_size, previous)), + free_list_head(NULL), + beyond_end(nodes + group_size), + number_of_elements(0) + {} + #else + // This is a hack around the fact that allocator_type::construct only supports copy construction in C++03 and copy elision does not occur on the vast majority of compilers in this circumstance. And to avoid running out of memory (and performance loss) from allocating the same block twice, we're allocating in this constructor and moving data in the copy constructor. + group(const group_size_type group_size, node_pointer_type const previous = NULL) PLF_LIST_NOEXCEPT: + nodes(NULL), + free_list_head(previous), + beyond_end(NULL), + number_of_elements(group_size) + {} + + // Not a real copy constructor ie. actually a move constructor. Only used for allocator.construct in C++03 for reasons stated above: + group(const group &source): + node_allocator_type(source), + nodes(PLF_LIST_ALLOCATE_INITIALIZATION(node_allocator_type, source.number_of_elements, source.free_list_head)), + free_list_head(NULL), + beyond_end(nodes + source.number_of_elements), + number_of_elements(0) + {} + #endif + + + group & operator = (const group &source) PLF_LIST_NOEXCEPT // Actually a move operator, used by c++03 in group_vector's remove, expand_capacity and append + { + nodes = source.nodes; + free_list_head = source.free_list_head; + beyond_end = source.beyond_end; + number_of_elements = source.number_of_elements; + return *this; + } + + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + group(group &&source) PLF_LIST_NOEXCEPT: + node_allocator_type(source), + nodes(std::move(source.nodes)), + free_list_head(std::move(source.free_list_head)), + beyond_end(std::move(source.beyond_end)), + number_of_elements(source.number_of_elements) + { + source.nodes = NULL; + source.beyond_end = NULL; + } + + + group & operator = (group &&source) PLF_LIST_NOEXCEPT + { + nodes = std::move(source.nodes); + free_list_head = std::move(source.free_list_head); + beyond_end = std::move(source.beyond_end); + number_of_elements = std::move(source.number_of_elements); + source.nodes = NULL; + source.beyond_end = NULL; + return *this; + } + #endif + + + ~group() PLF_LIST_NOEXCEPT + { + PLF_LIST_DEALLOCATE(node_allocator_type, (*this), nodes, static_cast(beyond_end - nodes)); + } + }; + + + + + class group_vector : private node_pointer_allocator_type + { + public: + group_pointer_type last_endpoint_group, block_pointer, last_searched_group; // last_endpoint_group is the last -active- group in the block. Other -inactive- (previously used, now empty of elements) groups may be stored after this group for future usage (to reduce deallocation/reallocation of nodes). block_pointer + size - 1 == the last group in the block, regardless of whether or not the group is active. + size_type size; + + + struct ebco_pair2 : allocator_type // empty-base-class optimisation + { + size_type capacity; // Total element capacity of all initialized groups + explicit ebco_pair2(const size_type number_of_elements) PLF_LIST_NOEXCEPT: capacity(number_of_elements) {}; + } element_allocator_pair; + + struct ebco_pair : group_allocator_type + { + size_type capacity; // Total group capacity + explicit ebco_pair(const size_type number_of_groups) PLF_LIST_NOEXCEPT: capacity(number_of_groups) {}; + } group_allocator_pair; + + + + group_vector() PLF_LIST_NOEXCEPT: + node_pointer_allocator_type(node_pointer_allocator_type()), + last_endpoint_group(NULL), + block_pointer(NULL), + last_searched_group(NULL), + size(0), + element_allocator_pair(0), + group_allocator_pair(0) + {} + + + + inline PLF_LIST_FORCE_INLINE void blank() PLF_LIST_NOEXCEPT + { + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_trivial::value) + { + std::memset(static_cast(this), 0, sizeof(group_vector)); + } + else + #endif + { + last_endpoint_group = NULL; + block_pointer = NULL; + last_searched_group = NULL; + size = 0; + element_allocator_pair.capacity = 0; + group_allocator_pair.capacity = 0; + } + } + + + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + group_vector(group_vector &&source) PLF_LIST_NOEXCEPT: + last_endpoint_group(std::move(source.last_endpoint_group)), + block_pointer(std::move(source.block_pointer)), + last_searched_group(std::move(source.last_searched_group)), + size(source.size), + element_allocator_pair(source.element_allocator_pair.capacity), + group_allocator_pair(source.group_allocator_pair.capacity) + { + source.blank(); + } + + + group_vector & operator = (group_vector &&source) PLF_LIST_NOEXCEPT + { + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_trivial::value) + { + std::memcpy(static_cast(this), &source, sizeof(group_vector)); + } + else + #endif + { + last_endpoint_group = std::move(source.last_endpoint_group); + block_pointer = std::move(source.block_pointer); + last_searched_group = std::move(source.last_searched_group); + size = source.size; + element_allocator_pair.capacity = source.element_allocator_pair.capacity; + group_allocator_pair.capacity = source.group_allocator_pair.capacity; + } + + source.blank(); + return *this; + } + #endif + + + + ~group_vector() PLF_LIST_NOEXCEPT + {} + + + + void destroy_all_data(const node_pointer_type last_endpoint_node) PLF_LIST_NOEXCEPT + { + if (block_pointer == NULL) + { + return; + } + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value || !std::is_trivially_destructible::value) + #endif + { + clear(last_endpoint_node); // If clear has already been called, last_endpoint_node will already be == block_pointer->nodes, so no work will occur + } + + const group_pointer_type end_group = block_pointer + size; + for (group_pointer_type current_group = block_pointer; current_group != end_group; ++current_group) + { + PLF_LIST_DESTROY(group_allocator_type, group_allocator_pair, current_group); + } + + PLF_LIST_DEALLOCATE(group_allocator_type, group_allocator_pair, block_pointer, group_allocator_pair.capacity); + blank(); + } + + + + void clear(const node_pointer_type last_endpoint_node) PLF_LIST_NOEXCEPT + { + for (group_pointer_type current_group = block_pointer; current_group != last_endpoint_group; ++current_group) + { + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value || !std::is_trivially_destructible::value) + #endif + { + const node_pointer_type end = current_group->beyond_end; + + if ((end - current_group->nodes) != current_group->number_of_elements) // If there are erased nodes present in the group + { + for (node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node) + { + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + if (current_node->next != NULL) // ie. is not part of free list + { + PLF_LIST_DESTROY(element_allocator_type, element_allocator_pair, &(current_node->element)); + } + } + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + PLF_LIST_DESTROY(node_pointer_allocator_type, (*this), &(current_node->next)); + PLF_LIST_DESTROY(node_pointer_allocator_type, (*this), &(current_node->previous)); + } + } + } + else + { + for (node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node) + { + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + PLF_LIST_DESTROY(element_allocator_type, element_allocator_pair, &(current_node->element)); + } + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + PLF_LIST_DESTROY(node_pointer_allocator_type, (*this), &(current_node->next)); + PLF_LIST_DESTROY(node_pointer_allocator_type, (*this), &(current_node->previous)); + } + } + } + } + + current_group->free_list_head = NULL; + current_group->number_of_elements = 0; + } + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value || !std::is_trivially_destructible::value) + #endif + { + if ((last_endpoint_node - last_endpoint_group->nodes) != last_endpoint_group->number_of_elements) // If there are erased nodes present in the group + { + for (node_pointer_type current_node = last_endpoint_group->nodes; current_node != last_endpoint_node; ++current_node) + { + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + if (current_node->next != NULL) // is not part of free list ie. element has not already had it's destructor called + { + PLF_LIST_DESTROY(element_allocator_type, element_allocator_pair, &(current_node->element)); + } + } + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + PLF_LIST_DESTROY(node_pointer_allocator_type, (*this), &(current_node->next)); + PLF_LIST_DESTROY(node_pointer_allocator_type, (*this), &(current_node->previous)); + } + } + } + else + { + for (node_pointer_type current_node = last_endpoint_group->nodes; current_node != last_endpoint_node; ++current_node) + { + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + PLF_LIST_DESTROY(element_allocator_type, element_allocator_pair, &(current_node->element)); + } + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + PLF_LIST_DESTROY(node_pointer_allocator_type, (*this), &(current_node->next)); + PLF_LIST_DESTROY(node_pointer_allocator_type, (*this), &(current_node->previous)); + } + } + } + } + + last_endpoint_group->free_list_head = NULL; + last_endpoint_group->number_of_elements = 0; + last_searched_group = last_endpoint_group = block_pointer; + } + + + + void expand_capacity(const size_type new_capacity) // used by add_new and append + { + group_pointer_type const old_block = block_pointer; + block_pointer = PLF_LIST_ALLOCATE(group_allocator_type, group_allocator_pair, new_capacity, 0); + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_trivially_copyable::value && std::is_trivially_destructible::value) + { // Dereferencing here in order to deal with smart pointer situations ie. obtaining the raw pointer from the smart pointer + std::memcpy(static_cast(&*block_pointer), static_cast(&*old_block), sizeof(group) * size); // reinterpret_cast necessary to deal with GCC 8 warnings + } + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + else if PLF_LIST_CONSTEXPR (std::is_move_constructible::value) + { + std::uninitialized_copy(std::make_move_iterator(old_block), std::make_move_iterator(old_block + size), block_pointer); + } + #endif + else + #endif + { + // If allocator supplies non-trivial pointers it becomes necessary to destroy the group. uninitialized_copy will not work in this context as the copy constructor for "group" is overriden in C++03/98. The = operator for "group" has been overriden to make the following work: + const group_pointer_type beyond_end = old_block + size; + group_pointer_type current_new_group = block_pointer; + + for (group_pointer_type current_group = old_block; current_group != beyond_end; ++current_group) + { + *(current_new_group++) = *(current_group); + + current_group->nodes = NULL; + current_group->beyond_end = NULL; + PLF_LIST_DESTROY(group_allocator_type, group_allocator_pair, current_group); + } + } + + last_searched_group = block_pointer + (last_searched_group - old_block); // correct pointer post-reallocation + PLF_LIST_DEALLOCATE(group_allocator_type, group_allocator_pair, old_block, group_allocator_pair.capacity); + group_allocator_pair.capacity = new_capacity; + } + + + + void add_new(const group_size_type group_size) + { + if (group_allocator_pair.capacity == size) + { + expand_capacity(group_allocator_pair.capacity * 2); + } + + last_endpoint_group = block_pointer + size - 1; + + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(group_allocator_type, group_allocator_pair, last_endpoint_group + 1, group_size, last_endpoint_group->nodes); + #else + PLF_LIST_CONSTRUCT(group_allocator_type, group_allocator_pair, last_endpoint_group + 1, group(group_size, last_endpoint_group->nodes)); + #endif + + ++last_endpoint_group; // Doing this here instead of pre-construct to avoid need for a try-catch block + element_allocator_pair.capacity += group_size; + ++size; + } + + + + void initialize(const group_size_type group_size) // For adding first group *only* when group vector is completely empty and block_pointer is NULL + { + last_endpoint_group = block_pointer = last_searched_group = PLF_LIST_ALLOCATE(group_allocator_type, group_allocator_pair, 1, 0); + group_allocator_pair.capacity = 1; + + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(group_allocator_type, group_allocator_pair, last_endpoint_group, group_size); + #else + PLF_LIST_CONSTRUCT(group_allocator_type, group_allocator_pair, last_endpoint_group, group(group_size)); + #endif + + size = 1; // Doing these here instead of pre-construct to avoid need for a try-catch block + element_allocator_pair.capacity = group_size; + } + + + + void remove(group_pointer_type const group_to_erase) PLF_LIST_NOEXCEPT + { + if (last_searched_group >= group_to_erase && last_searched_group != block_pointer) + { + --last_searched_group; + } + + element_allocator_pair.capacity -= static_cast(group_to_erase->beyond_end - group_to_erase->nodes); + + PLF_LIST_DESTROY(group_allocator_type, group_allocator_pair, group_to_erase); + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_trivially_copyable::value && std::is_trivially_destructible::value) + { // Dereferencing here in order to deal with smart pointer situations ie. obtaining the raw pointer from the smart pointer + std::memmove(static_cast(&*group_to_erase), static_cast(&*group_to_erase + 1), sizeof(group) * (--size - static_cast(&*group_to_erase - &*block_pointer))); + } + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + else if PLF_LIST_CONSTEXPR (std::is_move_constructible::value) + { + std::move(group_to_erase + 1, block_pointer + size--, group_to_erase); + } + #endif + else + #endif + { + group_pointer_type back = block_pointer + size--; + std::copy(group_to_erase + 1, back--, group_to_erase); + + back->nodes = NULL; + back->beyond_end = NULL; + PLF_LIST_DESTROY(group_allocator_type, group_allocator_pair, back); + } + } + + + + void move_to_back(group_pointer_type const group_to_erase) + { + if (last_searched_group >= group_to_erase && last_searched_group != block_pointer) + { + --last_searched_group; + } + + group *temp_group = PLF_LIST_ALLOCATE(group_allocator_type, group_allocator_pair, 1, NULL); + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_trivially_copyable::value && std::is_trivially_destructible::value) + { + std::memcpy(static_cast(&*temp_group), static_cast(&*group_to_erase), sizeof(group)); + std::memmove(static_cast(&*group_to_erase), static_cast(&*group_to_erase + 1), sizeof(group) * ((size - 1) - static_cast(&*group_to_erase - &*block_pointer))); + std::memcpy(static_cast(&*(block_pointer + size - 1)), static_cast(&*temp_group), sizeof(group)); + } + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + else if PLF_LIST_CONSTEXPR (std::is_move_constructible::value) + { + PLF_LIST_CONSTRUCT(group_allocator_type, group_allocator_pair, temp_group, std::move(*group_to_erase)); + std::move(group_to_erase + 1, block_pointer + size, group_to_erase); + *(block_pointer + size - 1) = std::move(*temp_group); + + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value) + { + PLF_LIST_DESTROY(group_allocator_type, group_allocator_pair, temp_group); + } + } + #endif + else + #endif + { + PLF_LIST_CONSTRUCT(group_allocator_type, group_allocator_pair, temp_group, group()); + + *temp_group = *group_to_erase; + std::copy(group_to_erase + 1, block_pointer + size, group_to_erase); + *(block_pointer + --size) = *temp_group; + + temp_group->nodes = NULL; + PLF_LIST_DESTROY(group_allocator_type, group_allocator_pair, temp_group); + } + + PLF_LIST_DEALLOCATE(group_allocator_type, group_allocator_pair, temp_group, 1); + } + + + + group_pointer_type get_nearest_freelist_group(const node_pointer_type location_node) PLF_LIST_NOEXCEPT // In working implementation this cannot throw + { + const group_pointer_type beyond_end_group = last_endpoint_group + 1; + group_pointer_type left = last_searched_group - 1, right = last_searched_group + 1, freelist_group = NULL; + bool right_not_beyond_back = (right < beyond_end_group); + bool left_not_beyond_front = (left >= block_pointer); + + + if (location_node >= last_searched_group->nodes && location_node < last_searched_group->beyond_end) // ie. location is within last_search_group + { + if (last_searched_group->free_list_head != NULL) // if last_searched_group has previously-erased nodes + { + return last_searched_group; + } + } + else // search for the node group which location_node is located within, using last_searched_group as a starting point and searching left and right. Try and find the closest node group with reusable erased-element locations along the way: + { + group_pointer_type closest_freelist_left = (last_searched_group->free_list_head == NULL) ? NULL : last_searched_group, closest_freelist_right = (last_searched_group->free_list_head == NULL) ? NULL : last_searched_group; + + while (true) + { + if (right_not_beyond_back) + { + if ((location_node < right->beyond_end) && (location_node >= right->nodes)) // location_node's group is found + { + if (right->free_list_head != NULL) // group has erased nodes, reuse them: + { + last_searched_group = right; + return right; + } + + difference_type left_distance; + + if (closest_freelist_right != NULL) + { + last_searched_group = right; + left_distance = right - closest_freelist_right; + + if (left_distance <= 2) // ie. this group is close enough to location_node's group + { + return closest_freelist_right; + } + + freelist_group = closest_freelist_right; + } + else + { + last_searched_group = right; + left_distance = right - left; + } + + + // Otherwise find closest group with freelist - check an equal distance on the right to the distance we've checked on the left: + const group_pointer_type end_group = (((right + left_distance) > beyond_end_group) ? beyond_end_group : (right + left_distance - 1)); + + while (++right != end_group) + { + if (right->free_list_head != NULL) + { + return right; + } + } + + if (freelist_group != NULL) + { + return freelist_group; + } + + right_not_beyond_back = (right < beyond_end_group); + break; // group with reusable erased nodes not found yet, continue searching in loop below + } + + if (right->free_list_head != NULL) // location_node's group not found, but a reusable location found + { + if ((closest_freelist_right == NULL) & (closest_freelist_left == NULL)) + { + closest_freelist_left = right; + } + + closest_freelist_right = right; + } + + right_not_beyond_back = (++right < beyond_end_group); + } + + + if (left_not_beyond_front) + { + if ((location_node >= left->nodes) && (location_node < left->beyond_end)) + { + if (left->free_list_head != NULL) + { + last_searched_group = left; + return left; + } + + difference_type right_distance; + + if (closest_freelist_left != NULL) + { + last_searched_group = left; + right_distance = closest_freelist_left - left; + + if (right_distance <= 2) + { + return closest_freelist_left; + } + + freelist_group = closest_freelist_left; + } + else + { + last_searched_group = left; + right_distance = right - left; + } + + // Otherwise find closest group with freelist: + const group_pointer_type end_group = (((left - right_distance) < block_pointer) ? block_pointer - 1 : (left - right_distance) + 1); + + while (--left != end_group) + { + if (left->free_list_head != NULL) + { + return left; + } + } + + if (freelist_group != NULL) + { + return freelist_group; + } + + left_not_beyond_front = (left >= block_pointer); + break; + } + + if (left->free_list_head != NULL) + { + if ((closest_freelist_left == NULL) & (closest_freelist_right == NULL)) + { + closest_freelist_right = left; + } + + closest_freelist_left = left; + } + + left_not_beyond_front = (--left >= block_pointer); + } + } + } + + + // The node group which location_node is located within, is known at this point. Continue searching outwards from this group until a group is found with a reusable location: + while (true) + { + if (right_not_beyond_back) + { + if (right->free_list_head != NULL) + { + return right; + } + + right_not_beyond_back = (++right < beyond_end_group); + } + + if (left_not_beyond_front) + { + if (left->free_list_head != NULL) + { + return left; + } + + left_not_beyond_front = (--left >= block_pointer); + } + } + + // Will never reach here on functioning implementations + } + + + + void swap(group_vector &source) PLF_LIST_NOEXCEPT_SWAP(group_allocator_type) + { + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_trivial::value) // if all pointer types are trivial we can just copy using memcpy - faster - avoids constructors/destructors etc + { + char temp[sizeof(group_vector)]; + std::memcpy(static_cast(&temp), static_cast(this), sizeof(group_vector)); + std::memcpy(static_cast(this), static_cast(&source), sizeof(group_vector)); + std::memcpy(static_cast(&source), static_cast(&temp), sizeof(group_vector)); + } + else + #endif + { + const group_pointer_type swap_last_endpoint_group = last_endpoint_group, swap_block_pointer = block_pointer, swap_last_searched_group = last_searched_group; + const size_type swap_size = size, swap_element_capacity = element_allocator_pair.capacity, swap_capacity = group_allocator_pair.capacity; + + last_endpoint_group = source.last_endpoint_group; + block_pointer = source.block_pointer; + last_searched_group = source.last_searched_group; + size = source.size; + element_allocator_pair.capacity = source.element_allocator_pair.capacity; + group_allocator_pair.capacity = source.group_allocator_pair.capacity; + + source.last_endpoint_group = swap_last_endpoint_group; + source.block_pointer = swap_block_pointer; + source.last_searched_group = swap_last_searched_group; + source.size = swap_size; + source.element_allocator_pair.capacity = swap_element_capacity; + source.group_allocator_pair.capacity = swap_capacity; + } + } + + + + void trim_trailing_groups() PLF_LIST_NOEXCEPT + { + const group_pointer_type beyond_last = block_pointer + size; + + for (group_pointer_type current_group = last_endpoint_group + 1; current_group != beyond_last; ++current_group) + { + element_allocator_pair.capacity -= static_cast(current_group->beyond_end - current_group->nodes); + PLF_LIST_DESTROY(group_allocator_type, group_allocator_pair, current_group); + } + + size -= static_cast(beyond_last - (last_endpoint_group + 1)); + } + + + + void append(group_vector &source) + { + source.trim_trailing_groups(); + trim_trailing_groups(); + + if (size + source.size > group_allocator_pair.capacity) + { + expand_capacity(size + source.size); + } + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_trivially_copyable::value && std::is_trivially_destructible::value) + { // &* in order to deal with smart pointer situations ie. obtaining the raw pointer from the smart pointer + std::memcpy(static_cast(&*block_pointer + size), static_cast(&*source.block_pointer), sizeof(group) * source.size); + } + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + else if PLF_LIST_CONSTEXPR (std::is_move_constructible::value) + { + std::uninitialized_copy(std::make_move_iterator(source.block_pointer), std::make_move_iterator(source.block_pointer + source.size), block_pointer + size); + } + #endif + else + #endif + { + group_pointer_type current_new_group = block_pointer + size; + const group_pointer_type beyond_end_source = source.block_pointer + source.size; + + for (group_pointer_type current_group = source.block_pointer; current_group != beyond_end_source; ++current_group) + { + *(current_new_group++) = *(current_group); + + current_group->nodes = NULL; + current_group->beyond_end = NULL; + PLF_LIST_DESTROY(group_allocator_type, source.group_allocator_pair, current_group); + } + } + + PLF_LIST_DEALLOCATE(group_allocator_type, source.group_allocator_pair, source.block_pointer, source.group_allocator_pair.capacity); + size += source.size; + last_endpoint_group = block_pointer + size - 1; + element_allocator_pair.capacity += source.element_allocator_pair.capacity; + source.blank(); + } + }; + + + + // Implement const/non-const iterator switching pattern: + template struct choose; + + template struct choose + { + typedef IsTrue type; + }; + + template struct choose + { + typedef IsFalse type; + }; + + +public: + + template class list_iterator + { + private: + node_pointer_type node_pointer; + + public: + typedef std::bidirectional_iterator_tag iterator_category; + typedef typename list::value_type value_type; + typedef typename list::difference_type difference_type; + typedef typename choose::type pointer; + typedef typename choose::type reference; + + friend class list; + + + + inline PLF_LIST_FORCE_INLINE bool operator == (const list_iterator rh) const PLF_LIST_NOEXCEPT + { + return (node_pointer == rh.node_pointer); + } + + + + inline PLF_LIST_FORCE_INLINE bool operator == (const list_iterator rh) const PLF_LIST_NOEXCEPT + { + return (node_pointer == rh.node_pointer); + } + + + + inline PLF_LIST_FORCE_INLINE bool operator != (const list_iterator rh) const PLF_LIST_NOEXCEPT + { + return (node_pointer != rh.node_pointer); + } + + + + inline PLF_LIST_FORCE_INLINE bool operator != (const list_iterator rh) const PLF_LIST_NOEXCEPT + { + return (node_pointer != rh.node_pointer); + } + + + + inline PLF_LIST_FORCE_INLINE reference operator * () const + { + return node_pointer->element; + } + + + + inline PLF_LIST_FORCE_INLINE pointer operator -> () const + { + return &(node_pointer->element); + } + + + + inline PLF_LIST_FORCE_INLINE list_iterator & operator ++ () PLF_LIST_NOEXCEPT + { + assert(node_pointer != NULL); // covers uninitialised list_iterator + node_pointer = node_pointer->next; + return *this; + } + + + + inline list_iterator operator ++(int) PLF_LIST_NOEXCEPT + { + const list_iterator copy(*this); + ++*this; + return copy; + } + + + + inline PLF_LIST_FORCE_INLINE list_iterator & operator -- () PLF_LIST_NOEXCEPT + { + assert(node_pointer != NULL); // covers uninitialised list_iterator + node_pointer = node_pointer->previous; + return *this; + } + + + + inline list_iterator operator -- (int) PLF_LIST_NOEXCEPT + { + const list_iterator copy(*this); + --*this; + return copy; + } + + + + inline list_iterator & operator = (const list_iterator &rh) PLF_LIST_NOEXCEPT + { + node_pointer = rh.node_pointer; + return *this; + } + + + + inline list_iterator & operator = (const list_iterator &rh) PLF_LIST_NOEXCEPT + { + node_pointer = rh.node_pointer; + return *this; + } + + + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + inline list_iterator & operator = (const list_iterator &&rh) PLF_LIST_NOEXCEPT + { + node_pointer = std::move(rh.node_pointer); + return *this; + } + + + inline list_iterator & operator = (const list_iterator &&rh) PLF_LIST_NOEXCEPT + { + node_pointer = std::move(rh.node_pointer); + return *this; + } + #endif + + + + list_iterator() PLF_LIST_NOEXCEPT: node_pointer(NULL) {} + + list_iterator(const list_iterator &source) PLF_LIST_NOEXCEPT: node_pointer(source.node_pointer) {} + + list_iterator(const list_iterator &source) PLF_LIST_NOEXCEPT: node_pointer(source.node_pointer) {} + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + list_iterator (const list_iterator &&source) PLF_LIST_NOEXCEPT: node_pointer(std::move(source.node_pointer)) {} + + list_iterator(const list_iterator &&source) PLF_LIST_NOEXCEPT: node_pointer(std::move(source.node_pointer)) {} + #endif + + private: + + list_iterator (const node_pointer_type node_p) PLF_LIST_NOEXCEPT: node_pointer(node_p) {} + }; + + + + + template class list_reverse_iterator + { + private: + node_pointer_type node_pointer; + + public: + typedef std::bidirectional_iterator_tag iterator_category; + typedef typename list::value_type value_type; + typedef typename list::difference_type difference_type; + typedef typename choose::type pointer; + typedef typename choose::type reference; + + friend class list; + + + inline PLF_LIST_FORCE_INLINE bool operator == (const list_reverse_iterator rh) const PLF_LIST_NOEXCEPT + { + return (node_pointer == rh.node_pointer); + } + + + + inline PLF_LIST_FORCE_INLINE bool operator == (const list_reverse_iterator rh) const PLF_LIST_NOEXCEPT + { + return (node_pointer == rh.node_pointer); + } + + + + inline PLF_LIST_FORCE_INLINE bool operator != (const list_reverse_iterator rh) const PLF_LIST_NOEXCEPT + { + return (node_pointer != rh.node_pointer); + } + + + + inline PLF_LIST_FORCE_INLINE bool operator != (const list_reverse_iterator rh) const PLF_LIST_NOEXCEPT + { + return (node_pointer != rh.node_pointer); + } + + + + inline PLF_LIST_FORCE_INLINE reference operator * () const + { + return node_pointer->element; + } + + + + inline PLF_LIST_FORCE_INLINE pointer operator -> () const + { + return &(node_pointer->element); + } + + + + inline PLF_LIST_FORCE_INLINE list_reverse_iterator & operator ++ () PLF_LIST_NOEXCEPT + { + assert(node_pointer != NULL); // covers uninitialised list_reverse_iterator + node_pointer = node_pointer->previous; + return *this; + } + + + + inline list_reverse_iterator operator ++(int) PLF_LIST_NOEXCEPT + { + const list_reverse_iterator copy(*this); + ++*this; + return copy; + } + + + + inline PLF_LIST_FORCE_INLINE list_reverse_iterator & operator -- () PLF_LIST_NOEXCEPT + { + assert(node_pointer != NULL); + node_pointer = node_pointer->next; + return *this; + } + + + + inline list_reverse_iterator operator -- (int) PLF_LIST_NOEXCEPT + { + const list_reverse_iterator copy(*this); + --*this; + return copy; + } + + + + inline list_reverse_iterator & operator = (const list_reverse_iterator &rh) PLF_LIST_NOEXCEPT + { + node_pointer = rh.node_pointer; + return *this; + } + + + + inline list_reverse_iterator & operator = (const list_reverse_iterator &rh) PLF_LIST_NOEXCEPT + { + node_pointer = rh.node_pointer; + return *this; + } + + + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + inline list_reverse_iterator & operator = (const list_reverse_iterator &&rh) PLF_LIST_NOEXCEPT + { + node_pointer = std::move(rh.node_pointer); + return *this; + } + + + inline list_reverse_iterator & operator = (const list_reverse_iterator &&rh) PLF_LIST_NOEXCEPT + { + node_pointer = std::move(rh.node_pointer); + return *this; + } + #endif + + + + inline typename list::iterator base() const PLF_LIST_NOEXCEPT + { + return typename list::iterator(node_pointer->next); + } + + + + list_reverse_iterator() PLF_LIST_NOEXCEPT: node_pointer(NULL) {} + + list_reverse_iterator(const list_reverse_iterator &source) PLF_LIST_NOEXCEPT: node_pointer(source.node_pointer) {} + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + list_reverse_iterator (const list_reverse_iterator &&source) PLF_LIST_NOEXCEPT: node_pointer(std::move(source.node_pointer)) {} + #endif + + private: + + list_reverse_iterator (const node_pointer_type node_p) PLF_LIST_NOEXCEPT: node_pointer(node_p) {} + }; + + + +private: + + // Used by range-insert and range-constructor to prevent fill-insert and fill-constructor function calls mistakenly resolving to the range insert/constructor + template + struct plf_enable_if_c + { + typedef T type; + }; + + template + struct plf_enable_if_c + {}; + + + + group_vector groups; + node_base end_node; + node_pointer_type last_endpoint; // last_endpoint being NULL means no elements have been constructed, but there may still be groups available due to clear() or reservee() + iterator end_iterator, begin_iterator; // end_iterator is always the last entry point in last group in list (or one past the end of group) + + struct ebco_pair1 : node_pointer_allocator_type // Packaging the group allocator with least-used member variables, for empty-base-class optimisation + { + size_type total_number_of_elements; + explicit ebco_pair1(const size_type total_num_elements) PLF_LIST_NOEXCEPT: total_number_of_elements(total_num_elements) {} + } node_pointer_allocator_pair; + + struct ebco_pair2 : node_allocator_type + { + size_type number_of_erased_nodes; + explicit ebco_pair2(const size_type num_erased_nodes) PLF_LIST_NOEXCEPT: number_of_erased_nodes(num_erased_nodes) {} + } node_allocator_pair; + + + +public: + + // Default constructor: + + list() PLF_LIST_NOEXCEPT: + element_allocator_type(element_allocator_type()), + end_node(reinterpret_cast(&end_node), reinterpret_cast(&end_node)), + last_endpoint(NULL), + end_iterator(reinterpret_cast(&end_node)), + begin_iterator(reinterpret_cast(&end_node)), + node_pointer_allocator_pair(0), + node_allocator_pair(0) + {} + + + + // Allocator-extended constructor: + + explicit list(const element_allocator_type &alloc): + element_allocator_type(alloc), + end_node(reinterpret_cast(&end_node), reinterpret_cast(&end_node)), + last_endpoint(NULL), + end_iterator(reinterpret_cast(&end_node)), + begin_iterator(reinterpret_cast(&end_node)), + node_pointer_allocator_pair(0), + node_allocator_pair(0) + {} + + + + // Copy constructor: + + list(const list &source): + element_allocator_type(source), + end_node(reinterpret_cast(&end_node), reinterpret_cast(&end_node)), + last_endpoint(NULL), + end_iterator(reinterpret_cast(&end_node)), + begin_iterator(reinterpret_cast(&end_node)), + node_pointer_allocator_pair(0), + node_allocator_pair(0) + { + reserve(source.node_pointer_allocator_pair.total_number_of_elements); + insert(end_iterator, source.begin_iterator, source.end_iterator); + } + + + + // Allocator-extended copy constructor: + + list(const list &source, const allocator_type &alloc): + element_allocator_type(alloc), + end_node(reinterpret_cast(&end_node), reinterpret_cast(&end_node)), + last_endpoint(NULL), + end_iterator(reinterpret_cast(&end_node)), + begin_iterator(reinterpret_cast(&end_node)), + node_pointer_allocator_pair(0), + node_allocator_pair(0) + { + reserve(source.node_pointer_allocator_pair.total_number_of_elements); + insert(end_iterator, source.begin_iterator, source.end_iterator); + } + + + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + // Move constructor: + + list(list &&source) PLF_LIST_NOEXCEPT: + element_allocator_type(source), + groups(std::move(source.groups)), + end_node(std::move(source.end_node)), + last_endpoint(std::move(source.last_endpoint)), + end_iterator(reinterpret_cast(&end_node)), + begin_iterator((source.begin_iterator.node_pointer == source.end_iterator.node_pointer) ? reinterpret_cast(&end_node) : std::move(source.begin_iterator)), + node_pointer_allocator_pair(source.node_pointer_allocator_pair.total_number_of_elements), + node_allocator_pair(source.node_allocator_pair.number_of_erased_nodes) + { + end_node.previous->next = begin_iterator.node_pointer->previous = end_iterator.node_pointer; + source.groups.blank(); + source.reset(); + } + + + + // Allocator-extended move constructor: + + list(list &&source, const allocator_type &alloc): + element_allocator_type(alloc), + groups(std::move(source.groups)), + end_node(std::move(source.end_node)), + last_endpoint(std::move(source.last_endpoint)), + end_iterator(reinterpret_cast(&end_node)), + begin_iterator((source.begin_iterator.node_pointer == source.end_iterator.node_pointer) ? reinterpret_cast(&end_node) : std::move(source.begin_iterator)), + node_pointer_allocator_pair(source.node_pointer_allocator_pair.total_number_of_elements), + node_allocator_pair(source.node_allocator_pair.number_of_erased_nodes) + { + end_node.previous->next = begin_iterator.node_pointer->previous = end_iterator.node_pointer; + source.groups.blank(); + source.reset(); + } + #endif + + + + // Fill constructor: + + list(const size_type fill_number, const element_type &element, const element_allocator_type &alloc = element_allocator_type()): + element_allocator_type(alloc), + end_node(reinterpret_cast(&end_node), reinterpret_cast(&end_node)), + last_endpoint(NULL), + end_iterator(reinterpret_cast(&end_node)), + begin_iterator(reinterpret_cast(&end_node)), + node_pointer_allocator_pair(0), + node_allocator_pair(0) + { + reserve(fill_number); + insert(end_iterator, fill_number, element); + } + + + + // Range constructor: + + template + list(const typename plf_enable_if_c::is_integer, iterator_type>::type &first, const iterator_type &last, const element_allocator_type &alloc = element_allocator_type()): + element_allocator_type(alloc), + end_node(reinterpret_cast(&end_node), reinterpret_cast(&end_node)), + last_endpoint(NULL), + end_iterator(reinterpret_cast(&end_node)), + begin_iterator(reinterpret_cast(&end_node)), + node_pointer_allocator_pair(0), + node_allocator_pair(0) + { + insert(end_iterator, first, last); + } + + + + // Initializer-list constructor: + + #ifdef PLF_LIST_INITIALIZER_LIST_SUPPORT + list(const std::initializer_list &element_list, const element_allocator_type &alloc = element_allocator_type()): + element_allocator_type(alloc), + end_node(reinterpret_cast(&end_node), reinterpret_cast(&end_node)), + last_endpoint(NULL), + end_iterator(reinterpret_cast(&end_node)), + begin_iterator(reinterpret_cast(&end_node)), + node_pointer_allocator_pair(0), + node_allocator_pair(0) + { + reserve(element_list.size()); + insert(end_iterator, element_list); + } + + #endif + + + + ~list() PLF_LIST_NOEXCEPT + { + groups.destroy_all_data(last_endpoint); + } + + + + inline iterator begin() PLF_LIST_NOEXCEPT + { + return begin_iterator; + } + + + + inline const_iterator begin() const PLF_LIST_NOEXCEPT + { + return begin_iterator; + } + + + + inline iterator end() PLF_LIST_NOEXCEPT + { + return end_iterator; + } + + + + inline const_iterator end() const PLF_LIST_NOEXCEPT + { + return end_iterator; + } + + + + inline const_iterator cbegin() const PLF_LIST_NOEXCEPT + { + return const_iterator(begin_iterator.node_pointer); + } + + + + inline const_iterator cend() const PLF_LIST_NOEXCEPT + { + return const_iterator(end_iterator.node_pointer); + } + + + + inline reverse_iterator rbegin() const PLF_LIST_NOEXCEPT + { + return reverse_iterator(end_node.previous); + } + + + + inline reverse_iterator rend() const PLF_LIST_NOEXCEPT + { + return reverse_iterator(end_iterator.node_pointer); + } + + + + inline const_reverse_iterator crbegin() const PLF_LIST_NOEXCEPT + { + return const_reverse_iterator(end_node.previous); + } + + + + inline const_reverse_iterator crend() const PLF_LIST_NOEXCEPT + { + return const_reverse_iterator(end_iterator.node_pointer); + } + + + + inline reference front() + { + assert(begin_iterator.node_pointer != &end_node); + return begin_iterator.node_pointer->element; + } + + + + inline const_reference front() const + { + assert(begin_iterator.node_pointer != &end_node); + return begin_iterator.node_pointer->element; + } + + + + inline reference back() + { + assert(end_node.previous != &end_node); + return end_node.previous->element; + } + + + + inline const_reference back() const + { + assert(end_node.previous != &end_node); + return end_node.previous->element; + } + + + + void clear() PLF_LIST_NOEXCEPT + { + if (last_endpoint == NULL) + { + return; + } + + if (node_pointer_allocator_pair.total_number_of_elements != 0) + { + groups.clear(last_endpoint); + } + + end_node.next = reinterpret_cast(&end_node); + end_node.previous = reinterpret_cast(&end_node); + last_endpoint = groups.block_pointer->nodes; + begin_iterator.node_pointer = end_iterator.node_pointer; + node_pointer_allocator_pair.total_number_of_elements = 0; + node_allocator_pair.number_of_erased_nodes = 0; + } + + + +private: + + + void reset() PLF_LIST_NOEXCEPT + { + groups.destroy_all_data(last_endpoint); + last_endpoint = NULL; + end_node.next = reinterpret_cast(&end_node); + end_node.previous = reinterpret_cast(&end_node); + begin_iterator.node_pointer = end_iterator.node_pointer; + node_pointer_allocator_pair.total_number_of_elements = 0; + node_allocator_pair.number_of_erased_nodes = 0; + } + + + + +public: + + + iterator insert(const iterator it, const element_type &element) + { + if (last_endpoint != NULL) // ie. list is not empty + { + if (node_allocator_pair.number_of_erased_nodes == 0) // No erased nodes available for reuse + { + if (last_endpoint == groups.last_endpoint_group->beyond_end) // last_endpoint is beyond the end of a group + { + if (static_cast(groups.last_endpoint_group - groups.block_pointer) == groups.size - 1) // ie. there are no reusable groups available at the back of group vector + { + groups.add_new((node_pointer_allocator_pair.total_number_of_elements < PLF_LIST_BLOCK_MAX) ? static_cast(node_pointer_allocator_pair.total_number_of_elements) : PLF_LIST_BLOCK_MAX); + } + else + { + ++groups.last_endpoint_group; + } + + last_endpoint = groups.last_endpoint_group->nodes; + } + + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint, it.node_pointer, it.node_pointer->previous, element); + #else + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint, node(it.node_pointer, it.node_pointer->previous, element)); + #endif + + ++(groups.last_endpoint_group->number_of_elements); + ++node_pointer_allocator_pair.total_number_of_elements; + + if (it.node_pointer == begin_iterator.node_pointer) + { + begin_iterator.node_pointer = last_endpoint; + } + + it.node_pointer->previous->next = last_endpoint; + it.node_pointer->previous = last_endpoint; + + return iterator(last_endpoint++); + } + else + { + group_pointer_type const node_group = groups.get_nearest_freelist_group((it.node_pointer != end_iterator.node_pointer) ? it.node_pointer : end_node.previous); + node_pointer_type const selected_node = node_group->free_list_head; + const node_pointer_type previous = node_group->free_list_head->previous; + + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, selected_node, it.node_pointer, it.node_pointer->previous, element); + #else + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, selected_node, node(it.node_pointer, it.node_pointer->previous, element)); + #endif + + node_group->free_list_head = previous; + ++(node_group->number_of_elements); + ++node_pointer_allocator_pair.total_number_of_elements; + --node_allocator_pair.number_of_erased_nodes; + + it.node_pointer->previous->next = selected_node; + it.node_pointer->previous = selected_node; + + if (it.node_pointer == begin_iterator.node_pointer) + { + begin_iterator.node_pointer = selected_node; + } + + return iterator(selected_node); + } + } + else // list is empty + { + if (groups.block_pointer == NULL) // In case of prior reserve/clear call as opposed to being uninitialized + { + groups.initialize(PLF_LIST_BLOCK_MIN); + } + + groups.last_endpoint_group->number_of_elements = 1; + end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = groups.last_endpoint_group->nodes; + node_pointer_allocator_pair.total_number_of_elements = 1; + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_nothrow_copy_constructible::value) // Avoid try-catch code generation + { + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint++, end_iterator.node_pointer, end_iterator.node_pointer, element); + #else + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint++, node(end_iterator.node_pointer, end_iterator.node_pointer, element)); + #endif + } + else + #endif + { + try + { + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint++, end_iterator.node_pointer, end_iterator.node_pointer, element); + #else + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint++, node(end_iterator.node_pointer, end_iterator.node_pointer, element)); + #endif + } + catch (...) + { + reset(); + throw; + } + } + + return begin_iterator; + } + } + + + + inline PLF_LIST_FORCE_INLINE void push_back(const element_type &element) + { + insert(end_iterator, element); + } + + + + inline PLF_LIST_FORCE_INLINE void push_front(const element_type &element) + { + insert(begin_iterator, element); + } + + + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + iterator insert(const iterator it, element_type &&element) // This is almost identical to the insert implementation above with the only change being std::move of the element + { + if (last_endpoint != NULL) + { + if (node_allocator_pair.number_of_erased_nodes == 0) + { + if (last_endpoint == groups.last_endpoint_group->beyond_end) + { + if (static_cast(groups.last_endpoint_group - groups.block_pointer) == groups.size - 1) + { + groups.add_new((node_pointer_allocator_pair.total_number_of_elements < PLF_LIST_BLOCK_MAX) ? static_cast(node_pointer_allocator_pair.total_number_of_elements) : PLF_LIST_BLOCK_MAX); + } + else + { + ++groups.last_endpoint_group; + } + + last_endpoint = groups.last_endpoint_group->nodes; + } + + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint, it.node_pointer, it.node_pointer->previous, std::move(element)); + #else + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint, node(it.node_pointer, it.node_pointer->previous, std::move(element))); + #endif + + ++(groups.last_endpoint_group->number_of_elements); + ++node_pointer_allocator_pair.total_number_of_elements; + + if (it.node_pointer == begin_iterator.node_pointer) + { + begin_iterator.node_pointer = last_endpoint; + } + + it.node_pointer->previous->next = last_endpoint; + it.node_pointer->previous = last_endpoint; + + return iterator(last_endpoint++); + } + else + { + group_pointer_type const node_group = groups.get_nearest_freelist_group((it.node_pointer != end_iterator.node_pointer) ? it.node_pointer : end_node.previous); + node_pointer_type const selected_node = node_group->free_list_head; + const node_pointer_type previous = node_group->free_list_head->previous; + + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, selected_node, it.node_pointer, it.node_pointer->previous, std::move(element)); + #else + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, selected_node, node(it.node_pointer, it.node_pointer->previous, std::move(element))); + #endif + + node_group->free_list_head = previous; + ++(node_group->number_of_elements); + ++node_pointer_allocator_pair.total_number_of_elements; + --node_allocator_pair.number_of_erased_nodes; + + it.node_pointer->previous->next = selected_node; + it.node_pointer->previous = selected_node; + + if (it.node_pointer == begin_iterator.node_pointer) + { + begin_iterator.node_pointer = selected_node; + } + + return iterator(selected_node); + } + } + else + { + if (groups.block_pointer == NULL) + { + groups.initialize(PLF_LIST_BLOCK_MIN); + } + + groups.last_endpoint_group->number_of_elements = 1; + end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = groups.last_endpoint_group->nodes; + node_pointer_allocator_pair.total_number_of_elements = 1; + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_nothrow_move_constructible::value) + { + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint++, end_iterator.node_pointer, end_iterator.node_pointer, std::move(element)); + #else + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint++, node(end_iterator.node_pointer, end_iterator.node_pointer, std::move(element))); + #endif + } + else + #endif + { + try + { + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint++, end_iterator.node_pointer, end_iterator.node_pointer, std::move(element)); + #else + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint++, node(end_iterator.node_pointer, end_iterator.node_pointer, std::move(element))); + #endif + } + catch (...) + { + reset(); + throw; + } + } + + return begin_iterator; + } + } + + + + inline PLF_LIST_FORCE_INLINE void push_back(element_type &&element) + { + insert(end_iterator, std::move(element)); + } + + + + inline PLF_LIST_FORCE_INLINE void push_front(element_type &&element) + { + insert(begin_iterator, std::move(element)); + } + #endif + + + + + #ifdef PLF_LIST_VARIADICS_SUPPORT + template + iterator emplace(const iterator it, arguments &&... parameters) // This is almost identical to the insert implementations above with the only changes being std::forward of element parameters and removal of VARIADICS support checking + { + if (last_endpoint != NULL) + { + if (node_allocator_pair.number_of_erased_nodes == 0) + { + if (last_endpoint == groups.last_endpoint_group->beyond_end) + { + if (static_cast(groups.last_endpoint_group - groups.block_pointer) == groups.size - 1) + { + groups.add_new((node_pointer_allocator_pair.total_number_of_elements < PLF_LIST_BLOCK_MAX) ? static_cast(node_pointer_allocator_pair.total_number_of_elements) : PLF_LIST_BLOCK_MAX); + } + else + { + ++groups.last_endpoint_group; + } + + last_endpoint = groups.last_endpoint_group->nodes; + } + + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint, it.node_pointer, it.node_pointer->previous, std::forward(parameters)...); + + ++(groups.last_endpoint_group->number_of_elements); + ++node_pointer_allocator_pair.total_number_of_elements; + + if (it.node_pointer == begin_iterator.node_pointer) + { + begin_iterator.node_pointer = last_endpoint; + } + + it.node_pointer->previous->next = last_endpoint; + it.node_pointer->previous = last_endpoint; + + return iterator(last_endpoint++); + } + else + { + group_pointer_type const node_group = groups.get_nearest_freelist_group((it.node_pointer != end_iterator.node_pointer) ? it.node_pointer : end_node.previous); + node_pointer_type const selected_node = node_group->free_list_head; + const node_pointer_type previous = node_group->free_list_head->previous; + + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, selected_node, it.node_pointer, it.node_pointer->previous, std::forward(parameters)...); + + node_group->free_list_head = previous; + ++(node_group->number_of_elements); + ++node_pointer_allocator_pair.total_number_of_elements; + --node_allocator_pair.number_of_erased_nodes; + + it.node_pointer->previous->next = selected_node; + it.node_pointer->previous = selected_node; + + if (it.node_pointer == begin_iterator.node_pointer) + { + begin_iterator.node_pointer = selected_node; + } + + return iterator(selected_node); + } + } + else + { + if (groups.block_pointer == NULL) + { + groups.initialize(PLF_LIST_BLOCK_MIN); + } + + groups.last_endpoint_group->number_of_elements = 1; + end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = groups.last_endpoint_group->nodes; + node_pointer_allocator_pair.total_number_of_elements = 1; + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_nothrow_constructible::value) + { + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint++, end_iterator.node_pointer, end_iterator.node_pointer, std::forward(parameters)...); + } + else + #endif + { + try + { + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint++, end_iterator.node_pointer, end_iterator.node_pointer, std::forward(parameters)...); + } + catch (...) + { + reset(); + throw; + } + } + + return begin_iterator; + } + } + + + + template + inline PLF_LIST_FORCE_INLINE reference emplace_back(arguments &&... parameters) + { + return (emplace(end_iterator, std::forward(parameters)...)).node_pointer->element; + } + + + + template + inline PLF_LIST_FORCE_INLINE reference emplace_front(arguments &&... parameters) + { + return (emplace(begin_iterator, std::forward(parameters)...)).node_pointer->element; + } + + + #endif + + + +private: + + void group_fill_position(const element_type &element, group_size_type number_of_elements, node_pointer_type const position) + { + position->previous->next = last_endpoint; + groups.last_endpoint_group->number_of_elements += number_of_elements; + node_pointer_type previous = position->previous; + + do + { + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_nothrow_copy_constructible::value) + { + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint, last_endpoint + 1, previous, element); + #else + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint, node(last_endpoint + 1, previous, element)); + #endif + } + else + #endif + { + try + { + #ifdef PLF_LIST_VARIADICS_SUPPORT + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint, last_endpoint + 1, previous, element); + #else + PLF_LIST_CONSTRUCT(node_allocator_type, node_allocator_pair, last_endpoint, node(last_endpoint + 1, previous, element)); + #endif + } + catch (...) + { + previous->next = position; + position->previous = --previous; + groups.last_endpoint_group->number_of_elements -= static_cast(number_of_elements - (last_endpoint - position)); + throw; + } + } + + previous = last_endpoint++; + } while (--number_of_elements != 0); + + previous->next = position; + position->previous = previous; + } + + + +public: + + // Fill insert + + iterator insert(iterator position, const size_type number_of_elements, const element_type &element) + { + if (number_of_elements == 0) + { + return end_iterator; + } + else if (number_of_elements == 1) + { + return insert(position, element); + } + + + if (node_pointer_allocator_pair.total_number_of_elements == 0 && last_endpoint != NULL && (static_cast(groups.block_pointer->beyond_end - groups.block_pointer->nodes) < number_of_elements) && (static_cast(groups.block_pointer->beyond_end - groups.block_pointer->nodes) < PLF_LIST_BLOCK_MAX)) + { + reset(); + } + + + if (groups.block_pointer == NULL) // ie. Uninitialized list + { + if (number_of_elements > PLF_LIST_BLOCK_MAX) + { + size_type multiples = number_of_elements / PLF_LIST_BLOCK_MAX; + const group_size_type remainder = static_cast(number_of_elements - (multiples++ * PLF_LIST_BLOCK_MAX)); // ++ to aid while loop below + + // Create and fill first group: + if (remainder != 0) // make sure smallest block is first + { + if (remainder >= PLF_LIST_BLOCK_MIN) + { + groups.initialize(remainder); + end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = groups.last_endpoint_group->nodes; + group_fill_position(element, remainder, end_iterator.node_pointer); + } + else + { // Create first group as BLOCK_MIN size then subtract difference between BLOCK_MIN and remainder from next group: + groups.initialize(PLF_LIST_BLOCK_MIN); + end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = groups.last_endpoint_group->nodes; + group_fill_position(element, PLF_LIST_BLOCK_MIN, end_iterator.node_pointer); + + groups.add_new(PLF_LIST_BLOCK_MAX - (PLF_LIST_BLOCK_MIN - remainder)); + end_node.previous = last_endpoint = groups.last_endpoint_group->nodes; + group_fill_position(element, PLF_LIST_BLOCK_MAX - (PLF_LIST_BLOCK_MIN - remainder), end_iterator.node_pointer); + --multiples; + } + } + else + { + groups.initialize(PLF_LIST_BLOCK_MAX); + end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = groups.last_endpoint_group->nodes; + group_fill_position(element, PLF_LIST_BLOCK_MAX, end_iterator.node_pointer); + --multiples; + } + + while (--multiples != 0) + { + groups.add_new(PLF_LIST_BLOCK_MAX); + end_node.previous = last_endpoint = groups.last_endpoint_group->nodes; + group_fill_position(element, PLF_LIST_BLOCK_MAX, end_iterator.node_pointer); + } + + } + else + { + groups.initialize((number_of_elements < PLF_LIST_BLOCK_MIN) ? PLF_LIST_BLOCK_MIN : static_cast(number_of_elements)); // Construct first group + end_node.next = end_node.previous = last_endpoint = begin_iterator.node_pointer = groups.last_endpoint_group->nodes; + group_fill_position(element, static_cast(number_of_elements), end_iterator.node_pointer); + } + + node_pointer_allocator_pair.total_number_of_elements = number_of_elements; + return begin_iterator; + } + else + { + // Insert first element, then use up any erased nodes: + size_type remainder = number_of_elements - 1; + const iterator return_iterator = insert(position, element); + + while (node_allocator_pair.number_of_erased_nodes != 0) + { + insert(position, element); + --node_allocator_pair.number_of_erased_nodes; + + if (--remainder == 0) + { + return return_iterator; + } + } + + node_pointer_allocator_pair.total_number_of_elements += remainder; + + // then use up remainder of last_endpoint_group: + const group_size_type remaining_nodes_in_group = static_cast(groups.last_endpoint_group->beyond_end - last_endpoint); + + if (remaining_nodes_in_group != 0) + { + if (remaining_nodes_in_group < remainder) + { + group_fill_position(element, remaining_nodes_in_group, position.node_pointer); + remainder -= remaining_nodes_in_group; + } + else + { + group_fill_position(element, static_cast(remainder), position.node_pointer); + return return_iterator; + } + } + + + // use up trailing groups: + while ((groups.last_endpoint_group != (groups.block_pointer + groups.size - 1)) & (remainder != 0)) + { + last_endpoint = (++groups.last_endpoint_group)->nodes; + const group_size_type group_size = static_cast(groups.last_endpoint_group->beyond_end - groups.last_endpoint_group->nodes); + + if (group_size < remainder) + { + group_fill_position(element, group_size, position.node_pointer); + remainder -= group_size; + } + else + { + group_fill_position(element, static_cast(remainder), position.node_pointer); + return return_iterator; + } + } + + size_type multiples = remainder / static_cast(PLF_LIST_BLOCK_MAX); + remainder -= multiples * PLF_LIST_BLOCK_MAX; + + while (multiples-- != 0) + { + groups.add_new(PLF_LIST_BLOCK_MAX); + last_endpoint = groups.last_endpoint_group->nodes; + group_fill_position(element, PLF_LIST_BLOCK_MAX, position.node_pointer); + } + + if (remainder != 0) // Bit annoying to create a large block to house a lower number of elements, but beats the alternatives + { + groups.add_new(PLF_LIST_BLOCK_MAX); + last_endpoint = groups.last_endpoint_group->nodes; + group_fill_position(element, static_cast(remainder), position.node_pointer); + } + + return return_iterator; + } + } + + + + // Range insert + + template + iterator insert(const iterator it, typename plf_enable_if_c::is_integer, iterator_type>::type first, const iterator_type last) + { + if (first == last) + { + return end_iterator; + } + + const iterator return_iterator = insert(it, *first); + + while(++first != last) + { + insert(it, *first); + } + + return return_iterator; + } + + + + // Initializer-list insert + + #ifdef PLF_LIST_INITIALIZER_LIST_SUPPORT + inline iterator insert(const iterator it, const std::initializer_list &element_list) + { // use range insert: + return insert(it, element_list.begin(), element_list.end()); + } + #endif + + + +private: + + inline PLF_LIST_FORCE_INLINE void destroy_all_node_pointers(group_pointer_type const group_to_process, const node_pointer_type beyond_end_node) PLF_LIST_NOEXCEPT + { + for (node_pointer_type current_node = group_to_process->nodes; current_node != beyond_end_node; ++current_node) + { + PLF_LIST_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, &(current_node->next)); // Destruct element + PLF_LIST_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, &(current_node->previous)); // Destruct element + } + } + + + +public: + + + // Single erase: + + iterator erase(const const_iterator it) // if uninitialized/invalid iterator supplied, function could generate an exception, hence no noexcept + { + assert(node_pointer_allocator_pair.total_number_of_elements != 0); + assert(it.node_pointer != NULL); + assert(it.node_pointer != end_iterator.node_pointer); + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!(std::is_trivially_destructible::value)) + #endif + { + PLF_LIST_DESTROY(element_allocator_type, (*this), &(it.node_pointer->element)); // Destruct element + } + + --node_pointer_allocator_pair.total_number_of_elements; + ++node_allocator_pair.number_of_erased_nodes; + + + group_pointer_type node_group = groups.last_searched_group; + + // find nearest group with reusable (erased element) memory location: + if ((it.node_pointer < node_group->nodes) || (it.node_pointer >= node_group->beyond_end)) + { + // Search left and right: + const group_pointer_type beyond_end_group = groups.last_endpoint_group + 1; + group_pointer_type left = node_group - 1; + bool right_not_beyond_back = (++node_group < beyond_end_group); + bool left_not_beyond_front = (left >= groups.block_pointer); + + while (true) + { + if (right_not_beyond_back) + { + if ((it.node_pointer < node_group->beyond_end) && (it.node_pointer >= node_group->nodes)) // usable location found + { + break; + } + + right_not_beyond_back = (++node_group < beyond_end_group); + } + + if (left_not_beyond_front) + { + if ((it.node_pointer >= left->nodes) && (it.node_pointer < left->beyond_end)) // usable location found + { + node_group = left; + break; + } + + left_not_beyond_front = (--left >= groups.block_pointer); + } + } + + groups.last_searched_group = node_group; + } + + it.node_pointer->next->previous = it.node_pointer->previous; + it.node_pointer->previous->next = it.node_pointer->next; + + if (it.node_pointer == begin_iterator.node_pointer) + { + begin_iterator.node_pointer = it.node_pointer->next; + } + + + const iterator return_iterator(it.node_pointer->next); + + if (--(node_group->number_of_elements) != 0) // ie. group is not empty yet, add node to free list + { + it.node_pointer->next = NULL; // next == NULL so that destructor can detect the free list item as opposed to non-free-list item + it.node_pointer->previous = node_group->free_list_head; + node_group->free_list_head = it.node_pointer; + return return_iterator; + } + else if (node_group != groups.last_endpoint_group--) // remove group (and decrement active back group) + { + const group_size_type group_size = static_cast(node_group->beyond_end - node_group->nodes); + node_allocator_pair.number_of_erased_nodes -= group_size; + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!(std::is_trivially_destructible::value)) + #endif + { + destroy_all_node_pointers(node_group, node_group->beyond_end); + } + + node_group->free_list_head = NULL; + + if ((group_size == PLF_LIST_BLOCK_MAX) | (node_group >= groups.last_endpoint_group - 1)) // Preserve only last (active) group or second/third-to-last group - seems to be best for performance under high-modification benchmarks + { + groups.move_to_back(node_group); + } + else + { + groups.remove(node_group); + } + + return return_iterator; + } + else // clear back group, leave trailing + { + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!(std::is_trivially_destructible::value)) + #endif + { + destroy_all_node_pointers(node_group, last_endpoint); + } + + node_group->free_list_head = NULL; + + if (node_pointer_allocator_pair.total_number_of_elements != 0) + { + node_allocator_pair.number_of_erased_nodes -= static_cast(last_endpoint - node_group->nodes); + last_endpoint = groups.last_endpoint_group->beyond_end; + } + else + { + groups.last_endpoint_group = groups.block_pointer; // If number of elements is zero, it indicates that this was the first group in the vector. In which case the last_endpoint_group would be invalid at this point due to the decrement in the above else-if statement. So it needs to be reset, as it will not be reset in the function call below. + clear(); + } + + + return return_iterator; + } + } + + + + // Range-erase: + + inline void erase(const const_iterator iterator1, const const_iterator iterator2) // if uninitialized/invalid iterator supplied, function could generate an exception + { + for (const_iterator current = iterator1; current != iterator2;) + { + current = erase(current); + } + } + + + + inline void pop_back() // Exception will occur on empty list + { + erase(iterator(end_node.previous)); + } + + + + inline void pop_front() // Exception will occur on empty list + { + erase(begin_iterator); + } + + + + inline list & operator = (const list &source) + { + assert (&source != this); + + clear(); + reserve(source.node_pointer_allocator_pair.total_number_of_elements); + insert(end_iterator, source.begin_iterator, source.end_iterator); + + return *this; + } + + + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + // Move assignment + list & operator = (list &&source) PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT(allocator_type) + { + assert (&source != this); + + // Move source values across: + groups.destroy_all_data(last_endpoint); + + groups = std::move(source.groups); + end_node = std::move(source.end_node); + last_endpoint = std::move(source.last_endpoint); + begin_iterator.node_pointer = (source.begin_iterator.node_pointer == source.end_iterator.node_pointer) ? end_iterator.node_pointer : std::move(source.begin_iterator.node_pointer); + node_pointer_allocator_pair.total_number_of_elements = source.node_pointer_allocator_pair.total_number_of_elements; + node_allocator_pair.number_of_erased_nodes = source.node_allocator_pair.number_of_erased_nodes; + + end_node.previous->next = begin_iterator.node_pointer->previous = end_iterator.node_pointer; + + source.groups.blank(); + source.reset(); + return *this; + } + #endif + + + + bool operator == (const list &rh) const PLF_LIST_NOEXCEPT + { + assert (this != &rh); + + if (node_pointer_allocator_pair.total_number_of_elements != rh.node_pointer_allocator_pair.total_number_of_elements) + { + return false; + } + + iterator rh_iterator = rh.begin_iterator; + + for (iterator lh_iterator = begin_iterator; lh_iterator != end_iterator;) + { + if (*rh_iterator++ != *lh_iterator++) + { + return false; + } + } + + return true; + } + + + + inline bool operator != (const list &rh) const PLF_LIST_NOEXCEPT + { + return !(*this == rh); + } + + + + inline bool empty() const PLF_LIST_NOEXCEPT + { + return node_pointer_allocator_pair.total_number_of_elements == 0; + } + + + + inline size_type size() const PLF_LIST_NOEXCEPT + { + return node_pointer_allocator_pair.total_number_of_elements; + } + + + + inline size_type max_size() const PLF_LIST_NOEXCEPT + { + #ifdef PLF_LIST_ALLOCATOR_TRAITS_SUPPORT + return std::allocator_traits::max_size(*this); + #else + return element_allocator_type::max_size(); + #endif + } + + + + inline size_type capacity() const PLF_LIST_NOEXCEPT + { + return groups.element_allocator_pair.capacity; + } + + + + inline size_type approximate_memory_use() const PLF_LIST_NOEXCEPT + { + return static_cast(sizeof(*this) + (groups.element_allocator_pair.capacity * sizeof(node)) + (sizeof(group) * groups.group_allocator_pair.capacity)); + } + + + +private: + + + struct less + { + inline bool operator() (const element_type &a, const element_type &b) const PLF_LIST_NOEXCEPT + { + return a < b; + } + }; + + + + // Function-object to redirect the sort function to sort pointers by the elements they point to, not the pointer value + template + struct sort_dereferencer + { + comparison_function stored_instance; + + explicit sort_dereferencer(const comparison_function &function_instance): + stored_instance(function_instance) + {} + + sort_dereferencer() PLF_LIST_NOEXCEPT + {} + + inline bool operator() (const node_pointer_type first, const node_pointer_type second) + { + return stored_instance(first->element, second->element); + } + }; + + + +public: + + + template + void sort(comparison_function compare) + { + if (node_pointer_allocator_pair.total_number_of_elements < 2) + { + return; + } + + node_pointer_type * const node_pointers = PLF_LIST_ALLOCATE(node_pointer_allocator_type, node_pointer_allocator_pair, node_pointer_allocator_pair.total_number_of_elements, NULL); + node_pointer_type *node_pointer = node_pointers; + + + // According to the C++ standard, construction of a pointer (of any type) may not trigger an exception - hence, no try-catch blocks are necessary for constructing the pointers: + for (group_pointer_type current_group = groups.block_pointer; current_group != groups.last_endpoint_group; ++current_group) + { + const node_pointer_type end = current_group->beyond_end; + + if ((end - current_group->nodes) != current_group->number_of_elements) // If there are erased nodes present in the group + { + for (node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node) + { + if (current_node->next != NULL) // is not free list node + { + PLF_LIST_CONSTRUCT(node_pointer_allocator_type, node_pointer_allocator_pair, node_pointer++, current_node); + } + } + } + else + { + for (node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node) + { + PLF_LIST_CONSTRUCT(node_pointer_allocator_type, node_pointer_allocator_pair, node_pointer++, current_node); + } + } + } + + if ((last_endpoint - groups.last_endpoint_group->nodes) != groups.last_endpoint_group->number_of_elements) // If there are erased nodes present in the group + { + for (node_pointer_type current_node = groups.last_endpoint_group->nodes; current_node != last_endpoint; ++current_node) + { + if (current_node->next != NULL) + { + PLF_LIST_CONSTRUCT(node_pointer_allocator_type, node_pointer_allocator_pair, node_pointer++, current_node); + } + } + } + else + { + for (node_pointer_type current_node = groups.last_endpoint_group->nodes; current_node != last_endpoint; ++current_node) + { + PLF_LIST_CONSTRUCT(node_pointer_allocator_type, node_pointer_allocator_pair, node_pointer++, current_node); + } + } + + + #ifdef GFX_TIMSORT_HPP + gfx::timsort(node_pointers, node_pointers + node_pointer_allocator_pair.total_number_of_elements, sort_dereferencer(compare)); + #else + std::sort(node_pointers, node_pointers + node_pointer_allocator_pair.total_number_of_elements, sort_dereferencer(compare)); + #endif + + begin_iterator.node_pointer = node_pointers[0]; + begin_iterator.node_pointer->next = node_pointers[1]; + begin_iterator.node_pointer->previous = end_iterator.node_pointer; + + end_node.next = node_pointers[0]; + end_node.previous = node_pointers[node_pointer_allocator_pair.total_number_of_elements - 1]; + end_node.previous->next = end_iterator.node_pointer; + end_node.previous->previous = node_pointers[node_pointer_allocator_pair.total_number_of_elements - 2]; + + node_pointer_type * const back = node_pointers + node_pointer_allocator_pair.total_number_of_elements - 1; + + for(node_pointer = node_pointers + 1; node_pointer != back; ++node_pointer) + { + (*node_pointer)->next = *(node_pointer + 1); + (*node_pointer)->previous = *(node_pointer - 1); + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + PLF_LIST_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, node_pointer - 1); + } + } + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (!std::is_trivially_destructible::value) + #endif + { + PLF_LIST_DESTROY(node_pointer_allocator_type, node_pointer_allocator_pair, back); + } + + PLF_LIST_DEALLOCATE(node_pointer_allocator_type, node_pointer_allocator_pair, node_pointers, node_pointer_allocator_pair.total_number_of_elements); + } + + + + inline void sort() + { + sort(less()); + } + + + + void reorder(const iterator position, const iterator first, const iterator last) PLF_LIST_NOEXCEPT + { + last.node_pointer->next->previous = first.node_pointer->previous; + first.node_pointer->previous->next = last.node_pointer->next; + + last.node_pointer->next = position.node_pointer; + first.node_pointer->previous = position.node_pointer->previous; + + position.node_pointer->previous->next = first.node_pointer; + position.node_pointer->previous = last.node_pointer; + + if (begin_iterator == position) + { + begin_iterator = first; + } + } + + + + inline void reorder(const iterator position, const iterator location) PLF_LIST_NOEXCEPT + { + reorder(position, location, location); + } + + + + void reserve(size_type reserve_amount) + { + if (reserve_amount == 0 || reserve_amount <= groups.element_allocator_pair.capacity) + { + return; + } + else if (reserve_amount < PLF_LIST_BLOCK_MIN) + { + reserve_amount = PLF_LIST_BLOCK_MIN; + } + else if (reserve_amount > max_size()) + { + reserve_amount = max_size(); + } + + + if (groups.block_pointer != NULL && node_pointer_allocator_pair.total_number_of_elements == 0) + { // edge case: has been filled with elements then clear()'d - some groups may be smaller than would be desired, should be replaced + group_size_type end_group_size = static_cast((groups.block_pointer + groups.size - 1)->beyond_end - (groups.block_pointer + groups.size - 1)->nodes); + + if (reserve_amount > end_group_size && end_group_size != PLF_LIST_BLOCK_MAX) // if last group isn't large enough, remove all groups + { + reset(); + } + else + { + size_type number_of_full_groups_needed = reserve_amount / PLF_LIST_BLOCK_MAX; + group_size_type remainder = static_cast(reserve_amount - (number_of_full_groups_needed * PLF_LIST_BLOCK_MAX)); + + // Remove any max_size groups which're not needed and any groups that're smaller than remainder: + for (group_pointer_type current_group = groups.block_pointer; current_group < groups.block_pointer + groups.size;) + { + const group_size_type current_group_size = static_cast(groups.block_pointer->beyond_end - groups.block_pointer->nodes); + + if (number_of_full_groups_needed != 0 && current_group_size == PLF_LIST_BLOCK_MAX) + { + --number_of_full_groups_needed; + ++current_group; + } + else if (remainder != 0 && current_group_size >= remainder) + { + remainder = 0; + ++current_group; + } + else + { + groups.remove(current_group); + } + } + + last_endpoint = groups.block_pointer->nodes; + } + } + + reserve_amount -= groups.element_allocator_pair.capacity; + + // To correct from possible reallocation caused by add_new: + const difference_type last_endpoint_group_number = groups.last_endpoint_group - groups.block_pointer; + + size_type number_of_full_groups = (reserve_amount / PLF_LIST_BLOCK_MAX); + reserve_amount -= (number_of_full_groups++ * PLF_LIST_BLOCK_MAX); // ++ to aid while loop below + + if (groups.block_pointer == NULL) // Previously uninitialized list or reset in above if statement; most common scenario + { + if (reserve_amount != 0) + { + groups.initialize(static_cast(((reserve_amount < PLF_LIST_BLOCK_MIN) ? PLF_LIST_BLOCK_MIN : reserve_amount))); + } + else + { + groups.initialize(PLF_LIST_BLOCK_MAX); + --number_of_full_groups; + } + } + else if (reserve_amount != 0) + { // Create a group at least as large as the last group - may allocate more than necessary, but better solution than creating a veyr small group in the middle of the group vector, I think: + const group_size_type last_endpoint_group_capacity = static_cast(groups.last_endpoint_group->beyond_end - groups.last_endpoint_group->nodes); + groups.add_new(static_cast((reserve_amount < last_endpoint_group_capacity) ? last_endpoint_group_capacity : reserve_amount)); + } + + while (--number_of_full_groups != 0) + { + groups.add_new(PLF_LIST_BLOCK_MAX); + } + + groups.last_endpoint_group = groups.block_pointer + last_endpoint_group_number; + } + + + + inline PLF_LIST_FORCE_INLINE void free_unused_memory() PLF_LIST_NOEXCEPT + { + groups.trim_trailing_groups(); + } + + + + void shrink_to_fit() + { + if ((last_endpoint == NULL) | (node_pointer_allocator_pair.total_number_of_elements == groups.element_allocator_pair.capacity)) // uninitialized list or full + { + return; + } + else if (node_pointer_allocator_pair.total_number_of_elements == 0) // Edge case + { + reset(); + return; + } + else if (node_allocator_pair.number_of_erased_nodes == 0 && last_endpoint == groups.last_endpoint_group->beyond_end) //edge case - currently no waste except for possible trailing groups + { + groups.trim_trailing_groups(); + return; + } + + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + list temp; + temp.reserve(node_pointer_allocator_pair.total_number_of_elements); + + #ifdef PLF_LIST_TYPE_TRAITS_SUPPORT + if PLF_LIST_CONSTEXPR (std::is_move_assignable::value && std::is_move_constructible::value) // move elements if possible, otherwise copy them + { + temp.insert(temp.end_iterator, std::make_move_iterator(begin_iterator), std::make_move_iterator(end_iterator)); + } + else + #endif + { + temp.insert(temp.end_iterator, begin_iterator, end_iterator); + } + + *this = std::move(temp); + #else + list temp(*this); + reset(); + swap(temp); + #endif + } + + + +private: + + void append_process(list &source) // used by merge and splice + { + if (last_endpoint != groups.last_endpoint_group->beyond_end) + { // Add unused nodes to group's free list + const node_pointer_type back_node = last_endpoint - 1; + for (node_pointer_type current_node = groups.last_endpoint_group->beyond_end - 1; current_node != back_node; --current_node) + { + current_node->next = NULL; + current_node->previous = groups.last_endpoint_group->free_list_head; + groups.last_endpoint_group->free_list_head = current_node; + } + + node_allocator_pair.number_of_erased_nodes += static_cast(groups.last_endpoint_group->beyond_end - last_endpoint); + } + + groups.append(source.groups); + last_endpoint = source.last_endpoint; + node_pointer_allocator_pair.total_number_of_elements += source.node_pointer_allocator_pair.total_number_of_elements; + source.reset(); + } + + + + +public: + + void splice(iterator position, list &source) + { + assert(&source != this); + + if (source.node_pointer_allocator_pair.total_number_of_elements == 0) + { + return; + } + else if (node_pointer_allocator_pair.total_number_of_elements == 0) + { + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + *this = std::move(source); + #else + reset(); + swap(source); + #endif + + return; + } + + if (position.node_pointer == begin_iterator.node_pointer) // put source groups at front rather than back + { + swap(source); + position.node_pointer = end_iterator.node_pointer; + } + + position.node_pointer->previous->next = source.begin_iterator.node_pointer; + source.begin_iterator.node_pointer->previous = position.node_pointer->previous; + position.node_pointer->previous = source.end_node.previous; + source.end_node.previous->next = position.node_pointer; + + append_process(source); + } + + + + template + void merge(list &source, comparison_function compare) + { + assert(&source != this); + splice((source.node_pointer_allocator_pair.total_number_of_elements >= node_pointer_allocator_pair.total_number_of_elements) ? end_iterator : begin_iterator, source); + sort(compare); + } + + + + void merge(list &source) + { + assert(&source != this); + + if (source.node_pointer_allocator_pair.total_number_of_elements == 0) + { + return; + } + else if (node_pointer_allocator_pair.total_number_of_elements == 0) + { + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + *this = std::move(source); + #else + reset(); + swap(source); + #endif + + return; + } + + node_pointer_type current1 = begin_iterator.node_pointer->next, current2 = source.begin_iterator.node_pointer->next; + node_pointer_type previous = source.begin_iterator.node_pointer; + const node_pointer_type source_end = source.end_iterator.node_pointer, this_end = end_iterator.node_pointer; + + begin_iterator.node_pointer->next = source.begin_iterator.node_pointer; + source.begin_iterator.node_pointer->previous = begin_iterator.node_pointer; + + + while ((current1 != this_end) & (current2 != source_end)) + { + previous->next = current1; + current1->previous = previous; + previous = current1; + current1 = current1->next; + + previous->next = current2; + current2->previous = previous; + previous = current2; + current2 = current2->next; + } + + if (current1 != this_end) + { + previous->next = current1; + current1->previous = previous; + } + else + { + end_node.previous = source.end_node.previous; + source.end_node.previous->next = end_iterator.node_pointer; + } + + append_process(source); + } + + + + void reverse() PLF_LIST_NOEXCEPT + { + if (node_pointer_allocator_pair.total_number_of_elements > 1) + { + for (group_pointer_type current_group = groups.block_pointer; current_group != groups.last_endpoint_group; ++current_group) + { + const node_pointer_type end = current_group->beyond_end; + + for (node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node) + { + if (current_node->next != NULL) // is not free list node + { // swap the pointers: + const node_pointer_type temp = current_node->next; + current_node->next = current_node->previous; + current_node->previous = temp; + } + } + } + + for (node_pointer_type current_node = groups.last_endpoint_group->nodes; current_node != last_endpoint; ++current_node) + { + if (current_node->next != NULL) + { + const node_pointer_type temp = current_node->next; + current_node->next = current_node->previous; + current_node->previous = temp; + } + } + + const node_pointer_type temp = end_node.previous; + end_node.previous = begin_iterator.node_pointer; + begin_iterator.node_pointer = temp; + + end_node.previous->next = end_iterator.node_pointer; + begin_iterator.node_pointer->previous = end_iterator.node_pointer; + } + } + + + +private: + + // Used by unique() + struct eq + { + inline bool operator() (const element_type &a, const element_type &b) const PLF_LIST_NOEXCEPT + { + return a == b; + } + }; + + + + // Used by remove() + struct eq_to + { + const element_type value; + + explicit eq_to(const element_type store_value): + value(store_value) + {} + + eq_to() PLF_LIST_NOEXCEPT + {} + + inline bool operator() (const element_type compare_value) const PLF_LIST_NOEXCEPT + { + return value == compare_value; + } + }; + + + +public: + + template + size_type unique(comparison_function compare) + { + const size_type original_number_of_elements = node_pointer_allocator_pair.total_number_of_elements; + + if (original_number_of_elements > 1) + { + element_type *previous = &(begin_iterator.node_pointer->element); + + for (iterator current = ++iterator(begin_iterator); current != end_iterator;) + { + if (compare(*current, *previous)) + { + current = erase(current); + } + else + { + previous = &(current++.node_pointer->element); + } + } + } + + return original_number_of_elements - node_pointer_allocator_pair.total_number_of_elements; + } + + + + inline size_type unique() + { + return unique(eq()); + } + + + + template + size_type remove_if(predicate_function predicate) + { + const size_type original_number_of_elements = node_pointer_allocator_pair.total_number_of_elements; + + if (original_number_of_elements != 0) + { + for (group_pointer_type current_group = groups.block_pointer; current_group != groups.last_endpoint_group; ++current_group) + { + group_size_type num_elements = current_group->number_of_elements; + const node_pointer_type end = current_group->beyond_end; + + if (end - current_group->nodes != num_elements) // If there are erased nodes present in the group + { + for (node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node) + { + if (current_node->next != NULL && predicate(current_node->element)) // is not free list node and validates predicate + { + erase(current_node); + + if (--num_elements == 0) // ie. group will be empty (and removed) now - nothing left to iterate over + { + --current_group; // As current group has been removed, subsequent groups have already shifted back by one, hence, the ++ to the current group in the for loop is unnecessary, and negated here + break; + } + } + } + } + else // No erased nodes in group + { + for (node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node) + { + if (predicate(current_node->element)) + { + erase(current_node); + + if (--num_elements == 0) + { + --current_group; + break; + } + } + } + } + } + + group_size_type num_elements = groups.last_endpoint_group->number_of_elements; + + if (last_endpoint - groups.last_endpoint_group->nodes != num_elements) // If there are erased nodes present in the group + { + for (node_pointer_type current_node = groups.last_endpoint_group->nodes; current_node != last_endpoint; ++current_node) + { + if (current_node->next != NULL && predicate(current_node->element)) + { + erase(current_node); + + if (--num_elements == 0) + { + break; + } + } + } + } + else + { + for (node_pointer_type current_node = groups.last_endpoint_group->nodes; current_node != last_endpoint; ++current_node) + { + if (predicate(current_node->element)) + { + erase(current_node); + + if (--num_elements == 0) + { + break; + } + } + } + } + } + + return original_number_of_elements - node_pointer_allocator_pair.total_number_of_elements; + } + + + + inline size_type remove(const element_type &value) + { + return remove_if(eq_to(value)); + } + + + + void resize(const size_type number_of_elements, const element_type &value = element_type()) + { + if (node_pointer_allocator_pair.total_number_of_elements == number_of_elements) + { + return; + } + else if (number_of_elements == 0) + { + clear(); + return; + } + else if (node_pointer_allocator_pair.total_number_of_elements < number_of_elements) + { + insert(end_iterator, number_of_elements - node_pointer_allocator_pair.total_number_of_elements, value); + } + else // ie. node_pointer_allocator_pair.total_number_of_elements > number_of_elements + { + iterator current(end_node.previous); + + for (size_type number_to_remove = node_pointer_allocator_pair.total_number_of_elements - number_of_elements; number_to_remove != 0; --number_to_remove) + { + const node_pointer_type temp = current.node_pointer->previous; + erase(current); + current.node_pointer = temp; + } + } + } + + + + // Range assign: + template + inline void assign(const typename plf_enable_if_c::is_integer, iterator_type>::type first, const iterator_type last) + { + clear(); + insert(end_iterator, first, last); + groups.trim_trailing_groups(); + } + + + + // Fill assign: + inline void assign(const size_type number_of_elements, const element_type &value) + { + clear(); + reserve(number_of_elements); // Will return anyway if capacity already > number_of_elements + insert(end_iterator, number_of_elements, value); + } + + + + #ifdef PLF_LIST_INITIALIZER_LIST_SUPPORT + // Initializer-list assign: + inline void assign(const std::initializer_list &element_list) + { + clear(); + reserve(element_list.size()); + insert(end_iterator, element_list); + } + #endif + + + + inline allocator_type get_allocator() const PLF_LIST_NOEXCEPT + { + return element_allocator_type(); + } + + + + void swap(list &source) PLF_LIST_NOEXCEPT_SWAP(allocator_type) + { + #ifdef PLF_LIST_MOVE_SEMANTICS_SUPPORT + list temp(std::move(source)); + source = std::move(*this); + *this = std::move(temp); + #else + groups.swap(source.groups); + + const node_pointer_type swap_end_node_previous = end_node.previous, swap_last_endpoint = last_endpoint; + const iterator swap_begin_iterator = begin_iterator; + const size_type swap_total_number_of_elements = node_pointer_allocator_pair.total_number_of_elements, swap_number_of_erased_nodes = node_allocator_pair.number_of_erased_nodes; + + last_endpoint = source.last_endpoint; + end_node.next = begin_iterator.node_pointer = (source.begin_iterator.node_pointer != source.end_iterator.node_pointer) ? source.begin_iterator.node_pointer : end_iterator.node_pointer; + end_node.previous = (source.begin_iterator.node_pointer != source.end_iterator.node_pointer) ? source.end_node.previous : end_iterator.node_pointer; + end_node.previous->next = begin_iterator.node_pointer->previous = end_iterator.node_pointer; + node_pointer_allocator_pair.total_number_of_elements = source.node_pointer_allocator_pair.total_number_of_elements; + node_allocator_pair.number_of_erased_nodes = source.node_allocator_pair.number_of_erased_nodes; + + source.last_endpoint = swap_last_endpoint; + source.end_node.next = source.begin_iterator.node_pointer = (swap_begin_iterator.node_pointer != end_iterator.node_pointer) ? swap_begin_iterator.node_pointer : source.end_iterator.node_pointer; + source.end_node.previous = (swap_begin_iterator.node_pointer != end_iterator.node_pointer) ? swap_end_node_previous : source.end_iterator.node_pointer; + source.end_node.previous->next = source.begin_iterator.node_pointer->previous = source.end_iterator.node_pointer; + source.node_pointer_allocator_pair.total_number_of_elements = swap_total_number_of_elements; + source.node_allocator_pair.number_of_erased_nodes = swap_number_of_erased_nodes; + #endif + } + +}; + + + +template +inline void swap(list &a, list &b) PLF_LIST_NOEXCEPT_SWAP(swap_element_allocator_type) +{ + a.swap(b); +} + + + +} // plf namespace + +#undef PLF_LIST_BLOCK_MAX +#undef PLF_LIST_BLOCK_MIN + +#undef PLF_LIST_FORCE_INLINE + +#undef PLF_LIST_INITIALIZER_LIST_SUPPORT +#undef PLF_LIST_TYPE_TRAITS_SUPPORT +#undef PLF_LIST_ALLOCATOR_TRAITS_SUPPORT +#undef PLF_LIST_VARIADICS_SUPPORT +#undef PLF_LIST_MOVE_SEMANTICS_SUPPORT +#undef PLF_LIST_NOEXCEPT +#undef PLF_LIST_NOEXCEPT_SWAP +#undef PLF_LIST_NOEXCEPT_MOVE_ASSIGNMENT +#undef PLF_LIST_CONSTEXPR + +#undef PLF_LIST_CONSTRUCT +#undef PLF_LIST_DESTROY +#undef PLF_LIST_ALLOCATE +#undef PLF_LIST_ALLOCATE_INITIALIZATION +#undef PLF_LIST_DEALLOCATE + + +#endif // PLF_LIST_H diff --git a/tests/npc_test.cpp b/tests/npc_test.cpp index 0165a2290b2..2614ce3bade 100644 --- a/tests/npc_test.cpp +++ b/tests/npc_test.cpp @@ -42,7 +42,6 @@ #include "item_group.h" #include "item_location.h" #include "itype.h" -#include "list.h" #include "map.h" #include "map_helpers.h" #include "mapdata.h" diff --git a/tests/test_main.cpp b/tests/test_main.cpp index 3df7533d401..9d3c03495fe 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -325,6 +325,13 @@ TEST_CASE( "noop_test", "[.]" ) // NOLINTNEXTLINE(bugprone-exception-escape): test runner intentionally lets exceptions propagate int main( int argc, const char *argv[] ) { +#ifdef _WIN32 + // Suppress the "Microsoft Visual C++ Runtime Library" abort dialog so that + // a raw assert() in any third-party header (e.g. plf colony/list, flatbuffers) + // cannot hang headless CI test runs. The Cataclysm test vcxproj does not + // define NDEBUG for Release, so asserts are live in CI binaries. + _set_abort_behavior( 0, _CALL_REPORTFAULT | _WRITE_ABORT_MSG ); +#endif cata::init_allocator(); #if defined(_MSC_VER) bool supports_color = _isatty( _fileno( stdout ) );