/
shared_index.hpp
119 lines (95 loc) · 2.66 KB
/
shared_index.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//==================
// shared_index.hpp
//==================
// Thread-safe implementation of a sorted index
// Items can be inserted, removed and looked-up in constant low time
// Copyright 2022, Sven Bieg (svenbieg@web.de)
// http://github.com/svenbieg/clusters
#ifndef _CLUSTERS_SHARED_INDEX_HPP
#define _CLUSTERS_SHARED_INDEX_HPP
//=======
// Using
//=======
#include "index.hpp"
#include "shared_cluster.hpp"
//===========
// Namespace
//===========
namespace Clusters {
//==============
// Shared Index
//==============
template <typename _item_t, typename _size_t=uint32_t, uint16_t _group_size=10>
class shared_index: public iterable_shared_cluster<index_traits<_item_t, _item_t, _size_t, _group_size>>
{
public:
// Using
using _traits_t=index_traits<_item_t, _item_t, _size_t, _group_size>;
using _cluster_t=typename _traits_t::cluster_t;
using iterator=typename _traits_t::shared_iterator_t;
using const_iterator=typename _traits_t::shared_const_iterator_t;
// Con-/Destructors
shared_index() {}
// Access
inline const_iterator cfind(_item_t const& item, find_func func=find_func::equal)
{
const_iterator it(this);
it.find(item, func);
return it;
}
inline bool contains(_item_t const& item)
{
std::shared_lock<std::shared_mutex> lock(this->m_mutex);
return _cluster_t::contains(item);
}
inline iterator find(_item_t const& item, find_func func=find_func::equal)
{
iterator it(this);
it.find(item, func);
return it;
}
// Modification
template <typename _item_param_t> inline bool add(_item_param_t&& item)
{
std::unique_lock<std::shared_mutex> lock(this->m_mutex);
return _cluster_t::add(std::forward<_item_param_t>(item));
}
inline bool remove(_item_t const& item)
{
std::unique_lock<std::shared_mutex> lock(this->m_mutex);
return _cluster_t::remove(item);
}
template <typename _item_param_t> inline bool set(_item_param_t&& item)
{
std::unique_lock<std::shared_mutex> lock(this->m_mutex);
return _cluster_t::set(std::forward<_item_param_t>(item));
}
};
//==========
// Iterator
//==========
template <typename _traits_t, bool _is_const>
class shared_index_iterator: public shared_cluster_iterator<_traits_t, _is_const>
{
public:
// Using
using _base_t=shared_cluster_iterator<_traits_t, _is_const>;
using _item_t=typename _traits_t::item_t;
using _iterator_t=typename _traits_t::iterator_t;
// Con-/Destructors
using _base_t::_base_t;
// Navigation
bool find(_item_t const& item, find_func func=find_func::equal)
{
if(this->is_outside())
this->lock();
if(!_iterator_t::find(item, func))
{
this->unlock();
return false;
}
return true;
}
};
} // namespace
#endif // _CLUSTERS_SHARED_INDEX_HPP