Text formatting, docfix
[libcds.git] / cds / container / split_list_map_nogc.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_CONTAINER_SPLIT_LIST_MAP_NOGC_H
4 #define CDSLIB_CONTAINER_SPLIT_LIST_MAP_NOGC_H
5
6 #include <cds/container/split_list_set_nogc.h>
7 #include <cds/details/binary_functor_wrapper.h>
8
9 namespace cds { namespace container {
10
11     /// Split-ordered list map (template specialization for gc::nogc)
12     /** @ingroup cds_nonintrusive_map
13         \anchor cds_nonintrusive_SplitListMap_nogc
14
15         This specialization is so-called append-only.
16         The map does not support the removal of list item.
17
18         See \ref cds_nonintrusive_SplitListMap_hp "SplitListMap" for description of template parameters.
19
20         @warning Many member functions return an iterator pointing to an item.
21         The iterator can be used to set up field of the item,
22         but you should provide an exclusive access to it,
23         see \ref cds_intrusive_item_creating "insert item troubleshooting".
24     */
25     template <
26         typename Key,
27         typename Value,
28 #ifdef CDS_DOXYGEN_INVOKED
29         class Traits = split_list::traits
30 #else
31         class Traits
32 #endif
33     >
34     class SplitListMap<cds::gc::nogc, Key, Value, Traits>:
35         protected container::SplitListSet<
36             cds::gc::nogc,
37             std::pair<Key const, Value>,
38             split_list::details::wrap_map_traits<Key, Value, Traits>
39         >
40     {
41         //@cond
42         typedef container::SplitListSet<
43             cds::gc::nogc,
44             std::pair<Key const, Value>,
45             split_list::details::wrap_map_traits<Key, Value, Traits>
46         > base_class;
47         //@endcond
48     public:
49         typedef cds::gc::nogc gc;          ///< Garbage collector
50         typedef Key           key_type;    ///< key type
51         typedef Value         mapped_type; ///< type of value stored in the map
52
53         typedef std::pair<key_type const, mapped_type>  value_type  ;   ///< Pair type
54         typedef typename base_class::ordered_list       ordered_list;   ///< Underlying ordered list class
55         typedef typename base_class::key_comparator     key_comparator; ///< key comparison functor
56
57         typedef typename base_class::hash           hash;         ///< Hash functor for \ref key_type
58         typedef typename base_class::item_counter   item_counter; ///< Item counter type
59         typedef typename base_class::stat           stat;         ///< Internal statistics
60
61     protected:
62         //@cond
63         typedef typename base_class::traits::key_accessor key_accessor;
64         //@endcond
65
66     public:
67         /// Forward iterator (see \p SplitListSet::iterator)
68         /**
69             Remember, the iterator <tt>operator -> </tt> and <tt>operator *</tt> returns \ref value_type pointer and reference.
70             To access item key and value use <tt>it->first</tt> and <tt>it->second</tt> respectively.
71         */
72         typedef typename base_class::iterator iterator;
73
74         /// Const forward iterator (see SplitListSet::const_iterator)
75         typedef typename base_class::const_iterator const_iterator;
76
77         /// Returns a forward iterator addressing the first element in a map
78         /**
79             For empty set \code begin() == end() \endcode
80         */
81         iterator begin()
82         {
83             return base_class::begin();
84         }
85
86         /// Returns an iterator that addresses the location succeeding the last element in a map
87         /**
88             Do not use the value returned by <tt>end</tt> function to access any item.
89             The returned value can be used only to control reaching the end of the set.
90             For empty set \code begin() == end() \endcode
91         */
92         iterator end()
93         {
94             return base_class::end();
95         }
96
97         /// Returns a forward const iterator addressing the first element in a map
98         //@{
99         const_iterator begin() const
100         {
101             return base_class::begin();
102         }
103         const_iterator cbegin() const
104         {
105             return base_class::cbegin();
106         }
107         //@}
108
109         /// Returns an const iterator that addresses the location succeeding the last element in a map
110         //@{
111         const_iterator end() const
112         {
113             return base_class::end();
114         }
115         const_iterator cend() const
116         {
117             return base_class::cend();
118         }
119         //@}
120
121     public:
122         /// Initialize split-ordered map of default capacity
123         /**
124             The default capacity is defined in bucket table constructor.
125             See \p intrusive::split_list::expandable_bucket_table, \p intrusive::split_list::static_ducket_table
126             which selects by \p intrusive::split_list::traits::dynamic_bucket_table.
127         */
128         SplitListMap()
129             : base_class()
130         {}
131
132         /// Initialize split-ordered map
133         SplitListMap(
134             size_t nItemCount           ///< estimated average item count
135             , size_t nLoadFactor = 1    ///< load factor - average item count per bucket. Small integer up to 10, default is 1.
136             )
137             : base_class( nItemCount, nLoadFactor )
138         {}
139
140     public:
141         /// Inserts new node with key and default value
142         /**
143             The function creates a node with \p key and default value, and then inserts the node created into the map.
144
145             Preconditions:
146             - The \p key_type should be constructible from value of type \p K.
147                 In trivial case, \p K is equal to \ref key_type.
148             - The \p mapped_type should be default-constructible.
149
150             Returns an iterator pointed to inserted value, or \p end() if inserting is failed
151         */
152         template <typename K>
153         iterator insert( K const& key )
154         {
155             //TODO: pass arguments by reference (make_pair makes copy)
156             return base_class::insert( std::make_pair( key, mapped_type() ) );
157         }
158
159         /// Inserts new node
160         /**
161             The function creates a node with copy of \p val value
162             and then inserts the node created into the map.
163
164             Preconditions:
165             - The \p key_type should be constructible from \p key of type \p K.
166             - The \p mapped_type should be constructible from \p val of type \p V.
167
168             Returns an iterator pointed to inserted value, or \p end() if inserting is failed
169         */
170         template <typename K, typename V>
171         iterator insert( K const& key, V const& val )
172         {
173             //TODO: pass arguments by reference (make_pair makes copy)
174             return base_class::insert( std::make_pair( key, val ) );
175         }
176
177         /// Inserts new node and initialize it by a functor
178         /**
179             This function inserts new node with key \p key and if inserting is successful then it calls
180             \p func functor with signature
181             \code
182             struct functor {
183                 void operator()( value_type& item );
184             };
185             \endcode
186
187             The argument \p item of user-defined functor \p func is the reference
188             to the map's item inserted. \p item.second is a reference to item's value that may be changed.
189             User-defined functor \p func should guarantee that during changing item's value no any other changes
190             could be made on this map's item by concurrent threads.
191             The user-defined functor is called only if the inserting is successful.
192
193             The \p key_type should be constructible from value of type \p K.
194
195             The function allows to split creating of new item into two part:
196             - create item from \p key;
197             - insert new item into the map;
198             - if inserting is successful, initialize the value of item by calling \p f functor
199
200             This can be useful if complete initialization of object of \p mapped_type is heavyweight and
201             it is preferable that the initialization should be completed only if inserting is successful.
202
203             Returns an iterator pointed to inserted value, or \p end() if inserting is failed
204         */
205         template <typename K, typename Func>
206         iterator insert_with( const K& key, Func func )
207         {
208             iterator it = insert( key );
209             if ( it != end() )
210                 func( (*it) );
211             return it;
212         }
213
214         /// For key \p key inserts data of type \p mapped_type created in-place from \p args
215         /**
216             \p key_type should be constructible from type \p K
217
218             Returns \p true if inserting successful, \p false otherwise.
219         */
220         template <typename K, typename... Args>
221         iterator emplace( K&& key, Args&&... args )
222         {
223             return base_class::emplace( std::forward<K>(key), std::move(mapped_type(std::forward<Args>(args)...)));
224         }
225
226         /// Updates the item
227         /**
228             If \p key is not in the map and \p bAllowInsert is \p true, the function inserts a new item.
229             Otherwise, the function returns an iterator pointing to the item found.
230
231             Returns <tt> std::pair<iterator, bool> </tt> where \p first is an iterator pointing to
232             item found or inserted (if inserting is not allowed and \p key is not found, the iterator will be \p end()),
233
234             \p second is true if new item has been added or \p false if the item
235             already is in the map.
236         */
237         template <typename K>
238         std::pair<iterator, bool> update( K const& key, bool bAllowInsert = true )
239         {
240             //TODO: pass arguments by reference (make_pair makes copy)
241             return base_class::update( std::make_pair( key, mapped_type() ), bAllowInsert );
242         }
243         //@cond
244         template <typename K>
245         CDS_DEPRECATED("ensure() is deprecated, use update()")
246         std::pair<iterator, bool> ensure( K const& key )
247         {
248             return update( key, true );
249         }
250         //@endcond
251
252         /// Checks whether the map contains \p key
253         /**
254             The function searches the item with key equal to \p key
255             and returns an iterator pointed to item found and \ref end() otherwise
256         */
257         template <typename K>
258         iterator contains( K const& key )
259         {
260             return base_class::contains( key );
261         }
262         //@cond
263         template <typename K>
264         CDS_DEPRECATED("deprecated, use contains()")
265         iterator find( K const& key )
266         {
267             return contains( key );
268         }
269         //@endcond
270
271         /// Checks whether the map contains \p key using \p pred predicate for searching
272         /**
273             The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
274             \p Less functor has the interface like \p std::less.
275             \p pred must imply the same element order as the comparator used for building the map.
276         */
277         template <typename K, typename Less>
278         iterator contains( K const& key, Less pred )
279         {
280             CDS_UNUSED( pred );
281             return base_class::contains( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>() );
282         }
283         //@cond
284         template <typename K, typename Less>
285         CDS_DEPRECATED("deprecated, use contains()")
286         iterator find_with( K const& key, Less pred )
287         {
288             return contains( key, pred );
289         }
290         //@endcond
291
292         /// Checks if the map is empty
293         /**
294             Emptiness is checked by item counting: if item count is zero then the map is empty.
295             Thus, the correct item counting feature is an important part of Michael's map implementation.
296         */
297         bool empty() const
298         {
299             return base_class::empty();
300         }
301
302         /// Returns item count in the map
303         size_t size() const
304         {
305             return base_class::size();
306         }
307
308         /// Returns internal statistics
309         stat const& statistics() const
310         {
311             return base_class::statistics();
312         }
313     };
314 }}  // namespace cds::container
315
316
317 #endif // #ifndef CDSLIB_CONTAINER_SPLIT_LIST_MAP_NOGC_H