tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
SomeNumericArray.hpp
Go to the documentation of this file.
1#ifndef TATAMI_SOME_NUMERIC_ARRAY_HPP
2#define TATAMI_SOME_NUMERIC_ARRAY_HPP
3
4#include <cstdint>
5#include <cstddef>
6#include <iterator>
7
14namespace tatami {
15
23#ifdef INT8_MAX
24 I8,
25#endif
26#ifdef UINT8_MAX
27 U8,
28#endif
29#ifdef INT16_MAX
30 I16,
31#endif
32#ifdef UINT16_MAX
33 U16,
34#endif
35#ifdef INT32_MAX
36 I32,
37#endif
38#ifdef UINT32_MAX
39 U32,
40#endif
41#ifdef INT64_MAX
42 I64,
43#endif
44#ifdef UINT64_MAX
45 U64,
46#endif
47 F32,
48 F64
49};
50
63template<typename Value_>
65public:
72 SomeNumericArray(void* ptr, std::size_t number, SomeNumericType type) : my_number(number), my_type(type) {
73 switch(my_type) {
74#ifdef INT8_MAX
75 case I8:
76 my_i8 = static_cast<const std::int8_t*>(ptr);
77 break;
78#endif
79#ifdef UINT8_MAX
80 case U8:
81 my_u8 = static_cast<const std::uint8_t*>(ptr);
82 break;
83#endif
84#ifdef INT16_MAX
85 case I16:
86 my_i16 = static_cast<const std::int16_t*>(ptr);
87 break;
88#endif
89#ifdef UINT16_MAX
90 case U16:
91 my_u16 = static_cast<const std::uint16_t*>(ptr);
92 break;
93#endif
94#ifdef INT32_MAX
95 case I32:
96 my_i32 = static_cast<const std::int32_t*>(ptr);
97 break;
98#endif
99#ifdef UINT32_MAX
100 case U32:
101 my_u32 = static_cast<const std::uint32_t*>(ptr);
102 break;
103#endif
104#ifdef INT64_MAX
105 case I64:
106 my_i64 = static_cast<const std::int64_t*>(ptr);
107 break;
108#endif
109#ifdef UINT64_MAX
110 case U64:
111 my_u64 = static_cast<const std::uint64_t*>(ptr);
112 break;
113#endif
114 case F32:
115 my_f32 = static_cast<const float*>(ptr);
116 break;
117 case F64:
118 my_f64 = static_cast<const double*>(ptr);
119 break;
120 }
121 }
122
123#ifdef INT8_MAX
128 SomeNumericArray(const std::int8_t* ptr, std::size_t number) : my_i8(ptr), my_number(number), my_type(I8) {}
129#endif
130
131#ifdef UINT8_MAX
136 SomeNumericArray(const std::uint8_t* ptr, std::size_t number) : my_u8(ptr), my_number(number), my_type(U8) {}
137#endif
138
139#ifdef INT16_MAX
144 SomeNumericArray(const std::int16_t* ptr, std::size_t number) : my_i16(ptr), my_number(number), my_type(I16) {}
145#endif
146
147#ifdef UINT16_MAX
152 SomeNumericArray(const std::uint16_t* ptr, std::size_t number) : my_u16(ptr), my_number(number), my_type(U16) {}
153#endif
154
155#ifdef INT32_MAX
160 SomeNumericArray(const std::int32_t* ptr, std::size_t number) : my_i32(ptr), my_number(number), my_type(I32) {}
161#endif
162
163#ifdef UINT32_MAX
168 SomeNumericArray(const std::uint32_t* ptr, std::size_t number) : my_u32(ptr), my_number(number), my_type(U32) {}
169#endif
170
171#ifdef INT64_MAX
176 SomeNumericArray(const std::int64_t* ptr, std::size_t number) : my_i64(ptr), my_number(number), my_type(I64) {}
177#endif
178
179#ifdef UINT64_MAX
184 SomeNumericArray(const std::uint64_t* ptr, std::size_t number) : my_u64(ptr), my_number(number), my_type(U64) {}
185#endif
186
191 SomeNumericArray(const float* ptr, std::size_t number) : my_f32(ptr), my_number(number), my_type(F32) {}
192
197 SomeNumericArray(const double* ptr, std::size_t number) : my_f64(ptr), my_number(number), my_type(F64) {}
198
199private:
200#ifdef INT8_MAX
201 const std::int8_t* my_i8 = NULL;
202#endif
203#ifdef UINT8_MAX
204 const std::uint8_t* my_u8 = NULL;
205#endif
206#ifdef INT16_MAX
207 const std::int16_t* my_i16 = NULL;
208#endif
209#ifdef UINT16_MAX
210 const std::uint16_t* my_u16 = NULL;
211#endif
212#ifdef INT32_MAX
213 const std::int32_t* my_i32 = NULL;
214#endif
215#ifdef UINT32_MAX
216 const std::uint32_t* my_u32 = NULL;
217#endif
218#ifdef INT64_MAX
219 const std::int64_t* my_i64 = NULL;
220#endif
221#ifdef UINT64_MAX
222 const std::uint64_t* my_u64 = NULL;
223#endif
224 const float* my_f32 = NULL;
225 const double* my_f64 = NULL;
226
227 std::size_t my_number;
228 SomeNumericType my_type;
229
230public:
235 Value_ operator[](std::size_t i) const {
236 switch (my_type) {
237#ifdef INT8_MAX
238 case I8:
239 return my_i8[i];
240#endif
241#ifdef UINT8_MAX
242 case U8:
243 return my_u8[i];
244#endif
245#ifdef INT16_MAX
246 case I16:
247 return my_i16[i];
248#endif
249#ifdef UINT16_MAX
250 case U16:
251 return my_u16[i];
252#endif
253#ifdef INT32_MAX
254 case I32:
255 return my_i32[i];
256#endif
257#ifdef UINT32_MAX
258 case U32:
259 return my_u32[i];
260#endif
261#ifdef INT64_MAX
262 case I64:
263 return my_i64[i];
264#endif
265#ifdef UINT64_MAX
266 case U64:
267 return my_u64[i];
268#endif
269 case F32:
270 return my_f32[i];
271 case F64:
272 return my_f64[i];
273 }
274 return 0; // shouldn't reach here, but whatever.
275 }
276
280 std::size_t size() const {
281 return my_number;
282 }
283
284public:
290 struct Iterator {
294 Iterator() : my_parent(NULL), my_index(0) {}
295
302 Iterator(const SomeNumericArray* parent, std::size_t index) : my_parent(parent), my_index(index) {}
303
304 public:
305 // Tags to pretend it's an iterator, at least enough for tatami to work;
306 // see https://internalpointers.com/post/writing-custom-iterators-modern-cpp for details.
307
311 using iterator_category = std::random_access_iterator_tag;
312
316 using difference_type = std::ptrdiff_t;
317
321 using value_type = Value_;
322
326 using pointer = const Value_*;
327
331 using reference = const Value_&;
332
333 private:
334 const SomeNumericArray* my_parent;
335 std::size_t my_index;
336
337 public:
342 return (*my_parent)[my_index];
343 }
344
349 value_type operator[](std::size_t i) const {
350 return (*my_parent)[my_index + i];
351 }
352
353 public:
358 bool operator==(const Iterator& right) const {
359 return my_index == right.my_index;
360 }
361
366 bool operator!=(const Iterator& right) const {
367 return !(*this == right);
368 }
369
374 bool operator<(const Iterator& right) const {
375 return my_index < right.my_index;
376 }
377
382 bool operator>=(const Iterator& right) const {
383 return !(*this < right);
384 }
385
390 bool operator>(const Iterator& right) const {
391 return my_index > right.my_index;
392 }
393
398 bool operator<=(const Iterator& right) const {
399 return !(*this > right);
400 }
401
402 public:
407 Iterator& operator+=(std::size_t n) {
408 my_index += n;
409 return *this;
410 }
411
416 *this += 1;
417 return *this;
418 }
419
425 auto copy = *this;
426 ++(*this);
427 return copy;
428 }
429
434 Iterator& operator-=(std::size_t n) {
435 my_index -= n;
436 return *this;
437 }
438
443 *this -= 1;
444 return *this;
445 }
446
452 auto copy = *this;
453 --(*this);
454 return copy;
455 }
456
457 public:
462 Iterator operator+(std::size_t n) const {
463 return Iterator(my_parent, my_index + n);
464 }
465
470 Iterator operator-(std::size_t n) const {
471 return Iterator(my_parent, my_index - n);
472 }
473
479 friend Iterator operator+(std::size_t n, const Iterator& it) {
480 return Iterator(it.my_parent, it.my_index + n);
481 }
482
487 std::ptrdiff_t operator-(const Iterator& right) const {
488 std::ptrdiff_t out;
489 if (right.my_index > my_index) {
490 out = right.my_index - my_index;
491 out *= -1;
492 } else {
493 out = my_index - right.my_index;
494 }
495 return out;
496 }
497 };
498
502 Iterator begin() const {
503 return Iterator(this, 0);
504 }
505
509 Iterator end() const {
510 return Iterator(this, this->size());
511 }
512};
513
514}
515
516#endif
Array of some numeric type, determined at runtime.
Definition SomeNumericArray.hpp:64
SomeNumericArray(const std::uint32_t *ptr, std::size_t number)
Definition SomeNumericArray.hpp:168
Value_ operator[](std::size_t i) const
Definition SomeNumericArray.hpp:235
SomeNumericArray(const std::uint16_t *ptr, std::size_t number)
Definition SomeNumericArray.hpp:152
SomeNumericArray(const std::uint64_t *ptr, std::size_t number)
Definition SomeNumericArray.hpp:184
SomeNumericArray(const std::int64_t *ptr, std::size_t number)
Definition SomeNumericArray.hpp:176
SomeNumericArray(const double *ptr, std::size_t number)
Definition SomeNumericArray.hpp:197
SomeNumericArray(void *ptr, std::size_t number, SomeNumericType type)
Definition SomeNumericArray.hpp:72
SomeNumericArray(const std::int32_t *ptr, std::size_t number)
Definition SomeNumericArray.hpp:160
SomeNumericArray(const float *ptr, std::size_t number)
Definition SomeNumericArray.hpp:191
SomeNumericArray(const std::int8_t *ptr, std::size_t number)
Definition SomeNumericArray.hpp:128
SomeNumericArray(const std::uint8_t *ptr, std::size_t number)
Definition SomeNumericArray.hpp:136
SomeNumericArray(const std::int16_t *ptr, std::size_t number)
Definition SomeNumericArray.hpp:144
Iterator end() const
Definition SomeNumericArray.hpp:509
std::size_t size() const
Definition SomeNumericArray.hpp:280
Iterator begin() const
Definition SomeNumericArray.hpp:502
Flexible representations for matrix data.
Definition Extractor.hpp:15
SomeNumericType
Definition SomeNumericArray.hpp:22
Random-access iterator class.
Definition SomeNumericArray.hpp:290
Iterator operator-(std::size_t n) const
Definition SomeNumericArray.hpp:470
value_type operator*() const
Definition SomeNumericArray.hpp:341
friend Iterator operator+(std::size_t n, const Iterator &it)
Definition SomeNumericArray.hpp:479
bool operator>(const Iterator &right) const
Definition SomeNumericArray.hpp:390
bool operator<=(const Iterator &right) const
Definition SomeNumericArray.hpp:398
std::random_access_iterator_tag iterator_category
Definition SomeNumericArray.hpp:311
Iterator & operator--()
Definition SomeNumericArray.hpp:442
bool operator>=(const Iterator &right) const
Definition SomeNumericArray.hpp:382
std::ptrdiff_t operator-(const Iterator &right) const
Definition SomeNumericArray.hpp:487
const Value_ & reference
Definition SomeNumericArray.hpp:331
value_type operator[](std::size_t i) const
Definition SomeNumericArray.hpp:349
const Value_ * pointer
Definition SomeNumericArray.hpp:326
Value_ value_type
Definition SomeNumericArray.hpp:321
Iterator operator+(std::size_t n) const
Definition SomeNumericArray.hpp:462
Iterator(const SomeNumericArray *parent, std::size_t index)
Definition SomeNumericArray.hpp:302
Iterator operator++(int)
Definition SomeNumericArray.hpp:424
Iterator & operator++()
Definition SomeNumericArray.hpp:415
bool operator==(const Iterator &right) const
Definition SomeNumericArray.hpp:358
bool operator!=(const Iterator &right) const
Definition SomeNumericArray.hpp:366
Iterator & operator+=(std::size_t n)
Definition SomeNumericArray.hpp:407
Iterator operator--(int)
Definition SomeNumericArray.hpp:451
Iterator()
Definition SomeNumericArray.hpp:294
Iterator & operator-=(std::size_t n)
Definition SomeNumericArray.hpp:434
std::ptrdiff_t difference_type
Definition SomeNumericArray.hpp:316
bool operator<(const Iterator &right) const
Definition SomeNumericArray.hpp:374