24 #ifndef GRAPHLAB_UTIL_HOPSCOTCH_SET_HPP
25 #define GRAPHLAB_UTIL_HOPSCOTCH_SET_HPP
27 #include <graphlab/util/hopscotch_table.hpp>
28 #include <graphlab/parallel/pthread_tools.hpp>
29 #include <graphlab/parallel/atomic.hpp>
31 #include <graphlab/serialization/serialization_includes.hpp>
34 #include <boost/functional/hash.hpp>
35 #define _HOPSCOTCH_SET_DEFAULT_HASH boost::hash<Key>
60 template <
typename Key,
61 bool Synchronized =
true,
62 typename Hash = _HOPSCOTCH_SET_DEFAULT_HASH,
63 typename KeyEqual = std::equal_to<Key> >
68 typedef Key value_type;
69 typedef size_t size_type;
71 typedef KeyEqual equality_function;
72 typedef value_type* pointer;
73 typedef value_type& reference;
74 typedef const value_type* const_pointer;
75 typedef const value_type& const_reference;
78 typedef Key storage_type;
100 equality_function equalfun;
112 container_type* rehash_to_new_container(
size_t newsize = (
size_t)(-1)) {
119 if (newsize == (
size_t)(-1)) newsize = container->
size() * 2;
122 while (citer != end()) {
123 assert(newcontainer->
insert(*citer) != newcontainer->
end());
131 iterator do_insert(
const value_type &v) {
134 if (iter != container->
end()) {
139 iter = newcontainer->
insert(v);
140 assert(iter != newcontainer->
end());
141 std::swap(container, newcontainer);
150 Hash hashfun = Hash(),
151 KeyEqual equalfun = KeyEqual()):
153 hashfun(hashfun), equalfun(equalfun) {
154 container = create_new_container(initialsize);
158 hashfun(h.hashfun), equalfun(h.equalfun) {
159 container = create_new_container(h.capacity());
160 (*container) = *(h.container);
165 void rehash(
size_t s) {
166 if (s > capacity()) {
168 std::swap(container, newcontainer);
177 hasher hash_function()
const {
181 KeyEqual key_eq()
const {
186 (*container) = *(other.container);
187 hashfun = other.hashfun;
188 equalfun = other.equalfun;
192 size_type size()
const {
193 return container->
size();
197 return container->
begin();
201 return container->
end();
206 return container->
begin();
210 return container->
end();
214 std::pair<iterator, bool> insert(
const value_type& v) {
216 if (i != end())
return std::make_pair(i,
false);
217 else return std::make_pair(do_insert(v),
true);
222 return insert(v).first;
225 iterator find(value_type
const& v) {
226 return container->
find(v);
230 return container->
find(v);
233 size_t count(value_type
const& v)
const {
234 return container->
count(v);
239 return container->
erase(iter);
242 bool erase(value_type
const& v) {
243 return container->
erase(v);
247 std::swap(container, other.container);
248 std::swap(hashfun, other.hashfun);
249 std::swap(equalfun, other.equalfun);
254 container = create_new_container(128);
258 size_t capacity()
const {
263 float load_factor()
const {
264 return container->load_factor();
268 oarc << size() << capacity();
270 while (iter != end()) {
280 if (capacity() != c) {
282 container = create_new_container(c);
287 for (
size_t i = 0;i < s; ++i) {