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
22enum SomeNumericType { I8, U8, I16, U16, I32, U32, I64, U64, F32, F64 };
23
36template<typename Value_ = double>
38public:
45 SomeNumericArray(void* ptr, size_t number, SomeNumericType type) : my_number(number), my_type(type) {
46 switch(my_type) {
47 case I8:
48 my_i8 = static_cast<const int8_t*>(ptr);
49 break;
50 case U8:
51 my_u8 = static_cast<const uint8_t*>(ptr);
52 break;
53 case I16:
54 my_i16 = static_cast<const int16_t*>(ptr);
55 break;
56 case U16:
57 my_u16 = static_cast<const uint16_t*>(ptr);
58 break;
59 case I32:
60 my_i32 = static_cast<const int32_t*>(ptr);
61 break;
62 case U32:
63 my_u32 = static_cast<const uint32_t*>(ptr);
64 break;
65 case I64:
66 my_i64 = static_cast<const int64_t*>(ptr);
67 break;
68 case U64:
69 my_u64 = static_cast<const uint64_t*>(ptr);
70 break;
71 case F32:
72 my_f32 = static_cast<const float*>(ptr);
73 break;
74 case F64:
75 my_f64 = static_cast<const double*>(ptr);
76 break;
77 }
78 }
79
84 SomeNumericArray(const int8_t* ptr, size_t number) : my_i8(ptr), my_number(number), my_type(I8) {}
85
90 SomeNumericArray(const uint8_t* ptr, size_t number) : my_u8(ptr), my_number(number), my_type(U8) {}
91
96 SomeNumericArray(const int16_t* ptr, size_t number) : my_i16(ptr), my_number(number), my_type(I16) {}
97
102 SomeNumericArray(const uint16_t* ptr, size_t number) : my_u16(ptr), my_number(number), my_type(U16) {}
103
108 SomeNumericArray(const int32_t* ptr, size_t number) : my_i32(ptr), my_number(number), my_type(I32) {}
109
114 SomeNumericArray(const uint32_t* ptr, size_t number) : my_u32(ptr), my_number(number), my_type(U32) {}
115
120 SomeNumericArray(const int64_t* ptr, size_t number) : my_i64(ptr), my_number(number), my_type(I64) {}
121
126 SomeNumericArray(const uint64_t* ptr, size_t number) : my_u64(ptr), my_number(number), my_type(U64) {}
127
132 SomeNumericArray(const float* ptr, size_t number) : my_f32(ptr), my_number(number), my_type(F32) {}
133
138 SomeNumericArray(const double* ptr, size_t number) : my_f64(ptr), my_number(number), my_type(F64) {}
139
140private:
141 const int8_t* my_i8 = NULL;
142 const uint8_t* my_u8 = NULL;
143 const int16_t* my_i16 = NULL;
144 const uint16_t* my_u16 = NULL;
145 const int32_t* my_i32 = NULL;
146 const uint32_t* my_u32 = NULL;
147 const int64_t* my_i64 = NULL;
148 const uint64_t* my_u64 = NULL;
149 const float* my_f32 = NULL;
150 const double* my_f64 = NULL;
151
152 size_t my_number;
153 SomeNumericType my_type;
154
155public:
160 Value_ operator[](size_t i) const {
161 switch (my_type) {
162 case I8:
163 return my_i8[i];
164 case U8:
165 return my_u8[i];
166 case I16:
167 return my_i16[i];
168 case U16:
169 return my_u16[i];
170 case I32:
171 return my_i32[i];
172 case U32:
173 return my_u32[i];
174 case I64:
175 return my_i64[i];
176 case U64:
177 return my_u64[i];
178 case F32:
179 return my_f32[i];
180 case F64:
181 return my_f64[i];
182 }
183 return 0; // shouldn't reach here, but whatever.
184 }
185
189 size_t size() const {
190 return my_number;
191 }
192
193public:
199 struct Iterator {
203 Iterator() : my_parent(NULL), my_index(0) {}
204
211 Iterator(const SomeNumericArray* parent, size_t index) : my_parent(parent), my_index(index) {}
212
213 public:
214 // Tags to pretend it's an iterator, at least enough for tatami to work;
215 // see https://internalpointers.com/post/writing-custom-iterators-modern-cpp for details.
216
220 using iterator_category = std::random_access_iterator_tag;
221
225 using difference_type = std::ptrdiff_t;
226
231
235 using pointer = const Value_*;
236
240 using reference = const Value_&;
241
242 private:
243 const SomeNumericArray* my_parent;
244 size_t my_index;
245
246 public:
251 return (*my_parent)[my_index];
252 }
253
258 value_type operator[](size_t i) const {
259 return (*my_parent)[my_index + i];
260 }
261
262 public:
267 bool operator==(const Iterator& right) const {
268 return my_index == right.my_index;
269 }
270
275 bool operator!=(const Iterator& right) const {
276 return !(*this == right);
277 }
278
283 bool operator<(const Iterator& right) const {
284 return my_index < right.my_index;
285 }
286
291 bool operator>=(const Iterator& right) const {
292 return !(*this < right);
293 }
294
299 bool operator>(const Iterator& right) const {
300 return my_index > right.my_index;
301 }
302
307 bool operator<=(const Iterator& right) const {
308 return !(*this > right);
309 }
310
311 public:
317 my_index += n;
318 return *this;
319 }
320
325 *this += 1;
326 return *this;
327 }
328
334 auto copy = *this;
335 ++(*this);
336 return copy;
337 }
338
344 my_index -= n;
345 return *this;
346 }
347
352 *this -= 1;
353 return *this;
354 }
355
361 auto copy = *this;
362 --(*this);
363 return copy;
364 }
365
366 public:
371 Iterator operator+(size_t n) const {
372 return Iterator(my_parent, my_index + n);
373 }
374
379 Iterator operator-(size_t n) const {
380 return Iterator(my_parent, my_index - n);
381 }
382
388 friend Iterator operator+(size_t n, const Iterator& it) {
389 return Iterator(it.my_parent, it.my_index + n);
390 }
391
396 std::ptrdiff_t operator-(const Iterator& right) const {
397 std::ptrdiff_t out;
398 if (right.my_index > my_index) {
399 out = right.my_index - my_index;
400 out *= -1;
401 } else {
402 out = my_index - right.my_index;
403 }
404 return out;
405 }
406 };
407
411 Iterator begin() const {
412 return Iterator(this, 0);
413 }
414
418 Iterator end() const {
419 return Iterator(this, this->size());
420 }
421};
422
423}
424
425#endif
Array of some numeric type, determined at runtime.
Definition SomeNumericArray.hpp:37
SomeNumericArray(const int32_t *ptr, size_t number)
Definition SomeNumericArray.hpp:108
SomeNumericArray(const float *ptr, size_t number)
Definition SomeNumericArray.hpp:132
SomeNumericArray(const double *ptr, size_t number)
Definition SomeNumericArray.hpp:138
SomeNumericArray(void *ptr, size_t number, SomeNumericType type)
Definition SomeNumericArray.hpp:45
SomeNumericArray(const int64_t *ptr, size_t number)
Definition SomeNumericArray.hpp:120
SomeNumericArray(const uint64_t *ptr, size_t number)
Definition SomeNumericArray.hpp:126
SomeNumericArray(const int8_t *ptr, size_t number)
Definition SomeNumericArray.hpp:84
SomeNumericArray(const uint16_t *ptr, size_t number)
Definition SomeNumericArray.hpp:102
Value_ operator[](size_t i) const
Definition SomeNumericArray.hpp:160
SomeNumericArray(const int16_t *ptr, size_t number)
Definition SomeNumericArray.hpp:96
SomeNumericArray(const uint8_t *ptr, size_t number)
Definition SomeNumericArray.hpp:90
size_t size() const
Definition SomeNumericArray.hpp:189
Iterator end() const
Definition SomeNumericArray.hpp:418
SomeNumericArray(const uint32_t *ptr, size_t number)
Definition SomeNumericArray.hpp:114
Iterator begin() const
Definition SomeNumericArray.hpp:411
Flexible representations for matrix data.
Definition Extractor.hpp:15
SomeNumericType
Definition SomeNumericArray.hpp:22
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35
Random-access iterator class.
Definition SomeNumericArray.hpp:199
value_type operator*() const
Definition SomeNumericArray.hpp:250
bool operator>(const Iterator &right) const
Definition SomeNumericArray.hpp:299
bool operator<=(const Iterator &right) const
Definition SomeNumericArray.hpp:307
std::random_access_iterator_tag iterator_category
Definition SomeNumericArray.hpp:220
Iterator(const SomeNumericArray *parent, size_t index)
Definition SomeNumericArray.hpp:211
Iterator & operator--()
Definition SomeNumericArray.hpp:351
bool operator>=(const Iterator &right) const
Definition SomeNumericArray.hpp:291
std::ptrdiff_t operator-(const Iterator &right) const
Definition SomeNumericArray.hpp:396
const Value_ & reference
Definition SomeNumericArray.hpp:240
const Value_ * pointer
Definition SomeNumericArray.hpp:235
Value_ value_type
Definition SomeNumericArray.hpp:230
Iterator & operator-=(size_t n)
Definition SomeNumericArray.hpp:343
Iterator & operator+=(size_t n)
Definition SomeNumericArray.hpp:316
Iterator operator++(int)
Definition SomeNumericArray.hpp:333
Iterator & operator++()
Definition SomeNumericArray.hpp:324
bool operator==(const Iterator &right) const
Definition SomeNumericArray.hpp:267
value_type operator[](size_t i) const
Definition SomeNumericArray.hpp:258
bool operator!=(const Iterator &right) const
Definition SomeNumericArray.hpp:275
Iterator operator--(int)
Definition SomeNumericArray.hpp:360
Iterator operator-(size_t n) const
Definition SomeNumericArray.hpp:379
Iterator()
Definition SomeNumericArray.hpp:203
std::ptrdiff_t difference_type
Definition SomeNumericArray.hpp:225
Iterator operator+(size_t n) const
Definition SomeNumericArray.hpp:371
bool operator<(const Iterator &right) const
Definition SomeNumericArray.hpp:283
friend Iterator operator+(size_t n, const Iterator &it)
Definition SomeNumericArray.hpp:388