/* Associative array for OctaSTD. Implemented as a hash table. * * This file is part of OctaSTD. See COPYING.md for futher information. */ #ifndef OCTA_MAP_H #define OCTA_MAP_H #include "octa/types.h" #include "octa/utility.h" #include "octa/memory.h" #include "octa/functional.h" #include "octa/initializer_list.h" #include "octa/internal/hashtable.h" namespace octa { namespace detail { template struct MapBase { using Element = octa::Pair; static inline const K &get_key(Element &e) { return e.first; } static inline T &get_data(Element &e) { return e.second; } template static inline void set_key(Element &e, U &&key, A &alloc) { octa::allocator_destroy(alloc, &e); octa::allocator_construct(alloc, &e, octa::forward(key), octa::move(T())); } static inline void swap_elem(Element &a, Element &b) { octa::swap(*((K *)&a.first), *((K *)&b.first)); octa::swap(*((T *)&a.second), *((T *)&b.second)); } }; } template< typename K, typename T, typename H = octa::ToHash, typename C = octa::Equal, typename A = octa::Allocator> > struct Map { private: using Base = octa::detail::Hashtable< octa::detail::MapBase, octa::Pair, K, T, H, C, A >; Base p_table; template octa::Size estimate_rsize(const R &range, octa::EnableIf::value, bool> = true ) { return range.size(); } template octa::Size estimate_rsize(const R &, octa::EnableIf::value, bool> = true ) { /* we have no idea how big the range actually is */ return 16; } public: using Key = K; using Mapped = T; using Size = octa::Size; using Difference = octa::Ptrdiff; using Hasher = H; using KeyEqual = C; using Value = octa::Pair; using Reference = Value &; using Pointer = octa::AllocatorPointer; using ConstPointer = octa::AllocatorConstPointer; using Range = octa::HashRange>; using ConstRange = octa::HashRange>; using LocalRange = octa::BucketRange>; using ConstLocalRange = octa::BucketRange>; using Allocator = A; explicit Map(octa::Size size, const H &hf = H(), const C &eqf = C(), const A &alloc = A() ): p_table(size, hf, eqf, alloc) {} Map(): Map(0) {} explicit Map(const A &alloc): Map(0, H(), C(), alloc) {} Map(octa::Size size, const A &alloc): Map(size, H(), C(), alloc) {} Map(octa::Size size, const H &hf, const A &alloc): Map(size, hf, C(), alloc) {} Map(const Map &m): p_table(m.p_table, octa::allocator_container_copy(m.p_table.get_alloc())) {} Map(const Map &m, const A &alloc): p_table(m.p_table, alloc) {} Map(Map &&m): p_table(octa::move(m.p_table)) {} Map(Map &&m, const A &alloc): p_table(octa::move(m.p_table), alloc) {} template Map(R range, octa::Size size = 0, const H &hf = H(), const C &eqf = C(), const A &alloc = A(), octa::EnableIf< octa::IsInputRange::value && octa::IsConvertible, Value>::value, bool > = true ): p_table(size ? size : estimate_rsize(range), hf, eqf, alloc) { for (; !range.empty(); range.pop_front()) emplace(range.front()); p_table.rehash_up(); } template Map(R range, octa::Size size, const A &alloc) : Map(range, size, H(), C(), alloc) {} template Map(R range, octa::Size size, const H &hf, const A &alloc) : Map(range, size, hf, C(), alloc) {} Map(octa::InitializerList init, octa::Size size = 0, const H &hf = H(), const C &eqf = C(), const A &alloc = A() ): Map(octa::each(init), size, hf, eqf, alloc) {} Map(octa::InitializerList init, octa::Size size, const A &alloc) : Map(octa::each(init), size, H(), C(), alloc) {} Map(octa::InitializerList init, octa::Size size, const H &hf, const A &alloc ): Map(octa::each(init), size, hf, C(), alloc) {} Map &operator=(const Map &m) { p_table = m.p_table; return *this; } Map &operator=(Map &&m) { p_table = octa::move(m.p_table); return *this; } template octa::EnableIf< octa::IsInputRange::value && octa::IsConvertible, Value>::value, Map & > operator=(R range) { clear(); p_table.reserve_at_least(estimate_rsize(range)); for (; !range.empty(); range.pop_front()) emplace(range.front()); p_table.rehash_up(); return *this; } Map &operator=(InitializerList il) { const Value *beg = il.begin(), *end = il.end(); clear(); p_table.reserve_at_least(end - beg); while (beg != end) emplace(*beg++); return *this; } bool empty() const { return p_table.empty(); } octa::Size size() const { return p_table.size(); } octa::Size max_size() const { return p_table.max_size(); } octa::Size bucket_count() const { return p_table.bucket_count(); } octa::Size max_bucket_count() const { return p_table.max_bucket_count(); } octa::Size bucket(const K &key) const { return p_table.bucket(key); } octa::Size bucket_size(octa::Size n) const { return p_table.bucket_size(n); } void clear() { p_table.clear(); } A get_allocator() const { return p_table.get_alloc(); } T &at(const K &key) { return *p_table.access(key); } const T &at(const K &key) const { return *p_table.access(key); } T &operator[](const K &key) { octa::Size h; T *v = p_table.access_base(key, h); if (v) return *v; p_table.rehash_ahead(1); return p_table.insert(h, key); } T &operator[](K &&key) { octa::Size h; T *v = p_table.access_base(key, h); if (v) return *v; p_table.rehash_ahead(1); return p_table.insert(h, octa::move(key)); } template octa::Pair emplace(Args &&...args) { return p_table.emplace(octa::forward(args)...); } octa::Size erase(const K &key) { if (p_table.remove(key)) return 1; return 0; } octa::Size count(const K &key) { octa::Size h; T *v = p_table.access_base(key, h); if (v) return 1; return 0; } Range find(const K &key) { return p_table.find(key); } ConstRange find(const K &key) const { return p_table.find(key); } float load_factor() const { return p_table.load_factor(); } float max_load_factor() const { return p_table.max_load_factor(); } void max_load_factor(float lf) { p_table.max_load_factor(lf); } void rehash(octa::Size count) { p_table.rehash(count); } void reserve(octa::Size count) { p_table.reserve(count); } Range each() { return p_table.each(); } ConstRange each() const { return p_table.each(); } ConstRange ceach() const { return p_table.ceach(); } LocalRange each(octa::Size n) { return p_table.each(n); } ConstLocalRange each(octa::Size n) const { return p_table.each(n); } ConstLocalRange ceach(octa::Size n) const { return p_table.each(n); } void swap(Map &v) { octa::swap(p_table, v.p_table); } }; } /* namespace detail */ #endif