6 #ifndef TURI_UTIL_HOPSCOTCH_SET_HPP 7 #define TURI_UTIL_HOPSCOTCH_SET_HPP 9 #include <core/generics/hopscotch_table.hpp> 11 #include <core/storage/serialization/serialization_includes.hpp> 14 #define _HOPSCOTCH_SET_DEFAULT_HASH std::hash<Key> 34 template <
typename Key,
35 typename Hash = _HOPSCOTCH_SET_DEFAULT_HASH,
36 typename KeyEqual = std::equal_to<Key> >
41 typedef Key value_type;
42 typedef size_t size_type;
44 typedef KeyEqual equality_function;
45 typedef value_type* pointer;
46 typedef value_type& reference;
47 typedef const value_type* const_pointer;
48 typedef const value_type& const_reference;
51 typedef Key storage_type;
64 container_type* container;
71 equality_function equalfun;
73 container_type* create_new_container(
size_t size) {
74 return new container_type(size, hashfun, equalfun);
83 container_type* rehash_to_new_container(
size_t newsize = (
size_t)(-1)) {
90 if (newsize == (
size_t)(-1)) newsize = container->
size() * 2;
91 container_type* newcontainer = create_new_container(newsize);
92 const_iterator citer = begin();
93 while (citer != end()) {
102 iterator do_insert(
const value_type &v) {
103 iterator iter = container->
insert(v);
105 if (iter != container->
end()) {
109 container_type* newcontainer = rehash_to_new_container();
110 iter = newcontainer->
insert(v);
112 std::swap(container, newcontainer);
121 Hash hashfun = Hash(),
122 KeyEqual equalfun = KeyEqual()):
124 hashfun(hashfun), equalfun(equalfun) {
125 container = create_new_container(initialsize);
129 hashfun(h.hashfun), equalfun(h.equalfun) {
130 container = create_new_container(h.capacity());
131 (*container) = *(h.container);
136 void rehash(
size_t s) {
137 if (s > capacity()) {
138 container_type* newcontainer = rehash_to_new_container(s);
139 std::swap(container, newcontainer);
148 hasher hash_function()
const {
152 KeyEqual key_eq()
const {
157 (*container) = *(other.container);
158 hashfun = other.hashfun;
159 equalfun = other.equalfun;
163 size_type size()
const {
164 return container->
size();
168 return container->
begin();
172 return container->
end();
176 const_iterator begin()
const {
177 return container->
begin();
180 const_iterator end()
const {
181 return container->
end();
185 std::pair<iterator, bool> insert(
const value_type& v) {
186 iterator i = find(v);
187 if (i != end())
return std::make_pair(i,
false);
188 else return std::make_pair(do_insert(v),
true);
192 iterator insert(const_iterator hint,
const value_type& v) {
193 return insert(v).first;
196 iterator find(value_type
const& v) {
197 return container->
find(v);
200 const_iterator find(value_type
const& v)
const {
201 return container->
find(v);
204 size_t count(value_type
const& v)
const {
205 return container->
count(v);
209 bool erase(iterator iter) {
210 return container->
erase(iter);
213 bool erase(value_type
const& v) {
214 return container->
erase(v);
218 std::swap(container, other.container);
219 std::swap(hashfun, other.hashfun);
220 std::swap(equalfun, other.equalfun);
225 container = create_new_container(128);
229 size_t capacity()
const {
234 float load_factor()
const {
235 return container->load_factor();
239 oarc << size() << capacity();
240 const_iterator iter = begin();
241 while (iter != end()) {
251 if (capacity() != c) {
253 container = create_new_container(c);
258 for (
size_t i = 0;i < s; ++i) {
size_t capacity() const
Returns the capacity of the table.
The serialization input archive object which, provided with a reference to an istream, will read from the istream, providing deserialization capabilities.
iterator end()
Returns an iterator to the end of the table.
size_t count(const value_type &v) const
Returns 1 if the table contains a given element. 0 otherwise.
const_iterator find(const value_type &key) const
iterator begin()
Returns an iterator to the start of the table.
bool erase(iterator iter)
iterator insert(const value_type &newdata)
size_t size() const
Returns the number of elements in the table.
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
#define DASSERT_TRUE(cond)