2ffdc33ef1df755f9c594bcdfeaa053e4d2c4d65
[libcds.git] / cds / intrusive / striped_set / resizing_policy.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_INTRUSIVE_STRIPED_SET_RESIZING_POLICY_H
4 #define __CDS_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     /**
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     /**
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 #   ifdef CDS_RVALUE_SUPPORT
67         /// Move ctor (for the compilers supporting rvalue reference)
68         load_factor_resizing( load_factor_resizing&& src )
69             : m_nLoadFactor( src.m_nLoadFactor )
70         {}
71 #   endif
72
73         /// Main policy operator returns \p true when resizing is needed
74         template <typename Container, typename Bucket>
75         bool operator ()(
76             size_t nSize,                   ///< Current item count of \p container
77             Container const& container,     ///< Container
78             Bucket const& /*bucket*/        ///< reference to a container's bucket (not used)
79         )
80         {
81             return nSize > container.bucket_count() * m_nLoadFactor;
82         }
83
84         /// Resets internal state of the policy (does nothing)
85         void reset()
86         {}
87     };
88
89
90     /// Single bucket threshold resizing policy
91     /**
92         If any single bucket size exceeds the global \p Threshold then resizing is needed.
93
94         This policy is stateless.
95     */
96     template <size_t Threshold>
97     struct single_bucket_size_threshold
98     {
99         /// Main policy operator returns \p true when resizing is needed
100         template <typename Container, typename Bucket>
101         bool operator ()(
102             size_t /*nSize*/,                   ///< Current item count of \p container (not used)
103             Container const& /*container*/,     ///< Container (not used)
104             Bucket const& bucket                ///< reference to a container's bucket
105             ) const
106         {
107             return bucket.size() > Threshold;
108         }
109
110         /// Resets internal state of the policy (does nothing)
111         void reset()
112         {}
113     };
114
115
116     /// Single bucket threshold resizing policy, stateful specialization
117     /**
118         This specialization allows to specify and modify a threshold at runtime.
119     */
120     template <>
121     struct single_bucket_size_threshold<0>
122     {
123         size_t  m_nThreshold    ;   ///< The bucket size threshold
124
125         /// Default ctor, the threshold is 4
126         single_bucket_size_threshold()
127             : m_nThreshold(4)
128         {}
129
130         /// Ctor with explicitly defined \p nThreshold
131         explicit single_bucket_size_threshold( size_t nThreshold )
132             : m_nThreshold( nThreshold )
133         {}
134
135         /// Copy ctor
136         single_bucket_size_threshold( single_bucket_size_threshold const& src )
137             : m_nThreshold( src.m_nThreshold )
138         {}
139
140 #   ifdef CDS_RVALUE_SUPPORT
141         /// Move ctor (for the compilers supporting rvalue reference)
142         single_bucket_size_threshold( single_bucket_size_threshold&& src )
143             : m_nThreshold( src.m_nThreshold )
144         {}
145 #   endif
146
147         /// Main policy operator returns \p true when resizing is needed
148         template <typename Container, typename Bucket>
149         bool operator ()(
150             size_t /*nSize*/,                   ///< Current item count of \p container (not used)
151             Container const& /*container*/,     ///< Container (not used)
152             Bucket const& bucket                ///< reference to a container's bucket
153             ) const
154         {
155             return bucket.size() > m_nThreshold;
156         }
157
158         /// Resets internal state of the policy (does nothing)
159         void reset()
160         {}
161     };
162
163     /// Dummy resizing policy
164     /**
165         This policy is dummy and always returns \p false that means no resizing is needed.
166
167         This policy is stateless.
168     */
169     struct no_resizing
170     {
171         /// Main policy operator always returns \p false
172         template <typename Container, typename Bucket>
173         bool operator ()(
174             size_t /*nSize*/,                   ///< Current item count of \p container (not used)
175             Container const& /*container*/,     ///< Container (not used)
176             Bucket const& /*bucket*/            ///< reference to a container's bucket (not used)
177         ) const
178         {
179             return false;
180         }
181
182         /// Resets internal state of the policy (does nothing)
183         void reset()
184         {}
185     };
186
187 }}} // namespace cds::intrusive::striped_set
188
189 #endif // #define __CDS_INTRUSIVE_STRIPED_SET_RESIZING_POLICY_H