class T,
class Compare = std::less<T>,
class Allocator = std::allocator<T>,
- class GrowthPolicy = void>
+ class GrowthPolicy = void,
+ class Container = std::vector<T, Allocator>>
class sorted_vector_set
- : boost::totally_ordered1<
- sorted_vector_set<T,Compare,Allocator,GrowthPolicy>
- , detail::growth_policy_wrapper<GrowthPolicy> >
-{
- typedef std::vector<T,Allocator> ContainerT;
-
+ : boost::totally_ordered1<
+ sorted_vector_set<T, Compare, Allocator, GrowthPolicy>,
+ detail::growth_policy_wrapper<GrowthPolicy>> {
detail::growth_policy_wrapper<GrowthPolicy>&
get_growth_policy() { return *this; }
typedef Compare key_compare;
typedef Compare value_compare;
- typedef typename ContainerT::pointer pointer;
- typedef typename ContainerT::reference reference;
- typedef typename ContainerT::const_reference const_reference;
+ typedef typename Container::pointer pointer;
+ typedef typename Container::reference reference;
+ typedef typename Container::const_reference const_reference;
/*
* XXX: Our normal iterator ought to also be a constant iterator
* (cf. Defect Report 103 for std::set), but this is a bit more of a
* pain.
*/
- typedef typename ContainerT::iterator iterator;
- typedef typename ContainerT::const_iterator const_iterator;
- typedef typename ContainerT::difference_type difference_type;
- typedef typename ContainerT::size_type size_type;
- typedef typename ContainerT::reverse_iterator reverse_iterator;
- typedef typename ContainerT::const_reverse_iterator const_reverse_iterator;
+ typedef typename Container::iterator iterator;
+ typedef typename Container::const_iterator const_iterator;
+ typedef typename Container::difference_type difference_type;
+ typedef typename Container::size_type size_type;
+ typedef typename Container::reverse_iterator reverse_iterator;
+ typedef typename Container::const_reverse_iterator const_reverse_iterator;
explicit sorted_vector_set(const Compare& comp = Compare(),
const Allocator& alloc = Allocator())
// those performed by the caller. (The iterator range constructor performs at
// least one allocation).
//
- // Note that `sorted_vector_set(const ContainerT& container)` is not provided,
+ // Note that `sorted_vector_set(const Container& container)` is not provided,
// since the purpose of this constructor is to avoid an unnecessary copy.
explicit sorted_vector_set(
- ContainerT&& container,
+ Container&& container,
const Compare& comp = Compare())
: m_(comp, container.get_allocator()) {
std::sort(container.begin(), container.end(), value_comp());
: Compare(c)
, cont_(alloc)
{}
- ContainerT cont_;
+ Container cont_;
} m_;
template <typename Self>
class Value,
class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<Key, Value>>,
- class GrowthPolicy = void>
+ class GrowthPolicy = void,
+ class Container = std::vector<std::pair<Key, Value>, Allocator>>
class sorted_vector_map
- : boost::totally_ordered1<
- sorted_vector_map<Key,Value,Compare,Allocator,GrowthPolicy>
- , detail::growth_policy_wrapper<GrowthPolicy> >
-{
- typedef std::vector<std::pair<Key,Value>,Allocator> ContainerT;
-
+ : boost::totally_ordered1<
+ sorted_vector_map<Key, Value, Compare, Allocator, GrowthPolicy>,
+ detail::growth_policy_wrapper<GrowthPolicy>> {
detail::growth_policy_wrapper<GrowthPolicy>&
get_growth_policy() { return *this; }
explicit value_compare(const Compare& c) : Compare(c) {}
};
- typedef typename ContainerT::pointer pointer;
- typedef typename ContainerT::reference reference;
- typedef typename ContainerT::const_reference const_reference;
- typedef typename ContainerT::iterator iterator;
- typedef typename ContainerT::const_iterator const_iterator;
- typedef typename ContainerT::difference_type difference_type;
- typedef typename ContainerT::size_type size_type;
- typedef typename ContainerT::reverse_iterator reverse_iterator;
- typedef typename ContainerT::const_reverse_iterator const_reverse_iterator;
+ typedef typename Container::pointer pointer;
+ typedef typename Container::reference reference;
+ typedef typename Container::const_reference const_reference;
+ typedef typename Container::iterator iterator;
+ typedef typename Container::const_iterator const_iterator;
+ typedef typename Container::difference_type difference_type;
+ typedef typename Container::size_type size_type;
+ typedef typename Container::reverse_iterator reverse_iterator;
+ typedef typename Container::const_reverse_iterator const_reverse_iterator;
explicit sorted_vector_map(const Compare& comp = Compare(),
const Allocator& alloc = Allocator())
// those performed by the caller. (The iterator range constructor performs at
// least one allocation).
//
- // Note that `sorted_vector_map(const ContainerT& container)` is not provided,
+ // Note that `sorted_vector_map(const Container& container)` is not provided,
// since the purpose of this constructor is to avoid an unnecessary copy.
explicit sorted_vector_map(
- ContainerT&& container,
+ Container&& container,
const Compare& comp = Compare())
: m_(value_compare(comp), container.get_allocator()) {
std::sort(container.begin(), container.end(), value_comp());
: value_compare(c)
, cont_(alloc)
{}
- ContainerT cont_;
+ Container cont_;
} m_;
template <typename Self>
*/
#include <folly/small_vector.h>
+#include <folly/sorted_vector_types.h>
#include <iostream>
#include <iterator>
namespace {
+template <typename Key, typename Value, size_t N>
+using small_sorted_vector_map = folly::sorted_vector_map<
+ Key,
+ Value,
+ std::less<Key>,
+ std::allocator<std::pair<Key, Value>>,
+ void,
+ folly::small_vector<std::pair<Key, Value>, N>>;
+
+template <typename Key, typename Value, size_t N>
+using noheap_sorted_vector_map = folly::sorted_vector_map<
+ Key,
+ Value,
+ std::less<Key>,
+ std::allocator<std::pair<Key, Value>>,
+ void,
+ folly::small_vector<
+ std::pair<Key, Value>,
+ N,
+ folly::small_vector_policy::NoHeap>>;
+
+template <typename T, size_t N>
+using small_sorted_vector_set = folly::sorted_vector_set<
+ T,
+ std::less<T>,
+ std::allocator<T>,
+ void,
+ folly::small_vector<T, N>>;
+
+template <typename T, size_t N>
+using noheap_sorted_vector_set = folly::sorted_vector_set<
+ T,
+ std::less<T>,
+ std::allocator<T>,
+ void,
+ folly::small_vector<T, N, folly::small_vector_policy::NoHeap>>;
+
struct NontrivialType {
static int ctored;
explicit NontrivialType() : a(0) {}
EXPECT_EQ(test.size() - 1, counts.moveCount);
EXPECT_LT(test.size(), test.capacity());
}
+
+TEST(small_vector, StorageForSortedVectorMap) {
+ small_sorted_vector_map<int32_t, int32_t, 2> test;
+ test.insert(std::make_pair(10, 10));
+ EXPECT_EQ(test.size(), 1);
+ test.insert(std::make_pair(10, 10));
+ EXPECT_EQ(test.size(), 1);
+ test.insert(std::make_pair(20, 10));
+ EXPECT_EQ(test.size(), 2);
+ test.insert(std::make_pair(30, 10));
+ EXPECT_EQ(test.size(), 3);
+}
+
+TEST(small_vector, NoHeapStorageForSortedVectorMap) {
+ noheap_sorted_vector_map<int32_t, int32_t, 2> test;
+ test.insert(std::make_pair(10, 10));
+ EXPECT_EQ(test.size(), 1);
+ test.insert(std::make_pair(10, 10));
+ EXPECT_EQ(test.size(), 1);
+ test.insert(std::make_pair(20, 10));
+ EXPECT_EQ(test.size(), 2);
+ EXPECT_THROW(test.insert(std::make_pair(30, 10)), std::length_error);
+ EXPECT_EQ(test.size(), 2);
+}
+
+TEST(small_vector, StorageForSortedVectorSet) {
+ small_sorted_vector_set<int32_t, 2> test;
+ test.insert(10);
+ EXPECT_EQ(test.size(), 1);
+ test.insert(10);
+ EXPECT_EQ(test.size(), 1);
+ test.insert(20);
+ EXPECT_EQ(test.size(), 2);
+ test.insert(30);
+ EXPECT_EQ(test.size(), 3);
+}
+
+TEST(small_vector, NoHeapStorageForSortedVectorSet) {
+ noheap_sorted_vector_set<int32_t, 2> test;
+ test.insert(10);
+ EXPECT_EQ(test.size(), 1);
+ test.insert(10);
+ EXPECT_EQ(test.size(), 1);
+ test.insert(20);
+ EXPECT_EQ(test.size(), 2);
+ EXPECT_THROW(test.insert(30), std::length_error);
+ EXPECT_EQ(test.size(), 2);
+}