Removed redundant spaces
[libcds.git] / cds / container / details / make_lazy_kvlist.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef CDSLIB_CONTAINER_DETAILS_MAKE_LAZY_KVLIST_H
32 #define CDSLIB_CONTAINER_DETAILS_MAKE_LAZY_KVLIST_H
33
34 #include <cds/details/binary_functor_wrapper.h>
35
36 namespace cds { namespace container {
37
38     //@cond
39     namespace details {
40
41         template <class GC, typename K, typename T, class Traits>
42         struct make_lazy_kvlist
43         {
44             typedef Traits original_type_traits;
45
46             typedef GC      gc;
47             typedef K       key_type;
48             typedef T       mapped_type;
49             typedef std::pair<key_type const, mapped_type> value_type;
50
51             struct node_type: public intrusive::lazy_list::node<gc, typename original_type_traits::lock_type>
52             {
53                 value_type   m_Data;
54
55                 node_type( key_type const& key )
56                     : m_Data( key, mapped_type())
57                 {}
58
59                 template <typename Q>
60                 node_type( Q const& key )
61                     : m_Data( key_type( key ), mapped_type())
62                 {}
63
64                 template <typename Q, typename R>
65                 explicit node_type( std::pair<Q, R> const& pair )
66                     : m_Data( pair )
67                 {}
68
69                 node_type( key_type const& key, mapped_type const& value )
70                     : m_Data( key, value )
71                 {}
72
73                 template <typename R>
74                 node_type( key_type const& key, R const& value )
75                     : m_Data( key, mapped_type( value ))
76                 {}
77
78                 template <typename Q>
79                 node_type( Q const& key, mapped_type const& value )
80                     : m_Data( key_type( key ), value )
81                 {}
82
83                 template <typename Q, typename R>
84                 node_type( Q const& key, R const& value )
85                     : m_Data( key_type( key ), mapped_type( value ))
86                 {}
87
88                 template <typename Ky, typename... Args>
89                 node_type( Ky&& key, Args&&... args )
90                     : m_Data( key_type( std::forward<Ky>( key )), std::move( mapped_type( std::forward<Args>( args )... )) )
91                 {}
92             };
93
94             typedef typename original_type_traits::allocator::template rebind<node_type>::other allocator_type;
95             typedef cds::details::Allocator< node_type, allocator_type > cxx_allocator;
96
97             struct node_deallocator
98             {
99                 void operator ()( node_type * pNode )
100                 {
101                     cxx_allocator().Delete( pNode );
102                 }
103             };
104
105             struct key_field_accessor {
106                 key_type const& operator()( node_type const& pair )
107                 {
108                     return pair.m_Data.first;
109                 }
110             };
111
112             typedef typename std::conditional< original_type_traits::sort,
113                 typename opt::details::make_comparator< value_type, original_type_traits >::type,
114                 typename opt::details::make_equal_to< value_type, original_type_traits >::type
115             >::type key_comparator;
116
117
118             template <typename Less>
119             struct less_wrapper {
120                 typedef cds::details::compare_wrapper< node_type, cds::opt::details::make_comparator_from_less<Less>, key_field_accessor >    type;
121             };
122
123             template <typename Equal>
124             struct equal_to_wrapper {
125                 typedef cds::details::predicate_wrapper< node_type, Equal, key_field_accessor >    type;
126             };
127
128             struct intrusive_traits: public original_type_traits
129             {
130                 typedef intrusive::lazy_list::base_hook< opt::gc<gc>, opt::lock_type< typename original_type_traits::lock_type >> hook;
131                 typedef node_deallocator disposer;
132
133                 typedef typename std::conditional< std::is_same< typename original_type_traits::equal_to, cds::opt::none >::value,
134                     cds::opt::none,
135                     typename equal_to_wrapper< typename original_type_traits::equal_to >::type
136                 >::type equal_to;
137
138                 typedef typename std::conditional<
139                     original_type_traits::sort
140                         || !std::is_same< typename original_type_traits::compare, cds::opt::none >::value
141                         || !std::is_same< typename original_type_traits::less, cds::opt::none >::value,
142                     cds::details::compare_wrapper<
143                         node_type,
144                         typename opt::details::make_comparator< value_type, original_type_traits >::type,
145                         key_field_accessor
146                     >,
147                     cds::opt::none
148                 >::type compare;
149
150                 static const opt::link_check_type link_checker = cds::intrusive::lazy_list::traits::link_checker;
151             };
152
153             typedef intrusive::LazyList<gc, node_type, intrusive_traits>  type;
154         };
155     }   // namespace details
156     //@endcond
157
158 }}  // namespace cds::container
159
160 #endif  // #ifndef CDSLIB_CONTAINER_DETAILS_MAKE_LAZY_KVLIST_H