diff --git a/Combinatorial_map/include/CGAL/Cell_attribute.h b/Combinatorial_map/include/CGAL/Cell_attribute.h index e2387d773c3..796b71ba629 100644 --- a/Combinatorial_map/include/CGAL/Cell_attribute.h +++ b/Combinatorial_map/include/CGAL/Cell_attribute.h @@ -26,7 +26,7 @@ template class Concurrent_compact_container; template -class Compact_container_with_index_2; +class Compact_container_with_index; template class Combinatorial_map_storage_1; @@ -105,7 +105,7 @@ struct Init_id; friend class Concurrent_compact_container; template - friend class Compact_container_with_index_2; + friend class Compact_container_with_index; template friend class Combinatorial_map_storage_1; @@ -230,7 +230,7 @@ struct Init_id; friend class Concurrent_compact_container; template - friend class Compact_container_with_index_2; + friend class Compact_container_with_index; template friend class Combinatorial_map_storage_1; @@ -362,7 +362,7 @@ struct Init_id; friend class Concurrent_compact_container; template - friend class Compact_container_with_index_2; + friend class Compact_container_with_index; template friend class Combinatorial_map_storage_1; @@ -417,7 +417,7 @@ struct Init_id; friend class Concurrent_compact_container; template - friend class Compact_container_with_index_2; + friend class Compact_container_with_index; template friend class Combinatorial_map_storage_1; diff --git a/Combinatorial_map/include/CGAL/Compact_container_with_index_2.h b/Combinatorial_map/include/CGAL/Compact_container_with_index_2.h index d3975724d46..37737fcfa09 100644 --- a/Combinatorial_map/include/CGAL/Compact_container_with_index_2.h +++ b/Combinatorial_map/include/CGAL/Compact_container_with_index_2.h @@ -16,6 +16,7 @@ #include #include #include +#include // An STL like container, similar to Compact_container, but uses indices // instead of handles. @@ -83,28 +84,32 @@ namespace internal { // * free elements stored in a data member of T, and used Booleans in its most significant bit // Note that version deque and data member does not exist (no interest?) // By default, use a deque and a vector. -template +template class Free_list_management {}; // Case with a deque for the freelist and a vector for Booleans. -template -class Free_list_management +template +class Free_list_management { - using Self=Free_list_management; - static size_type max_index=std::numeric_limits::max(); + using Self=Free_list_management; + using T=typename CC_with_index::value_type; + using size_type=typename CC_with_index::size_type; public: - bool is_used(const T& /*e*/, size_type i) const + Free_list_management(CC_with_index& cc_with_index): m_cc_with_index(cc_with_index) + {} + + bool is_used(size_type i) const { - CGAL_assertion(i m_free_list; std::vector m_used; size_type m_first_free_index=0; }; // Case with the "in place" free list, and a vector for Booleans. -template -class Free_list_management +template +class Free_list_management { - using Self=Free_list_management; - static size_type max_index=std::numeric_limits::max(); + using Self=Free_list_management; + using T=typename CC_with_index::value_type; + using size_type=typename CC_with_index::size_type; using Traits=Compact_container_with_index_traits ; public: - bool is_used(const T& /*e*/, size_type i) const + Free_list_management(CC_with_index& cc_with_index): m_cc_with_index(cc_with_index) + {} + + bool is_used(size_type i) const { - CGAL_assertion(i m_used; }; // Case with the "in place" free list, and "in place" Booleans. -template -class Free_list_management +template +class Free_list_management { - using Self=Free_list_management; - static size_type max_index=std::numeric_limits::max(); + using Self=Free_list_management; + using T=typename CC_with_index::value_type; + using size_type=typename CC_with_index::size_type; using Traits=Compact_container_with_index_traits ; public: - bool is_used(const T& e, size_type /*i*/) const - { return static_type(e)==USED; } + Free_list_management(CC_with_index& cc_with_index): m_cc_with_index(cc_with_index) + {} - bool is_empty_free_list() const - { return m_free_list==max_index; } + bool is_used(size_type i) const + { return static_type(m_cc_with_index[i])==USED; } - void put_on_free_list(T& e, size_type x) + bool is_empty() const + { return m_free_list==m_cc_with_index.capacity(); } + + void put_on_free_list(size_type i) { - CGAL_assertion(x Compact_container_with_index(InputIterator first, InputIterator last, const Allocator & a = Allocator()) - : alloc(a) + : alloc(a), + free_list(*this) { init(); std::copy(first, last, CGAL::inserter(*this)); @@ -402,7 +423,8 @@ public: // The copy constructor and assignment operator preserve the iterator order Compact_container_with_index(const Compact_container_with_index &c) - : alloc(c.get_allocator()) + : alloc(c.get_allocator()), + free_list(*this) { init(); block_size = c.block_size; @@ -410,7 +432,8 @@ public: } Compact_container_with_index(Compact_container_with_index&& c) noexcept - : alloc(c.get_allocator()) + : alloc(c.get_allocator()), + free_list(*this) { c.swap(*this); } @@ -433,14 +456,10 @@ public: } ~Compact_container_with_index() - { - clear(); - } + { clear(); } bool is_used(size_type i) const - { - return free_list.is_used(operator[](i), i); - } + { return free_list.is_used(i); } const T& operator[] (size_type i) const { @@ -461,6 +480,7 @@ public: std::swap(size_, c.size_); std::swap(block_size, c.block_size); std::swap(all_items, c.all_items); + free_list.swap(c.free_list); } iterator begin() { if(empty()) return end(); return iterator(this, 0, 0); } @@ -508,13 +528,11 @@ public: template < typename... Args > Index emplace(const Args&... args) { - if (free_list == bottom) - allocate_new_block(); + if (free_list.is_empty()) + { allocate_new_block(); } - Index ret = free_list; - T& e = operator[](free_list); - static_set_type(e, USED); - free_list = static_get_val(e); + Index ret=free_list.remove_from_free_list(); + T& e=operator[](ret); //std::allocator_traits::construct(alloc, &e, args...); new (&e) value_type(args...); ++size_; @@ -523,13 +541,11 @@ public: Index insert(const T &t) { - if (free_list == bottom) - allocate_new_block(); + if (free_list.is_empty()) + { allocate_new_block(); } - Index ret(free_list); - T& e = operator[](free_list); - static_set_type(e, USED); - free_list = static_get_val(e); + Index ret=free_list.remove_from_free_list(); + T& e=operator[](ret); //std::allocator_traits::construct(alloc, &e, t); new (&e) value_type(t); ++size_; @@ -550,16 +566,16 @@ public: insert(first, last); } - void erase(Index x) + void erase(Index i) { - CGAL_precondition(type(x) == USED); - T& e = operator[](x); + CGAL_precondition(is_used(i)); + T& e=operator[](i); std::allocator_traits::destroy(alloc, &e); //e.~T(); #ifndef CGAL_NO_ASSERTIONS std::memset(&e, 0, sizeof(T)); #endif - put_on_free_list(x); + put_on_free_list(i); --size_; } @@ -582,21 +598,15 @@ public: } size_type max_size() const - { - return std::allocator_traits::max_size(alloc); - } + { return std::allocator_traits::max_size(alloc); } size_type capacity() const - { - return capacity_; - } + { return capacity_; } // void resize(size_type sz, T c = T()); // TODO makes sense ??? bool empty() const - { - return size_ == 0; - } + { return size_ == 0; } allocator_type get_allocator() const { @@ -679,7 +689,7 @@ private: size_type size_; size_type block_size; pointer all_items; - Free_list_management free_list; + Free_list_management free_list; }; /*template < class T, class Allocator, class Increment_policy, class IndexType > diff --git a/Combinatorial_map/include/CGAL/Dart.h b/Combinatorial_map/include/CGAL/Dart.h index b3022b1ff65..9246004d586 100644 --- a/Combinatorial_map/include/CGAL/Dart.h +++ b/Combinatorial_map/include/CGAL/Dart.h @@ -27,7 +27,7 @@ namespace CGAL { class Concurrent_compact_container; template - class Compact_container_with_index_2; + class Compact_container_with_index; template class Combinatorial_map_storage_1; @@ -82,7 +82,7 @@ namespace CGAL { friend class Concurrent_compact_container; template - friend class Compact_container_with_index_2; + friend class Compact_container_with_index; template friend class Combinatorial_map_storage_1; @@ -245,7 +245,7 @@ namespace CGAL { friend class Concurrent_compact_container; template - friend class Compact_container_with_index_2; + friend class Compact_container_with_index; template friend class Combinatorial_map_storage_1;