Merge pull request #24 from krinkinmu/unordered-list-wip
[libcds.git] / cds / container / details / make_lazy_kvlist.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_CONTAINER_DETAILS_MAKE_LAZY_KVLIST_H
4 #define CDSLIB_CONTAINER_DETAILS_MAKE_LAZY_KVLIST_H
5
6 #include <cds/details/binary_functor_wrapper.h>
7
8 namespace cds { namespace container {
9
10     //@cond
11     namespace details {
12
13         template <class GC, typename K, typename T, class Traits>
14         struct make_lazy_kvlist
15         {
16             typedef Traits original_type_traits;
17
18             typedef GC      gc;
19             typedef K       key_type;
20             typedef T       mapped_type;
21             typedef std::pair<key_type const, mapped_type> value_type;
22
23             struct node_type: public intrusive::lazy_list::node<gc, typename original_type_traits::lock_type>
24             {
25                 value_type   m_Data;
26
27                 template <typename Q>
28                 node_type( Q const& key )
29                     : m_Data( key, mapped_type() )
30                 {}
31
32                 template <typename Q, typename R>
33                 explicit node_type( std::pair<Q, R> const& pair )
34                     : m_Data( pair )
35                 {}
36
37                 template <typename Q, typename R>
38                 node_type( Q const& key, R const& value )
39                     : m_Data( key, value )
40                 {}
41
42                 template <typename Ky, typename... Args>
43                 node_type( Ky&& key, Args&&... args )
44                     : m_Data( std::forward<Ky>( key ), std::move( mapped_type( std::forward<Args>( args )... ) ) )
45                 {}
46             };
47
48             typedef typename original_type_traits::allocator::template rebind<node_type>::other allocator_type;
49             typedef cds::details::Allocator< node_type, allocator_type > cxx_allocator;
50
51             struct node_deallocator
52             {
53                 void operator ()( node_type * pNode )
54                 {
55                     cxx_allocator().Delete( pNode );
56                 }
57             };
58
59             struct key_field_accessor {
60                 key_type const& operator()( node_type const& pair )
61                 {
62                     return pair.m_Data.first;
63                 }
64             };
65
66             typedef typename opt::details::make_comparator< key_type, original_type_traits >::type key_comparator;
67             typedef typename opt::details::make_equal_to< key_type, original_type_traits >::type equal_to_comparator;
68
69             template <typename Less>
70             struct less_wrapper {
71                 typedef cds::details::compare_wrapper< node_type, cds::opt::details::make_comparator_from_less<Less>, key_field_accessor >    type;
72             };
73
74             template <typename Equal>
75             struct equal_to_wrapper {
76                 typedef cds::details::predicate_wrapper< node_type, Equal, key_field_accessor >    type;
77             };
78
79             struct intrusive_traits: public original_type_traits
80             {
81                 typedef intrusive::lazy_list::base_hook< opt::gc<gc> >  hook;
82                 typedef node_deallocator disposer;
83                 typedef cds::details::compare_wrapper< node_type, key_comparator, key_field_accessor > compare;
84                 typedef cds::details::predicate_wrapper< node_type, equal_to_comparator, key_field_accessor > equal_to;
85                 static const opt::link_check_type link_checker = cds::intrusive::lazy_list::traits::link_checker;
86             };
87
88             typedef intrusive::LazyList<gc, node_type, intrusive_traits>  type;
89         };
90     }   // namespace details
91     //@endcond
92
93 }}  // namespace cds::container
94
95 #endif  // #ifndef CDSLIB_CONTAINER_DETAILS_MAKE_LAZY_KVLIST_H