Commit dcb399d0 authored by Mark Santaniello's avatar Mark Santaniello Committed by Facebook Github Bot

pmr aliases for sorted_vector_types

Summary: Add convenience aliases for std::pmr versions of folly::sorted_vector_set and folly::sorted_vector_map.  I also add the traditional one-argument constructor taking the allocator.

Reviewed By: yfeldblum

Differential Revision: D16523576

fbshipit-source-id: 33514db62eb8dc1b603f45204977751091e528d9
parent 62a3a550
...@@ -75,6 +75,7 @@ ...@@ -75,6 +75,7 @@
#include <folly/Traits.h> #include <folly/Traits.h>
#include <folly/Utility.h> #include <folly/Utility.h>
#include <folly/lang/Exception.h> #include <folly/lang/Exception.h>
#include <folly/memory/MemoryResource.h>
namespace folly { namespace folly {
...@@ -283,6 +284,8 @@ class sorted_vector_set : detail::growth_policy_wrapper<GrowthPolicy> { ...@@ -283,6 +284,8 @@ class sorted_vector_set : detail::growth_policy_wrapper<GrowthPolicy> {
sorted_vector_set() : m_(Compare(), Allocator()) {} sorted_vector_set() : m_(Compare(), Allocator()) {}
explicit sorted_vector_set(const Allocator& alloc) : m_(Compare(), alloc) {}
explicit sorted_vector_set( explicit sorted_vector_set(
const Compare& comp, const Compare& comp,
const Allocator& alloc = Allocator()) const Allocator& alloc = Allocator())
...@@ -651,6 +654,27 @@ inline void swap( ...@@ -651,6 +654,27 @@ inline void swap(
return a.swap(b); return a.swap(b);
} }
#if FOLLY_HAS_MEMORY_RESOURCE
namespace pmr {
template <
class T,
class Compare = std::less<T>,
class GrowthPolicy = void,
class Container =
std::vector<T, folly::detail::std_pmr::polymorphic_allocator<T>>>
using sorted_vector_set = folly::sorted_vector_set<
T,
Compare,
folly::detail::std_pmr::polymorphic_allocator<T>,
GrowthPolicy,
Container>;
} // namespace pmr
#endif
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
/** /**
...@@ -714,6 +738,9 @@ class sorted_vector_map : detail::growth_policy_wrapper<GrowthPolicy> { ...@@ -714,6 +738,9 @@ class sorted_vector_map : detail::growth_policy_wrapper<GrowthPolicy> {
sorted_vector_map() : m_(value_compare(Compare()), Allocator()) {} sorted_vector_map() : m_(value_compare(Compare()), Allocator()) {}
explicit sorted_vector_map(const Allocator& alloc)
: m_(value_compare(Compare()), alloc) {}
explicit sorted_vector_map( explicit sorted_vector_map(
const Compare& comp, const Compare& comp,
const Allocator& alloc = Allocator()) const Allocator& alloc = Allocator())
...@@ -1122,6 +1149,30 @@ inline void swap( ...@@ -1122,6 +1149,30 @@ inline void swap(
return a.swap(b); return a.swap(b);
} }
#if FOLLY_HAS_MEMORY_RESOURCE
namespace pmr {
template <
class Key,
class Value,
class Compare = std::less<Key>,
class GrowthPolicy = void,
class Container = std::vector<
std::pair<Key, Value>,
folly::detail::std_pmr::polymorphic_allocator<std::pair<Key, Value>>>>
using sorted_vector_map = folly::sorted_vector_map<
Key,
Value,
Compare,
folly::detail::std_pmr::polymorphic_allocator<std::pair<Key, Value>>,
GrowthPolicy,
Container>;
} // namespace pmr
#endif
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
} // namespace folly } // namespace folly
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <list> #include <list>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector>
#include <folly/Range.h> #include <folly/Range.h>
#include <folly/Utility.h> #include <folly/Utility.h>
...@@ -874,3 +875,32 @@ TEST(SortedVectorTypes, TestEmplaceHint) { ...@@ -874,3 +875,32 @@ TEST(SortedVectorTypes, TestEmplaceHint) {
check_invariant(map); check_invariant(map);
} }
} }
#if FOLLY_HAS_MEMORY_RESOURCE
TEST(SortedVectorTypes, TestPmrAllocatorSimple) {
folly::pmr::sorted_vector_set<std::pair<int, int>> set{
folly::detail::std_pmr::null_memory_resource()};
EXPECT_THROW(set.emplace(42, 42), std::bad_alloc);
folly::pmr::sorted_vector_map<int, int> map{
folly::detail::std_pmr::null_memory_resource()};
EXPECT_THROW(map.emplace(42, 42), std::bad_alloc);
}
TEST(SortedVectorTypes, TestPmrAllocatorScoped) {
using AllocT = folly::detail::std_pmr::polymorphic_allocator<char>;
using VectorT = std::vector<int, AllocT>;
AllocT alloc;
folly::pmr::sorted_vector_set<VectorT> set{alloc};
set.emplace(42);
EXPECT_EQ(set.begin()->get_allocator(), alloc);
folly::pmr::sorted_vector_map<int, VectorT> map{alloc};
map.emplace(42, 42);
EXPECT_EQ(map.begin()->second.get_allocator(), alloc);
}
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment