XdmfArray.tpp 19.7 KB
Newer Older
Kenneth Leiter's avatar
Kenneth Leiter committed
1
/*****************************************************************************/
Kenneth Leiter's avatar
Kenneth Leiter committed
2
3
/*                                    Xdmf                                   */
/*                       Extensible Data Model and Format                    */
Kenneth Leiter's avatar
Kenneth Leiter committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*                                                                           */
/*  Id : XdmfArray.tpp                                                       */
/*                                                                           */
/*  Author:                                                                  */
/*     Kenneth Leiter                                                        */
/*     kenneth.leiter@arl.army.mil                                           */
/*     US Army Research Laboratory                                           */
/*     Aberdeen Proving Ground, MD                                           */
/*                                                                           */
/*     Copyright @ 2011 US Army Research Laboratory                          */
/*     All Rights Reserved                                                   */
/*     See Copyright.txt for details                                         */
/*                                                                           */
/*     This software is distributed WITHOUT ANY WARRANTY; without            */
/*     even the implied warranty of MERCHANTABILITY or FITNESS               */
/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice             */
/*     for more information.                                                 */
/*                                                                           */
/*****************************************************************************/

24
25
#include <functional>
#include <numeric>
Kenneth Leiter's avatar
Kenneth Leiter committed
26
#include <sstream>
27
28
#include "XdmfArray.hpp"

29
30
31
32
33
34
35
36
37
38
template <typename T>
class XdmfArray::GetValue : public boost::static_visitor<T> {
public:

  GetValue(const unsigned int index) :
    mIndex(index)
  {
  }

  T
39
  operator()(const boost::blank &) const
40
  {
41
    return 0;
42
43
  }

Kenneth Leiter's avatar
Kenneth Leiter committed
44
45
46
47
48
49
  T
  operator()(const shared_ptr<std::vector<std::string> > & array) const
  {
    return (T)atof(array->operator[](mIndex).c_str());
  }

50
51
52
53
54
55
56
  template<typename U>
  T
  operator()(const shared_ptr<std::vector<U> > & array) const
  {
    return (T)array->operator[](mIndex);
  }

57
58
59
60
61
62
63
  template<typename U>
  T
  operator()(const boost::shared_array<const U> & array) const
  {
    return (T)array[mIndex];
  }

64
65
66
67
68
private:

  const unsigned int mIndex;
};

Kenneth Leiter's avatar
Kenneth Leiter committed
69
template <>
70
class XdmfArray::GetValue<std::string> :
Kenneth Leiter's avatar
Kenneth Leiter committed
71
72
73
74
75
76
77
78
79
  public boost::static_visitor<std::string> {
public:

  GetValue(const unsigned int index) :
    mIndex(index)
  {
  }

  std::string
80
  operator()(const boost::blank &) const
Kenneth Leiter's avatar
Kenneth Leiter committed
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
  {
    return "";
  }

  std::string
  operator()(const shared_ptr<std::vector<std::string> > & array) const
  {
    return array->operator[](mIndex);
  }

  template<typename U>
  std::string
  operator()(const shared_ptr<std::vector<U> > & array) const
  {
    std::stringstream value;
    value << array->operator[](mIndex);
    return value.str();
  }

  std::string
  operator()(const boost::shared_array<const std::string> & array) const
  {
    return array[mIndex];
  }

  template<typename U>
  std::string
  operator()(const boost::shared_array<const U> & array) const
  {
    std::stringstream value;
    value << array[mIndex];
    return value.str();
  }

private:

  const unsigned int mIndex;
};

120
template <typename T>
121
class XdmfArray::GetValues : public boost::static_visitor<void> {
122
123
public:

124
125
126
127
128
129
130
131
132
133
134
135
136
137
  GetValues(const unsigned int startIndex,
            T * valuesPointer,
            const unsigned int numValues,
            const unsigned int arrayStride,
            const unsigned int valuesStride) :
    mStartIndex(startIndex),
    mValuesPointer(valuesPointer),
    mNumValues(numValues),
    mArrayStride(arrayStride),
    mValuesStride(valuesStride)
  {
  }

  void
138
  operator()(const boost::blank &) const
139
  {
140
    return;
141
142
  }

Kenneth Leiter's avatar
Kenneth Leiter committed
143
144
145
146
147
148
149
150
151
  void
  operator()(const shared_ptr<std::vector<std::string> > & array) const
  {
    for(unsigned int i=0; i<mNumValues; ++i) {
      mValuesPointer[i*mValuesStride] =
        (T)atof(array->operator[](mStartIndex + i*mArrayStride).c_str());
    }
  }

152
153
  template<typename U>
  void
154
  operator()(const shared_ptr<std::vector<U> > & array) const
155
156
157
158
159
160
  {
    for(unsigned int i=0; i<mNumValues; ++i) {
      mValuesPointer[i*mValuesStride] =
        (T)array->operator[](mStartIndex + i*mArrayStride);
    }
  }
161

162
163
164
165
166
167
168
169
170
  template<typename U>
  void
  operator()(const boost::shared_array<const U> & array) const
  {
    for(unsigned int i=0; i<mNumValues; ++i) {
      mValuesPointer[i*mValuesStride] = (T)array[mStartIndex + i*mArrayStride];
    }
  }

171
172
private:

173
174
175
176
177
  const unsigned int mStartIndex;
  T * mValuesPointer;
  const unsigned int mNumValues;
  const unsigned int mArrayStride;
  const unsigned int mValuesStride;
178
179
};

Kenneth Leiter's avatar
Kenneth Leiter committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
template <>
class XdmfArray::GetValues<std::string> : public boost::static_visitor<void> {
public:

  GetValues(const unsigned int startIndex,
            std::string * valuesPointer,
            const unsigned int numValues,
            const unsigned int arrayStride,
            const unsigned int valuesStride) :
    mStartIndex(startIndex),
    mValuesPointer(valuesPointer),
    mNumValues(numValues),
    mArrayStride(arrayStride),
    mValuesStride(valuesStride)
  {
  }

  void
198
  operator()(const boost::blank &) const
Kenneth Leiter's avatar
Kenneth Leiter committed
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
  {
    return;
  }

  template<typename U>
  void
  operator()(const shared_ptr<std::vector<U> > & array) const
  {
    for(unsigned int i=0; i<mNumValues; ++i) {
      std::stringstream value;
      value << array->operator[](mStartIndex + i*mArrayStride);
      mValuesPointer[i*mValuesStride] = value.str();
    }
  }

  template<typename U>
  void
  operator()(const boost::shared_array<const U> & array) const
  {
    for(unsigned int i=0; i<mNumValues; ++i) {
      std::stringstream value;
      value << array[mStartIndex + i*mArrayStride];
      mValuesPointer[i*mValuesStride] = value.str();
    }
  }

private:

  const unsigned int mStartIndex;
  std::string * mValuesPointer;
  const unsigned int mNumValues;
  const unsigned int mArrayStride;
  const unsigned int mValuesStride;
};

234
template <typename T>
235
class XdmfArray::Insert : public boost::static_visitor<void> {
236
237
public:

238
239
  Insert(XdmfArray * const array,
         const unsigned int startIndex,
240
241
242
         const T * const valuesPointer,
         const unsigned int numValues,
         const unsigned int arrayStride,
243
244
         const unsigned int valuesStride,
         std::vector<unsigned int> & dimensions) :
245
    mArray(array),
246
247
248
249
    mStartIndex(startIndex),
    mValuesPointer(valuesPointer),
    mNumValues(numValues),
    mArrayStride(arrayStride),
250
251
    mValuesStride(valuesStride),
    mDimensions(dimensions)
252
253
254
  {
  }

255
  void
256
  operator()(const boost::blank &) const
257
258
259
260
261
262
  {
    mArray->initialize<T>();
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

Kenneth Leiter's avatar
Kenneth Leiter committed
263
264
265
  void
  operator()(shared_ptr<std::vector<std::string> > & array) const
  {
266
267
    const unsigned int size = 
      mStartIndex + (mNumValues - 1) * mArrayStride + 1;
Kenneth Leiter's avatar
Kenneth Leiter committed
268
269
270
271
272
273
274
275
276
277
278
    if(array->size() < size) {
      array->resize(size);
      mDimensions.clear();
    }
    for(unsigned int i=0; i<mNumValues; ++i) {
      std::stringstream value;
      value << mValuesPointer[i*mValuesStride];
      array->operator[](mStartIndex + i*mArrayStride) = value.str();
    }
  }

279
280
  template<typename U>
  void
281
  operator()(shared_ptr<std::vector<U> > & array) const
282
  {
283
284
    const unsigned int size = 
      mStartIndex + (mNumValues - 1) * mArrayStride + 1;
285
286
    if(array->size() < size) {
      array->resize(size);
287
      mDimensions.clear();
288
289
290
291
292
293
    }
    for(unsigned int i=0; i<mNumValues; ++i) {
      array->operator[](mStartIndex + i*mArrayStride) =
        (U)mValuesPointer[i*mValuesStride];
    }
  }
294

295
296
  template<typename U>
  void
297
  operator()(boost::shared_array<const U> &) const
298
299
300
301
302
303
  {
    mArray->internalizeArrayPointer();
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

304
305
private:

306
  XdmfArray * const mArray;
307
308
309
310
311
  const unsigned int mStartIndex;
  const T * const mValuesPointer;
  const unsigned int mNumValues;
  const unsigned int mArrayStride;
  const unsigned int mValuesStride;
312
  std::vector<unsigned int> & mDimensions;
313
314
};

Kenneth Leiter's avatar
Kenneth Leiter committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
template <>
class XdmfArray::Insert<std::string> : public boost::static_visitor<void> {
public:

  Insert(XdmfArray * const array,
         const unsigned int startIndex,
         const std::string * const valuesPointer,
         const unsigned int numValues,
         const unsigned int arrayStride,
         const unsigned int valuesStride,
         std::vector<unsigned int> & dimensions) :
    mArray(array),
    mStartIndex(startIndex),
    mValuesPointer(valuesPointer),
    mNumValues(numValues),
    mArrayStride(arrayStride),
    mValuesStride(valuesStride),
    mDimensions(dimensions)
  {
  }

  void
337
  operator()(const boost::blank &) const
Kenneth Leiter's avatar
Kenneth Leiter committed
338
339
340
341
342
343
344
345
346
  {
    mArray->initialize<std::string>();
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

  void
  operator()(shared_ptr<std::vector<std::string> > & array) const
  {
347
348
    const unsigned int size = 
      mStartIndex + (mNumValues - 1) * mArrayStride + 1;
Kenneth Leiter's avatar
Kenneth Leiter committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
    if(array->size() < size) {
      array->resize(size);
      mDimensions.clear();
    }
    for(unsigned int i=0; i<mNumValues; ++i) {
      array->operator[](mStartIndex + i*mArrayStride) =
        mValuesPointer[i*mValuesStride].c_str();
    }
  }

  template<typename U>
  void
  operator()(shared_ptr<std::vector<U> > & array) const
  {
363
364
    const unsigned int size = 
      mStartIndex + (mNumValues - 1) * mArrayStride + 1;
Kenneth Leiter's avatar
Kenneth Leiter committed
365
366
367
368
369
370
371
372
373
374
375
376
    if(array->size() < size) {
      array->resize(size);
      mDimensions.clear();
    }
    for(unsigned int i=0; i<mNumValues; ++i) {
      array->operator[](mStartIndex + i*mArrayStride) =
        (U)atof(mValuesPointer[i*mValuesStride].c_str());
    }
  }

  template<typename U>
  void
377
  operator()(boost::shared_array<const U> &) const
Kenneth Leiter's avatar
Kenneth Leiter committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  {
    mArray->internalizeArrayPointer();
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

private:

  XdmfArray * const mArray;
  const unsigned int mStartIndex;
  const std::string * const mValuesPointer;
  const unsigned int mNumValues;
  const unsigned int mArrayStride;
  const unsigned int mValuesStride;
  std::vector<unsigned int> & mDimensions;
};

395
template <typename T>
396
class XdmfArray::PushBack : public boost::static_visitor<void> {
397
398
public:

399
400
401
402
403
404
405
406
  PushBack(const T & val,
           XdmfArray * const array) :
    mVal(val),
    mArray(array)
  {
  }

  void
407
  operator()(const boost::blank &) const
408
  {
409
410
411
    mArray->initialize<T>();
    boost::apply_visitor(*this,
                         mArray->mArray);
412
  }
413

Kenneth Leiter's avatar
Kenneth Leiter committed
414
415
416
417
418
419
420
421
422
  void
  operator()(shared_ptr<std::vector<std::string> > & array) const
  {
    std::stringstream value;
    value << mVal;
    array->push_back(value.str());
    mArray->mDimensions.clear();
  }

423
424
  template<typename U>
  void
425
  operator()(shared_ptr<std::vector<U> > & array) const
426
427
  {
    array->push_back((U)mVal);
428
429
430
431
432
    mArray->mDimensions.clear();
  }

  template<typename U>
  void
433
  operator()(const boost::shared_array<const U> &) const
434
435
436
437
  {
    mArray->internalizeArrayPointer();
    boost::apply_visitor(*this,
                         mArray->mArray);
438
  }
439
440
441

private:

442
  const T & mVal;
443
  XdmfArray * const mArray;
444
445
};

Kenneth Leiter's avatar
Kenneth Leiter committed
446
447
448
449
450
451
452
453
454
455
456
457
template <>
class XdmfArray::PushBack<std::string> : public boost::static_visitor<void> {
public:

  PushBack(const std::string & val,
           XdmfArray * const array) :
    mVal(val),
    mArray(array)
  {
  }

  void
458
  operator()(const boost::blank &) const
Kenneth Leiter's avatar
Kenneth Leiter committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
  {
    mArray->initialize<std::string>();
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

  void
  operator()(shared_ptr<std::vector<std::string> > & array) const
  {
    array->push_back(mVal);
    mArray->mDimensions.clear();
  }

  template<typename U>
  void
  operator()(shared_ptr<std::vector<U> > & array) const
  {
    array->push_back((U)atof(mVal.c_str()));
    mArray->mDimensions.clear();
  }

  template<typename U>
  void
482
  operator()(const boost::shared_array<const U> &) const
Kenneth Leiter's avatar
Kenneth Leiter committed
483
484
485
486
487
488
489
490
491
492
493
494
  {
    mArray->internalizeArrayPointer();
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

private:

  const std::string & mVal;
  XdmfArray * const mArray;
};

495
template <typename T>
496
class XdmfArray::Resize : public boost::static_visitor<void> {
497
498
public:

499
500
  Resize(XdmfArray * const array,
         const unsigned int numValues,
501
         const T & val) :
502
    mArray(array),
503
504
505
506
    mNumValues(numValues),
    mVal(val)
  {
  }
507

508
  void
509
  operator()(const boost::blank &) const
510
511
512
513
514
515
  {
    mArray->initialize<T>();
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

Kenneth Leiter's avatar
Kenneth Leiter committed
516
517
518
519
520
521
522
523
524
  void
  operator()(shared_ptr<std::vector<std::string> > & array) const
  {
    std::stringstream value;
    value << mVal;
    array->resize(mNumValues, value.str());
    mArray->mDimensions.clear();
  }

525
526
  template<typename U>
  void
527
  operator()(shared_ptr<std::vector<U> > & array) const
528
529
  {
    array->resize(mNumValues, (U)mVal);
530
531
532
533
534
    mArray->mDimensions.clear();
  }

  template<typename U>
  void
535
  operator()(const boost::shared_array<const U> &) const
536
537
538
539
  {
    mArray->internalizeArrayPointer();
    boost::apply_visitor(*this,
                         mArray->mArray);
540
  }
541
542
543

private:

544
  XdmfArray * mArray;
545
546
  const unsigned int mNumValues;
  const T & mVal;
547
548
};

Kenneth Leiter's avatar
Kenneth Leiter committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
template <>
class XdmfArray::Resize<std::string> : public boost::static_visitor<void> {
public:

  Resize(XdmfArray * const array,
         const unsigned int numValues,
         const std::string & val) :
    mArray(array),
    mNumValues(numValues),
    mVal(val)
  {
  }

  void
563
  operator()(const boost::blank &) const
Kenneth Leiter's avatar
Kenneth Leiter committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
  {
    mArray->initialize<std::string>();
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

  void
  operator()(shared_ptr<std::vector<std::string> > & array) const
  {
    array->resize(mNumValues, mVal);
    mArray->mDimensions.clear();
  }

  template<typename U>
  void
  operator()(shared_ptr<std::vector<U> > & array) const
  {
    array->resize(mNumValues, (U)atof(mVal.c_str()));
    mArray->mDimensions.clear();
  }

  template<typename U>
  void
587
  operator()(const boost::shared_array<const U> &) const
Kenneth Leiter's avatar
Kenneth Leiter committed
588
589
590
591
592
593
594
595
596
597
598
599
600
  {
    mArray->internalizeArrayPointer();
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

private:

  XdmfArray * mArray;
  const unsigned int mNumValues;
  const std::string & mVal;
};

601
struct XdmfArray::NullDeleter
602
{
603
604
605
606
  void
  operator()(void const *) const
  {
  }
607
608
};

609
template <typename T>
610
611
T
XdmfArray::getValue(const unsigned int index) const
612
{
613
614
  return boost::apply_visitor(GetValue<T>(index),
                              mArray);
615
616
}

617
template <typename T>
618
619
620
621
622
623
void
XdmfArray::getValues(const unsigned int startIndex,
                     T * const valuesPointer,
                     const unsigned int numValues,
                     const unsigned int arrayStride,
                     const unsigned int valuesStride) const
624
{
625
626
627
628
629
630
  boost::apply_visitor(GetValues<T>(startIndex,
                                    valuesPointer,
                                    numValues,
                                    arrayStride,
                                    valuesStride),
                       mArray);
631
632
}

633
template <typename T>
634
shared_ptr<std::vector<T> >
635
XdmfArray::getValuesInternal()
636
{
637
  this->internalizeArrayPointer();
638
  try {
639
640
    shared_ptr<std::vector<T> > currArray =
      boost::get<shared_ptr<std::vector<T> > >(mArray);
641
642
643
    return currArray;
  }
  catch(const boost::bad_get & exception) {
644
    return shared_ptr<std::vector<T> >();
645
  }
646
647
}

648
template <typename T>
649
shared_ptr<std::vector<T> >
650
XdmfArray::initialize(const unsigned int size)
651
{
652
  // Set type of variant to type of pointer
653
  shared_ptr<std::vector<T> > newArray(new std::vector<T>(size));
654
655
656
657
658
659
  if(mTmpReserveSize > 0) {
    newArray->reserve(mTmpReserveSize);
    mTmpReserveSize = 0;
  }
  mArray = newArray;
  return newArray;
660
661
}

662
template <typename T>
663
shared_ptr<std::vector<T> >
664
665
666
667
668
669
670
671
672
673
XdmfArray::initialize(const std::vector<unsigned int> & dimensions)
{
  mDimensions = dimensions;
  const unsigned int size = std::accumulate(dimensions.begin(),
                                            dimensions.end(),
                                            1,
                                            std::multiplies<unsigned int>());
  return this->initialize<T>(size);
}

674
template<typename T>
675
676
677
void
XdmfArray::insert(const unsigned int index,
                  const T & value)
678
{
679
  boost::apply_visitor(Insert<T>(this,
680
681
682
683
684
685
                                 index,
                                 &value,
                                 1,
                                 0,
                                 0,
                                 mDimensions),
686
                       mArray);
687
688
}

689
template <typename T>
690
691
692
693
694
695
void
XdmfArray::insert(const unsigned int startIndex,
                  const T * const valuesPointer,
                  const unsigned int numValues,
                  const unsigned int arrayStride,
                  const unsigned int valuesStride)
696
{
697
698
  boost::apply_visitor(Insert<T>(this,
                                 startIndex,
699
700
701
                                 valuesPointer,
                                 numValues,
                                 arrayStride,
702
703
                                 valuesStride,
                                 mDimensions),
704
                       mArray);
705
706
}

707
template <typename T>
708
709
void
XdmfArray::pushBack(const T & value)
710
{
711
  return boost::apply_visitor(PushBack<T>(value,
712
713
                                          this),
                              mArray);
714
715
}

716
template<typename T>
717
718
719
void
XdmfArray::resize(const unsigned int numValues,
                  const T & value)
720
{
721
  return boost::apply_visitor(Resize<T>(this,
722
723
                                        numValues,
                                        value),
724
                              mArray);
725
726
}

727
template<typename T>
728
void
729
730
731
732
733
734
735
736
737
738
739
XdmfArray::resize(const std::vector<unsigned int> & dimensions,
                  const T & value)
{
  const unsigned int size = std::accumulate(dimensions.begin(),
                                            dimensions.end(),
                                            1,
                                            std::multiplies<unsigned int>());
  this->resize(size, value);
  mDimensions = dimensions;
}

740
template <typename T>
741
742
743
744
void
XdmfArray::setValuesInternal(const T * const arrayPointer,
                             const unsigned int numValues,
                             const bool transferOwnership)
745
{
746
747
748
  // Remove contents of internal array.
  if(transferOwnership) {
    const boost::shared_array<const T> newArrayPointer(arrayPointer);
749
    mArray = newArrayPointer;
750
751
752
753
  }
  else {
    const boost::shared_array<const T> newArrayPointer(arrayPointer,
                                                       NullDeleter());
754
    mArray = newArrayPointer;
755
756
  }
  mArrayPointerNumValues = numValues;
757
758
}

759
template <typename T>
760
761
762
void
XdmfArray::setValuesInternal(std::vector<T> & array,
                             const bool transferOwnership)
763
{
764
  if(transferOwnership) {
765
    shared_ptr<std::vector<T> > newArray(&array);
766
767
768
    mArray = newArray;
  }
  else {
769
    shared_ptr<std::vector<T> > newArray(&array, NullDeleter());
770
771
    mArray = newArray;
  }
772
773
}

774
template <typename T>
775
void
776
XdmfArray::setValuesInternal(const shared_ptr<std::vector<T> > array)
777
{
778
  mArray = array;
779
780
}

781
template <typename T>
782
783
bool
XdmfArray::swap(std::vector<T> & array)
784
{
785
786
787
  this->internalizeArrayPointer();
  if(!this->isInitialized()) {
    this->initialize<T>();
788
  }
789
  try {
790
791
    shared_ptr<std::vector<T> > currArray =
      boost::get<shared_ptr<std::vector<T> > >(mArray);
792
793
794
    currArray->swap(array);
    return true;
  }
795
  catch(const boost::bad_get & exception) {
796
797
    return false;
  }
798
799
}

800
template <typename T>
801
bool
802
XdmfArray::swap(const shared_ptr<std::vector<T> > array)
803
{
804
  return this->swap(*array.get());
805
}