XdmfArray.hpp 18.3 KB
Newer Older
Kenneth Leiter's avatar
Kenneth Leiter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfArray.hpp                                                       */
/*                                                                           */
/*  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.                                                 */
/*                                                                           */
/*****************************************************************************/

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
24 25 26
#ifndef XDMFARRAY_HPP_
#define XDMFARRAY_HPP_

27
// Forward Declarations
28
class XdmfArrayType;
29
class XdmfHeavyDataController;
30

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
31
// Includes
32
#include "XdmfCore.hpp"
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
33
#include "XdmfItem.hpp"
34
#include <boost/shared_array.hpp>
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
35 36
#include <boost/variant.hpp>

37
/**
38 39
 * @brief Provides storage for data values that are read in or will be
 * written to heavy data on disk.
40
 *
41 42 43 44 45
 * XdmfArray provides a single interface for storing a variety of data
 * types.  The data type stored is determined by the type initially
 * inserted into the array.  An array can be initialized with a
 * specific data type before insertion of values by calling
 * initialize().
46 47 48
 *
 * An XdmfArray is associated with heavy data files on disk through an
 * XdmfHeavyDataController. When an Xdmf file is read from disk,
49 50 51 52 53 54 55 56
 * XdmfHeavyDataControllers are attached to all created XdmfArrays
 * that contain values stored in heavy data. These values are not read
 * into memory when the Xdmf file is parsed. The array is
 * uninitialized and the return value of isInitialized() is false.  In
 * order to read the heavy data values into memory, read() must be
 * called. This will cause the array to ask for values to be read from
 * disk using the XdmfHeavyDataController. After the values have been
 * read from heavy data on disk, isInitialized() will return true.
57
 *
58 59
 * XdmfArray allows for insertion and retrieval of data in two
 * fundamental ways:
60 61 62
 *
 * By Copy:
 *
63 64 65 66
 *   getValue
 *   getValues
 *   insert
 *   pushBack
67
 *
68 69 70
 * XdmfArray stores its own copy of the data.  Modifications to the
 * data stored in the XdmfArray will not change values stored in the
 * original array.
71 72 73
 *
 * By Shared Reference:
 *
74 75
 *   getValuesInternal
 *   setValuesInternal
76
 *
77 78 79 80
 * XdmfArray shares a reference to the data.  No copy is
 * made. XdmfArray holds a shared pointer to the original data.
 * Modifications to the data stored in the XdmfArray also causes
 * modification to values stored in the original array.
81 82
 *
 * Xdmf supports the following datatypes:
83 84 85 86 87 88 89 90 91
 *   Int8
 *   Int16
 *   Int32
 *   Int64
 *   Float32
 *   Float64
 *   UInt8
 *   UInt16
 *   UInt32
Kenneth Leiter's avatar
Kenneth Leiter committed
92
 *   String
93
 */
94
class XDMFCORE_EXPORT XdmfArray : public XdmfItem {
95

96
public:
97 98 99 100 101 102

  /**
   * Create a new XdmfArray.
   *
   * @return constructed XdmfArray.
   */
103
  static shared_ptr<XdmfArray> New();
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

  virtual ~XdmfArray();

  LOKI_DEFINE_VISITABLE(XdmfArray, XdmfItem);
  static const std::string ItemTag;

  /**
   * Remove all values from this array.
   */
  void clear();

  /**
   * Remove a value from this array.
   */
  void erase(const unsigned int index);

  /**
   * Get the data type of this array.
   *
   * @return a XdmfArrayType containing the data type for the array.
   */
125
  shared_ptr<const XdmfArrayType> getArrayType() const;
126 127

  /**
128 129
   * Get the capacity of this array, the number of values the array
   * can store without reallocation.
130 131 132 133 134 135
   *
   * @return the capacity of this array.
   */
  unsigned int getCapacity() const;

  /**
136
   * Get the dimensions of the array.
137 138 139
   *
   * @return the dimensions of the array.
   */
140 141 142 143 144 145 146 147
  std::vector<unsigned int> getDimensions() const;

  /**
   * Get the dimensions of the array as a string.
   *
   * @return the dimensions of the array as a string.
   */
  std::string getDimensionsString() const;
148 149 150 151 152 153

  /**
   * Get the heavy data controller attached to this array.
   *
   * @return the heavy data controller attached to this array.
   */
154
  shared_ptr<XdmfHeavyDataController> getHeavyDataController();
155 156

  /**
157 158
   * Get the heavy data controller attached to this array (const
   * version).
159 160 161
   *
   * @return the heavy data controller attached to this array.
   */
162
  shared_ptr<const XdmfHeavyDataController>
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
  getHeavyDataController() const;

  std::map<std::string, std::string> getItemProperties() const;

  std::string getItemTag() const;

  /**
   * Get the name of the array.
   *
   * @return a string containing the name of the array.
   */
  std::string getName() const;

  /**
   * Get the number of values stored in this array.
   *
   * @return the number of values stored in this array.
   */
  unsigned int getSize() const;

  /**
   * Get a copy of a single value stored in this array.
   *
   * @return the requested value.
   */
  template <typename T>
  T getValue(const unsigned int index) const;

  /**
   * Get a copy of the values stored in this array
   *
   * @param startIndex the index in this array to begin copying from.
   * @param valuesPointer a pointer to an array to copy into.
   * @param numValues the number of values to copy.
   * @param arrayStride number of values to stride in this array between each
   * copy.
   * @param valuesStride number of values to stride in the pointer between each
   * copy.
   */
  template <typename T> void
  getValues(const unsigned int startIndex,
            T * const valuesPointer,
            const unsigned int numValues = 1,
            const unsigned int arrayStride = 1,
            const unsigned int valuesStride = 1) const;

  /**
   * Get a smart pointer to the internal values stored in this array.
   *
212 213
   * @return a smart pointer to the internal vector of values stored
   * in this array.
214 215
   */
  template <typename T>
216
  shared_ptr<std::vector<T> > getValuesInternal();
217 218 219 220 221 222 223 224 225

  /**
   * Get a pointer to the internal values stored in this array.
   *
   * @return a void pointer to the first value stored in this array.
   */
  void * getValuesInternal();

  /**
226 227
   * Get a pointer to the internal values stored in this array (const
   * version).
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
   *
   * @return a void pointer to the first value stored in this array.
   */
  const void * getValuesInternal() const;

  /**
   * Get the values stored in this array as a string.
   *
   * @return a string containing the contents of the array.
   */
  std::string getValuesString() const;

  /**
   * Initialize the array to contain a particular type.
   *
   * @param size the number of values in the initialized array.
   *
245 246
   * @return a smart pointer to the internal vector of values
   * initialized in this array.
247 248
   */
  template <typename T>
249
  shared_ptr<std::vector<T> > initialize(const unsigned int size = 0);
250

251 252 253 254 255 256 257 258 259
  /**
   * Initialize the array to contain a particular type.
   *
   * @param dimensions the dimensions of the initialized array.
   *
   * @return a smart pointer to the internal vector of values
   * initialized in this array.
   */
  template <typename T>
260
  shared_ptr<std::vector<T> >
261 262
  initialize(const std::vector<unsigned int> & dimensions);

263 264 265 266 267 268
  /**
   * Initialize the array to contain a particular type.
   *
   * @param arrayType the type of array to initialize.
   * @param size the number of values in the initialized array.
   */
269
  void initialize(const shared_ptr<const XdmfArrayType> arrayType,
270 271
                  const unsigned int size = 0);

272 273 274 275 276 277
  /**
   * Initialize the array to contain a particular type.
   *
   * @param arrayType the type of array to initialize.
   * @param dimensions the number dimensions of the initialized array.
   */
278
  void initialize(const shared_ptr<const XdmfArrayType> arrayType,
279 280
                  const std::vector<unsigned int> & dimensions);

281 282
  using XdmfItem::insert;

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  /**
   * Insert value into this array
   *
   * @param index the index in this array to insert.
   * @param value the value to insert
   */
  template<typename T>
  void insert(const unsigned int index,
              const T & value);

  /**
   * Insert values from an XdmfArray into this array.
   *
   * @param startIndex the index in this array to begin insertion.
   * @param values a shared pointer to an XdmfArray to copy into this array.
   * @param valuesStartIndex the index in the XdmfArray to begin copying.
   * @param numValues the number of values to copy into this array.
   * @param arrayStride number of values to stride in this array between each
   * copy.
   * @param valuesStride number of values to stride in the XdmfArray between
   * each copy.
   */
  void insert(const unsigned int startIndex,
306
              const shared_ptr<const XdmfArray> values,
307
              const unsigned int valuesStartIndex = 0,
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
              const unsigned int numValues = 1,
              const unsigned int arrayStride = 1,
              const unsigned int valuesStride = 1);

  /**
   * Insert values into this array.
   *
   * @param startIndex the index in this array to begin insertion.
   * @param valuesPointer a pointer to the values to copy into this array.
   * @param numValues the number of values to copy into this array.
   * @param arrayStride number of values to stride in this array between each
   * copy.
   * @param valuesStride number of values to stride in the pointer between
   * each copy.
   */
  template<typename T>
  void insert(const unsigned int startIndex,
              const T * const valuesPointer,
              const unsigned int numValues = 1,
              const unsigned int arrayStride = 1,
              const unsigned int valuesStride = 1);

  /**
331 332
   * Returns whether the array is initialized (contains values in
   * memory).
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
   */
  bool isInitialized() const;

  /**
   * Copy a value to the back of this array
   */
  template <typename T>
  void pushBack(const T & value);

  /**
   * Read data from disk into memory.
   */
  void read();
  /**
   * Release all data currently held in memory.
   */
  void release();

  /**
   * Set the capacity of the array to at least size.
   *
   * @param size the capacity to set this array to.
   */
  void reserve(const unsigned int size);

  /**
359 360 361 362
   * Resizes the array to contain a number of values. If numValues is
   * larger than the current size, values are appended to the end of
   * the array equal to value. If numValues is less than the current
   * size, values at indices larger than numValues are removed.
363 364 365 366 367 368 369 370
   *
   * @param numValues the number of values to resize this array to.
   * @param value the number to initialize newly created values to, if needed.
   */
  template<typename T>
  void resize(const unsigned int numValues,
              const T & value = 0);

371 372 373 374 375 376 377 378 379 380 381 382 383 384
  /**
   * Resizes the array to specified dimensions. If the number of
   * values specified by the dimensions is larger than the current
   * size, values are appended to the end of the array equal to
   * value. If numValues is less than the current size, values at
   * indices larger than numValues are removed.
   *
   * @param dimensions the dimensions to resize the arrat to.
   * @param value the number to intialize newly created values to, if needed.
   */
  template<typename T>
  void resize(const std::vector<unsigned int> & dimensions,
              const T & value = 0);

385 386 387
  /**
   * Attach an heavy data controller to this array.
   *
388 389
   * @param heavyDataController the heavy data controller to attach to
   * this array.
390 391
   */
  void
392
  setHeavyDataController(const shared_ptr<XdmfHeavyDataController> heavyDataController);
393 394 395 396 397 398 399 400 401

  /**
   * Set the name of the array.
   *
   * @param name of the array to set.
   */
  void setName(const std::string & name);

  /**
402 403 404 405 406 407 408 409 410 411 412 413 414
   * Sets the values of this array to the values stored in the
   * arrayPointer array. No copy is made. Modifications to the array
   * are not permitted through the XdmfArray API. Any calls through
   * the XdmfArray API to modify the array (i.e. any non-const
   * function) will result in the array being copied into internal
   * storage. The internal copy is then modified.  This prevents
   * situations where a realloc of the pointer could cause other
   * references to become invalid. The caller of this method can
   * continue to modify the values stored in arrayPointer on its
   * own. This function is meant for applications that have their own
   * array data structures that merely use Xdmf to output the data, an
   * operation that should not require a copy. Other applications that
   * use Xdmf for in memory data storage should avoid this function.
415 416 417 418 419 420 421 422 423 424 425 426
   *
   * @param arrayPointer a pointer to an array to store in this XdmfArray.
   * @param numValues the number of values in the array.
   * @param transferOwnership whether to transfer responsibility for deletion
   * of the array to XdmfArray.
   */
  template<typename T>
  void setValuesInternal(const T * const arrayPointer,
                         const unsigned int numValues,
                         const bool transferOwnership = 0);

  /**
427 428 429 430
   * Sets the values of this array to the values stored in the
   * vector. No copy is made. The caller of this method retains
   * ownership of the data and must ensure that the array is still
   * valid for the entire time Xdmf needs it.
431 432 433 434 435 436 437 438 439 440
   *
   * @param array a vector to store in this XdmfArray.
   * @param transferOwnership whether to transfer responsibility for deletion
   * of the array to XdmfArray.
   */
  template<typename T>
  void setValuesInternal(std::vector<T> & array,
                         const bool transferOwnership = 0);

  /**
441 442 443
   * Sets the values of this array to the values stored in the
   * vector. No copy is made. This array shares ownership with other
   * references to the smart pointer.
444 445 446 447
   *
   * @param array a smart pointer to a vector to store in this array.
   */
  template<typename T>
448
  void setValuesInternal(const shared_ptr<std::vector<T> > array);
449 450

  /**
451 452
   * Exchange the contents of the vector with the contents of this
   * array. No copy is made. The internal arrays are swapped.
453 454 455 456 457 458 459 460
   *
   * @param array a vector to exchange values with.
   * @return bool whether the swap was successful.
   */
  template<typename T>
  bool swap(std::vector<T> & array);

  /**
461 462
   * Exchange the contents of the vector with the contents of this
   * array. No copy is made. The internal arrays are swapped.
463 464 465 466 467
   *
   * @param array a smart pointer to a vector to exchange values with.
   * @return bool whether the swap was successful.
   */
  template<typename T>
468
  bool swap(const shared_ptr<std::vector<T> > array);
469 470

  /**
471 472
   * Exchange the contents of an XdmfArray with the contents of this
   * array. No copy is made. The internal arrays are swapped.
473 474 475
   *
   * @param array a smart pointer to a vector to exchange values with.
   */
476
  void swap(const shared_ptr<XdmfArray> array);
477

478
protected:
479 480 481 482 483

  XdmfArray();

  virtual void
  populateItem(const std::map<std::string, std::string> & itemProperties,
484
               const std::vector<shared_ptr<XdmfItem> > & childItems,
485 486
               const XdmfCoreReader * const reader);

487
private:
488 489 490 491 492 493 494 495 496

  XdmfArray(const XdmfArray &);  // Not implemented.
  void operator=(const XdmfArray &);  // Not implemented.

  // Variant Visitor Operations
  class Clear;
  class Erase;
  class GetArrayType;
  class GetCapacity;
497
  template <typename T> class GetValue;
498 499 500 501 502 503 504 505 506 507 508 509 510
  template <typename T> class GetValues;
  class GetValuesPointer;
  class GetValuesString;
  template <typename T> class Insert;
  class InsertArray;
  class InternalizeArrayPointer;
  struct NullDeleter;
  template <typename T> class PushBack;
  class Reserve;
  template <typename T> class Resize;
  class Size;

  /**
511 512 513 514 515 516
   * After setValues() is called, XdmfArray stores a pointer that is
   * not allowed to be modified through the XdmfArray API. If the user
   * desires to modify the contents of the pointer, they must do so
   * without calling any non-const functions of XdmfArray. If they do
   * call non-const functions of XdmfArray, we attempt to accommodate
   * by copying the array pointer into internal data structures.
517 518 519 520
   */
  void internalizeArrayPointer();

  typedef boost::variant<
521
    boost::blank,
522 523 524 525 526 527 528 529
    shared_ptr<std::vector<char> >,
    shared_ptr<std::vector<short> >,
    shared_ptr<std::vector<int> >,
    shared_ptr<std::vector<long> >,
    shared_ptr<std::vector<float> >,
    shared_ptr<std::vector<double> >,
    shared_ptr<std::vector<unsigned char> >,
    shared_ptr<std::vector<unsigned short> >,
530
    shared_ptr<std::vector<unsigned int> >,
Kenneth Leiter's avatar
Kenneth Leiter committed
531
    shared_ptr<std::vector<std::string> >,
532 533 534 535 536 537 538 539
    boost::shared_array<const char>,
    boost::shared_array<const short>,
    boost::shared_array<const int>,
    boost::shared_array<const long>,
    boost::shared_array<const float>,
    boost::shared_array<const double>,
    boost::shared_array<const unsigned char>,
    boost::shared_array<const unsigned short>,
540 541
    boost::shared_array<const unsigned int>  > ArrayVariant;
  
542 543
  ArrayVariant mArray;
  unsigned int mArrayPointerNumValues;
544
  std::vector<unsigned int> mDimensions;
545
  shared_ptr<XdmfHeavyDataController> mHeavyDataController;
546 547
  std::string mName;
  unsigned int mTmpReserveSize;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
548 549
};

550 551
#include "XdmfArray.tpp"

552
#ifdef _WIN32
553
XDMFCORE_TEMPLATE template class XDMFCORE_EXPORT
554
shared_ptr<const XdmfArrayType>;
555
XDMFCORE_TEMPLATE template class XDMFCORE_EXPORT
556
shared_ptr<XdmfHeavyDataController>;
557
XDMFCORE_TEMPLATE template class XDMFCORE_EXPORT
558
shared_ptr<const XdmfHeavyDataController>;
559
XDMFCORE_TEMPLATE template class XDMFCORE_EXPORT
560
shared_ptr<Loki::BaseVisitor>;
561
XDMFCORE_TEMPLATE template class XDMFCORE_EXPORT
562
Loki::Visitor<shared_ptr<XdmfArray>, shared_ptr<XdmfItem> >;
563 564
#endif

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
565
#endif /* XDMFARRAY_HPP_ */