Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JArray.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JARRAY__
2#define __JTOOLS__JARRAY__
3
4#include <cstddef>
5#include <istream>
6#include <ostream>
7#include <array>
8#include <algorithm>
9#include <type_traits>
10
11#include "JIO/JSerialisable.hh"
12#include "JTools/JMultiKey.hh"
13#include "JMath/JMath.hh"
14#include "JLang/JAssert.hh"
15#include "JLang/JClass.hh"
16#include "JLang/JEquals.hh"
17#include "JLang/JManip.hh"
18
19
20/**
21 * \author mdejong
22 */
23
24namespace JTOOLS {}
25namespace JPP { using namespace JTOOLS; }
26
27namespace JTOOLS {
28
29 using JIO::JReader;
30 using JIO::JWriter;
31 using JMATH::JMath;
32 using JLANG::JEquals;
33
34
35 /**
36 * Wrapper data structure around std::array.
37 */
38 template<unsigned int N, class T>
39 struct JArray :
40 public std::array<T, N>,
41 public JMath < JArray<N,T> >,
42 public JEquals< JArray<N,T> >
43 {
44 public:
45
47
48 using typename array_type::value_type;
49 using typename array_type::size_type;
50 using typename array_type::difference_type;
51 using typename array_type::reference;
52 using typename array_type::const_reference;
53 using typename array_type::pointer;
54 using typename array_type::const_pointer;
55 using typename array_type::iterator;
56 using typename array_type::const_iterator;
57 using typename array_type::reverse_iterator;
58 using typename array_type::const_reverse_iterator;
59
61
62 typedef typename std::conditional<std::is_const<T>::value,
63 typename std::remove_const<T>::type,
64 typename std::add_const <T>::type>::type U; // opposite constness
65
66
67 /**
68 * Default constructor.
69 */
71 std::array<T, N>()
72 {}
73
74
75 /**
76 * Copy constructor.
77 *
78 * \param array array
79 */
80 template<unsigned int M>
81 JArray(const JArray<M, T>& array) :
82 std::array<T, N>(match(array))
83 {}
84
85
86 /**
87 * Copy constructor.
88 *
89 * \param array array
90 */
91 template<unsigned int M>
92 JArray(const JArray<M, U>& array) :
93 std::array<T, N>(match(convert(array)))
94 {}
95
96
97 /**
98 * Assemble constructor.
99 *
100 * \param value value
101 * \param args argument list
102 */
103 template<class ...Args>
104 JArray(argument_type value, const Args& ...args) :
105 std::array<T, N>({value, args...})
106 {}
107
108
109 /**
110 * Multi-copy constructor.
111 *
112 * \param p pointer to data
113 */
114 JArray(const T* p)
115 {
116 std::copy_n(p, N, this->data());
117 }
118
119
120 /**
121 * Combine constructor.
122 *
123 * \param first first array
124 * \param second second array
125 */
126 template<size_t M>
127 JArray(const std::array<T, M>& first, const std::array<T, N-M>& second) :
128 std::array<T, N>(combine(first, second))
129 {}
130
131
132 /**
133 * Copy and assemble constructor.
134 *
135 * \param array array
136 * \param value value
137 * \param args argument list
138 */
139 template<size_t M, class ...Args>
140 JArray(const std::array<T, M>& array, argument_type value, const Args& ...args) :
141 JArray(array, std::array<T, N-M>({value, args...}))
142 {}
143
144
145 /**
146 * Copy and assemble constructor.
147 *
148 * \param array array
149 * \param value value
150 * \param args argument list
151 */
152 template<size_t M, class ...Args>
153 JArray(const std::array<U, M>& array, argument_type value, const Args& ...args) :
154 JArray(convert(array), std::array<T, N-M>({value, args...}))
155 {}
156
157
158 /**
159 * Convert constructor.
160 *
161 * \param key key
162 */
164 std::array<T, N>(key)
165 {}
166
167
168 /**
169 * Convert constructor.
170 *
171 * \param key key
172 */
174 std::array<T, N>(convert((std::array<U, N>) key))
175 {}
176
177
178 /**
179 * Convert and combine constructor.
180 *
181 * \param key key
182 * \param array array
183 */
184 template<unsigned int M>
185 JArray(const JMultiKey<M, T>& key, const std::array<std::remove_const_t<T>, N-M>& array) :
186 JArray((std::array<T, M>) key, array)
187 {}
188
189
190 /**
191 * Convert and combine constructor.
192 *
193 * \param key key
194 * \param array array
195 */
196 template<unsigned int M>
197 JArray(const JMultiKey<M, U>& key, const std::array<std::remove_const_t<T>, N-M>& array) :
198 JArray(convert((std::array<U, M>) key), array)
199 {}
200
201
202 /**
203 * Convert and assemble constructor.
204 *
205 * \param key key
206 * \param value value
207 * \param args argument list
208 */
209 template<unsigned int M, class ...Args>
210 JArray(const JMultiKey<M, T>& key, argument_type value, const Args& ...args) :
211 JArray(key, std::array<T, N-M>({value, args...}))
212 {}
213
214
215 /**
216 * Convert and assemble constructor.
217 *
218 * \param key key
219 * \param value value
220 * \param args argument list
221 */
222 template<unsigned int M, class ...Args>
223 JArray(const JMultiKey<M, U>& key, argument_type value, const Args& ...args) :
224 JArray(key, std::array<T, N-M>({value, args...}))
225 {}
226
227
228 /**
229 * Make a copy in which the first element is removed.
230 *
231 * \return array
232 */
233 JArray<N-1, T> pop_front() const
234 {
235 return pop_front(std::make_index_sequence<N>{});
236 }
237
238
239 /**
240 * Make a copy in which the last element is removed.
241 *
242 * \return array
243 */
244 JArray<N-1, T> pop_back() const
245 {
246 return pop_back(std::make_index_sequence<N-1>{});
247 }
248
249
250 /**
251 * Make a copy and push back value.
252 *
253 * \param value value
254 * \return array
255 */
257 {
258 return push_back(value, std::make_index_sequence<N>{});
259 }
260
261
262 /**
263 * Negate array.
264 *
265 * \return this array
266 */
268 {
269 for (int i = 0; i != N; ++i) {
270 (*this)[i] = -(*this)[i];
271 }
272
273 return *this;
274 }
275
276
277 /**
278 * Add array.
279 *
280 * \param array array
281 * \return this array
282 */
283 JArray& add(const JArray& array)
284 {
285 for (int i = 0; i != N; ++i) {
286 (*this)[i] += array[i];
287 }
288
289 return *this;
290 }
291
292
293 /**
294 * Subtract array.
295 *
296 * \param array array
297 * \return this array
298 */
299 JArray& sub(const JArray& array)
300 {
301 for (int i = 0; i != N; ++i) {
302 (*this)[i] -= array[i];
303 }
304
305 return *this;
306 }
307
308
309 /**
310 * Scale array.
311 *
312 * \param factor multiplication factor
313 * \return this array
314 */
315 JArray& mul(const double factor)
316 {
317 for (int i = 0; i != N; ++i) {
318 (*this)[i] *= factor;
319 }
320
321 return *this;
322 }
323
324
325 /**
326 * Scale array.
327 *
328 * \param factor division factor
329 * \return this array
330 */
331 JArray& div(const double factor)
332 {
333 for (int i = 0; i != N; ++i) {
334 (*this)[i] /= factor;
335 }
336
337 return *this;
338 }
339
340
341 /**
342 * Check equality.
343 *
344 * \param array array
345 * \return true if arrays are equal; else false
346 */
347 bool equals(const JArray<N, T>& array) const
348 {
349 for (int i = 0; i != N; ++i) {
350 if ((*this)[i] != array[i]) {
351 return false;
352 }
353 }
354
355 return true;
356 }
357
358
359 /**
360 * Read array from input stream.
361 *
362 * \param in input stream
363 * \param array array
364 * \return input stream
365 */
366 friend inline std::istream& operator>>(std::istream& in, JArray& array)
367 {
368 for (iterator i = array.begin(); i != array.end(); ++i) {
369 in >> *i;
370 }
371
372 return in;
373 }
374
375
376 /**
377 * Write array to output stream.
378 *
379 * \param out output stream
380 * \param array array
381 * \return output stream
382 */
383 friend inline std::ostream& operator<<(std::ostream& out, const JArray& array)
384 {
385 const JFormat format(out, getFormat<JArray>(JFormat_t(10, 3, std::ios::fixed | std::ios::showpos)));
386
387 for (const_iterator i = array.begin(); i != array.end(); ++i) {
388 out << ' ' << format << *i;
389 }
390
391 return out;
392 }
393
394
395 /**
396 * Read array from input.
397 *
398 * \param in reader
399 * \param buffer array
400 * \return reader
401 */
402 friend inline JReader& operator>>(JReader& in, JArray& buffer)
403 {
404 for (iterator i = buffer.begin(); i != buffer.end(); ++i) {
405 in >> *i;
406 }
407
408 return in;
409 }
410
411
412 /**
413 * Write array to output.
414 *
415 * \param out writer
416 * \param buffer array
417 * \return writer
418 */
419 friend inline JWriter& operator<<(JWriter& out, const JArray& buffer)
420 {
421 for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
422 out << *i;
423 }
424
425 return out;
426 }
427
428 protected:
429 /**
430 * Auxiliary method to match array.
431 *
432 * \param array array
433 * \param ls indices of array
434 * \return array
435 */
436 template<size_t M, size_t ...i>
437 static std::array<T, N> match(const std::array<T, M>& array, const std::index_sequence<i...>& ls)
438 {
439 return {array[i]...};
440 };
441
442
443 /**
444 * Match array.
445 *
446 * \param array array
447 * \return array
448 */
449 template<size_t M>
451 {
452 return match(array, std::make_index_sequence<N>{});
453 };
454
455
456 /**
457 * Auxiliary method to convert array.
458 *
459 * \param array array
460 * \param ls indices of array
461 * \return array
462 */
463 template<size_t ...i>
464 static std::array<T, sizeof...(i)> convert(const std::array<U, sizeof...(i)>& array, const std::index_sequence<i...>& ls)
465 {
466 return {array[i]...};
467 };
468
469
470 /**
471 * Convert array.
472 *
473 * \param array array
474 * \return array
475 */
476 template<size_t M>
478 {
479 return convert(array, std::make_index_sequence<M>{});
480 };
481
482
483 /**
484 * Auxiliary method combine arrays.
485 *
486 * \param v first array
487 * \param lv indices of first array
488 * \param w second array
489 * \param lw indices of second array
490 */
491 template<size_t ...iv, size_t ...iw>
492 static std::array<T, N> combine(const std::array<T, sizeof...(iv)>& v, const std::index_sequence<iv...>& lv,
493 const std::array<T, sizeof...(iw)>& w, const std::index_sequence<iw...>& lw)
494 {
495 return { v[iv]..., w[iw]... };
496 };
497
498
499 /**
500 * Combine two arrays.
501 *
502 * \param first first array
503 * \param second second array
504 * \return array
505 */
506 template<size_t M>
508 {
509 return combine(first, std::make_index_sequence< M >{},
510 second, std::make_index_sequence<N-M>{});
511 }
512
513
514 /**
515 * Auxiliary method to pop back array.
516 *
517 * \param ls indices of array
518 * \return array
519 */
520 template<size_t ...i>
521 JArray<sizeof...(i), T> pop_back(const std::index_sequence<i...>& ls) const
522 {
523 return {(*this)[i]...};
524 };
525
526
527 /**
528 * Auxiliary method to pop front array.
529 *
530 * \param value value
531 * \param args argument list
532 * \return array
533 */
534 template<class ...Args>
535 static JArray<N-1, T> pop_front(argument_type value, const Args& ...args)
536 {
537 return {args...};
538 };
539
540
541 /**
542 * Auxiliary method to pop front array.
543 *
544 * \param ls indices of array
545 * \return array
546 */
547 template<size_t ...i>
548 JArray<N-1, T> pop_front(const std::index_sequence<i...>& ls) const
549 {
550 return pop_front((*this)[i]...);
551 };
552
553
554 /**
555 * Push back array.
556 *
557 * \param value value
558 * \param ls indices of array
559 * \return array
560 */
561 template<size_t ...i>
562 JArray<N + 1, T> push_back(argument_type value, const std::index_sequence<i...>& ls) const
563 {
564 return {value, (*this)[i]...};
565 };
566 };
567}
568
569#endif
I/O manipulators.
JFormat_t & getFormat()
Get format for given type.
Definition JManip.hh:682
Base class for data structures with artithmetic capabilities.
Interface for binary input.
Interface for binary output.
Multidimensional key.
Definition JMultiKey.hh:44
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
JArgument< T >::argument_type argument_type
Definition JClass.hh:82
Template definition of auxiliary base class for comparison of data structures.
Definition JEquals.hh:84
Auxiliary base class for aritmetic operations of derived class types.
Definition JMath.hh:347
Wrapper data structure around std::array.
Definition JArray.hh:43
JArray(const JMultiKey< M, U > &key, argument_type value, const Args &...args)
Convert and assemble constructor.
Definition JArray.hh:223
JArray< N-1, T > pop_front(const std::index_sequence< i... > &ls) const
Auxiliary method to pop front array.
Definition JArray.hh:548
JArray(const T *p)
Multi-copy constructor.
Definition JArray.hh:114
JArray(const JMultiKey< N, T > &key)
Convert constructor.
Definition JArray.hh:163
JArray(const JMultiKey< M, U > &key, const std::array< std::remove_const_t< T >, N-M > &array)
Convert and combine constructor.
Definition JArray.hh:197
JArray< N-1, T > pop_back() const
Make a copy in which the last element is removed.
Definition JArray.hh:244
JArray & add(const JArray &array)
Add array.
Definition JArray.hh:283
JArray(const JMultiKey< M, T > &key, argument_type value, const Args &...args)
Convert and assemble constructor.
Definition JArray.hh:210
JArray & negate()
Negate array.
Definition JArray.hh:267
friend std::istream & operator>>(std::istream &in, JArray &array)
Read array from input stream.
Definition JArray.hh:366
JArray & div(const double factor)
Scale array.
Definition JArray.hh:331
JArray(const JArray< M, T > &array)
Copy constructor.
Definition JArray.hh:81
JArray(const JMultiKey< M, T > &key, const std::array< std::remove_const_t< T >, N-M > &array)
Convert and combine constructor.
Definition JArray.hh:185
static std::array< T, N > combine(const std::array< T, M > &first, const std::array< T, N-M > &second)
Combine two arrays.
Definition JArray.hh:507
JArray()
Default constructor.
Definition JArray.hh:70
JArray & mul(const double factor)
Scale array.
Definition JArray.hh:315
static std::array< T, N > match(const std::array< T, M > &array, const std::index_sequence< i... > &ls)
Auxiliary method to match array.
Definition JArray.hh:437
static std::array< T, sizeof...(i)> convert(const std::array< U, sizeof...(i)> &array, const std::index_sequence< i... > &ls)
Auxiliary method to convert array.
Definition JArray.hh:464
JArray< N-1, T > pop_front() const
Make a copy in which the first element is removed.
Definition JArray.hh:233
bool equals(const JArray< N, T > &array) const
Check equality.
Definition JArray.hh:347
JArray(const std::array< T, M > &first, const std::array< T, N-M > &second)
Combine constructor.
Definition JArray.hh:127
JArray(const std::array< U, M > &array, argument_type value, const Args &...args)
Copy and assemble constructor.
Definition JArray.hh:153
JArray(const std::array< T, M > &array, argument_type value, const Args &...args)
Copy and assemble constructor.
Definition JArray.hh:140
JArray & sub(const JArray &array)
Subtract array.
Definition JArray.hh:299
JArray< N+1, T > push_back(argument_type value, const std::index_sequence< i... > &ls) const
Push back array.
Definition JArray.hh:562
std::conditional< std::is_const< T >::value, typenamestd::remove_const< T >::type, typenamestd::add_const< T >::type >::type U
Definition JArray.hh:64
friend JWriter & operator<<(JWriter &out, const JArray &buffer)
Write array to output.
Definition JArray.hh:419
JArray(const JArray< M, U > &array)
Copy constructor.
Definition JArray.hh:92
JArray< sizeof...(i), T > pop_back(const std::index_sequence< i... > &ls) const
Auxiliary method to pop back array.
Definition JArray.hh:521
JArray(argument_type value, const Args &...args)
Assemble constructor.
Definition JArray.hh:104
static std::array< T, N > combine(const std::array< T, sizeof...(iv)> &v, const std::index_sequence< iv... > &lv, const std::array< T, sizeof...(iw)> &w, const std::index_sequence< iw... > &lw)
Auxiliary method combine arrays.
Definition JArray.hh:492
JArray< N+1, T > push_back(argument_type value) const
Make a copy and push back value.
Definition JArray.hh:256
JClass< T >::argument_type argument_type
Definition JArray.hh:60
friend JReader & operator>>(JReader &in, JArray &buffer)
Read array from input.
Definition JArray.hh:402
static std::array< T, M > convert(const std::array< U, M > &array)
Convert array.
Definition JArray.hh:477
friend std::ostream & operator<<(std::ostream &out, const JArray &array)
Write array to output stream.
Definition JArray.hh:383
static std::array< T, N > match(const std::array< T, M > &array)
Match array.
Definition JArray.hh:450
std::array< T, N > array_type
Definition JArray.hh:46
JArray(const JMultiKey< N, U > &key)
Convert constructor.
Definition JArray.hh:173
static JArray< N-1, T > pop_front(argument_type value, const Args &...args)
Auxiliary method to pop front array.
Definition JArray.hh:535