-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblBinaryMatrixIterator.hpp
1506 lines (955 loc) · 48.2 KB
/
blBinaryMatrixIterator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef BL_BINARYMATRIXITERATOR_HPP
#define BL_BINARYMATRIXITERATOR_HPP
//-------------------------------------------------------------------
// FILE: blBinaryMatrixIterator.hpp
// CLASS: blBinaryMatrixIterator
// BASE CLASS: None
//
//
//
// PURPOSE: A custom iterator that can be used to parse serialized
// binary data.
//
// The iterator assumes that the binary data is an (n x 1)
// vector of values, representing data points, where each data
// point is a matrix of data of size (rows x cols)
//
// The binary data is provided as a stream/string of bytes through
// user specified begin/end iterators and then converted to the
// user specified data type
//
// The binary data is assumed to be formatted as follows:
//
// Line 1 -- Serial number (a number representing a signature/type)
// Line 2 -- rows
// Line 3 -- cols
// Line 4 - Line n -- The data points one matrix at a time
//
// NOTE: If the matrix is a 2d matrix then the cols = 1, for
// 3d matrices the cols > 1
//
// -- For example a (2xN) matrix would look like the following:
//
// -- 123454321 (some serial number)
// -- 3 (the rows in each data point)
// -- 1 (the columns in each data point, so this means each
// data point is a single column vector
// -- 0,0
// -- 1,0
// -- 2,0
// -- 0,1
// -- 1,1
// -- 2,1
// -- 0,2
// -- 1,2
// -- 2,2
// -- 0,3
// -- ...
// -- 2,N
// -- End of file
//
// -- The iterator assumes that all single values in the data
// are of the same type (the type specified by the user through
// the template parameter)
//
// -- This class and its functions are defined within
// the "blAlgorithmsLIB" namespace
//
//
//
// AUTHOR: Vincenzo Barbato
// http://www.barbatolabs.com
//
//
//
// LISENSE: MIT-LICENCE
// http://www.opensource.org/licenses/mit-license.php
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes needed for this file
//-------------------------------------------------------------------
#include <iterator>
#include <iostream>
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// NOTE: This class is defined within the blAlgorithmsLIB namespace
//-------------------------------------------------------------------
namespace blAlgorithmsLIB
{
//-------------------------------------------------------------------
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
class blBinaryMatrixIterator
{
public: // Type aliases
// Iterator traits
using iterator_category = std::random_access_iterator_tag;
using value_type = blNumberType;
using difference_type = std::ptrdiff_t;
using pointer = blNumberType*;
using reference = blNumberType&;
public: // Constructors and destructor
// No default constructor
blBinaryMatrixIterator() = delete;
// Constructor from two iterators
blBinaryMatrixIterator(const blDataIteratorType& beginIter,
const blDataIteratorType& endIter,
const blAdvancingIteratorMethod& advancingIteratorMethod = COL_MAJOR);
// Default copy constructor
blBinaryMatrixIterator(const blBinaryMatrixIterator<blDataIteratorType,blNumberType>& binaryMatrixIterator) = default;
// Destructor
~blBinaryMatrixIterator();
public: // Assignment operator
// Default assignment operator
blBinaryMatrixIterator<blDataIteratorType,blNumberType>& operator=(const blBinaryMatrixIterator<blDataIteratorType,blNumberType>& binaryMatrixIterator) = default;
public: // Equality operators
bool operator==(const blBinaryMatrixIterator<blDataIteratorType,blNumberType>& binaryMatrixIterator)const;
bool operator!=(const blBinaryMatrixIterator<blDataIteratorType,blNumberType>& binaryMatrixIterator)const;
public: // Reference and Dereference operators
blNumberType* operator->();
blNumberType& operator*();
const blNumberType& operator*()const;
public: // Access operators
// Access operators treating the data
// as a one-dimensional data buffer
blNumberType& operator[](const std::ptrdiff_t& index);
blNumberType& operator()(const std::ptrdiff_t& index);
blNumberType& at(const std::ptrdiff_t& index);
const blNumberType& operator[](const std::ptrdiff_t& index)const;
const blNumberType& operator()(const std::ptrdiff_t& index)const;
const blNumberType& at(const std::ptrdiff_t& index)const;
// Access operators treating the data
// as a two-dimensional data buffer
blNumberType& operator()(const std::ptrdiff_t& rowIndex,
const std::ptrdiff_t& colIndex);
blNumberType& at(const std::ptrdiff_t& rowIndex,
const std::ptrdiff_t& colIndex);
const blNumberType& operator()(const std::ptrdiff_t& rowIndex,
const std::ptrdiff_t& colIndex)const;
const blNumberType& at(const std::ptrdiff_t& rowIndex,
const std::ptrdiff_t& colIndex)const;
// Access operators treating the data
// as a three-dimensional data buffer
blNumberType& operator()(const std::ptrdiff_t& rowIndex,
const std::ptrdiff_t& colIndex,
const std::ptrdiff_t& pageIndex);
blNumberType& at(const std::ptrdiff_t& rowIndex,
const std::ptrdiff_t& colIndex,
const std::ptrdiff_t& pageIndex);
const blNumberType& operator()(const std::ptrdiff_t& rowIndex,
const std::ptrdiff_t& colIndex,
const std::ptrdiff_t& pageIndex)const;
const blNumberType& at(const std::ptrdiff_t& rowIndex,
const std::ptrdiff_t& colIndex,
const std::ptrdiff_t& pageIndex)const;
public: // Public functions
// Overloaded function used to set
// the begin/end iterators
void setIterators(const blDataIteratorType& beginIter,
const blDataIteratorType& endIter);
// Functions used to move the current
// iterator position to the beginning
// or to the end or to some user specified
// position
blBinaryMatrixIterator<blDataIteratorType,blNumberType>& moveToTheBeginning();
blBinaryMatrixIterator<blDataIteratorType,blNumberType>& moveToTheEnd();
blBinaryMatrixIterator<blDataIteratorType,blNumberType>& moveToPosition(const std::ptrdiff_t& rowIndex,
const std::ptrdiff_t& colIndex = 0,
const std::ptrdiff_t& pageIndex = 0);
// Function used to move the iterator by a
// user specified amount
// In this class the iterator is moved in
// a column-major format
void moveIterator(const std::ptrdiff_t& movement);
// Overloaded arithmetic operators
blBinaryMatrixIterator<blDataIteratorType,blNumberType>& operator+=(const std::ptrdiff_t& movement);
blBinaryMatrixIterator<blDataIteratorType,blNumberType>& operator-=(const std::ptrdiff_t& movement);
blBinaryMatrixIterator<blDataIteratorType,blNumberType>& operator++();
blBinaryMatrixIterator<blDataIteratorType,blNumberType>& operator--();
blBinaryMatrixIterator<blDataIteratorType,blNumberType> operator++(int);
blBinaryMatrixIterator<blDataIteratorType,blNumberType> operator--(int);
blBinaryMatrixIterator<blDataIteratorType,blNumberType> operator+(const std::ptrdiff_t& movement)const;
blBinaryMatrixIterator<blDataIteratorType,blNumberType> operator-(const std::ptrdiff_t& movement)const;
// Operator used to "subtract"
// two pointers (it gives the
// distance)
std::ptrdiff_t operator-(const blBinaryMatrixIterator<blDataIteratorType,blNumberType>& binaryMatrixIterator)const;
// Functions used to get the
// matrix header information
const std::ptrdiff_t& serialNumber()const;
const std::size_t& rows()const;
const std::size_t& cols()const;
const std::size_t& pages()const;
const std::size_t& size()const;
const std::size_t& length()const;
const std::ptrdiff_t& currentIndex()const;
const std::ptrdiff_t& currentRow()const;
const std::ptrdiff_t& currentCol()const;
const std::ptrdiff_t& currentPage()const;
// Functions used to set/get
// the advancing iterator method
const blAdvancingIteratorMethod& getAdvancingIteratorMethod()const;
void setAdvancingIteratorMethod(const blAdvancingIteratorMethod& advancingIteratorMethod);
// Functions used to get the
// begin/end/iter iterators
// useful when copying this
// smart iterator
const blDataIteratorType& getBeginIter()const;
const blDataIteratorType& getEndIter()const;
const blDataIteratorType& getIter()const;
// Functions used to get the
// begin/end iterators so that
// this can be used in stl
// algorithms
blBinaryMatrixIterator<blDataIteratorType,blNumberType> begin()const;
blBinaryMatrixIterator<blDataIteratorType,blNumberType> end()const;
blBinaryMatrixIterator<const blDataIteratorType,blNumberType> cbegin()const;
blBinaryMatrixIterator<const blDataIteratorType,blNumberType> cend()const;
private: // Private functions
// Functions used to advance the iterator
// in different ways
void moveIterator_col_major(const std::ptrdiff_t& movement);
void moveIterator_row_major(const std::ptrdiff_t& movement);
void moveIterator_col_page_major(const std::ptrdiff_t& movement);
void moveIterator_row_page_major(const std::ptrdiff_t& movement);
protected: // Protected variables
// Begin and end iterators
// used to know where the
// given buffer begins and
// ends
blDataIteratorType m_beginIter;
blDataIteratorType m_endIter;
// The iterator used to move through
// the buffer
blDataIteratorType m_iter;
// Serial number
std::ptrdiff_t m_serialNumber;
// Data is represented as pages x rows x cols
std::size_t m_rows;
std::size_t m_cols;
std::size_t m_pages;
// Total size of matrix (pages * rows * cols)
std::size_t m_size;
// Current row, column and page in the buffer
std::ptrdiff_t m_currentIndex;
std::ptrdiff_t m_currentRow;
std::ptrdiff_t m_currentCol;
std::ptrdiff_t m_currentPage;
// Variable used to decide how to advance
// the iterator
blAdvancingIteratorMethod m_advancingIteratorMethod;
};
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
inline blBinaryMatrixIterator<blDataIteratorType,blNumberType>::blBinaryMatrixIterator(const blDataIteratorType& beginIter,
const blDataIteratorType& endIter,
const blAdvancingIteratorMethod& advancingIteratorMethod)
{
setIterators(beginIter,endIter);
setAdvancingIteratorMethod(advancingIteratorMethod);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
inline blBinaryMatrixIterator<blDataIteratorType,blNumberType>::~blBinaryMatrixIterator()
{
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Equality operators
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
inline bool blBinaryMatrixIterator<blDataIteratorType,blNumberType>::operator==(const blBinaryMatrixIterator<blDataIteratorType,blNumberType>& binaryMatrixIterator)const
{
if(m_iter == binaryMatrixIterator.getIter())
return true;
return false;
}
template<typename blDataIteratorType,
typename blNumberType>
inline bool blBinaryMatrixIterator<blDataIteratorType,blNumberType>::operator!=(const blBinaryMatrixIterator<blDataIteratorType,blNumberType>& binaryMatrixIterator)const
{
if(m_iter != binaryMatrixIterator.getIter())
return true;
return false;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Overloaded function used to set the begin/end iterators
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
inline void blBinaryMatrixIterator<blDataIteratorType,blNumberType>::setIterators(const blDataIteratorType& beginIter,
const blDataIteratorType& endIter)
{
// First we save the newly supplied
// begin/end iterators and set the
// current iterator position to the
// begin iterator
m_beginIter = beginIter;
m_endIter = endIter;
m_iter = m_beginIter;
// Then we calculate the total number
// of entrees in the supplied stream
std::size_t totalNumberOfEntreesInStream = static_cast<std::size_t>(std::distance(m_beginIter,m_endIter)) / static_cast<std::size_t>(sizeof(blNumberType));
// Then we read the serial number,
// rows and columns by reading the
// first 3 entrees in the supplied
// stream
if(totalNumberOfEntreesInStream <= 3)
{
// This means the supplied stream
// does not have the expected format
// We just default everything to 0
m_serialNumber = static_cast<std::ptrdiff_t>(0);
m_rows = static_cast<std::size_t>(0);
m_cols = static_cast<std::size_t>(0);
m_pages = static_cast<std::size_t>(0);
m_size = static_cast<std::size_t>(0);
}
else
{
// First we read the serial number, rows and cols
m_serialNumber = static_cast<std::ptrdiff_t>( *(reinterpret_cast<blNumberType*>(&(*m_iter))) );
m_iter += sizeof(blNumberType);
m_rows = static_cast<std::size_t>( *(reinterpret_cast<blNumberType*>(&(*m_iter))) );
m_iter += sizeof(blNumberType);
m_cols = static_cast<std::size_t>( *(reinterpret_cast<blNumberType*>(&(*m_iter))) );
m_iter += sizeof(blNumberType);
// From that information, knowing that
// the first three entrees represent
// the serial number, the rows and
// columns, we find the number of pages
// by calculating how many (rows x cols)
// entrees there are in the stream
m_pages = (totalNumberOfEntreesInStream - static_cast<std::size_t>(3)) / (m_rows * m_cols);
// Let's not forget to save the
// total length of the data
m_size = m_pages * m_rows * m_cols;
}
// We also default all current
// indexes to 0
m_currentIndex = static_cast<std::ptrdiff_t>(0);
m_currentRow = static_cast<std::ptrdiff_t>(0);
m_currentCol = static_cast<std::ptrdiff_t>(0);
m_currentPage = static_cast<std::ptrdiff_t>(0);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to move the current iterator position
// to the beginning or to the end
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
inline blBinaryMatrixIterator<blDataIteratorType,blNumberType>& blBinaryMatrixIterator<blDataIteratorType,blNumberType>::moveToTheBeginning()
{
// We set everything back to the beginning
// of the supplied binary data stream
// If the supplied binary data stream is not
// big enough to support the expected header
// (serialNumber,Rows,Cols), then we just
// set the begin iter equal to the end iter
if(m_size == 0)
m_iter = m_endIter;
else
m_iter = m_beginIter + 3*sizeof(blNumberType);
m_currentIndex = 0;
m_currentRow = 0;
m_currentCol = 0;
m_currentPage = 0;
return (*this);
}
template<typename blDataIteratorType,
typename blNumberType>
inline blBinaryMatrixIterator<blDataIteratorType,blNumberType>& blBinaryMatrixIterator<blDataIteratorType,blNumberType>::moveToTheEnd()
{
// We set everything to point to the end
// of the supplied binary data stream
m_iter = m_endIter;
m_currentIndex = m_size;
m_currentRow = m_rows;
m_currentCol = m_cols;
m_currentPage = m_pages;
return (*this);
}
template<typename blDataIteratorType,
typename blNumberType>
inline blBinaryMatrixIterator<blDataIteratorType,blNumberType>& blBinaryMatrixIterator<blDataIteratorType,blNumberType>::moveToPosition(const std::ptrdiff_t& rowIndex,
const std::ptrdiff_t& colIndex,
const std::ptrdiff_t& pageIndex)
{
// We move the iterator enough so that
// it points to the user specified
// position
// We do not check whether that position
// is valid or whether it would point to
// somewhere outside of the binary stream
// range
m_currentIndex = rowIndex + colIndex * static_cast<std::ptrdiff_t>(m_rows) + pageIndex * static_cast<std::ptrdiff_t>(m_cols * m_rows);
m_currentPage = m_currentIndex / static_cast<std::ptrdiff_t>(m_rows * m_cols);
m_currentCol = (m_currentIndex % static_cast<std::ptrdiff_t>(m_rows * m_cols)) / static_cast<std::ptrdiff_t>(m_rows);
m_currentRow = m_currentIndex % static_cast<std::ptrdiff_t>(m_rows);
m_iter = m_beginIter + ( (m_currentIndex + 3) * sizeof(blNumberType) );
return (*this);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to move the iterator by a
// user specified amount
// In this class the iterator is moved in
// a column-major format
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
inline void blBinaryMatrixIterator<blDataIteratorType,blNumberType>::moveIterator(const std::ptrdiff_t& movement)
{
// We do not check for out-of-bounds
// The user is responsible to make sure
// the iterator does not move beyond
// the end iterator or move behind the
// begin iterator
// NOTE: Some advancing methods such as
// column-page-major and row-page-major
// do check whether the iterator is
// past the end and make it equal to
// the end iterator
// Let's move the iterator the
// way the user has specified
// for us to move it
switch(m_advancingIteratorMethod)
{
default:
case COL_MAJOR:
this->moveIterator_col_major(movement);
break;
case ROW_MAJOR:
this->moveIterator_row_major(movement);
break;
case COL_PAGE_MAJOR:
this->moveIterator_col_page_major(movement);
break;
case ROW_PAGE_MAJOR:
this->moveIterator_row_page_major(movement);
break;
}
// We're done
return;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to advance the iterator
// in a column-major way
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
inline void blBinaryMatrixIterator<blDataIteratorType,blNumberType>::moveIterator_col_major(const std::ptrdiff_t& movement)
{
// We do not check for out-of-bounds
// The user is responsible to make sure
// the iterator does not move beyond
// the end iterator or move behind the
// begin iterator
m_currentIndex += movement;
// We then move the current iterator
// again not paying attention to whether
// the iterator has moved beyond the
// begin/end of the binary stream
m_iter = m_beginIter + ( (m_currentIndex + 3) * sizeof(blNumberType) );
// We now calculate the current row
// column and page
// If the user did not pay attention
// to the binary stream bounds, these
// indexes could be negative
m_currentPage = m_currentIndex / static_cast<std::ptrdiff_t>(m_rows * m_cols);
m_currentCol = (m_currentIndex % static_cast<std::ptrdiff_t>(m_rows * m_cols)) / static_cast<std::ptrdiff_t>(m_rows);
m_currentRow = m_currentIndex % static_cast<std::ptrdiff_t>(m_rows);
// We're done
return;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to advance the iterator
// in a row-major way
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
inline void blBinaryMatrixIterator<blDataIteratorType,blNumberType>::moveIterator_row_major(const std::ptrdiff_t& movement)
{
std::ptrdiff_t rowMajorIndex;
std::ptrdiff_t rowMajorRowIndex;
std::ptrdiff_t rowMajorColIndex;
std::ptrdiff_t rowMajorPageIndex;
// First we calculate the current
// index based on row-major iterator
// advancement
rowMajorIndex = m_currentCol + m_currentRow * m_cols + m_currentPage * m_cols * m_rows;
// We then advance the row-major
// current index
rowMajorIndex += movement;
// From that we calculate what the
// row, column and page coordinates
// are supposed to be
rowMajorPageIndex = rowMajorIndex / static_cast<std::ptrdiff_t>(m_rows * m_cols);
rowMajorRowIndex = (rowMajorIndex % static_cast<std::ptrdiff_t>(m_rows * m_cols)) / static_cast<std::ptrdiff_t>(m_cols);
rowMajorColIndex = rowMajorIndex % static_cast<std::ptrdiff_t>(m_cols);
// Finally we move the iterator
// to those coordinates
this->moveToPosition(rowMajorRowIndex,rowMajorColIndex,rowMajorPageIndex);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to advance the iterator
// in a column-page-major way
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
inline void blBinaryMatrixIterator<blDataIteratorType,blNumberType>::moveIterator_col_page_major(const std::ptrdiff_t& movement)
{
// We stack all the pages (each page is a matrix)
// together one below the other vertically
// so that this 3d-matrix setup becomes a long
// 2d-matrix (same number of columns but the
// number of rows equals m_rows * m_pages)
std::ptrdiff_t colPageMajorIndex;
std::ptrdiff_t colPageMajorRowIndex;
std::ptrdiff_t colPageMajorColIndex;
std::ptrdiff_t colPageMajorPageIndex;
// First thing is we calculate the current
// col-page-major row and column indexes
// noting that because we're stacking matrices
// vertically the number of columns doesn't change
colPageMajorRowIndex = m_currentRow + m_currentPage * static_cast<std::ptrdiff_t>(m_rows);
colPageMajorColIndex = m_currentCol;
colPageMajorIndex = colPageMajorRowIndex + colPageMajorColIndex * static_cast<std::ptrdiff_t>(m_rows * m_pages);
// We then advance the index
colPageMajorIndex += movement;
// We check to make sure that we
// did not go past the end
if(colPageMajorIndex >= m_size)
{
this->moveToTheEnd();
}
else
{
// Now we calculate back what the corresponding
// row, column and page index coordinates are
colPageMajorColIndex = colPageMajorIndex / static_cast<std::ptrdiff_t>(m_rows * m_pages);
colPageMajorRowIndex = colPageMajorIndex % static_cast<std::ptrdiff_t>(m_rows * m_pages);
// We then transform back to the 3d coordinates
colPageMajorPageIndex = colPageMajorRowIndex / static_cast<std::ptrdiff_t>(m_rows);
colPageMajorRowIndex = colPageMajorRowIndex % static_cast<std::ptrdiff_t>(m_rows);
// Finally we move the iterator to the
// calculated set of coordinates
this->moveToPosition(colPageMajorRowIndex,
colPageMajorColIndex,
colPageMajorPageIndex);
}
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to advance the iterator
// in a row-page-major way
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
inline void blBinaryMatrixIterator<blDataIteratorType,blNumberType>::moveIterator_row_page_major(const std::ptrdiff_t& movement)
{
m_currentIndex += (movement * static_cast<std::ptrdiff_t>(m_rows));
// The index has to be circulated around
// because the binary data stream is assumed
// to be formatted in a col-major format
// We only circulate the index around until
// it's circulated the entire data, otherwise
// we just move it linearly
if(m_currentIndex >= static_cast<std::ptrdiff_t>(m_size + m_rows - 1) ||
m_currentIndex <= -static_cast<std::ptrdiff_t>(m_size + m_rows - 1))
{
// This means the iterator has parsed all
// the data and we just make it point to
// the end of the binary data stream
this->moveToTheEnd();
}
else
{
// This means the iterator has not
// yet parsed the entire data, so we
// wrap it around if it is numerically
// bigger than m_size
// We also move it by 1 so that it points
// to the next number in the given row
// and not the same number over and over
if(m_currentIndex >= static_cast<std::ptrdiff_t>(m_size))
{
m_currentIndex %= static_cast<std::ptrdiff_t>(m_size);
++m_currentIndex;
}
// We then move the current iterator
// again not paying attention to whether
// the iterator has moved beyond the
// begin/end of the binary stream
m_iter = m_beginIter + ( (m_currentIndex + 3) * sizeof(blNumberType) );
// We now calculate the current row
// column and page
// If the user did not pay attention
// to the binary stream bounds, these
// indexes could be negative
m_currentPage = m_currentIndex / static_cast<std::ptrdiff_t>(m_rows * m_cols);
m_currentCol = (m_currentIndex % static_cast<std::ptrdiff_t>(m_rows * m_cols)) / static_cast<std::ptrdiff_t>(m_rows);
m_currentRow = m_currentIndex % static_cast<std::ptrdiff_t>(m_rows);
}
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Overloaded arithmetic operators
//-------------------------------------------------------------------
template<typename blDataIteratorType,
typename blNumberType>
inline blBinaryMatrixIterator<blDataIteratorType,blNumberType>& blBinaryMatrixIterator<blDataIteratorType,blNumberType>::operator+=(const std::ptrdiff_t& movement)
{
this->moveIterator(movement);
return (*this);
}
template<typename blDataIteratorType,
typename blNumberType>
inline blBinaryMatrixIterator<blDataIteratorType,blNumberType>& blBinaryMatrixIterator<blDataIteratorType,blNumberType>::operator-=(const std::ptrdiff_t& movement)
{
this->moveIterator(-movement);
return (*this);
}
template<typename blDataIteratorType,
typename blNumberType>
inline blBinaryMatrixIterator<blDataIteratorType,blNumberType>& blBinaryMatrixIterator<blDataIteratorType,blNumberType>::operator++()
{
this->moveIterator(1);
return (*this);
}
template<typename blDataIteratorType,
typename blNumberType>