Merge branch 'integration' into dev
[libcds.git] / cds / intrusive / striped_set / resizing_policy.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_INTRUSIVE_STRIPED_SET_RESIZING_POLICY_H
4 #define CDSLIB_INTRUSIVE_STRIPED_SET_RESIZING_POLICY_H
5
6 #include <cds/opt/options.h>
7
8 namespace cds { namespace intrusive { namespace striped_set {
9
10     /// Load factor based resizing policy
11     /** @ingroup cds_striped_resizing_policy
12         When total item count in a container exceeds
13         <tt>container.bucket_count() * LoadFactor</tt>
14         then resizing is needed.
15
16         This policy is stateless.
17
18         The <tt>reset()</tt> function is called after the resizing is done.
19         The function is intended for resetting internal state of the policy.
20     */
21     template <size_t LoadFactor>
22     struct load_factor_resizing
23     {
24         /// Main policy operator returns \p true when resizing is needed
25         template <typename Container, typename Bucket>
26         bool operator ()(
27             size_t nSize,                   ///< Current item count of \p container
28             Container const& container,     ///< Container
29             Bucket const& /*bucket*/        ///< reference to a container's bucket (not used)
30         ) const
31         {
32             return nSize > container.bucket_count() * LoadFactor;
33         }
34
35         /// Resets internal state of the policy (does nothing)
36         void reset()
37         {}
38     };
39
40     /// Load factor based resizing policy, stateful specialization
41     /** @ingroup cds_striped_resizing_policy
42         This specialization allows to specify a load factor at runtime.
43     */
44     template <>
45     struct load_factor_resizing<0>
46     {
47         ///@cond
48         const size_t m_nLoadFactor;
49         //@endcond
50     public:
51         /// Default ctor, load factor is 4
52         load_factor_resizing()
53             : m_nLoadFactor(4)
54         {}
55
56         /// Ctor with explicitly defined \p nLoadFactor
57         explicit load_factor_resizing( size_t nLoadFactor )
58             : m_nLoadFactor( nLoadFactor )
59         {}
60
61         /// Copy ctor
62         load_factor_resizing( load_factor_resizing const& src )
63             : m_nLoadFactor( src.m_nLoadFactor )
64         {}
65
66         /// Move ctor
67         load_factor_resizing( load_factor_resizing&& src )
68             : m_nLoadFactor( src.m_nLoadFactor )
69         {}
70
71         /// Main policy operator returns \p true when resizing is needed
72         template <typename Container, typename Bucket>
73         bool operator ()(
74             size_t nSize,                   ///< Current item count of \p container
75             Container const& container,     ///< Container
76             Bucket const& /*bucket*/        ///< reference to a container's bucket (not used)
77         )
78         {
79             return nSize > container.bucket_count() * m_nLoadFactor;
80         }
81
82         /// Resets internal state of the policy (does nothing)
83         void reset()
84         {}
85     };
86
87
88     /// Single bucket threshold resizing policy
89     /** @ingroup cds_striped_resizing_policy
90         If any single bucket size exceeds the global \p Threshold then resizing is needed.
91
92         This policy is stateless.
93     */
94     template <size_t Threshold>
95     struct single_bucket_size_threshold
96     {
97         /// Main policy operator returns \p true when resizing is needed
98         template <typename Container, typename Bucket>
99         bool operator ()(
100             size_t /*nSize*/,                   ///< Current item count of \p container (not used)
101             Container const& /*container*/,     ///< Container (not used)
102             Bucket const& bucket                ///< reference to a container's bucket
103             ) const
104         {
105             return bucket.size() > Threshold;
106         }
107
108         /// Resets internal state of the policy (does nothing)
109         void reset()
110         {}
111     };
112
113
114     /// Single bucket threshold resizing policy, stateful specialization
115     /** @ingroup cds_striped_resizing_policy
116         This specialization allows to specify and modify a threshold at runtime.
117     */
118     template <>
119     struct single_bucket_size_threshold<0>
120     {
121         size_t  m_nThreshold    ;   ///< The bucket size threshold
122
123         /// Default ctor, the threshold is 4
124         single_bucket_size_threshold()
125             : m_nThreshold(4)
126         {}
127
128         /// Ctor with explicitly defined \p nThreshold
129         explicit single_bucket_size_threshold( size_t nThreshold )
130             : m_nThreshold( nThreshold )
131         {}
132
133         /// Copy ctor
134         single_bucket_size_threshold( single_bucket_size_threshold const& src )
135             : m_nThreshold( src.m_nThreshold )
136         {}
137
138         /// Move ctor
139         single_bucket_size_threshold( single_bucket_size_threshold&& src )
140             : m_nThreshold( src.m_nThreshold )
141         {}
142
143         /// Main policy operator returns \p true when resizing is needed
144         template <typename Container, typename Bucket>
145         bool operator ()(
146             size_t /*nSize*/,                   ///< Current item count of \p container (not used)
147             Container const& /*container*/,     ///< Container (not used)
148             Bucket const& bucket                ///< reference to a container's bucket
149             ) const
150         {
151             return bucket.size() > m_nThreshold;
152         }
153
154         /// Resets internal state of the policy (does nothing)
155         void reset()
156         {}
157     };
158
159     /// Dummy resizing policy
160     /** @ingroup cds_striped_resizing_policy
161         This policy is dummy and always returns \p false that means no resizing is needed.
162
163         This policy is stateless.
164     */
165     struct no_resizing
166     {
167         /// Main policy operator always returns \p false
168         template <typename Container, typename Bucket>
169         bool operator ()(
170             size_t /*nSize*/,                   ///< Current item count of \p container (not used)
171             Container const& /*container*/,     ///< Container (not used)
172             Bucket const& /*bucket*/            ///< reference to a container's bucket (not used)
173         ) const
174         {
175             return false;
176         }
177
178         /// Resets internal state of the policy (does nothing)
179         void reset()
180         {}
181     };
182
183 }}} // namespace cds::intrusive::striped_set
184
185 #endif // #define CDSLIB_INTRUSIVE_STRIPED_SET_RESIZING_POLICY_H