diff --git a/include/amc/fixedcapacityvector.hpp b/include/amc/fixedcapacityvector.hpp index b6ab6af..748c885 100644 --- a/include/amc/fixedcapacityvector.hpp +++ b/include/amc/fixedcapacityvector.hpp @@ -6,6 +6,7 @@ #include #include +#include "config.hpp" #include "vectorcommon.hpp" namespace amc { diff --git a/include/amc/flatset.hpp b/include/amc/flatset.hpp index a5473df..111247a 100644 --- a/include/amc/flatset.hpp +++ b/include/amc/flatset.hpp @@ -2,11 +2,9 @@ #include #include -#include #include #include #include -#include #include #include "allocator.hpp" @@ -80,6 +78,8 @@ class FlatSet : private Compare { node_type &operator=(const node_type &) = delete; node_type &operator=(node_type &&o) noexcept(std::is_nothrow_move_assignable::value) = default; + ~node_type() = default; + bool empty() const noexcept { return !_optV.has_value(); } explicit operator bool() const noexcept { return _optV.has_value(); } allocator_type get_allocator() const { return static_cast(*this); } @@ -299,7 +299,7 @@ class FlatSet : private Compare { bool operator!=(const FlatSet &o) const { return !(*this == o); } #ifdef AMC_CXX20 - auto operator<=>(const FlatSet &o) const { return _sortedVector <=> o._sortedVector; } + auto operator<=>(const FlatSet &o) const = default; #else bool operator<(const FlatSet &o) const { return _sortedVector < o._sortedVector; } diff --git a/include/amc/smallset.hpp b/include/amc/smallset.hpp index 0a1378e..b13c1aa 100644 --- a/include/amc/smallset.hpp +++ b/include/amc/smallset.hpp @@ -71,7 +71,7 @@ class SmallSetIteratorCommon { using pointer = const T *; using reference = const T &; - // For a set, only equality and unequality operators are defined + // For a set, only equality and un-equality operators are defined #ifdef AMC_CXX20 bool operator==(const SmallSetIteratorCommon &) const noexcept = default; #else @@ -469,29 +469,29 @@ class SmallSet { grow(); } _set.merge(o._set); - } else { - bool small = isSmall(); - for (auto oit = o._vec.begin(); oit != o._vec.end();) { - FindFunctor fFunc(key_comp(), *oit); - if (small) { - if (std::none_of(_vec.begin(), _vec.end(), fFunc)) { - if (isSmallContFull()) { - grow(); - small = false; - _set.insert(std::move(*oit)); - } else { - _vec.push_back(std::move(*oit)); - } - oit = o._vec.erase(oit); + return; + } + bool small = isSmall(); + for (auto oit = o._vec.begin(); oit != o._vec.end();) { + FindFunctor fFunc(key_comp(), *oit); + if (small) { + if (std::none_of(_vec.begin(), _vec.end(), fFunc)) { + if (isSmallContFull()) { + grow(); + small = false; + _set.insert(std::move(*oit)); } else { - ++oit; + _vec.push_back(std::move(*oit)); } + oit = o._vec.erase(oit); } else { - if (_set.insert(std::move(*oit)).second) { - oit = o._vec.erase(oit); - } else { - ++oit; - } + ++oit; + } + } else { + if (_set.insert(std::move(*oit)).second) { + oit = o._vec.erase(oit); + } else { + ++oit; } } } diff --git a/include/amc/vectorcommon.hpp b/include/amc/vectorcommon.hpp index e573c11..bc5f8ec 100644 --- a/include/amc/vectorcommon.hpp +++ b/include/amc/vectorcommon.hpp @@ -69,7 +69,7 @@ inline void shift_right(T *first, SizeType n, SizeType count) noexcept { (void)amc::uninitialized_relocate_n(first, n, first + count); } -/// Fill 'count' 'v' values at memory starting at 'first', with first 'n' slots on inintialized memory, +/// Fill 'count' 'v' values at memory starting at 'first', with first 'n' slots on initialized memory, /// and next 'count - n' slots on uninitialized memory if there is overlap template ::value, bool>::type = true> inline void fill_after_shift(T *first, SizeType n, SizeType count, const T &v) { @@ -110,7 +110,7 @@ inline void assign_n(ForwardIt first, SizeType count, T *d_first, SizeType) { } /// Copy 'count' elements starting at 'first' to 'pos' location -/// To be used in conjonction with 'shift_right' +/// To be used in conjunction with 'shift_right' template ::value, bool>::type = true> inline void copy_after_shift(ForwardIt first, SizeType n, SizeType count, T *pos) { @@ -184,7 +184,7 @@ inline void erase_at(T *first, SizeType count) { /// Requirements: n < count template ::value, bool>::type = true> inline void fill(T *first, SizeType n, SizeType count, const T &v) { - // uninitialize fill first for slightly better exception safety + // uninitialized fill first for slightly better exception safety std::uninitialized_fill_n(first + n, count - n, v); std::fill_n(first, n, v); } @@ -517,7 +517,7 @@ class StdVectorBase : private Alloc { void move_assign(StdVectorBase &o, SizeType) noexcept { if (_storage) { amc::destroy_n(_storage, _size); - freeStorage(); // Compared to swap, we can free memory directly for move assigment + freeStorage(); // Compared to swap, we can free memory directly for move assignment } _storage = amc::exchange(o._storage, nullptr); _capa = amc::exchange(o._capa, 0); @@ -566,11 +566,12 @@ class StdVectorBase : private Alloc { iterator dynStorage() const noexcept { return _storage; } private: - SizeType _capa = 0, _size = 0; - T *_storage = nullptr; - void shrink(); void freeStorage() noexcept; + + SizeType _capa = 0; + SizeType _size = 0; + T *_storage = nullptr; }; template @@ -619,7 +620,7 @@ class SmallVectorBase : private Alloc { /// for this special configuration: we will set size to MAX in this case. /// Maximum size and capacity would make no sense in a SmallVector for a small state, because it could not grow /// (it could be transformed into a FixedCapacityVector, or, if larger size is needed, SizeType could be upgraded - /// to a larger type). This invalid configuration is catched in a static_assert in SmallVector class. + /// to a larger type). This invalid configuration is caught in a static_assert in SmallVector class. explicit SmallVectorBase(SizeType inplaceCapa) noexcept : _capa(0), _size(inplaceCapa) {} SmallVectorBase(SizeType inplaceCapa, const Alloc &alloc) noexcept : Alloc(alloc), _capa(0), _size(inplaceCapa) {} @@ -774,9 +775,6 @@ class SmallVectorBase : private Alloc { iterator dynStorage() const noexcept { return _storage.dyn(); } private: - SizeType _capa, _size; - ElemWithPtrStorage _storage; - static inline void SwapDynamicBuffer(SmallVectorBase &vDynBuf, SmallVectorBase &vSmall) noexcept(is_swap_noexcept::value) { T *oDynStorage = vDynBuf._storage.dyn(); @@ -787,6 +785,9 @@ class SmallVectorBase : private Alloc { void shrink(); void resetToSmall(SizeType); void freeStorage() noexcept; + + SizeType _capa, _size; + ElemWithPtrStorage _storage; }; template diff --git a/test/sets_test.cpp b/test/sets_test.cpp index c50be07..6187895 100644 --- a/test/sets_test.cpp +++ b/test/sets_test.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #ifdef AMC_SMALLSET #include @@ -68,8 +67,10 @@ TYPED_TEST(SetListTest, Insert) { TypeParam s; s.insert(8); s.insert(15); + s.insert(8); EXPECT_EQ(s.size(), 2U); EXPECT_FALSE(s.empty()); + EXPECT_TRUE(s.contains(15)); } TYPED_TEST(SetListTest, Emplace) { diff --git a/test/testtypes.hpp b/test/testtypes.hpp index 6946b18..cf010d5 100644 --- a/test/testtypes.hpp +++ b/test/testtypes.hpp @@ -78,7 +78,7 @@ struct NonCopyableType { bool operator==(const NonCopyableType &o) const { return _i == o._i; } #ifdef AMC_CXX20 - auto operator<=>(const NonCopyableType &o) const { return _i <=> o._i; } + auto operator<=>(const NonCopyableType &o) const = default; #endif operator int32_t() const { return _i; } @@ -86,6 +86,8 @@ struct NonCopyableType { int _i; }; +static_assert(!is_trivially_relocatable_v); + struct SimpleNonTriviallyCopyableType { SimpleNonTriviallyCopyableType(int i = 7) : _i(i) {} @@ -96,7 +98,7 @@ struct SimpleNonTriviallyCopyableType { operator int32_t() const { return _i; } #ifdef AMC_CXX20 - auto operator<=>(const SimpleNonTriviallyCopyableType &o) const { return _i <=> o._i; } + auto operator<=>(const SimpleNonTriviallyCopyableType &o) const = default; #endif int _i; @@ -108,7 +110,7 @@ struct NonTrivialType { operator uint32_t() const { return _i; } #ifdef AMC_CXX20 - auto operator<=>(const NonTrivialType &o) const { return _i <=> o._i; } + auto operator<=>(const NonTrivialType &o) const = default; #endif uint32_t _i; diff --git a/test/vectors_test.cpp b/test/vectors_test.cpp index addd67b..b8f21ba 100644 --- a/test/vectors_test.cpp +++ b/test/vectors_test.cpp @@ -366,12 +366,15 @@ TEST(VectorTest, CustomOperations) { } TEST(VectorTest, NonCopyableType) { - using NonCopyableTypeVectorType = FixedCapacityVector; + using NonCopyableTypeVectorType = SmallVector; NonCopyableTypeVectorType v(6); EXPECT_EQ(v.front(), NonCopyableType()); EXPECT_EQ(v.back(), NonCopyableType()); v.resize(7); EXPECT_EQ(v[6], NonCopyableType()); + + v.emplace_back(1); + EXPECT_EQ(v.back(), NonCopyableType(1)); } #ifdef AMC_NONSTD_FEATURES