Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JMultiKey.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JMULTIKEY__
2#define __JTOOLS__JMULTIKEY__
3
4#include <cstddef>
5#include <istream>
6#include <ostream>
7#include <utility>
8#include <array>
9#include <type_traits>
10
11#include "JLang/JClass.hh"
12#include "JLang/JComparable.hh"
13#include "JLang/JManip.hh"
14
15
16/**
17 * \author mdejong
18 */
19
20namespace JTOOLS {}
21namespace JPP { using namespace JTOOLS; }
22
23namespace JTOOLS {
24
26 using JLANG::JClass;
27
28
29 /**
30 * Multidimensional key.
31 *
32 * This class reproduces the key of a multidimensional map.
33 * The individual data members can be accessed as, e.g:
34 * <pre>
35 * JMultiKey<3, key_type> key;
36 *
37 * key[[.second].second].first;
38 * </pre>
39 */
40 template<unsigned int N, class JKey_t>
41 class JMultiKey :
42 public std::pair<JKey_t, JMultiKey<N-1, JKey_t> >,
43 public JComparable< JMultiKey<N, JKey_t> >
44 {
45 public:
46
47 typedef JKey_t key_type;
48 typedef JMultiKey<N-1, JKey_t> mapped_type;
51
52 typedef typename std::conditional<std::is_const<key_type>::value,
53 typename std::remove_const<key_type>::type,
54 typename std::add_const <key_type>::type>::type U; // opposite constness
55
56 /**
57 * Default constructor.
58 */
60 pair_type()
61 {}
62
63
64 /**
65 * Append constructor.
66 * The secondary key is appended to the end of the primary keys.
67 *
68 * \param first primary keys
69 * \param second secondary key
70 */
72 typename JClass<key_type> ::argument_type second) :
73 pair_type(first.first, mapped_type(first, second))
74 {}
75
76
77 /**
78 * Insert constructor.
79 * The primary key is inserted at the start of the secondary keys.
80 *
81 * \param first primary key
82 * \param second secondary keys
83 */
85 typename JClass<mapped_type>::argument_type second) :
86 pair_type(first, second)
87 {}
88
89
90 /**
91 * Shift constructor.
92 * The first of the primary keys is removed and the secondary key is appended to the end of the remainder of the primary keys.
93 *
94 * \param first primary keys
95 * \param second secondary key
96 */
97 JMultiKey(const JMultiKey& first, typename JClass<key_type>::argument_type second) :
98 JMultiKey(first.second, second)
99 {}
100
101
102 /**
103 * Copy constructor.
104 *
105 * \param key key
106 */
108 pair_type(key.first, key.second)
109 {}
110
111
112 /**
113 * Copy constructor.
114 *
115 * \param array array
116 */
118 pair_type(array[0], pop_front(array, std::make_index_sequence<N>{}))
119 {}
120
121
122 /**
123 * Type conversion operator.
124 *
125 * \return array
126 */
127 operator array_type() const
128 {
129 return to_array(this->first, this->second, std::make_index_sequence<N-1>{});
130 }
131
132
133 /**
134 * Less than method.
135 *
136 * \param key key
137 * \return true if this key less than given key; else false
138 */
139 bool less(const JMultiKey& key) const
140 {
141 if (this->first == key.first)
142 return this->second.less(key.second);
143 else
144 return this->first < key.first;
145 }
146
147
148 /**
149 * Get frontend key.
150 *
151 * \return frontend key
152 */
154 {
155 return JMultiKey<0, key_type>::front(*this);
156 }
157
158
159 /**
160 * Get backend key.
161 *
162 * \return backend key
163 */
165 {
166 return this->second.back();
167 }
168
169
170 /**
171 * Read key from input.
172 *
173 * \param in input stream
174 * \param key key
175 * \return input stream
176 */
177 friend inline std::istream& operator>>(std::istream& in, JMultiKey& key)
178 {
179 in >> key.first;
180 in >> key.second;
181
182 return in;
183 }
184
185
186 /**
187 * Write key to output.
188 *
189 * \param out output stream
190 * \param key key
191 * \return output stream
192 */
193 friend inline std::ostream& operator<<(std::ostream& out, const JMultiKey& key)
194 {
195 const JFormat format(out, getFormat< JMultiKey<0, key_type> >(JFormat_t(9, 3, std::ios::fixed | std::ios::showpos)));
196
197 out << format << key.first;
198 out << ' ';
199 out << key.second;
200
201 return out;
202 }
203
204
205 private:
206 /**
207 * Auxiliary method to convert keys to array.
208 *
209 * \param key key
210 * \param array array
211 * \return array
212 */
213 template<size_t ...i>
214 static array_type to_array(const key_type& key,
215 const typename mapped_type::array_type& array,
216 std::index_sequence<i...>)
217 {
218 return {key, array[i]...};
219 }
220
221
222 /**
223 * Auxiliary method to pop front array.
224 *
225 * \param key key
226 * \param args values
227 */
228 template<class ...Args>
229 std::array<key_type, N-1> pop_front(const key_type key, const Args& ...args)
230 {
231 return std::array<key_type, N-1>({args...});
232 }
233
234
235 /**
236 * Pop front array.
237 *
238 * \param array array
239 * \param ls indices of array
240 */
241 template<size_t ...i>
242 std::array<key_type, N-1> pop_front(const std::array<key_type, N>& array, const std::index_sequence<i...>& ls)
243 {
244 return pop_front(array[i]...);
245 }
246 };
247
248
249 /**
250 * One-dimensional key.
251 */
252 template<class JKey_t>
253 class JMultiKey<1, JKey_t> :
254 public JComparable< JMultiKey<1, JKey_t> >
255 {
256 public:
257
258 typedef JKey_t key_type;
261
262 typedef typename std::conditional<std::is_const<key_type>::value,
263 typename std::remove_const<key_type>::type,
264 typename std::add_const <key_type>::type>::type U; // opposite constness
265
266
267 /**
268 * Default constructor.
269 */
271 first()
272 {}
273
274
275 /**
276 * Append onstructor.
277 * The secondary key is appended to the end of the primary key.
278 *
279 * \param first primary key
280 * \param second secondary key
281 */
283 typename JClass<key_type> ::argument_type second) :
284 first(second)
285 {}
286
287
288 /**
289 * Insert constructor.
290 * The primary key is inserted at the start of the secondary key.
291 *
292 * \param first primary key
293 * \param second secondary key
294 */
296 typename JClass<mapped_type>::argument_type second) :
297 first(first)
298 {}
299
300
301 /**
302 * Shift constructor.
303 * The first of the primary keys is removed and the secondary key is appended to the end of the remainder of the primary keys.
304 *
305 * \param first primary keys
306 * \param second secondary key
307 */
308 JMultiKey(const JMultiKey& first, typename JClass<key_type>::argument_type second) :
309 first(second)
310 {}
311
312
313 /**
314 * Constructor.
315 *
316 * \param first key
317 */
319 first(first)
320 {}
321
322
323 /**
324 * Copy constructor.
325 *
326 * \param key key
327 */
329 first(key.first)
330 {}
331
332
333 /**
334 * Copy constructor.
335 *
336 * \param array array
337 */
339 first(array[0])
340 {}
341
342
343 /**
344 * Type conversion operator.
345 *
346 * \return array
347 */
348 operator array_type() const
349 {
350 return {this->first};
351 }
352
353
354 /**
355 * Less than method.
356 *
357 * \param key key
358 * \return true if this key less than given key; else false
359 */
360 bool less(const JMultiKey& key) const
361 {
362 return this->first < key.first;
363 }
364
365
366 /**
367 * Get backend key.
368 *
369 * \return backend key
370 */
372 {
373 return this->first;
374 }
375
376
377 /**
378 * Read key from input.
379 *
380 * \param in input stream
381 * \param key key
382 * \return input stream
383 */
384 friend inline std::istream& operator>>(std::istream& in, JMultiKey& key)
385 {
386 in >> key.first;
387
388 return in;
389 }
390
391
392 /**
393 * Write key to output.
394 *
395 * \param out output stream
396 * \param key key
397 * \return output stream
398 */
399 friend inline std::ostream& operator<<(std::ostream& out, const JMultiKey& key)
400 {
401 const JFormat format(out, getFormat< JMultiKey<0, key_type> >(JFormat_t(9, 3, std::ios::fixed | std::ios::showpos)));
402
403 out << format << key.first;
404
405 return out;
406 }
407
408
410 };
411
412
413 /**
414 * Empty key.
415 */
416 template<class JKey_t>
417 struct JMultiKey<0, JKey_t>
418 {
419 typedef JKey_t key_type;
420
421
422 /**
423 * Get frontend key.
424 *
425 * \param key key
426 * \return frontend key
427 */
428 template<unsigned int N>
429 static inline JMultiKey<N-1, key_type> front(const JMultiKey<N, key_type>& key)
430 {
431 return JMultiKey<N-1, key_type>(key.first, key.second.front());
432 }
433
434
435 /**
436 * Get frontend key.
437 *
438 * \param key key
439 * \return frontend key
440 */
442 {
443 return JMultiKey<1, key_type>(key.first);
444 }
445 };
446}
447
448#endif
I/O manipulators.
JFormat_t & getFormat()
Get format for given type.
Definition JManip.hh:682
key_type back() const
Get backend key.
Definition JMultiKey.hh:371
std::conditional< std::is_const< key_type >::value, typenamestd::remove_const< key_type >::type, typenamestd::add_const< key_type >::type >::type U
Definition JMultiKey.hh:264
friend std::istream & operator>>(std::istream &in, JMultiKey &key)
Read key from input.
Definition JMultiKey.hh:384
JMultiKey(const JMultiKey &first, typename JClass< key_type >::argument_type second)
Shift constructor.
Definition JMultiKey.hh:308
JMultiKey(const std::array< key_type, 1 > &array)
Copy constructor.
Definition JMultiKey.hh:338
JMultiKey< 0, key_type > mapped_type
Definition JMultiKey.hh:259
bool less(const JMultiKey &key) const
Less than method.
Definition JMultiKey.hh:360
JMultiKey(typename JClass< key_type > ::argument_type first, typename JClass< mapped_type >::argument_type second)
Insert constructor.
Definition JMultiKey.hh:295
friend std::ostream & operator<<(std::ostream &out, const JMultiKey &key)
Write key to output.
Definition JMultiKey.hh:399
JMultiKey()
Default constructor.
Definition JMultiKey.hh:270
JMultiKey(typename JClass< mapped_type >::argument_type first, typename JClass< key_type > ::argument_type second)
Append onstructor.
Definition JMultiKey.hh:282
JMultiKey(const JMultiKey< 1, U > &key)
Copy constructor.
Definition JMultiKey.hh:328
std::array< key_type, 1 > array_type
Definition JMultiKey.hh:260
JMultiKey(typename JClass< key_type >::argument_type first)
Constructor.
Definition JMultiKey.hh:318
Multidimensional key.
Definition JMultiKey.hh:44
static array_type to_array(const key_type &key, const typename mapped_type::array_type &array, std::index_sequence< i... >)
Auxiliary method to convert keys to array.
Definition JMultiKey.hh:214
JMultiKey(const JMultiKey &first, typename JClass< key_type >::argument_type second)
Shift constructor.
Definition JMultiKey.hh:97
friend std::istream & operator>>(std::istream &in, JMultiKey &key)
Read key from input.
Definition JMultiKey.hh:177
JMultiKey(const std::array< key_type, N > &array)
Copy constructor.
Definition JMultiKey.hh:117
std::array< key_type, N-1 > pop_front(const std::array< key_type, N > &array, const std::index_sequence< i... > &ls)
Pop front array.
Definition JMultiKey.hh:242
bool less(const JMultiKey &key) const
Less than method.
Definition JMultiKey.hh:139
key_type back() const
Get backend key.
Definition JMultiKey.hh:164
JMultiKey< N-1, key_type > front() const
Get frontend key.
Definition JMultiKey.hh:153
JMultiKey(typename JClass< key_type > ::argument_type first, typename JClass< mapped_type >::argument_type second)
Insert constructor.
Definition JMultiKey.hh:84
JMultiKey< N-1, JKey_t > mapped_type
Definition JMultiKey.hh:48
std::pair< key_type, mapped_type > pair_type
Definition JMultiKey.hh:49
std::array< key_type, N > array_type
Definition JMultiKey.hh:50
std::array< key_type, N-1 > pop_front(const key_type key, const Args &...args)
Auxiliary method to pop front array.
Definition JMultiKey.hh:229
friend std::ostream & operator<<(std::ostream &out, const JMultiKey &key)
Write key to output.
Definition JMultiKey.hh:193
std::conditional< std::is_const< key_type >::value, typenamestd::remove_const< key_type >::type, typenamestd::add_const< key_type >::type >::type U
Definition JMultiKey.hh:54
JMultiKey(typename JClass< mapped_type >::argument_type first, typename JClass< key_type > ::argument_type second)
Append constructor.
Definition JMultiKey.hh:71
JMultiKey(const JMultiKey< N, U > &key)
Copy constructor.
Definition JMultiKey.hh:107
JMultiKey()
Default constructor.
Definition JMultiKey.hh:59
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Data structure for format specifications.
Definition JManip.hh:524
Auxiliary class to temporarily define format specifications.
Definition JManip.hh:636
Template for generic class types.
Definition JClass.hh:80
JArgument< T >::argument_type argument_type
Definition JClass.hh:82
Template definition of auxiliary base class for comparison of data structures.
static JMultiKey< N-1, key_type > front(const JMultiKey< N, key_type > &key)
Get frontend key.
Definition JMultiKey.hh:429
static JMultiKey< 1, key_type > front(const JMultiKey< 2, key_type > &key)
Get frontend key.
Definition JMultiKey.hh:441