2 #include <boost/array.hpp>
3 #include <boost/lockfree/detail/atomic.hpp>
4 #include <boost/thread.hpp>
6 template <typename int_type
>
7 int_type
generate_id(void)
9 static boost::lockfree::detail::atomic
<int_type
> generator(0);
13 template <typename int_type
, unsigned int buckets
>
14 class static_hashed_set
17 bool insert(int_type
const & id
)
19 std::size_t index
= id
% buckets
;
21 boost::mutex::scoped_lock
lock (ref_mutex
[index
]);
23 std::pair
<typename
std::set
<int_type
>::iterator
, bool> p
;
24 p
= data
[index
].insert(id
);
29 bool find (int_type
const & id
)
31 std::size_t index
= id
% buckets
;
33 boost::mutex::scoped_lock
lock (ref_mutex
[index
]);
35 return data
[index
].find(id
) != data
[index
].end();
38 bool erase(int_type
const & id
)
40 std::size_t index
= id
% buckets
;
42 boost::mutex::scoped_lock
lock (ref_mutex
[index
]);
44 if (data
[index
].find(id
) != data
[index
].end())
46 data
[index
].erase(id
);
47 assert(data
[index
].find(id
) == data
[index
].end());
54 int count_nodes(void) const
57 for (int i
= 0; i
!= buckets
; ++i
)
59 boost::mutex::scoped_lock
lock (ref_mutex
[i
]);
60 ret
+= data
[i
].size();
66 boost::array
<std::set
<int_type
>, buckets
> data
;
67 mutable boost::array
<boost::mutex
, buckets
> ref_mutex
;