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 102912 2024-08-06 12:23:26Z 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 #include "impl/ct_bitset_cxx17.hpp"
41 
42 namespace ct
43 {
44  using namespace compile_time_bits;
45 
46  template<typename _Key, typename _Ty, typename _Traits, typename _Multi=tag_DuplicatesNo, typename _Backend = void>
47  class const_map_impl: public const_set_map_base<_Traits, _Backend, _Multi>
48  { // ordered or unordered compile time map
49  public:
52 
53  using init_type = typename _Traits::init_type;
55 
56  using value_type = typename _MyBase::value_type;
57  using key_type = typename value_type::first_type;
58  using mapped_type = typename value_type::second_type;
59 
60  using _MyBase::_MyBase;
61 
62  const mapped_type& at(intermediate _key) const
63  {
64  auto it = _MyBase::find(_key);
65  if (it == _MyBase::end())
66  throw std::out_of_range("invalid const_map<K, T> key");
67 
68  return it->second;
69  }
70 
71  const mapped_type& operator[](intermediate _key) const
72  {
73  return at(_key);
74  }
75 
76  template<size_t N, typename _Enabled = std::enable_if<!_MyBase::is_presorted, init_type>>
77  static constexpr auto construct(typename _Enabled::type const (&init)[N])
78  {
79  return construct(std::to_array(init));
80  }
81  template<size_t N, typename _Enabled = std::enable_if<!_MyBase::is_presorted, init_type>>
82  static constexpr auto construct(const const_array<typename _Enabled::type, N>& init)
83  {
84  auto backend=_MyBase::make_backend(init);
85  return const_map_impl<_Key, _Ty, _Traits, _Multi, decltype(backend)>{backend};
86  }
87  template<typename _Enabled = std::enable_if<_MyBase::is_presorted, _MyType>>
88  static constexpr typename _Enabled::type construct(const const_vector<init_type>& init)
89  {
90  auto backend=_MyBase::presorted(init);
91  return const_map_impl<_Key, _Ty, _Traits, _Multi, decltype(backend)>{backend};
92  }
93  protected:
94  };
95 
96  template<typename _Key, typename _Ty>
98 
99  template<typename _Key, typename _Ty>
101 
102  template<typename _Key, typename _Ty>
107 
108  template<typename _Ty, typename _Backend=void>
109  class const_set: public const_set_map_base<simple_sort_traits<_Ty>, _Backend>
110  {
111  public:
114 
115  using init_type = typename _MyBase::init_type;
116 
117  using value_type = typename _MyBase::value_type;
119 
120  using _MyBase::_MyBase;
121 
122  template<size_t N>
123  static constexpr auto construct(init_type const (&init)[N])
124  {
125  return construct(std::to_array(init));
126  }
127  template<size_t N>
128  static constexpr auto construct(const const_array<init_type, N>& init)
129  {
130  auto backend=_MyBase::make_backend(init);
131  return const_set<_Ty, decltype(backend)>{backend};
132  }
133  };
134 
135  template<typename T1, typename T2>
137  {
140 
144 
147 
148  template<size_t N>
149  static constexpr auto construct(init_type const (&init)[N])
150  {
151  return construct(std::to_array(init));
152  }
153  template<size_t N>
154  static constexpr auto construct(const const_array<init_type, N>& init)
155  {
156  return std::make_pair(
157  map_type1::construct(init),
158  map_type2::construct(init)
159  );
160  }
161  };
162 
163  template<typename _Ty>
165 
166  template<typename _Key, typename _Ty>
168 
169  template<typename T1, typename T2>
171 
172  template<typename _Init, typename _Elem=array_elem_t<_Init>>
173  constexpr auto sort(_Init&& init)
174  {
176  return std::get<1>(sorter::sort(tag_SortByValues{}, std::forward<_Init>(init)));
177  }
178 
179 }
180 
181 #ifdef const_array
182 # undef const_array
183 #endif
184 #ifdef const_tuple
185 # undef const_tuple
186 #endif
187 
188 // user can define some specific debug instructions
189 #ifndef DEBUG_MAKE_CONST_MAP
190 # define DEBUG_MAKE_CONST_MAP(name)
191 #endif
192 #ifndef DEBUG_MAKE_TWOWAY_CONST_MAP
193 # define DEBUG_MAKE_TWOWAY_CONST_MAP(name)
194 #endif
195 #ifndef DEBUG_MAKE_CONST_SET
196 # define DEBUG_MAKE_CONST_SET(name)
197 #endif
198 
199 // Some compilers (Clang < 3.9, GCC-7) still cannot deduce template parameter N for aggregate initialiazed arrays
200 // so we have to use two step initialization. This doesn't impact neither of compile time, run time or memory footprint
201 //
202 
203 #define MAKE_CONST_MAP(name, type1, type2, ...) \
204  static constexpr auto name = ct::const_map<type1, type2>::construct(__VA_ARGS__); \
205  DEBUG_MAKE_CONST_MAP(name)
206 
207 #define MAKE_TWOWAY_CONST_MAP(name, type1, type2, ...) \
208  static constexpr auto name = ct::const_map_twoway<type1, type2>::construct(__VA_ARGS__); \
209  DEBUG_MAKE_TWOWAY_CONST_MAP(name)
210 
211 #define MAKE_CONST_SET(name, type, ...) \
212  static constexpr ct::const_set<type>::init_type name ## _init [] = __VA_ARGS__; \
213  static constexpr auto name = ct::const_set<type>::construct(name ## _init); \
214  DEBUG_MAKE_CONST_SET(name)
215 
216 #define MAKE_CONST_SET1(name, type, ...) \
217  static constexpr auto name = ct::const_set<type>::construct(__VA_ARGS__); \
218  DEBUG_MAKE_CONST_SET(name)
219 
220 #ifdef CT_USE_STD_MAP
221 # define MAKE_CONST_MAP_COMPAT(name, type1, type2, ...) static const std::map<type1, type2> name = __VA_ARGS__;
222 #else
223 # define MAKE_CONST_MAP_COMPAT MAKE_CONST_MAP
224 #endif
225 
226 
227 #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
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 Fri Sep 20 14:58:00 2024 by modify_doxy.py rev. 669887