2015-06-16 21:19:20 +00:00
|
|
|
/* A set container for OctaSTD. Implemented as a hash table.
|
|
|
|
*
|
|
|
|
* This file is part of OctaSTD. See COPYING.md for futher information.
|
|
|
|
*/
|
|
|
|
|
2015-07-13 19:08:55 +00:00
|
|
|
#ifndef OSTD_SET_HH
|
|
|
|
#define OSTD_SET_HH
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2015-07-13 19:08:55 +00:00
|
|
|
#include "ostd/types.hh"
|
|
|
|
#include "ostd/utility.hh"
|
|
|
|
#include "ostd/memory.hh"
|
|
|
|
#include "ostd/functional.hh"
|
|
|
|
#include "ostd/initializer_list.hh"
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2015-07-13 19:08:55 +00:00
|
|
|
#include "ostd/internal/hashtable.hh"
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2015-07-13 19:07:14 +00:00
|
|
|
namespace ostd {
|
2015-06-16 21:19:20 +00:00
|
|
|
|
|
|
|
namespace detail {
|
2016-07-31 19:40:25 +00:00
|
|
|
template<typename T, typename A>
|
|
|
|
struct SetBase {
|
2016-06-23 18:18:35 +00:00
|
|
|
static inline T const &get_key(T const &e) {
|
2015-06-16 21:19:20 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
static inline T &get_data(T &e) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
template<typename U>
|
2016-06-23 18:18:35 +00:00
|
|
|
static inline void set_key(T &, U const &, A &) {}
|
2017-01-29 14:56:02 +00:00
|
|
|
static inline void swap_elem(T &a, T &b) {
|
|
|
|
using std::swap;
|
|
|
|
swap(a, b);
|
|
|
|
}
|
2015-06-16 21:19:20 +00:00
|
|
|
};
|
|
|
|
|
2015-06-18 01:32:43 +00:00
|
|
|
template<typename T, typename H, typename C, typename A, bool IsMultihash>
|
2015-07-05 22:59:36 +00:00
|
|
|
struct SetImpl: detail::Hashtable<
|
|
|
|
detail::SetBase<T, A>, T, T, T, H, C, A, IsMultihash
|
2015-06-18 01:32:43 +00:00
|
|
|
> {
|
|
|
|
private:
|
2015-07-05 22:59:36 +00:00
|
|
|
using Base = detail::Hashtable<
|
|
|
|
detail::SetBase<T, A>, T, T, T, H, C, A, IsMultihash
|
2015-06-18 01:32:43 +00:00
|
|
|
>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using Key = T;
|
2015-07-13 19:07:14 +00:00
|
|
|
using Size = ostd::Size;
|
2015-07-05 22:59:36 +00:00
|
|
|
using Difference = Ptrdiff;
|
2015-06-18 01:32:43 +00:00
|
|
|
using Hasher = H;
|
|
|
|
using KeyEqual = C;
|
|
|
|
using Value = T;
|
|
|
|
using Reference = Value &;
|
2015-07-05 22:59:36 +00:00
|
|
|
using Pointer = AllocatorPointer<A>;
|
|
|
|
using ConstPointer = AllocatorConstPointer<A>;
|
|
|
|
using Range = HashRange<T>;
|
2016-06-23 18:18:35 +00:00
|
|
|
using ConstRange = HashRange<T const>;
|
2015-07-05 22:59:36 +00:00
|
|
|
using LocalRange = BucketRange<T>;
|
2016-06-23 18:18:35 +00:00
|
|
|
using ConstLocalRange = BucketRange<T const>;
|
2015-06-18 01:32:43 +00:00
|
|
|
using Allocator = A;
|
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
explicit SetImpl(
|
|
|
|
Size size, H const &hf = H(),
|
2016-06-23 18:18:35 +00:00
|
|
|
C const &eqf = C(), A const &alloc = A()
|
2016-07-31 19:40:25 +00:00
|
|
|
):
|
|
|
|
Base(size, hf, eqf, alloc)
|
|
|
|
{}
|
2015-06-18 01:32:43 +00:00
|
|
|
|
|
|
|
SetImpl(): SetImpl(0) {}
|
2016-06-23 18:18:35 +00:00
|
|
|
explicit SetImpl(A const &alloc): SetImpl(0, H(), C(), alloc) {}
|
2015-06-18 01:32:43 +00:00
|
|
|
|
2016-06-23 18:18:35 +00:00
|
|
|
SetImpl(Size size, A const &alloc):
|
2016-07-31 19:40:25 +00:00
|
|
|
SetImpl(size, H(), C(), alloc)
|
|
|
|
{}
|
2016-06-23 18:18:35 +00:00
|
|
|
SetImpl(Size size, H const &hf, A const &alloc):
|
2016-07-31 19:40:25 +00:00
|
|
|
SetImpl(size, hf, C(), alloc)
|
|
|
|
{}
|
2015-06-18 01:32:43 +00:00
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
SetImpl(SetImpl const &m):
|
|
|
|
Base(m, allocator_container_copy(m.get_alloc()))
|
|
|
|
{}
|
2015-06-18 01:32:43 +00:00
|
|
|
|
2016-06-23 18:18:35 +00:00
|
|
|
SetImpl(SetImpl const &m, A const &alloc): Base(m, alloc) {}
|
2015-06-18 01:32:43 +00:00
|
|
|
|
2017-01-25 00:44:22 +00:00
|
|
|
SetImpl(SetImpl &&m): Base(std::move(m)) {}
|
|
|
|
SetImpl(SetImpl &&m, A const &alloc): Base(std::move(m), alloc) {}
|
2015-06-18 01:32:43 +00:00
|
|
|
|
2015-07-05 22:59:36 +00:00
|
|
|
template<typename R, typename = EnableIf<
|
2016-01-13 18:09:21 +00:00
|
|
|
IsInputRange<R> && IsConvertible<RangeReference<R>, Value>
|
2016-07-31 19:40:25 +00:00
|
|
|
>>
|
|
|
|
SetImpl(
|
|
|
|
R range, Size size = 0, H const &hf = H(),
|
2016-06-23 18:18:35 +00:00
|
|
|
C const &eqf = C(), A const &alloc = A()
|
2016-07-31 19:40:25 +00:00
|
|
|
):
|
|
|
|
Base(size ? size : detail::estimate_hrsize(range), hf, eqf, alloc)
|
|
|
|
{
|
|
|
|
for (; !range.empty(); range.pop_front()) {
|
2015-06-18 01:32:43 +00:00
|
|
|
Base::emplace(range.front());
|
2016-07-31 19:40:25 +00:00
|
|
|
}
|
2015-06-18 01:32:43 +00:00
|
|
|
Base::rehash_up();
|
|
|
|
}
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2015-06-18 01:32:43 +00:00
|
|
|
template<typename R>
|
2016-07-31 19:40:25 +00:00
|
|
|
SetImpl(R range, Size size, A const &alloc):
|
|
|
|
SetImpl(range, size, H(), C(), alloc)
|
|
|
|
{}
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2015-06-18 01:32:43 +00:00
|
|
|
template<typename R>
|
2016-07-31 19:40:25 +00:00
|
|
|
SetImpl(R range, Size size, H const &hf, A const &alloc):
|
|
|
|
SetImpl(range, size, hf, C(), alloc)
|
|
|
|
{}
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
SetImpl(
|
2017-01-14 14:09:27 +00:00
|
|
|
std::initializer_list<Value> init, Size size = 0,
|
2016-06-23 18:18:35 +00:00
|
|
|
H const &hf = H(), C const &eqf = C(), A const &alloc = A()
|
2016-07-31 19:40:25 +00:00
|
|
|
):
|
|
|
|
SetImpl(iter(init), size, hf, eqf, alloc)
|
|
|
|
{}
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2017-01-14 14:09:27 +00:00
|
|
|
SetImpl(std::initializer_list<Value> init, Size size, A const &alloc):
|
2016-07-31 19:40:25 +00:00
|
|
|
SetImpl(iter(init), size, H(), C(), alloc)
|
|
|
|
{}
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2016-07-31 19:40:25 +00:00
|
|
|
SetImpl(
|
2017-01-14 14:09:27 +00:00
|
|
|
std::initializer_list<Value> init, Size size, H const &hf, A const &alloc
|
2016-07-31 19:40:25 +00:00
|
|
|
):
|
|
|
|
SetImpl(iter(init), size, hf, C(), alloc)
|
|
|
|
{}
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2016-06-23 18:18:35 +00:00
|
|
|
SetImpl &operator=(SetImpl const &m) {
|
2015-06-18 01:32:43 +00:00
|
|
|
Base::operator=(m);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2015-06-18 01:32:43 +00:00
|
|
|
SetImpl &operator=(SetImpl &&m) {
|
2017-01-25 00:44:22 +00:00
|
|
|
Base::operator=(std::move(m));
|
2015-06-18 01:32:43 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2015-07-05 22:59:36 +00:00
|
|
|
template<typename R, typename = EnableIf<
|
2016-01-13 18:09:21 +00:00
|
|
|
IsInputRange<R> && IsConvertible<RangeReference<R>, Value>
|
2016-07-31 19:40:25 +00:00
|
|
|
>>
|
|
|
|
SetImpl &operator=(R range) {
|
2015-06-18 01:32:43 +00:00
|
|
|
Base::assign_range(range);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2017-01-14 14:09:27 +00:00
|
|
|
SetImpl &operator=(std::initializer_list<Value> il) {
|
2015-06-18 01:32:43 +00:00
|
|
|
Base::assign_init(il);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2015-06-18 01:32:43 +00:00
|
|
|
void swap(SetImpl &v) {
|
|
|
|
Base::swap(v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2015-06-17 20:25:34 +00:00
|
|
|
template<
|
|
|
|
typename T,
|
2017-01-30 17:56:38 +00:00
|
|
|
typename H = std::hash<T>,
|
2015-08-02 13:58:49 +00:00
|
|
|
typename C = EqualWithCstr<T>,
|
2015-07-05 22:59:36 +00:00
|
|
|
typename A = Allocator<T>
|
2016-07-31 19:40:25 +00:00
|
|
|
>
|
|
|
|
using Set = detail::SetImpl<T, H, C, A, false>;
|
2015-06-17 20:25:34 +00:00
|
|
|
|
2017-01-29 14:56:02 +00:00
|
|
|
template<typename T, typename H, typename C, typename A>
|
|
|
|
inline void swap(Set<T, H, C, A> &a, Set<T, H, C, A> &b) {
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2015-06-18 01:32:43 +00:00
|
|
|
template<
|
|
|
|
typename T,
|
2017-01-30 17:56:38 +00:00
|
|
|
typename H = std::hash<T>,
|
2015-08-02 13:58:49 +00:00
|
|
|
typename C = EqualWithCstr<T>,
|
2015-07-05 22:59:36 +00:00
|
|
|
typename A = Allocator<T>
|
2016-07-31 19:40:25 +00:00
|
|
|
>
|
|
|
|
using Multiset = detail::SetImpl<T, H, C, A, true>;
|
2015-06-17 20:25:34 +00:00
|
|
|
|
2017-01-29 14:56:02 +00:00
|
|
|
template<typename T, typename H, typename C, typename A>
|
|
|
|
inline void swap(Multiset<T, H, C, A> &a, Multiset<T, H, C, A> &b) {
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2015-07-13 19:07:14 +00:00
|
|
|
} /* namespace ostd */
|
2015-06-16 21:19:20 +00:00
|
|
|
|
2016-02-07 21:17:15 +00:00
|
|
|
#endif
|