mirror of
https://github.com/BlackMATov/flat.hpp.git
synced 2025-12-13 09:45:38 +07:00
add custom container to flat_set and flat_map
This commit is contained in:
57
README.md
57
README.md
@@ -34,6 +34,61 @@
|
||||
|
||||
## API
|
||||
|
||||
> coming soon!
|
||||
```cpp
|
||||
template < typename Key
|
||||
, typename Compare = std::less<Key>
|
||||
, typename Allocator = std::allocator<Key>
|
||||
, typename Container = std::vector<Key, Allocator> >
|
||||
class flat_set;
|
||||
```
|
||||
|
||||
#### Member types
|
||||
|
||||
| Member type | Definition |
|
||||
|------------------------|-----------------------------------|
|
||||
| key_type | Key |
|
||||
| value_type | Key |
|
||||
| size_type | Container::size_type |
|
||||
| difference_type | Container::difference_type |
|
||||
| key_compare | Compare |
|
||||
| value_compare | Compare |
|
||||
| allocator_type | Allocator |
|
||||
| container_type | Container |
|
||||
| reference | Container::reference |
|
||||
| const_reference | Container::const_reference |
|
||||
| pointer | Container::pointer |
|
||||
| const_pointer | Container::const_pointer |
|
||||
| iterator | Container::iterator |
|
||||
| const_iterator | Container::const_iterator |
|
||||
| reverse_iterator | Container::reverse_iterator |
|
||||
| const_reverse_iterator | Container::const_reverse_iterator |
|
||||
|
||||
```cpp
|
||||
template < typename Key
|
||||
, typename Value
|
||||
, typename Compare = std::less<Key>
|
||||
, typename Allocator = std::allocator<std::pair<Key, Value>>
|
||||
, typename Container = std::vector<std::pair<Key, Value>, Allocator> >
|
||||
class flat_map;
|
||||
```
|
||||
|
||||
| Member type | Definition |
|
||||
|------------------------|-----------------------------------|
|
||||
| key_type | Key |
|
||||
| mapped_type | Value |
|
||||
| value_type | Container::value_type |
|
||||
| size_type | Container::size_type |
|
||||
| difference_type | Container::difference_type |
|
||||
| key_compare | Compare |
|
||||
| allocator_type | Allocator |
|
||||
| container_type | Container |
|
||||
| reference | Container::reference |
|
||||
| const_reference | Container::const_reference |
|
||||
| pointer | Container::pointer |
|
||||
| const_pointer | Container::const_pointer |
|
||||
| iterator | Container::iterator |
|
||||
| const_iterator | Container::const_iterator |
|
||||
| reverse_iterator | Container::reverse_iterator |
|
||||
| const_reverse_iterator | Container::const_reverse_iterator |
|
||||
|
||||
## [License (MIT)](./LICENSE.md)
|
||||
|
||||
44
flat_map.hpp
44
flat_map.hpp
@@ -20,12 +20,9 @@ namespace flat_hpp
|
||||
template < typename Key
|
||||
, typename Value
|
||||
, typename Compare = std::less<Key>
|
||||
, typename Allocator = std::allocator<std::pair<Key, Value>> >
|
||||
, typename Allocator = std::allocator<std::pair<Key, Value>>
|
||||
, typename Container = std::vector<std::pair<Key, Value>, Allocator> >
|
||||
class flat_map final {
|
||||
using data_type = std::vector<
|
||||
std::pair<Key, Value>,
|
||||
Allocator>;
|
||||
|
||||
class uber_comparer_type : public Compare {
|
||||
public:
|
||||
uber_comparer_type() = default;
|
||||
@@ -35,34 +32,35 @@ namespace flat_hpp
|
||||
return Compare::operator()(l, r);
|
||||
}
|
||||
|
||||
bool operator()(const Key& l, typename data_type::const_reference r) const {
|
||||
bool operator()(const Key& l, typename Container::const_reference r) const {
|
||||
return Compare::operator()(l, r.first);
|
||||
}
|
||||
|
||||
bool operator()(typename data_type::const_reference l, const Key& r) const {
|
||||
bool operator()(typename Container::const_reference l, const Key& r) const {
|
||||
return Compare::operator()(l.first, r);
|
||||
}
|
||||
};
|
||||
public:
|
||||
using key_type = Key;
|
||||
using mapped_type = Value;
|
||||
using value_type = typename data_type::value_type;
|
||||
using value_type = typename Container::value_type;
|
||||
|
||||
using size_type = typename data_type::size_type;
|
||||
using difference_type = typename data_type::difference_type;
|
||||
using size_type = typename Container::size_type;
|
||||
using difference_type = typename Container::difference_type;
|
||||
|
||||
using key_compare = Compare;
|
||||
using allocator_type = Allocator;
|
||||
using container_type = Container;
|
||||
|
||||
using reference = typename data_type::reference;
|
||||
using const_reference = typename data_type::const_reference;
|
||||
using pointer = typename data_type::pointer;
|
||||
using const_pointer = typename data_type::const_pointer;
|
||||
using reference = typename Container::reference;
|
||||
using const_reference = typename Container::const_reference;
|
||||
using pointer = typename Container::pointer;
|
||||
using const_pointer = typename Container::const_pointer;
|
||||
|
||||
using iterator = typename data_type::iterator;
|
||||
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;
|
||||
using iterator = typename Container::iterator;
|
||||
using const_iterator = typename Container::const_iterator;
|
||||
using reverse_iterator = typename Container::reverse_iterator;
|
||||
using const_reverse_iterator = typename Container::const_reverse_iterator;
|
||||
|
||||
class value_compare final {
|
||||
public:
|
||||
@@ -80,6 +78,14 @@ namespace flat_hpp
|
||||
static_assert(
|
||||
std::is_same<typename allocator_type::value_type, value_type>::value,
|
||||
"Allocator::value_type must be same type as value_type");
|
||||
|
||||
static_assert(
|
||||
std::is_same<typename container_type::value_type, value_type>::value,
|
||||
"Container::value_type must be same type as value_type");
|
||||
|
||||
static_assert(
|
||||
std::is_same<typename container_type::allocator_type, allocator_type>::value,
|
||||
"Container::allocator_type must be same type as allocator_type");
|
||||
public:
|
||||
explicit flat_map(
|
||||
const Allocator& a)
|
||||
@@ -290,7 +296,7 @@ namespace flat_hpp
|
||||
return value_compare(compare_);
|
||||
}
|
||||
private:
|
||||
data_type data_;
|
||||
container_type data_;
|
||||
uber_comparer_type compare_;
|
||||
};
|
||||
}
|
||||
|
||||
46
flat_set.hpp
46
flat_set.hpp
@@ -19,33 +19,51 @@ namespace flat_hpp
|
||||
{
|
||||
template < typename Key
|
||||
, typename Compare = std::less<Key>
|
||||
, typename Allocator = std::allocator<Key> >
|
||||
, typename Allocator = std::allocator<Key>
|
||||
, typename Container = std::vector<Key, Allocator> >
|
||||
class flat_set final {
|
||||
using data_type = std::vector<Key, Allocator>;
|
||||
class uber_comparer_type : public Compare {
|
||||
public:
|
||||
uber_comparer_type() = default;
|
||||
uber_comparer_type(const Compare& c) : Compare(c) {}
|
||||
|
||||
bool operator()(const Key& l, const Key& r) const {
|
||||
return Compare::operator()(l, r);
|
||||
}
|
||||
};
|
||||
public:
|
||||
using key_type = Key;
|
||||
using value_type = Key;
|
||||
|
||||
using size_type = typename data_type::size_type;
|
||||
using difference_type = typename data_type::difference_type;
|
||||
using size_type = typename Container::size_type;
|
||||
using difference_type = typename Container::difference_type;
|
||||
|
||||
using key_compare = Compare;
|
||||
using value_compare = Compare;
|
||||
using allocator_type = Allocator;
|
||||
using container_type = Container;
|
||||
|
||||
using reference = typename data_type::reference;
|
||||
using const_reference = typename data_type::const_reference;
|
||||
using pointer = typename data_type::pointer;
|
||||
using const_pointer = typename data_type::const_pointer;
|
||||
using reference = typename Container::reference;
|
||||
using const_reference = typename Container::const_reference;
|
||||
using pointer = typename Container::pointer;
|
||||
using const_pointer = typename Container::const_pointer;
|
||||
|
||||
using iterator = typename data_type::iterator;
|
||||
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;
|
||||
using iterator = typename Container::iterator;
|
||||
using const_iterator = typename Container::const_iterator;
|
||||
using reverse_iterator = typename Container::reverse_iterator;
|
||||
using const_reverse_iterator = typename Container::const_reverse_iterator;
|
||||
|
||||
static_assert(
|
||||
std::is_same<typename allocator_type::value_type, value_type>::value,
|
||||
"Allocator::value_type must be same type as value_type");
|
||||
|
||||
static_assert(
|
||||
std::is_same<typename container_type::value_type, value_type>::value,
|
||||
"Container::value_type must be same type as value_type");
|
||||
|
||||
static_assert(
|
||||
std::is_same<typename container_type::allocator_type, allocator_type>::value,
|
||||
"Container::allocator_type must be same type as allocator_type");
|
||||
public:
|
||||
explicit flat_set(
|
||||
const Allocator& a)
|
||||
@@ -246,8 +264,8 @@ namespace flat_hpp
|
||||
return value_compare(compare_);
|
||||
}
|
||||
private:
|
||||
data_type data_;
|
||||
key_compare compare_;
|
||||
container_type data_;
|
||||
uber_comparer_type compare_;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user