-
Notifications
You must be signed in to change notification settings - Fork 11
/
prpl-cellspace.h
1932 lines (1790 loc) · 61 KB
/
prpl-cellspace.h
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 PRPL_CELLSPACE_H
#define PRPL_CELLSPACE_H
#include "prpl-basicTypes.h"
#include "prpl-cellspaceInfo.h"
#include "prpl-neighborhood.h"
using namespace std;
namespace pRPL {
class Cellspace {
public:
/* Constructors and Destructor */
Cellspace();
Cellspace(const pRPL::Cellspace &rhs);
Cellspace(pRPL::CellspaceInfo *pInfo,
bool ifInitMem = true);
~Cellspace();
/* Memory allocation and de-allocation */
bool initMem();
bool init(pRPL::CellspaceInfo *pInfo,
bool ifInitMem = true);
template <class ElemType>
bool initVal(const ElemType *pVal = NULL);
template<class InputType>
bool initValAs(const InputType *pVal = NULL,
bool warning = true);
void clear();
/* Operators */
pRPL::Cellspace& operator=(const pRPL::Cellspace& rhs);
/* Information */
const pRPL::CellspaceInfo* info() const;
pRPL::CellspaceInfo* info();
void info(pRPL::CellspaceInfo *pInfo);
bool isEmpty(bool warning = false) const;
/* Elements */
void* at(long idx,
bool warning = true);
const void* at(long idx,
bool warning = true) const;
template<class RtrType>
RtrType* row(long iRow,
bool warning = true);
template<class RtrType>
const RtrType* row(long iRow,
bool warning = true) const;
template<class RtrType>
RtrType& at(const pRPL::CellCoord &coord,
bool warning = true);
template<class RtrType>
const RtrType& at(const pRPL::CellCoord &coord,
bool warning = true) const;
template<class RtrType>
RtrType& at(long iRow, long iCol,
bool warning = true);
template<class RtrType>
const RtrType& at(long iRow, long iCol,
bool warning = true) const;
template<class RtrType>
RtrType& at(long idx,
bool warning = true);
template<class RtrType>
const RtrType& at(long idx,
bool warning = true) const;
template<class RtrType>
RtrType atAs(const pRPL::CellCoord &coord,
bool warning = true) const;
template<class RtrType>
RtrType atAs(long iRow, long iCol,
bool warning = true) const;
template<class RtrType>
RtrType atAs(long idx,
bool warning = true) const;
template<class RtrType>
bool values(vector<RtrType> *pvVals,
const pRPL::CoordBR *pRectangle = NULL,
bool warning = true) const;
template<class RtrType>
bool valuesAs(vector<RtrType> *pvVals,
const pRPL::CoordBR *pRectangle = NULL,
bool warning = true) const;
/*
* example: myCellspace.find(&vIdxs, &val, &br);
*/
template<class ElemType>
bool find(pRPL::LongVect *pvFoundIdxs,
const ElemType *pVal,
const pRPL::CoordBR *pRectangle = NULL,
bool warning = true) const;
/*
* example: myCellspace.find(&vIdx, &vVals, &br);
*/
template<class ElemType>
bool find(pRPL::LongVect *pvFoundIdxs,
const vector<ElemType> *pvVals,
const pRPL::CoordBR *pRectangle = NULL,
bool warning = true) const;
/*
* example: myCellspace.find<int>(&vIdxs, bind2nd(greater<int>(), 10), &br);
*/
template<class ElemType, class Predicate>
bool find(pRPL::LongVect *pvFoundIdxs,
Predicate pred,
const pRPL::CoordBR *pRectangle = NULL,
bool excldNoData = true,
bool warning = true) const;
/*
* example: myCellspace.count(&val, &br);
*/
template<class ElemType>
long count(const ElemType *pVal,
const pRPL::CoordBR *pRectangle = NULL,
bool warning = true) const;
/*
* example: myCellspace.count(&vVals, &br);
*/
template<class ElemType>
long count(const vector<ElemType> *pvVals,
const pRPL::CoordBR *pRectangle = NULL,
bool warning = true) const;
/*
* example: myCellspace.count<int>(bind2nd(greater<int>(), 10), &br);
*/
template<class ElemType, class Predicate>
long count(Predicate pred,
const pRPL::CoordBR *pRectangle = NULL,
bool excldNoData = true,
bool warning = true) const;
template<class ElemType>
bool frequences(map<ElemType, long> *pmFreqs,
const pRPL::CoordBR *pRectangle = NULL,
bool warning = true) const;
template<class RtrType>
bool frequencesAs(map<RtrType, long> *pmFreqs,
const pRPL::CoordBR *pRectangle = NULL,
bool warning = true) const;
template<class RtrType>
bool nbrhdVals(vector<RtrType> &vVals,
const pRPL::Neighborhood &nbrhd,
const pRPL::CellCoord &ctrCoord,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool nbrhdVals(vector<RtrType> &vVals,
const pRPL::Neighborhood &nbrhd,
long ctrRow, long ctrCol,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool nbrhdVals(vector<RtrType> &vVals,
const pRPL::Neighborhood &nbrhd,
long idx,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool nbrhdValsAs(vector<RtrType> &vVals,
const pRPL::Neighborhood &nbrhd,
const pRPL::CellCoord &ctrCoord,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool nbrhdValsAs(vector<RtrType> &vVals,
const pRPL::Neighborhood &nbrhd,
long ctrRow, long ctrCol,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool nbrhdValsAs(vector<RtrType> &vVals,
const pRPL::Neighborhood &nbrhd,
long idx,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool nbrhdFreqs(map<RtrType, int> &mFreqs,
const pRPL::Neighborhood &nbrhd,
const pRPL::CellCoord &ctrCoord,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool nbrhdFreqs(map<RtrType, int> &mFreqs,
const pRPL::Neighborhood &nbrhd,
long ctrRow, long ctrCol,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool nbrhdFreqs(map<RtrType, int> &mFreqs,
const pRPL::Neighborhood &nbrhd,
long idx,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool wnbrhdFreqs(map<RtrType, double> &mFreqs,
const pRPL::Neighborhood &nbrhd,
const pRPL::CellCoord &ctrCoord,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool wnbrhdFreqs(map<RtrType, double> &mFreqs,
const pRPL::Neighborhood &nbrhd,
long ctrRow, long ctrCol,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool wnbrhdFreqs(map<RtrType, double> &mFreqs,
const pRPL::Neighborhood &nbrhd,
long idx,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool nbrhdFreqsAs(map<RtrType, int> &mFreqs,
const pRPL::Neighborhood &nbrhd,
const pRPL::CellCoord &ctrCoord,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool nbrhdFreqsAs(map<RtrType, int> &mFreqs,
const pRPL::Neighborhood &nbrhd,
long ctrRow, long ctrCol,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool nbrhdFreqsAs(map<RtrType, int> &mFreqs,
const pRPL::Neighborhood &nbrhd,
long idx,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool wnbrhdFreqsAs(map<RtrType, double> &mFreqs,
const pRPL::Neighborhood &nbrhd,
const pRPL::CellCoord &ctrCoord,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool wnbrhdFreqsAs(map<RtrType, double> &mFreqs,
const pRPL::Neighborhood &nbrhd,
long ctrRow, long ctrCol,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
bool wnbrhdFreqsAs(map<RtrType, double> &mFreqs,
const pRPL::Neighborhood &nbrhd,
long idx,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
RtrType nbrhdTotalVal(const pRPL::Neighborhood &nbrhd,
const pRPL::CellCoord &ctrCoord,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
RtrType nbrhdTotalVal(const pRPL::Neighborhood &nbrhd,
long ctrRow, long ctrCol,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
RtrType nbrhdTotalVal(const pRPL::Neighborhood &nbrhd,
long idx,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
RtrType nbrhdTotalValAs(const pRPL::Neighborhood &nbrhd,
const pRPL::CellCoord &ctrCoord,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
RtrType nbrhdTotalValAs(const pRPL::Neighborhood &nbrhd,
long ctrRow, long ctrCol,
bool includeCtr = true,
bool warning = true) const;
template<class RtrType>
RtrType nbrhdTotalValAs(const pRPL::Neighborhood &nbrhd,
long idx,
bool includeCtr = true,
bool warning = true) const;
/* Update */
void setUpdateTracking(bool toTrack);
const pRPL::LongVect& getUpdatedIdxs() const;
void clearUpdatedIdxs();
template<class ElemType>
bool updateCell(const pRPL::CellCoord &coord, const ElemType &val,
bool warning = true);
template<class ElemType>
bool updateCell(long iRow, long iCol, const ElemType &val,
bool warning = true);
template<class ElemType>
bool updateCell(long idx, const ElemType &val,
bool warning = true);
template<class InputType>
bool updateCellAs(const pRPL::CellCoord &coord, const InputType &val,
bool warning = true);
template<class InputType>
bool updateCellAs(long iRow, long iCol, const InputType &val,
bool warning = true);
template<class InputType>
bool updateCellAs(long idx, const InputType &val,
bool warning = true);
template<class ElemType>
bool updateCells(const vector< pair<pRPL::CellCoord, ElemType> > &vCells,
bool warning = true);
template<class ElemType>
bool updateCells(const vector< pair<long, ElemType> > &vCells,
bool warning = true);
template<class ElemType>
bool updateCells(const vector< pair< ElemType, vector<long> > > &vCells,
bool warning = true);
template<class InputType>
bool updateCellsAs(const vector< pair<pRPL::CellCoord, InputType> > &vCells,
bool warning = true);
template<class InputType>
bool updateCellsAs(const vector< pair<long, InputType> > &vCells,
bool warning = true);
template<class InputType>
bool updateCellsAs(const vector< pair< InputType, vector<long> > > &vCells,
bool warning = true);
/*
template<class ElemType>
bool update(Transition *pTrans,
const CoordBR *pWorkBR = NULL);
*/
bool createGdalDS(GDALDataset *&pDataset,
const char *aFileName,
GDALDriver *pGdalDriver,
char **aGdalOptions = NULL) const;
bool createGdalDS(GDALDataset *&pDataset,
const char *aFileName,
const char *aGdalFormat,
char **aGdalOptions = NULL) const;
bool readGDALData(GDALDataset *pDataset,
int iBand = 1,
const pRPL::CellCoord *pGDALOff = NULL,
bool warning = true);
bool writeGDALData(GDALDataset *pDataset,
int iBand = 1,
const pRPL::CellCoord *pGDALOff = NULL,
const pRPL::CoordBR *pWorkBR = NULL,
bool warning = true);
/*pgtiol--------------*/
bool createPgtiolDS(PGTIOLDataset *&pDataset,
const char *aFileName,
char **aPgtiolOptions = NULL) const;
bool readPGTIOLData(PGTIOLDataset *pDataset,
int iBand = 1,
const pRPL::CellCoord *pGDALOff = NULL,
bool warning = true);
bool writePGTIOLData(PGTIOLDataset *pDataset,
int iBand = 1,
const pRPL::CellCoord *pGDALOff = NULL,
const pRPL::CoordBR *pWorkBR = NULL,
bool warning = true);
protected:
/*-------GDAL--------------*/
bool _checkGDALSettings(GDALDataset *pDataset,
int iBand,
const pRPL::CellCoord *pGDALOff = NULL,
const pRPL::CoordBR *pWorkBR = NULL,
bool warning = true) const;
/*-------PGTIOL--------------*/
bool _checkPGTIOLSettings(PGTIOLDataset *pDataset,
int iBand,
const pRPL::CellCoord *pGDALOff = NULL,
const pRPL::CoordBR *pWorkBR = NULL,
bool warning = true) const;
protected:
pRPL::CellspaceInfo *_pInfo;
void *_pData;
bool _trackUpdt;
pRPL::LongVect _vUpdtIdxs;
friend class DataManager;
};
};
/****************************************************
* Public Methods *
****************************************************/
template <class ElemType>
bool pRPL::Cellspace::
initVal(const ElemType *pVal) {
bool done = true;
if(isEmpty(true) ||
!(_pInfo->isDataType<ElemType>(true))) {
done = false;
}
else {
long mySize = _pInfo->size();
ElemType val = (pVal == NULL)?_pInfo->getNoDataVal<ElemType>(true):*pVal;
for(long iElem = 0; iElem < mySize; iElem++) {
(ElemType&)(*((ElemType*)_pData + iElem)) = val;
}
}
return done;
}
template<class InputType>
bool pRPL::Cellspace::
initValAs(const InputType *pVal,
bool warning) {
bool done = true;
if(isEmpty(true)) {
done = false;
}
else {
InputType val = (pVal == NULL) ? _pInfo->getNoDataValAs<InputType>(true) : *pVal;
long mySize = _pInfo->size();
for(long iElem = 0; iElem < mySize; iElem++) {
if(_pInfo->isDataType<bool>()) {
(bool&)(*((bool*)_pData + iElem)) = (bool)val;
}
else if(_pInfo->isDataType<char>()) {
(char&)(*((char*)_pData + iElem)) = (char)val;
}
else if(_pInfo->isDataType<unsigned short>()) {
(unsigned short&)(*((unsigned short*)_pData + iElem)) = (unsigned short)val;
}
else if(_pInfo->isDataType<short>()) {
(short&)(*((short*)_pData + iElem)) = (short)val;
}
else if(_pInfo->isDataType<unsigned int>()) {
(unsigned int&)(*((unsigned int*)_pData + iElem)) = (unsigned int)val;
}
else if(_pInfo->isDataType<int>()) {
(int&)(*((int*)_pData + iElem)) = (int)val;
}
else if(_pInfo->isDataType<unsigned long>()) {
(unsigned long&)(*((unsigned long*)_pData + iElem)) = (unsigned long)val;
}
else if(_pInfo->isDataType<long>()) {
(long&)(*((long*)_pData + iElem)) = (long)val;
}
else if(_pInfo->isDataType<float>()) {
(float&)(*((float*)_pData + iElem)) = (float)val;
}
else if(_pInfo->isDataType<double>()) {
(double&)(*((double*)_pData + iElem)) = (double)val;
}
else if(_pInfo->isDataType<long double>()) {
(long double&)(*((long double*)_pData + iElem)) = (long double)val;
}
else {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: unable to convert input data type (" \
<< typeid(InputType).name() << ") to Cellspace data type (" \
<< _pInfo->dataType() << ")" << endl;
done = false;
}
}
} // end -- for(iElem)
}
return done;
}
template<class RtrType>
RtrType* pRPL::Cellspace::
row(long iRow,
bool warning) {
RtrType* pRowElems = NULL;
if(!isEmpty(warning) && _pInfo->isDataType<RtrType>(warning)) {
if(iRow < 0 || iRow >= _pInfo->nRows()) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: row index [" \
<< iRow << "] out of Cellspace boundary [" \
<< _pInfo->dims() << "]" << endl;
}
}
else {
pRowElems = (RtrType*)_pData + iRow*(_pInfo->nCols());
}
}
return pRowElems;
}
template<class RtrType>
const RtrType* pRPL::Cellspace::
row(long iRow,
bool warning) const {
RtrType* pRowElems = NULL;
if(!isEmpty(warning) && _pInfo->isDataType<RtrType>(warning)) {
if(iRow < 0 || iRow >= _pInfo->nRows()) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: row index [" \
<< iRow << "] out of Cellspace boundary [" \
<< _pInfo->dims() << "]" << endl;
}
}
else {
pRowElems = (RtrType*)_pData + iRow*(_pInfo->nCols());
}
}
return pRowElems;
}
template <class RtrType>
RtrType& pRPL::Cellspace::
at(const pRPL::CellCoord& coord,
bool warning) {
RtrType* pElem = NULL;
if(!isEmpty(warning) &&
_pInfo->isDataType<RtrType>(warning) &&
_pInfo->validCoord(coord, warning)) {
pElem = (RtrType*)_pData + coord.iRow()*(_pInfo->nCols()) + coord.iCol();
}
return *pElem;
}
template <class RtrType>
const RtrType& pRPL::Cellspace::
at(const pRPL::CellCoord& coord,
bool warning) const {
RtrType* pElem = NULL;
if(!isEmpty(warning) &&
_pInfo->isDataType<RtrType>(warning) &&
_pInfo->validCoord(coord, warning)) {
pElem = (RtrType*)_pData + coord.iRow()*(_pInfo->nCols()) + coord.iCol();
}
return *pElem;
}
template <class RtrType>
RtrType& pRPL::Cellspace::
at(long iRow, long iCol,
bool warning) {
RtrType* pElem = NULL;
if(!isEmpty(warning) &&
_pInfo->isDataType<RtrType>(warning) &&
_pInfo->validCoord(iRow, iCol, warning)) {
pElem = (RtrType*)_pData + iRow*(_pInfo->nCols()) + iCol;
}
return *pElem;
}
template <class RtrType>
const RtrType& pRPL::Cellspace::
at(long iRow, long iCol,
bool warning) const {
RtrType* pElem = NULL;
if(!isEmpty(warning) &&
_pInfo->isDataType<RtrType>(warning) &&
_pInfo->validCoord(iRow, iCol, warning)) {
pElem = (RtrType*)_pData + iRow*(_pInfo->nCols()) + iCol;
}
return *pElem;
}
template <class RtrType>
RtrType& pRPL::Cellspace::
at(long idx,
bool warning) {
RtrType* pElem = NULL;
if(!isEmpty(warning) &&
_pInfo->isDataType<RtrType>(warning) &&
_pInfo->validIdx(idx, warning)) {
pElem = (RtrType*)_pData + idx;
}
return *pElem;
}
template <class RtrType>
const RtrType& pRPL::Cellspace::
at(long idx,
bool warning) const {
RtrType *pElem = NULL;
if(!isEmpty(warning) &&
_pInfo->isDataType<RtrType>(warning) &&
_pInfo->validIdx(idx, warning)) {
pElem = (RtrType*)_pData + idx;
}
return *pElem;
}
template<class RtrType>
RtrType pRPL::Cellspace::
atAs(const pRPL::CellCoord &coord,
bool warning) const {
return atAs<RtrType>(_pInfo->coord2idx(coord), warning);
}
template<class RtrType>
RtrType pRPL::Cellspace::
atAs(long iRow, long iCol,
bool warning) const {
return atAs<RtrType>(_pInfo->coord2idx(iRow, iCol), warning);
}
template<class RtrType>
RtrType pRPL::Cellspace::
atAs(long idx,
bool warning) const {
RtrType val = (RtrType)pRPL::ERROR_VAL;
if(!isEmpty(warning) && _pInfo->validIdx(idx, warning)) {
if(_pInfo->isDataType<bool>()) {
val = (RtrType)(at<bool>(idx));
}
else if(_pInfo->isDataType<unsigned char>()) {
val = (RtrType)(at<unsigned char>(idx));
}
else if(_pInfo->isDataType<char>()) {
val = (RtrType)(at<char>(idx));
}
else if(_pInfo->isDataType<unsigned short>()) {
val = (RtrType)(at<unsigned short>(idx));
}
else if(_pInfo->isDataType<short>()) {
val = (RtrType)(at<short>(idx));
}
else if(_pInfo->isDataType<unsigned int>()) {
val = (RtrType)(at<unsigned int>(idx));
}
else if(_pInfo->isDataType<int>()) {
val = (RtrType)(at<int>(idx));
}
else if(_pInfo->isDataType<unsigned long>()) {
val = (RtrType)(at<unsigned long>(idx));
}
else if(_pInfo->isDataType<long>()) {
val = (RtrType)(at<long>(idx));
}
else if(_pInfo->isDataType<float>()) {
val = (RtrType)(at<float>(idx));
}
else if(_pInfo->isDataType<double>()) {
val = (RtrType)(at<double>(idx));
}
else if(_pInfo->isDataType<long double>()) {
val = (RtrType)(at<long double>(idx));
}
else {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: unable to convert Cellspace data type (" \
<< _pInfo->dataType() << ") to output data type (" << typeid(RtrType).name() \
<< "). Returned an INVALID value. "<< endl;
}
}
}
return val;
}
template<class RtrType>
bool pRPL::Cellspace::
values(vector<RtrType> *pvVals,
const pRPL::CoordBR *pRectangle,
bool warning) const {
bool done = true;
if(pvVals == NULL) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NULL pointer to the vector of values" << endl;
}
done = false;
}
else {
pvVals->clear();
if(!_pInfo->isDataType<RtrType>(warning)) {
done = false;
}
else if(!isEmpty(warning)) {
if(pRectangle == NULL) {
long mySize = _pInfo->size();
for(long iElem = 0; iElem < mySize; iElem++) {
const RtrType &val = *((const RtrType*)_pData + iElem);
if(std::find(pvVals->begin(), pvVals->end(), val) == pvVals->end()) {
pvVals->push_back(val);
}
} // End of iElem loop
} // if(pRectangle == NULL)
else if(_pInfo->validBR(*pRectangle, warning)) {
for(long iRow = pRectangle->minIRow(); iRow <= pRectangle->maxIRow(); iRow++) {
for(long iCol = pRectangle->minICol(); iCol <= pRectangle->maxICol(); iCol++) {
long iElem = _pInfo->coord2idx(iRow, iCol);
const RtrType &val = *((const RtrType*)_pData + iElem);
if(std::find(pvVals->begin(), pvVals->end(), val) == pvVals->end()) {
pvVals->push_back(val);
}
}
}
}
stable_sort(pvVals->begin(), pvVals->end());
}
}
return done;
}
template<class RtrType>
bool pRPL::Cellspace::
valuesAs(vector<RtrType> *pvVals,
const pRPL::CoordBR *pRectangle,
bool warning) const {
if(pvVals == NULL) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NULL pointer to the vector of values" << endl;
}
return false;
}
pvVals->clear();
if(isEmpty(warning)) {
return false;
}
if(pRectangle == NULL) {
long mySize = _pInfo->size();
for(long iElem = 0; iElem < mySize; iElem++) {
RtrType val = atAs<RtrType>(iElem, warning);
if(fabs(val - (RtrType)pRPL::ERROR_VAL) < pRPL::EPSINON) {
return false;
}
else if(std::find(pvVals->begin(), pvVals->end(), val) == pvVals->end()) {
pvVals->push_back(val);
}
} // End of iElem loop
}
else if(_pInfo->validBR(*pRectangle, warning)) {
for(long iRow = pRectangle->minIRow(); iRow <= pRectangle->maxIRow(); iRow++) {
for(long iCol = pRectangle->minICol(); iCol <= pRectangle->maxICol(); iCol++) {
long iElem = _pInfo->coord2idx(iRow, iCol);
RtrType val = atAs<RtrType>(iElem, warning);
if(fabs(val - (RtrType)pRPL::ERROR_VAL) < pRPL::EPSINON) {
return false;
}
if(std::find(pvVals->begin(), pvVals->end(), val) == pvVals->end()) {
pvVals->push_back(val);
}
}
}
}
else {
return false;
}
stable_sort(pvVals->begin(), pvVals->end());
return true;
}
template<class ElemType>
bool pRPL::Cellspace::
find(LongVect *pvFoundIdxs,
const ElemType *pVal,
const pRPL::CoordBR *pRectangle,
bool warning) const {
bool found = false;
if(pvFoundIdxs == NULL) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NULL pointer to the vector of found indices" << endl;
}
found = false;
}
else {
pvFoundIdxs->clear();
if(_pInfo->isDataType<ElemType>(warning) && !isEmpty(warning)) {
if(pRectangle == NULL) {
long mySize = _pInfo->size();
const ElemType *pElem = (const ElemType*)_pData;
while(pElem != (const ElemType*)_pData + mySize) {
pElem = (const ElemType*)std::find(pElem, (const ElemType*)_pData+mySize, *pVal);
if(pElem != (const ElemType*)_pData + mySize) {
pvFoundIdxs->push_back(pElem - (const ElemType*)_pData);
if(!found) {
found = true;
}
pElem++;
}
} // End of while loop
}
else if(_pInfo->validBR(*pRectangle, warning)){
long nRecCols = pRectangle->nCols();
for(long iRow = pRectangle->minIRow(); iRow <= pRectangle->maxIRow(); iRow++) {
const ElemType *aRow = (const ElemType*)_pData + iRow*(_pInfo->nCols());
const ElemType *pElem = aRow+pRectangle->minICol();
while(pElem != aRow + pRectangle->maxICol() + 1) {
pElem = std::find(pElem, aRow+pRectangle->maxICol()+1, *pVal);
if(pElem != aRow + pRectangle->maxICol() + 1) {
long idx = _pInfo->coord2idx(iRow, pElem-aRow);
pvFoundIdxs->push_back(idx);
if(!found) {
found = true;
}
pElem++;
}
} // End of while loop
}
}
}
}
return found;
}
template<class ElemType>
bool pRPL::Cellspace::
find(pRPL::LongVect *pvFoundIdxs,
const vector<ElemType> *pvVals,
const pRPL::CoordBR *pRectangle,
bool warning) const {
bool found = false;
if(pvFoundIdxs == NULL) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NULL pointer to the vector of found indices" << endl;
}
found = false;
}
else if(pvVals == NULL) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NULL pointer to the vector of values" << endl;
}
found = false;
}
else {
pvFoundIdxs->clear();
if(_pInfo->isDataType<ElemType>(warning) && !isEmpty(warning)) {
if(pRectangle == NULL) {
long mySize = _pInfo->size();
for(long iElem = 0; iElem < mySize; iElem++) {
const ElemType &elemVal = *((const ElemType*)_pData + iElem);
if(std::find(pvVals->begin(), pvVals->end(), elemVal) != pvVals->end()) {
pvFoundIdxs->push_back(iElem);
if(!found) {
found = true;
}
}
}
}
else if(_pInfo->validBR(*pRectangle, warning)){
for(long iRow = pRectangle->minIRow(); iRow <= pRectangle->maxIRow(); iRow++) {
for(long iCol = pRectangle->minICol(); iCol <= pRectangle->maxICol(); iCol++) {
long iElem = _pInfo->coord2idx(iRow, iCol);
const ElemType& elemVal = *((const ElemType*)_pData + iElem);
if(std::find(pvVals->begin(), pvVals->end(), elemVal) != pvVals->end()) {
pvFoundIdxs->push_back(iElem);
if(!found) {
found = true;
}
}
}
}
}
}
}
return found;
}
template<class ElemType, class Predicate>
bool pRPL::Cellspace::
find(LongVect *pvFoundIdxs,
Predicate pred,
const pRPL::CoordBR *pRectangle,
bool excldNoData,
bool warning) const {
bool found = false;
if(pvFoundIdxs == NULL) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NULL pointer to the vector of found indices" << endl;
found = false;
}
else {
pvFoundIdxs->clear();
if(_pInfo->isDataType<ElemType>(warning) && !isEmpty(warning)) {
if(pRectangle == NULL) {
long mySize = _pInfo->size();
const ElemType *pElem = (const ElemType*)_pData;
while(pElem != (const ElemType*)_pData + mySize) {
pElem = (const ElemType*)std::find_if(pElem, (const ElemType*)_pData+mySize, pred);
if(pElem != (const ElemType*)_pData + mySize) {
if(excldNoData &&
fabs(*pElem - _pInfo->getNoDataValAs<ElemType>()) < pRPL::EPSINON) {
continue;
}
else {
pvFoundIdxs->push_back(pElem - (const ElemType*)_pData);
if(!found) {
found = true;
}
}
pElem++;
}
} // End of while loop
}
else if(_pInfo->validBR(*pRectangle, warning)) {
for(long iRow = pRectangle->minIRow(); iRow <= pRectangle->maxIRow(); iRow++) {
const ElemType* aRow = (const ElemType*)_pData + iRow*(_pInfo->nCols());
const ElemType* pElem = aRow+pRectangle->minICol();
while(pElem != aRow + pRectangle->maxICol() + 1) {
pElem = std::find_if(pElem, aRow+pRectangle->maxICol()+1, pred);
if(pElem != aRow + pRectangle->maxICol() + 1) {
if(excldNoData &&
fabs(*pElem - _pInfo->getNoDataValAs<ElemType>()) < pRPL::EPSINON) {
continue;
}
else {
long idx = _pInfo->coord2idx(iRow, pElem-aRow);
pvFoundIdxs->push_back(idx);
if(!found) {
found = true;
}
}
pElem++;
}
} // End of while loop
}
}
}
}
return found;
}
template<class ElemType>
long pRPL::Cellspace::
count(const ElemType *pVal,
const pRPL::CoordBR *pRectangle,
bool warning) const {
long num = 0;
if(pVal == NULL) {
if(warning) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NULL pointer to the value to count" << endl;
}
}
else if(_pInfo->isDataType<ElemType>(warning) && !isEmpty(warning)) {
if(pRectangle == NULL) {
num = std::count((const ElemType*)_pData, (const ElemType*)_pData+_pInfo->size(), *pVal);
}
else if(_pInfo->validBR(*pRectangle, true)) {
for(long iRow = pRectangle->minIRow(); iRow <= pRectangle->maxIRow(); iRow++) {
long iColStart = pRectangle->minICol();
long iColEnd = pRectangle->maxICol();
long iElemStart = _pInfo->coord2idx(iRow, iColStart);
long iElemEnd = _pInfo->coord2idx(iRow, iColEnd) + 1;
num += std::count((const ElemType*)_pData+iElemStart, (const ElemType*)_pData+iElemEnd, *pVal);
} // End of iRow loop
}
}
return num;
}
template<class ElemType>
long pRPL::Cellspace::
count(const vector<ElemType> *pvVals,
const pRPL::CoordBR *pRectangle,