HdfItem.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //
  2. // Created by patrik on 11.08.17.
  3. //
  4. #ifndef HDF4CPP_HDFITEM_H
  5. #define HDF4CPP_HDFITEM_H
  6. #include <hdf4cpp/HdfDefines.h>
  7. #include <hdf4cpp/HdfAttribute_priv.h>
  8. #include <hdf4cpp/HdfException.h>
  9. #include <hdf4cpp/HdfDestroyer.h>
  10. #include <algorithm>
  11. #include <map>
  12. #include <memory>
  13. #include <vector>
  14. #include <hdf/hdf.h>
  15. namespace hdf4cpp {
  16. struct Range {
  17. int32 begin;
  18. int32 quantity;
  19. int32 stride;
  20. Range(int32 begin = 0, int32 quantity = 0, int32 stride = 1) : begin(begin), quantity(quantity), stride(stride) {}
  21. intn size() const {
  22. if (!stride) {
  23. return FAIL;
  24. }
  25. return quantity / stride;
  26. }
  27. bool check(const int32& dim) const {
  28. return begin >= 0 ||
  29. begin < dim ||
  30. quantity >= 0 ||
  31. begin + quantity <= dim ||
  32. stride > 0;
  33. }
  34. static void fill(std::vector<Range>& ranges, const std::vector<int32>& dims) {
  35. for (size_t i = ranges.size(); i < dims.size(); ++i) {
  36. ranges.push_back(Range(0, dims[i]));
  37. }
  38. }
  39. };
  40. class HdfItemBase : public HdfObject {
  41. public:
  42. HdfItemBase(int32 id, const Type& type, const HdfDestroyerChain& chain) : HdfObject(type, ITEM, chain), id(id) {
  43. if (id == FAIL) {
  44. raiseException(INVALID_ID);
  45. }
  46. }
  47. virtual ~HdfItemBase() {}
  48. virtual int32 getId() const = 0;
  49. virtual std::string getName() const = 0;
  50. virtual std::vector<int32> getDims() = 0;
  51. virtual intn size() const = 0;
  52. virtual HdfAttribute getAttribute(const std::string& name) const = 0;
  53. protected:
  54. int32 id;
  55. virtual int32 getDataType() const = 0;
  56. };
  57. class HdfDatasetItem : public HdfItemBase {
  58. public:
  59. HdfDatasetItem(int32 id, const HdfDestroyerChain& chain);
  60. ~HdfDatasetItem();
  61. int32 getId() const;
  62. std::string getName() const;
  63. std::vector<int32> getDims();
  64. intn size() const;
  65. HdfAttribute getAttribute(const std::string& name) const;
  66. template <class T>
  67. void read(std::vector<T>& dest, std::vector<Range>& ranges) {
  68. std::vector<int32> dims = getDims();
  69. Range::fill(ranges, dims);
  70. intn length = 1;
  71. for (size_t i = 0; i < ranges.size(); ++i) {
  72. if (!ranges[i].check(dims[i])) {
  73. raiseException(INVALID_RANGES);
  74. }
  75. length *= ranges[i].size();
  76. }
  77. auto it = typeSizeMap.find(getDataType());
  78. if (it != typeSizeMap.end()) {
  79. if ((size_t)it->second != sizeof(T)) {
  80. raiseException(BUFFER_SIZE_NOT_ENOUGH);
  81. }
  82. } else {
  83. raiseException(INVALID_DATA_TYPE);
  84. }
  85. dest.resize(length);
  86. std::vector<int32> start, quantity, stride;
  87. for (const auto& range : ranges) {
  88. start.push_back(range.begin);
  89. quantity.push_back(range.quantity);
  90. stride.push_back(range.stride);
  91. }
  92. if (SDreaddata(id, start.data(), stride.data(), quantity.data(), dest.data()) == FAIL) {
  93. raiseException(STATUS_RETURN_FAIL);
  94. }
  95. }
  96. template <class T>
  97. void read(std::vector<T>& dest) {
  98. std::vector<int32> dims = getDims();
  99. std::vector<Range> ranges;
  100. for (const auto& dim : dims) {
  101. ranges.push_back(Range(0, dim));
  102. }
  103. read(dest, ranges);
  104. }
  105. private:
  106. intn _size;
  107. int32 dataType;
  108. std::string name;
  109. std::vector<int32> dims;
  110. int32 getDataType() const;
  111. };
  112. class HdfGroupItem : public HdfItemBase {
  113. public:
  114. HdfGroupItem(int32 id, const HdfDestroyerChain& chain);
  115. ~HdfGroupItem();
  116. int32 getId() const;
  117. std::string getName() const;
  118. std::vector<int32> getDims();
  119. intn size() const;
  120. HdfAttribute getAttribute(const std::string& name) const;
  121. private:
  122. std::string name;
  123. int32 getDataType() const;
  124. };
  125. class HdfDataItem : public HdfItemBase {
  126. public:
  127. HdfDataItem(int32 id, const HdfDestroyerChain& chain);
  128. ~HdfDataItem();
  129. int32 getId() const;
  130. std::string getName() const;
  131. std::vector<int32> getDims();
  132. intn size() const;
  133. HdfAttribute getAttribute(const std::string& name) const;
  134. template <class T>
  135. void read(std::vector<T>& dest, const std::string& field, int32 records) {
  136. if (!records) {
  137. records = nrRecords;
  138. }
  139. if (VSsetfields(id, field.c_str()) == FAIL) {
  140. raiseException(STATUS_RETURN_FAIL);
  141. }
  142. int32 fieldSize = VSsizeof(id, (char*)field.c_str());
  143. if (sizeof(T) < (size_t) fieldSize) {
  144. raiseException(BUFFER_SIZE_NOT_ENOUGH);
  145. }
  146. size_t size = records * fieldSize;
  147. std::vector<uint8> buff(size);
  148. if (VSread(id, buff.data(), records, interlace) == FAIL) {
  149. raiseException(STATUS_RETURN_FAIL);
  150. }
  151. dest.resize(records);
  152. VOIDP buffptrs[1];
  153. buffptrs[0] = dest.data();
  154. if (VSfpack(id, _HDF_VSUNPACK, field.c_str(), buff.data(), size, records, field.c_str(), buffptrs) == FAIL) {
  155. raiseException(STATUS_RETURN_FAIL);
  156. }
  157. }
  158. template <class T>
  159. void read(std::vector<std::vector<T> >& dest, const std::string& field, int32 records) {
  160. if (!records) {
  161. records = nrRecords;
  162. }
  163. if (VSsetfields(id, field.c_str()) == FAIL) {
  164. raiseException(STATUS_RETURN_FAIL);
  165. }
  166. int32 fieldSize = VSsizeof(id, (char*)field.c_str());
  167. if (fieldSize % sizeof(T) != 0) {
  168. raiseException(BUFFER_SIZE_NOT_DIVISIBLE);
  169. }
  170. size_t size = records * fieldSize;
  171. std::vector<uint8> buff(size);
  172. if (VSread(id, buff.data(), records, interlace) == FAIL) {
  173. raiseException(STATUS_RETURN_FAIL);
  174. }
  175. int32 divided = fieldSize / sizeof(T);
  176. dest.resize(records, std::vector<T>(divided));
  177. std::vector<T> linearDest(records * divided);
  178. VOIDP buffptrs[1];
  179. buffptrs[0] = linearDest.data();
  180. VSfpack(id, _HDF_VSUNPACK, field.c_str(), buff.data(), size, records, field.c_str(), buffptrs);
  181. int i = 0;
  182. int j;
  183. while (i < records) {
  184. j = 0;
  185. while (j < divided) {
  186. dest[i][j] = linearDest[i * divided + j];
  187. ++j;
  188. }
  189. ++i;
  190. }
  191. }
  192. private:
  193. intn _size;
  194. std::string name;
  195. int32 nrRecords;
  196. int32 interlace;
  197. int32 recordSize;
  198. int32 getDataType() const;
  199. };
  200. class HdfItem : public HdfObject {
  201. public:
  202. HdfItem(HdfItemBase *item, int32 sId, int32 vId);
  203. HdfItem(const HdfItem& item) = delete;
  204. HdfItem(HdfItem&& item);
  205. HdfItem& operator=(const HdfItem& item) = delete;
  206. HdfItem& operator=(HdfItem&& it);
  207. Type getType() const;
  208. std::string getName() const;
  209. std::vector<int32> getDims();
  210. intn size() const;
  211. HdfAttribute getAttribute(const std::string& name) const;
  212. template <class T> void read(std::vector<T>& dest) {
  213. switch(item->getType()) {
  214. case SDATA: {
  215. HdfDatasetItem *dItem = dynamic_cast<HdfDatasetItem *>(item.get());
  216. dItem->read(dest);
  217. break;
  218. }
  219. default:
  220. raiseException(INVALID_OPERATION);
  221. }
  222. }
  223. template <class T> void read(std::vector<T>& dest, std::vector<Range> ranges) {
  224. switch(item->getType()) {
  225. case SDATA: {
  226. HdfDatasetItem *dItem = dynamic_cast<HdfDatasetItem *>(item.get());
  227. dItem->read(dest, ranges);
  228. break;
  229. }
  230. default:
  231. raiseException(INVALID_OPERATION);
  232. }
  233. }
  234. template <class T> void read(std::vector<T>& dest, const std::string& field, int32 records = 0) {
  235. switch(item->getType()) {
  236. case VDATA: {
  237. HdfDataItem *vItem = dynamic_cast<HdfDataItem*>(item.get());
  238. vItem->read(dest, field, records);
  239. break;
  240. }
  241. default:
  242. raiseException(INVALID_OPERATION);
  243. }
  244. }
  245. class Iterator;
  246. Iterator begin() const;
  247. Iterator end() const;
  248. private:
  249. std::unique_ptr<HdfItemBase> item;
  250. int32 sId;
  251. int32 vId;
  252. };
  253. class HdfItem::Iterator : public HdfObject, public std::iterator<std::bidirectional_iterator_tag, HdfItem> {
  254. public:
  255. Iterator(int32 sId, int32 vId, int32 key, int32 index, Type type, const HdfDestroyerChain& chain) :
  256. HdfObject(type, ITERATOR, chain),
  257. sId(sId),
  258. vId(vId),
  259. key(key),
  260. index(index) {}
  261. bool operator!=(const Iterator& it) { return index != it.index; }
  262. bool operator==(const Iterator& it) { return index == it.index; }
  263. Iterator& operator++() {
  264. ++index;
  265. return *this;
  266. }
  267. Iterator operator++(int) {
  268. Iterator it(*this);
  269. ++index;
  270. return it;
  271. }
  272. Iterator& operator--() {
  273. --index;
  274. return *this;
  275. }
  276. Iterator operator--(int) {
  277. Iterator it(*this);
  278. --index;
  279. return it;
  280. }
  281. HdfItem operator*() {
  282. int32 tag, ref;
  283. if(Vgettagref(key, index, &tag, &ref) == FAIL) {
  284. raiseException(OUT_OF_RANGE);
  285. }
  286. if(Visvs(key, ref)) {
  287. int32 id = VSattach(vId, ref, "r");
  288. return HdfItem(new HdfDataItem(id, chain), sId, vId);
  289. } else if(Visvg(key, ref)) {
  290. int32 id = Vattach(vId, ref, "r");
  291. return HdfItem(new HdfGroupItem(id, chain), sId, vId);
  292. } else {
  293. int32 id = SDselect(sId, SDreftoindex(sId, ref));
  294. return HdfItem(new HdfDatasetItem(id, chain), sId, vId);
  295. }
  296. }
  297. private:
  298. int32 sId;
  299. int32 vId;
  300. int32 key;
  301. int32 index;
  302. };
  303. }
  304. #endif //HDF4CPP_HDFITEM_H