From 33fd5f964a6ed2efcecb09c02124a747743d060c Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sat, 4 May 2019 10:17:52 +0700 Subject: [PATCH] add set and map ctors --- flat_map.hpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++ flat_map_tests.cpp | 51 ++++++++++++++++++++++++++-- flat_set.hpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++ flat_set_tests.cpp | 45 +++++++++++++++++++++++-- 4 files changed, 256 insertions(+), 4 deletions(-) diff --git a/flat_map.hpp b/flat_map.hpp index 8e03418..ac75aa1 100644 --- a/flat_map.hpp +++ b/flat_map.hpp @@ -9,8 +9,11 @@ #include #include #include +#include +#include #include #include +#include namespace flat_hpp { @@ -40,5 +43,84 @@ namespace flat_hpp using const_iterator = typename data_type::const_iterator; using reverse_iterator = typename data_type::reverse_iterator; using const_reverse_iterator = typename data_type::const_reverse_iterator; + + static_assert( + std::is_same::value, + "Allocator::value_type must be same type as value_type"); + public: + explicit flat_map( + const Allocator& a) + : data_(a) {} + + explicit flat_map( + const Compare& c = Compare(), + const Allocator& a = Allocator()) + : data_(a) + , compare_(c) {} + + template < typename InputIter > + flat_map( + InputIter first, + InputIter last, + const Allocator& a) + : data_(a) { + insert(first, last); + } + + template < typename InputIter > + flat_map( + InputIter first, + InputIter last, + const Compare& c = Compare(), + const Allocator& a = Allocator()) + : data_(a) + , compare_(c) { + insert(first, last); + } + + flat_map( + std::initializer_list il, + const Allocator& a) + : data_(a) { + insert(il.begin(), il.end()); + } + + flat_map( + std::initializer_list il, + const Compare& c = Compare(), + const Allocator& a = Allocator()) + : data_(a) + , compare_(c) { + insert(il.begin(), il.end()); + } + + iterator begin() noexcept { return data_.begin(); } + const_iterator begin() const noexcept { return data_.begin(); } + iterator end() noexcept { return data_.end(); } + const_iterator end() const noexcept { return data_.end(); } + reverse_iterator rbegin() noexcept { return data_.rbegin(); } + const_reverse_iterator rbegin() const noexcept { return data_.rbegin(); } + reverse_iterator rend() noexcept { return data_.rend(); } + const_reverse_iterator rend() const noexcept { return data_.rend(); } + + std::pair insert(value_type&& value) { + //TODO(BlackMat): implme + return std::make_pair(end(), false); + } + + std::pair insert(const value_type& value) { + //TODO(BlackMat): implme + return std::make_pair(end(), false); + } + + template < typename InputIter > + void insert(InputIter first, InputIter last) { + for ( auto iter = first; iter != last; ++iter ) { + insert(*iter); + } + } + private: + data_type data_; + key_compare compare_; }; } diff --git a/flat_map_tests.cpp b/flat_map_tests.cpp index 2a51191..aa0b702 100644 --- a/flat_map_tests.cpp +++ b/flat_map_tests.cpp @@ -8,15 +8,30 @@ #include "catch.hpp" #include "flat_map.hpp" -namespace flat = flat_hpp; +using namespace flat_hpp; namespace { + template < typename T > + class dummy_allocator { + public: + using value_type = T; + + T* allocate(std::size_t n) { + (void)n; + return nullptr; + } + + void deallocate(T* p, std::size_t n) { + (void)p; + (void)n; + } + }; } TEST_CASE("flat_map") { { - using map_t = flat::flat_map; + using map_t = flat_map; static_assert( std::is_same::value, @@ -49,4 +64,36 @@ TEST_CASE("flat_map") { std::is_same*>::value, "unit test static error"); } + { + using alloc_t = dummy_allocator< + std::pair>; + + using map_t = flat_map< + int, + unsigned, + std::less, + alloc_t>; + + { + auto s0 = map_t(); + auto s1 = map_t(alloc_t()); + auto s2 = map_t(std::less()); + auto s3 = map_t(std::less(), alloc_t()); + } + + { + std::vector> v; + auto s0 = map_t(v.cbegin(), v.cend()); + auto s1 = map_t(v.cbegin(), v.cend(), alloc_t()); + auto s2 = map_t(v.cbegin(), v.cend(), std::less()); + auto s3 = map_t(v.cbegin(), v.cend(), std::less(), alloc_t()); + } + + { + auto s0 = map_t({{0,1}, {1,2}}); + auto s1 = map_t({{0,1}, {1,2}}, alloc_t()); + auto s2 = map_t({{0,1}, {1,2}}, std::less()); + auto s3 = map_t({{0,1}, {1,2}}, std::less(), alloc_t()); + } + } } diff --git a/flat_set.hpp b/flat_set.hpp index f132a1a..a6d1267 100644 --- a/flat_set.hpp +++ b/flat_set.hpp @@ -9,8 +9,11 @@ #include #include #include +#include +#include #include #include +#include namespace flat_hpp { @@ -39,5 +42,84 @@ namespace flat_hpp using const_iterator = typename data_type::const_iterator; using reverse_iterator = typename data_type::reverse_iterator; using const_reverse_iterator = typename data_type::const_reverse_iterator; + + static_assert( + std::is_same::value, + "Allocator::value_type must be same type as value_type"); + public: + explicit flat_set( + const Allocator& a) + : data_(a) {} + + explicit flat_set( + const Compare& c = Compare(), + const Allocator& a = Allocator()) + : data_(a) + , compare_(c) {} + + template < typename InputIter > + flat_set( + InputIter first, + InputIter last, + const Allocator& a) + : data_(a) { + insert(first, last); + } + + template < typename InputIter > + flat_set( + InputIter first, + InputIter last, + const Compare& c = Compare(), + const Allocator& a = Allocator()) + : data_(a) + , compare_(c) { + insert(first, last); + } + + flat_set( + std::initializer_list il, + const Allocator& a) + : data_(a) { + insert(il.begin(), il.end()); + } + + flat_set( + std::initializer_list il, + const Compare& c = Compare(), + const Allocator& a = Allocator()) + : data_(a) + , compare_(c) { + insert(il.begin(), il.end()); + } + + iterator begin() noexcept { return data_.begin(); } + const_iterator begin() const noexcept { return data_.begin(); } + iterator end() noexcept { return data_.end(); } + const_iterator end() const noexcept { return data_.end(); } + reverse_iterator rbegin() noexcept { return data_.rbegin(); } + const_reverse_iterator rbegin() const noexcept { return data_.rbegin(); } + reverse_iterator rend() noexcept { return data_.rend(); } + const_reverse_iterator rend() const noexcept { return data_.rend(); } + + std::pair insert(value_type&& value) { + //TODO(BlackMat): implme + return std::make_pair(end(), false); + } + + std::pair insert(const value_type& value) { + //TODO(BlackMat): implme + return std::make_pair(end(), false); + } + + template < typename InputIter > + void insert(InputIter first, InputIter last) { + for ( auto iter = first; iter != last; ++iter ) { + insert(*iter); + } + } + private: + data_type data_; + key_compare compare_; }; } diff --git a/flat_set_tests.cpp b/flat_set_tests.cpp index 284ee7a..e0ca0f7 100644 --- a/flat_set_tests.cpp +++ b/flat_set_tests.cpp @@ -8,15 +8,30 @@ #include "catch.hpp" #include "flat_set.hpp" -namespace flat = flat_hpp; +using namespace flat_hpp; namespace { + template < typename T > + class dummy_allocator { + public: + using value_type = T; + + T* allocate(std::size_t n) { + (void)n; + return nullptr; + } + + void deallocate(T* p, std::size_t n) { + (void)p; + (void)n; + } + }; } TEST_CASE("flat_set") { { - using set_t = flat::flat_set; + using set_t = flat_set; static_assert( std::is_same::value, @@ -46,4 +61,30 @@ TEST_CASE("flat_set") { std::is_same::value, "unit test static error"); } + { + using alloc_t = dummy_allocator; + using set_t = flat_set, alloc_t>; + + { + auto s0 = set_t(); + auto s1 = set_t(alloc_t()); + auto s2 = set_t(std::less()); + auto s3 = set_t(std::less(), alloc_t()); + } + + { + std::vector v; + auto s0 = set_t(v.cbegin(), v.cend()); + auto s1 = set_t(v.cbegin(), v.cend(), alloc_t()); + auto s2 = set_t(v.cbegin(), v.cend(), std::less()); + auto s3 = set_t(v.cbegin(), v.cend(), std::less(), alloc_t()); + } + + { + auto s0 = set_t({0,1,2}); + auto s1 = set_t({0,1,2}, alloc_t()); + auto s2 = set_t({0,1,2}, std::less()); + auto s3 = set_t({0,1,2}, std::less(), alloc_t()); + } + } }