halapi
hierarchichalalignmentformatapi
hdf5_impl/hdf5ExternalArray.h
00001 /*
00002  * Copyright (C) 2012 by Glenn Hickey (hickey@soe.ucsc.edu)
00003  *
00004  * Released under the MIT license, see LICENSE.txt
00005  */
00006 
00007 #ifndef _HDF5EXTERNALARRAY_H
00008 #define _HDF5EXTERNALARRAY_H
00009 
00010 #include <cassert>
00011 #include <H5Cpp.h>
00012 #include "halDefs.h"
00013 
00014 namespace hal {
00015 
00024 class HDF5ExternalArray
00025 {
00026 public:
00027  
00029    HDF5ExternalArray();
00030 
00032    virtual ~HDF5ExternalArray();
00033 
00041    void create(H5::CommonFG* file, 
00042                const H5std_string& path, 
00043                const H5::DataType& dataType,
00044                hsize_t numElements,
00045                const H5::DSetCreatPropList& cparms =
00046                H5::DSetCreatPropList::DEFAULT);
00047  
00056    void load(H5::CommonFG* file, const H5std_string& path,
00057              hsize_t chunksInBuffer = 1);
00058    
00060    void write();
00061 
00065    const char* get(hsize_t i);
00066 
00070    char* getUpdate(hsize_t i);
00071 
00075    template <typename T>
00076    T getValue(hsize_t index, hsize_t offset) const;
00077 
00082    template <typename T>
00083    void setValue(hsize_t index, hsize_t offset, T value);
00084 
00089    hsize_t getSize() const;
00090 
00092    const H5::DataType& getDataType() const;
00093    
00094 protected:
00095 
00097    void page(hsize_t i);
00098 
00100    H5::CommonFG* _file;
00102    H5std_string _path;
00104    H5::DataType _dataType;
00106    H5::DataSpace _dataSpace;
00108    H5::DataSet _dataSet;
00110    hsize_t _size;
00112    hsize_t _chunkSize;
00114    hsize_t _dataSize;
00116    hsize_t _bufStart;
00118    hsize_t _bufEnd;
00120    hsize_t _bufSize;
00122    char* _buf;
00124    H5::DataSpace _chunkSpace;
00127    bool _dirty;
00128 
00129 private:
00130 
00131    HDF5ExternalArray(const HDF5ExternalArray&);
00132    HDF5ExternalArray& operator=(const HDF5ExternalArray&);
00133 };  
00134 
00135 // INLINE MEMBERS
00136 
00137 inline const char* HDF5ExternalArray::get(hsize_t i)
00138 {
00139   assert(i < _size);
00140   if (i < _bufStart || i > _bufEnd)
00141   {
00142     page(i);
00143   }
00144   assert((i - _bufStart) < _bufSize);
00145   return _buf + (i - _bufStart) * _dataSize;
00146 }
00147 
00148 inline char* HDF5ExternalArray::getUpdate(hsize_t i)
00149 {
00150   if (i >= _size)
00151   {
00152     throw hal_exception("error: attempt to write hdf5 array out of bounds");
00153   }
00154   if (i < _bufStart || i > _bufEnd)
00155   {
00156     page(i);
00157   }
00158   _dirty = true;
00159   assert((i - _bufStart) < _bufSize);
00160   return _buf + (i - _bufStart) * _dataSize;
00161 }
00162 
00163 inline hsize_t HDF5ExternalArray::getSize() const
00164 {
00165   return _size;
00166 }
00167 
00168 template<typename T> 
00169 inline T HDF5ExternalArray::getValue(hsize_t index, hsize_t offset) const 
00170 {
00171   assert (offset + sizeof(T) <= _dataSize);
00172   //even though paging causes the class to change state, we
00173   //still consider getValue a const function since the array's 
00174   //contents don't change.
00175   HDF5ExternalArray* stripConstThis = const_cast<HDF5ExternalArray*>(this);
00176   return *reinterpret_cast<const T*>(stripConstThis->get(index) + offset);
00177 }
00178 
00179 template<typename T> 
00180 inline void HDF5ExternalArray::setValue(hsize_t index, hsize_t offset, T val)
00181 {
00182   assert (offset + sizeof(T) <= _dataSize);
00183   T* entry = reinterpret_cast<T*>(getUpdate(index) + offset);
00184   *entry = val;
00185 }
00186 
00187 inline const H5::DataType& HDF5ExternalArray::getDataType() const
00188 {
00189   return _dataType;
00190 }
00191 
00192 }
00193 #endif
 All Classes Namespaces Functions Variables Friends