NCBI C++ ToolKit
compile_time.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef __COMPILE_TIME_HPP_INCLUDED__
2 #define __COMPILE_TIME_HPP_INCLUDED__
3 
4 /* $Id: compile_time.hpp 98669 2022-12-19 14:38:17Z gotvyans $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Sergiy Gotvyanskyy
30  *
31  * File Description:
32  *
33  * const_map -- constexpr equivalent of std::map
34  *
35  *
36  */
37 
39 
40 #ifdef NCBI_HAVE_CXX17
41 # include "impl/ct_bitset_cxx17.hpp"
42 #else
43 # include "impl/ct_bitset_cxx14.hpp"
44 #endif
45 
46 
47 
48 namespace ct
49 {
50  using namespace compile_time_bits;
51 
52  template<typename _Key, typename _Ty, typename _Traits, typename _Multi=tag_DuplicatesNo, typename _Backend = void>
53  class const_map_impl: public const_set_map_base<_Traits, _Backend, _Multi>
54  { // ordered or unordered compile time map
55  public:
58 
59  using init_type = typename _Traits::init_type;
61 
62  using value_type = typename _MyBase::value_type;
63  using key_type = typename value_type::first_type;
64  using mapped_type = typename value_type::second_type;
65 
66  using _MyBase::_MyBase;
67 
68  const mapped_type& at(intermediate _key) const
69  {
70  auto it = _MyBase::find(_key);
71  if (it == _MyBase::end())
72  throw std::out_of_range("invalid const_map<K, T> key");
73 
74  return it->second;
75  }
76 
77  const mapped_type& operator[](intermediate _key) const
78  {
79  return at(_key);
80  }
81 
82  template<size_t N, typename _Enabled = std::enable_if<!_MyBase::is_presorted, init_type>>
83  static constexpr auto construct(typename _Enabled::type const (&init)[N])
84  {
85  return construct(make_array(init));
86  }
87  template<size_t N, typename _Enabled = std::enable_if<!_MyBase::is_presorted, init_type>>
89  {
90  auto backend=_MyBase::make_backend(init);
91  return const_map_impl<_Key, _Ty, _Traits, _Multi, decltype(backend)>{backend};
92  }
93  template<typename _Enabled = std::enable_if<_MyBase::is_presorted, _MyType>>
94  static constexpr typename _Enabled::type construct(const const_vector<init_type>& init)
95  {
96  auto backend=_MyBase::presorted(init);
97  return const_map_impl<_Key, _Ty, _Traits, _Multi, decltype(backend)>{backend};
98  }
99  protected:
100  };
101 
102  template<typename _Key, typename _Ty>
104 
105  template<typename _Key, typename _Ty>
107 
108  template<typename _Key, typename _Ty>
113 
114  template<typename _Ty, typename _Backend=void>
115  class const_set: public const_set_map_base<simple_sort_traits<_Ty>, _Backend>
116  {
117  public:
120 
121  using init_type = typename _MyBase::init_type;
122 
123  using value_type = typename _MyBase::value_type;
125 
126  using _MyBase::_MyBase;
127 
128  template<size_t N>
129  static constexpr auto construct(init_type const (&init)[N])
130  {
131  return construct(make_array(init));
132  }
133  template<size_t N>
134  static constexpr auto construct(const const_array<init_type, N>& init)
135  {
136  auto backend=_MyBase::make_backend(init);
137  return const_set<_Ty, decltype(backend)>{backend};
138  }
139  };
140 
141  template<typename T1, typename T2>
143  {
146 
150 
153 
154  template<size_t N>
155  static constexpr auto construct(init_type const (&init)[N])
156  {
157  return construct(make_array(init));
158  }
159  template<size_t N>
160  static constexpr auto construct(const const_array<init_type, N>& init)
161  {
162  return std::make_pair(
163  map_type1::construct(init),
164  map_type2::construct(init)
165  );
166  }
167  };
168 
169  template<typename _Ty>
171 
172  template<typename _Key, typename _Ty>
174 
175  template<typename T1, typename T2>
177 
178  template<typename _Init, typename _Elem=array_elem_t<_Init>>
179  constexpr auto sort(_Init&& init)
180  {
182  return std::get<1>(sorter::sort(tag_SortByValues{}, std::forward<_Init>(init)));
183  }
184 
185 }
186 
187 #ifdef const_array
188 # undef const_array
189 #endif
190 #ifdef const_tuple
191 # undef const_tuple
192 #endif
193 
194 // user can define some specific debug instructions
195 #ifndef DEBUG_MAKE_CONST_MAP
196 # define DEBUG_MAKE_CONST_MAP(name)
197 #endif
198 #ifndef DEBUG_MAKE_TWOWAY_CONST_MAP
199 # define DEBUG_MAKE_TWOWAY_CONST_MAP(name)
200 #endif
201 #ifndef DEBUG_MAKE_CONST_SET
202 # define DEBUG_MAKE_CONST_SET(name)
203 #endif
204 
205 // Some compilers (Clang < 3.9, GCC-7) still cannot deduce template parameter N for aggregate initialiazed arrays
206 // so we have to use two step initialization. This doesn't impact neither of compile time, run time or memory footprint
207 //
208 
209 #define MAKE_CONST_MAP(name, type1, type2, ...) \
210  static constexpr auto name = ct::const_map<type1, type2>::construct(__VA_ARGS__); \
211  DEBUG_MAKE_CONST_MAP(name)
212 
213 #define MAKE_TWOWAY_CONST_MAP(name, type1, type2, ...) \
214  static constexpr auto name = ct::const_map_twoway<type1, type2>::construct(__VA_ARGS__); \
215  DEBUG_MAKE_TWOWAY_CONST_MAP(name)
216 
217 #define MAKE_CONST_SET(name, type, ...) \
218  static constexpr ct::const_set<type>::init_type name ## _init [] = __VA_ARGS__; \
219  static constexpr auto name = ct::const_set<type>::construct(name ## _init); \
220  DEBUG_MAKE_CONST_SET(name)
221 
222 #define MAKE_CONST_SET1(name, type, ...) \
223  static constexpr auto name = ct::const_set<type>::construct(__VA_ARGS__); \
224  DEBUG_MAKE_CONST_SET(name)
225 
226 #ifdef CT_USE_STD_MAP
227 # define MAKE_CONST_MAP_COMPAT(name, type1, type2, ...) static const std::map<type1, type2> name = __VA_ARGS__;
228 #else
229 # define MAKE_CONST_MAP_COMPAT MAKE_CONST_MAP
230 #endif
231 
232 
233 #endif
typename _Traits::init_type init_type
typename _Traits::key_type intermediate
static constexpr auto construct(typename _Enabled::type const (&init)[N])
typename value_type::second_type mapped_type
typename _MyBase::intermediate intermediate
typename value_type::first_type key_type
const mapped_type & at(intermediate _key) const
typename _MyBase::value_type value_type
const mapped_type & operator[](intermediate _key) const
typename _Traits::init_type init_type
static constexpr auto construct(const std::array< typename _Enabled::type, N > &init)
static constexpr _Enabled::type construct(const const_vector< init_type > &init)
value_type key_type
typename _MyBase::value_type value_type
static constexpr auto construct(const std::array< init_type, N > &init)
typename _MyBase::init_type init_type
static constexpr auto construct(init_type const (&init)[N])
static void DLIST_NAME() init(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:40
static int type
Definition: getdata.c:31
std::false_type tag_DuplicatesNo
constexpr auto make_array(T &&a)
std::true_type tag_DuplicatesYes
std::false_type tag_SortByValues
constexpr auto sort(_Init &&init)
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
typename pair_type::init_type init_type
typename straight_traits::init_type init_type
static constexpr auto construct(const std::array< init_type, N > &init)
static constexpr auto construct(init_type const (&init)[N])
#define N
Definition: crc32.c:57
Modified on Wed Apr 17 13:09:27 2024 by modify_doxy.py rev. 669887