mdds
types_util.hpp
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * Copyright (c) 2022 Kohei Yoshida
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  ************************************************************************/
28 
29 #pragma once
30 
31 #include <vector>
32 
33 namespace mdds { namespace mtv { namespace detail {
34 
35 template<typename T>
37 {
38  using yes_type = char;
39  using no_type = int;
40 
41  template<typename U, std::size_t (U::*)() const>
43  {
44  };
45 
46  template<typename U>
47  static yes_type test(test_has_method<U, &U::capacity>*);
48  template<typename U>
49  static no_type test(...);
50 
51  using type = std::conditional_t<sizeof(test<T>(0)) == sizeof(yes_type), std::true_type, std::false_type>;
52 };
53 
54 template<typename T>
55 std::size_t get_block_capacity(const T& blk, std::true_type)
56 {
57  return blk.capacity();
58 }
59 
60 template<typename T>
61 std::size_t get_block_capacity(const T&, std::false_type)
62 {
63  return 0;
64 }
65 
66 template<typename T>
67 std::size_t get_block_capacity(const T& blk)
68 {
69  typename has_capacity_method<T>::type v;
70  return get_block_capacity(blk, v);
71 }
72 
73 template<typename T>
75 {
76  using yes_type = char;
77  using no_type = int;
78 
79  template<typename U, void (U::*)(typename T::size_type)>
81  {
82  };
83 
84  template<typename U>
85  static yes_type test(test_has_method<U, &U::reserve>*);
86  template<typename U>
87  static no_type test(...);
88 
89  using type = std::conditional_t<sizeof(test<T>(0)) == sizeof(yes_type), std::true_type, std::false_type>;
90 };
91 
92 template<typename T>
93 void reserve(T& blk, typename T::size_type size, std::true_type)
94 {
95  return blk.reserve(size);
96 }
97 
98 template<typename T>
99 void reserve(T&, typename T::size_type, std::false_type)
100 {}
101 
102 template<typename T>
103 void reserve(T& blk, typename T::size_type size)
104 {
105  typename has_reserve_method<T>::type v;
106  reserve(blk, size, v);
107 }
108 
109 template<typename T>
111 {
112  using yes_type = char;
113  using no_type = int;
114 
115  template<typename U, void (U::*)()>
117  {
118  };
119 
120  template<typename U>
121  static yes_type test(test_has_method<U, &U::shrink_to_fit>*);
122  template<typename U>
123  static no_type test(...);
124 
125  using type = std::conditional_t<sizeof(test<T>(0)) == sizeof(yes_type), std::true_type, std::false_type>;
126 };
127 
128 template<typename T>
129 void shrink_to_fit(T& blk, std::true_type)
130 {
131  return blk.shrink_to_fit();
132 }
133 
134 template<typename T>
135 void shrink_to_fit(T&, std::false_type)
136 {}
137 
138 template<typename T>
139 void shrink_to_fit(T& blk)
140 {
141  typename has_shrink_to_fit_method<T>::type v;
142  shrink_to_fit(blk, v);
143 }
144 
145 template<typename T>
147 {
148  using type = std::false_type;
149 };
150 
151 template<>
152 struct is_std_vector_bool_store<std::vector<bool>>
153 {
154  using type = std::true_type;
155 };
156 
157 template<typename Blk>
159 {
160  using type = typename is_std_vector_bool_store<typename Blk::store_type>::type;
161 };
162 
163 }}} // namespace mdds::mtv::detail
164 
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: types_util.hpp:74
Definition: types_util.hpp:146
Definition: types_util.hpp:36
Definition: types_util.hpp:158
Definition: flat_segment_tree.hpp:46
Definition: types_util.hpp:110