h5cpp 0.7
A modern C++ wrapper for the HDF5 C library
Loading...
Searching...
No Matches
dataset.hpp
Go to the documentation of this file.
1//
2// (c) Copyright 2017 DESY,ESS
3//
4// This file is part of h5cpp.
5//
6// This library is free software; you can redistribute it and/or modify it
7// under the terms of the GNU Lesser General Public License as published
8// by the Free Software Foundation; either version 2.1 of the License, or
9// (at your option) any later version.
10//
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY
13// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14// License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with this library; if not, write to the
18// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
19// Boston, MA 02110-1301 USA
20// ===========================================================================
21//
22// Authors:
23// Eugen Wintersberger <eugen.wintersberger@desy.de>
24// Jan Kotanski <jan.kotanski@desy.de>
25//
26// Created on: Sep 7, 2017
27//
28#pragma once
29
30#include <h5cpp/node/node.hpp>
36#include <h5cpp/core/types.hpp>
45#include <h5cpp/error/error.hpp>
46#include <vector>
48
49namespace hdf5 {
50namespace node {
51
52class Selection;
53
54#ifdef __clang__
55#pragma clang diagnostic push
56#pragma clang diagnostic ignored "-Wpadded"
57#pragma clang diagnostic ignored "-Wweak-vtables"
58#endif
59class DLL_EXPORT Dataset : public Node
60{
61 public:
70 Dataset() = default;
71
81 Dataset(const Node &node);
82
95 Dataset(const Group &base,const Path &path,
96 const datatype::Datatype &type,
101
111
122
133
144
159 #ifdef __clang__
160 #pragma clang diagnostic push
161 #pragma clang diagnostic ignored "-Wdocumentation-deprecated-sync"
162 #endif
163 void extent(const Dimensions &dims) const;
164 #ifdef __clang__
165 #pragma clang diagnostic pop
166 #endif
167
168
169
170 void extent(size_t dim,ssize_t delta_elements) const;
171
172
186 void resize(const Dimensions &dims) const;
187
188
189
190#if (defined(_DOXYGEN_) || H5_VERSION_GE(1,10,0))
197 void refresh() const;
198#endif
199
200
218 template<typename T>
219 void write(const T &data,const datatype::Datatype &mem_type,
220 const dataspace::Dataspace &mem_space,
221 const dataspace::Dataspace &file_space,
223 property::DatasetTransferList::get()) const;
224
231 template<typename T>
232 void write(const T &data,const datatype::Datatype &mem_type,
233 const dataspace::Dataspace &mem_space,
235 property::DatasetTransferList::get()) const;
236
254 template<typename T>
255 void read(T &data,const datatype::Datatype &mem_type,
256 const dataspace::Dataspace &mem_space,
257 const dataspace::Dataspace &file_space,
259 property::DatasetTransferList::get()) const;
260
261 template<typename T>
262 void read(T &data,
263 const datatype::Datatype &mem_type,
264 const dataspace::Dataspace &mem_space,
266 property::DatasetTransferList::get()) const;
267
268 template<typename T>
269 void read_reshape(T &data,
270 const datatype::Datatype &mem_type,
271 const dataspace::Dataspace &mem_space,
273 property::DatasetTransferList::get()) const;
274
275
286 template<typename T>
287 void write(const T &data,const property::DatasetTransferList &dtpl =
288 property::DatasetTransferList::get());
289 template<typename T>
290 void write(const T &data,const property::DatasetTransferList &dtpl =
291 property::DatasetTransferList::get()) const;
292
306 template<typename T>
307 void write_reshape(const T &data,
308 const datatype::Datatype &mem_type,
309 const dataspace::Dataspace &mem_space,
311 property::DatasetTransferList::get()) const;
312
322 void write(const char *data,const property::DatasetTransferList &dtpl =
323 property::DatasetTransferList::get());
324 void write(const char *data,const property::DatasetTransferList &dtpl =
325 property::DatasetTransferList::get()) const;
326
339 template<typename T>
340 void write_chunk(const T &data,
341 std::vector<hsize_t> offset,
342 std::uint32_t filter_mask = 0,
344 property::DatasetTransferList::get());
345
346 template<typename T>
347 void write_chunk(const T &data,
348 std::vector<hsize_t> offset,
349 std::uint32_t filter_mask = 0,
351 property::DatasetTransferList::get()) const;
352
367 template<typename T>
368 void write_chunk(const T &data,
369 const datatype::Datatype &mem_type,
370 const dataspace::Dataspace &mem_space,
371 std::vector<hsize_t> & offset,
372 std::uint32_t filter_mask = 0,
374 property::DatasetTransferList::get()) const;
375
376#if (defined(_DOXYGEN_) || H5_VERSION_GE(1,10,2))
377
390 template<typename T>
391 std::uint32_t read_chunk(T &data,
392 std::vector<hsize_t> offset,
394 property::DatasetTransferList::get()) const;
395
396
410 template<typename T>
411 std::uint32_t read_chunk(T &data,
412 const datatype::Datatype &mem_type,
413 std::vector<hsize_t> & offset,
415 property::DatasetTransferList::get()) const;
416
417
431 template<typename T>
432 std::uint32_t read_chunk(T &data,
433 size_t byte_size,
434 std::vector<hsize_t> offset,
436 property::DatasetTransferList::get()) const;
437
438
453 template<typename T>
454 std::uint32_t read_chunk(T &data,
455 size_t byte_size,
456 const datatype::Datatype &mem_type,
457 std::vector<hsize_t> & offset,
459 property::DatasetTransferList::get()) const;
460
461
471 hsize_t chunk_storage_size(std::vector<hsize_t> offset) const;
472
473#endif
474
485 template<typename T>
486 void read(T &data,const property::DatasetTransferList &dtpl =
487 property::DatasetTransferList::get());
488 template<typename T>
489 void read(T &data,const property::DatasetTransferList &dtpl =
490 property::DatasetTransferList::get()) const;
491
503 template<typename T>
504 void write(const T &data,const dataspace::Selection &selection,
506 property::DatasetTransferList::get());
507 template<typename T>
508 void write(const T &data,const dataspace::Selection &selection,
510 property::DatasetTransferList::get()) const;
511
526 template<typename T>
527 void write_reshape(const T &data,
528 const datatype::Datatype &mem_type,
529 const dataspace::Dataspace &mem_space,
530 const dataspace::Selection &selection,
532 property::DatasetTransferList::get()) const;
533
546 template<typename T>
547 void read(T &data,const dataspace::Selection &selection,
549 property::DatasetTransferList::get());
550 template<typename T>
551 void read(T &data,const dataspace::Selection &selection,
553 property::DatasetTransferList::get()) const;
554
571 template<typename T>
572 void read_reshape(T &data,
573 const datatype::Datatype &mem_type,
574 const dataspace::Dataspace &mem_space,
575 const dataspace::Selection &selection,
577 property::DatasetTransferList::get()) const;
578
589 template<typename T>
590 void read(T &data,
591 const datatype::Datatype &memory_type,
592 const dataspace::Dataspace &memory_space,
593 const dataspace::Selection &file_selection,
594 const property::DatasetTransferList &dtpl = property::DatasetTransferList::get()) const;
595
606
607 private:
608 datatype::Datatype file_type_;
609 datatype::Class file_type_class;
610 dataspace::DataspacePool space_pool;
617 static Node create_dataset(const Group &base,
618 const Path &path,
619 const datatype::Datatype &type,
620 const dataspace::Dataspace &space,
621 const property::LinkCreationList &lcpl,
623 const property::DatasetAccessList &dapl);
624
625 //
626 // writing template methods for various data configurations
627 //
628 template<typename T>
629 void write_variable_length_data(const T &data,
630 const datatype::Datatype &mem_type,
631 const dataspace::Dataspace &mem_space,
632 const datatype::Datatype &,
633 const dataspace::Dataspace &file_space,
634 const property::DatasetTransferList &dtpl) const
635 {
636 VarLengthDataBuffer buffer;
638 if(H5Dwrite(static_cast<hid_t>(*this),
639 static_cast<hid_t>(mem_type),
640 static_cast<hid_t>(mem_space),
641 static_cast<hid_t>(file_space),
642 static_cast<hid_t>(dtpl),
643 buffer.data())<0)
644 {
645 std::stringstream ss;
646 ss<<"Failure to write variable length data to dataset ["<<link().path()<<"]!";
647 error::Singleton::instance().throw_with_stack(ss.str());
648 }
649 }
650
651 template<typename T>
652 void write_contiguous_data(const T &data,
653 const datatype::Datatype &mem_type,
654 const dataspace::Dataspace &mem_space,
655 const datatype::Datatype &,
656 const dataspace::Dataspace &file_space,
657 const property::DatasetTransferList &dtpl) const
658 {
659 if(H5Dwrite(static_cast<hid_t>(*this),
660 static_cast<hid_t>(mem_type),
661 static_cast<hid_t>(mem_space),
662 static_cast<hid_t>(file_space),
663 static_cast<hid_t>(dtpl),
664 dataspace::cptr(data))<0)
665 {
666 std::stringstream ss;
667 ss<<"Failure to write contiguous data to dataset ["<<link().path()<<"]!";
668 error::Singleton::instance().throw_with_stack(ss.str());
669 }
670 }
671
672 template<typename T>
673 void write_variable_length_string_data(const T &data,
674 const datatype::Datatype &mem_type,
675 const dataspace::Dataspace &mem_space,
676 const datatype::Datatype &,
677 const dataspace::Dataspace &file_space,
678 const property::DatasetTransferList &dtpl) const
679 {
680 using Trait = VarLengthStringTrait<T>;
681 auto buffer = Trait::to_buffer(data);
682
683 if(H5Dwrite(static_cast<hid_t>(*this),
684 static_cast<hid_t>(mem_type),
685 static_cast<hid_t>(mem_space),
686 static_cast<hid_t>(file_space),
687 static_cast<hid_t>(dtpl),
688 reinterpret_cast<void*>(buffer.data()))<0)
689 {
690 std::stringstream ss;
691 ss<<"Failure to write variable length string data to dataset ["<<link().path()<<"]!";
692 error::Singleton::instance().throw_with_stack(ss.str());
693 }
694 }
695
696 template<typename T>
697 void write_fixed_length_string_data(const T &data,
698 const datatype::Datatype &mem_type,
699 const dataspace::Dataspace &mem_space,
700 const datatype::Datatype &,
701 const dataspace::Dataspace &file_space,
702 const property::DatasetTransferList &dtpl) const
703 {
704 using Trait = FixedLengthStringTrait<T>;
705 auto buffer = Trait::to_buffer(data,mem_type,mem_space);
706
707 if(H5Dwrite(static_cast<hid_t>(*this),
708 static_cast<hid_t>(mem_type),
709 static_cast<hid_t>(mem_space),
710 static_cast<hid_t>(file_space),
711 static_cast<hid_t>(dtpl),
712 reinterpret_cast<void*>(buffer.data()))<0)
713 {
714 std::stringstream ss;
715 ss<<"Failure to write fixed length string data to dataset ["<<link().path()<<"]!";
716 error::Singleton::instance().throw_with_stack(ss.str());
717 }
718
719
720 }
721
722 //
723 // reading template methods for various data configurations
724 //
725 template<typename T>
726 void read_variable_length_data(T &data,
727 const datatype::Datatype &mem_type,
728 const dataspace::Dataspace &mem_space,
729 const datatype::Datatype &file_type,
730 const dataspace::Dataspace &file_space,
731 const property::DatasetTransferList &dtpl) const
732 {
733 VarLengthDataBuffer buffer;
734 if(file_space.selection.type() != dataspace::SelectionType::All)
735 {
736 buffer.resize(file_space.selection.size());
737 }
738 else
739 {
740 buffer.resize(signed2unsigned<size_t>(file_space.size()));
741 }
742
743 if(H5Dread(static_cast<hid_t>(*this),
744 static_cast<hid_t>(mem_type),
745 static_cast<hid_t>(mem_space),
746 static_cast<hid_t>(file_space),
747 static_cast<hid_t>(dtpl),
748 buffer.data())<0)
749 {
750 std::stringstream ss;
751 ss<<"Failure to read variable length data from dataset ["<<link().path()<<"]!";
752 error::Singleton::instance().throw_with_stack(ss.str());
753 }
754
755 VarLengthBufferTrait<T>::from_buffer(buffer,data);
756
757 if(H5Dvlen_reclaim(static_cast<hid_t>(file_type),
758 static_cast<hid_t>(file_space),
759 static_cast<hid_t>(dtpl),
760 buffer.data())<0)
761 {
762 error::Singleton::instance().throw_with_stack("Error reclaiming variable length memory!");
763 }
764 }
765
766 template<typename T>
767 void read_contiguous_data(T &data,
768 const datatype::Datatype &mem_type,
769 const dataspace::Dataspace &mem_space,
770 const datatype::Datatype &,
771 const dataspace::Dataspace &file_space,
772 const property::DatasetTransferList &dtpl) const
773 {
774 if(H5Dread(static_cast<hid_t>(*this),
775 static_cast<hid_t>(mem_type),
776 static_cast<hid_t>(mem_space),
777 static_cast<hid_t>(file_space),
778 static_cast<hid_t>(dtpl),
779 dataspace::ptr(data))<0)
780 {
781 std::stringstream ss;
782 ss<<"Failure to read contiguous data from dataset ["<<link().path()<<"]!";
783 error::Singleton::instance().throw_with_stack(ss.str());
784 }
785 }
786
787 template<typename T>
788 void read_variable_length_string_data(T &data,
789 const datatype::Datatype &mem_type,
790 const dataspace::Dataspace &mem_space,
791 const datatype::Datatype &,
792 const dataspace::Dataspace &file_space,
793 const property::DatasetTransferList &dtpl) const
794 {
795 using Trait = VarLengthStringTrait<T>;
796 using size_type = typename std::vector<T>::size_type;
797
798 typename Trait::BufferType buffer(static_cast<size_type>(mem_space.size()));
799
800
801 if(H5Dread(static_cast<hid_t>(*this),
802 static_cast<hid_t>(mem_type),
803 static_cast<hid_t>(mem_space),
804 static_cast<hid_t>(file_space),
805 static_cast<hid_t>(dtpl),
806 buffer.data())<0)
807 {
808 std::stringstream ss;
809 ss<<"Failure to read variable length string data from dataset ["
810 <<link().path()<<"]!";
811 error::Singleton::instance().throw_with_stack(ss.str());
812 }
813
814 Trait::from_buffer(buffer,data);
815
816 if(buffer.size() > 0)
817 {
818 if(H5Dvlen_reclaim(static_cast<hid_t>(mem_type),
819 static_cast<hid_t>(mem_space),
820 static_cast<hid_t>(dtpl),
821 buffer.data())<0)
822 {
823 std::stringstream ss;
824 ss<<"Error reclaiming memory from variable length string data in "
825 <<"dataset ["<<link().path()<<"]!";
826 error::Singleton::instance().throw_with_stack(ss.str());
827 }
828 }
829 }
830
831 template<typename T>
832 void read_fixed_length_string_data(T &data,
833 const datatype::Datatype &mem_type,
834 const dataspace::Dataspace &mem_space,
835 const datatype::Datatype &,
836 const dataspace::Dataspace &file_space,
837 const property::DatasetTransferList &dtpl) const
838 {
839 using Trait = FixedLengthStringTrait<T>;
840
841 auto buffer = Trait::BufferType::create(mem_type,mem_space);
842
843 if(file_space.size() > 0)
844 {
845 if(H5Dread(static_cast<hid_t>(*this),
846 static_cast<hid_t>(mem_type),
847 static_cast<hid_t>(mem_space),
848 static_cast<hid_t>(file_space),
849 static_cast<hid_t>(dtpl),
850 buffer.data())<0)
851 {
852 std::stringstream ss;
853 ss<<"Failure to read fixed length string data to dataset ["<<link().path()<<"]!";
854 error::Singleton::instance().throw_with_stack(ss.str());
855 }
856
857 //get data out of the buffer
858 data = Trait::from_buffer(buffer,mem_type,mem_space);
859 }
860 }
861};
862#ifdef __clang__
863#pragma clang diagnostic pop
864#endif
865
866template<typename T>
867void Dataset::write(const T &data,const datatype::Datatype &mem_type,
868 const dataspace::Dataspace &mem_space,
869 const dataspace::Dataspace &file_space,
870 const property::DatasetTransferList &dtpl) const
871{
872 if(file_type_class == datatype::Class::VarLength)
873 {
874 write_variable_length_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
875 }
876 else if(file_type_class == datatype::Class::String)
877 {
878 datatype::String string_type(file_type_);
879 if(string_type.is_variable_length())
880 {
881 write_variable_length_string_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
882 }
883 else
884 {
885 write_fixed_length_string_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
886 }
887 }
888 else
889 {
890 write_contiguous_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
891 }
892
893}
894
895template<typename T>
896void Dataset::write(const T &data,
897 const datatype::Datatype &mem_type,
898 const dataspace::Dataspace &mem_space,
899 const property::DatasetTransferList &dtpl) const
900{
901 auto file_space = dataspace();
902 file_space.selection.all();
903
904 write(data,mem_type,mem_space,file_space,dtpl);
905
906}
907
908template<typename T>
909void Dataset::write_chunk(const T &data,
910 std::vector<hsize_t> offset,
911 std::uint32_t filter_mask,
913{
914 hdf5::datatype::DatatypeHolder mem_type_holder;
915 hdf5::dataspace::DataspaceHolder mem_space_holder(space_pool);
916 write_chunk(data, mem_type_holder.get(data), mem_space_holder.get(data), offset, filter_mask, dtpl);
917}
918
919template<typename T>
920void Dataset::write_chunk(const T &data,
921 std::vector<hsize_t> offset,
922 std::uint32_t filter_mask,
923 const property::DatasetTransferList &dtpl) const
924{
925 hdf5::datatype::DatatypeHolder mem_type_holder;
926 hdf5::dataspace::DataspaceHolder mem_space_holder;
927 write_chunk(data, mem_type_holder.get(data), mem_space_holder.get(data), offset, filter_mask, dtpl);
928}
929
930template<typename T>
931void Dataset::write_chunk(const T &data,
932 const datatype::Datatype &mem_type,
933 const dataspace::Dataspace &mem_space,
934 std::vector<hsize_t> & offset,
935 std::uint32_t filter_mask,
936 const property::DatasetTransferList &dtpl) const
937{
938 size_t databytesize = signed2unsigned<hsize_t>(mem_space.size()) * mem_type.size();
939
940 if(mem_type.get_class() == datatype::Class::Integer)
941 {
942#if H5_VERSION_GE(1,10,3)
943 if(H5Dwrite_chunk(static_cast<hid_t>(*this),
944 static_cast<hid_t>(dtpl),
945 filter_mask,
946 offset.data(),
947 databytesize,
948 dataspace::cptr(data))<0)
949 {
950 std::stringstream ss;
951 ss<<"Failure to write chunk data to dataset ["<<link().path()<<"]!";
953 }
954#else
955 if(H5DOwrite_chunk(static_cast<hid_t>(*this),
956 static_cast<hid_t>(dtpl),
957 filter_mask,
958 offset.data(),
959 databytesize,
960 dataspace::cptr(data))<0)
961 {
962 std::stringstream ss;
963 ss<<"Failure to write chunk data to dataset ["<<link().path()<<"]!";
965 }
966#endif
967 }
968 else
969 {
970 std::stringstream ss;
971 ss<<"Failure to write non-integer chunk data to dataset ["<<link().path()<<"]!";
973 }
974}
975
976#if H5_VERSION_GE(1,10,2)
977
978template<typename T>
979std::uint32_t Dataset::read_chunk(T &data,
980 std::vector<hsize_t> offset,
981 const property::DatasetTransferList &dtpl) const
982{
983 hdf5::datatype::DatatypeHolder mem_type_holder;
984 return read_chunk(data, mem_type_holder.get(data), offset, dtpl);
985}
986
987template<typename T>
988std::uint32_t Dataset::read_chunk(T &data,
989 size_t byte_size,
990 std::vector<hsize_t> offset,
991 const property::DatasetTransferList &dtpl) const
992{
993 hdf5::datatype::DatatypeHolder mem_type_holder;
994 return read_chunk(data, byte_size, mem_type_holder.get(data), offset, dtpl);
995}
996
997template<typename T>
998std::uint32_t Dataset::read_chunk(T &data,
999 const datatype::Datatype &mem_type,
1000 std::vector<hsize_t> & offset,
1001 const property::DatasetTransferList &dtpl) const
1002{
1003 std::uint32_t filter_mask;
1004 if(mem_type.get_class() == datatype::Class::Integer)
1005 {
1006#if H5_VERSION_GE(2,0,0)
1007 if(H5Dread_chunk1(static_cast<hid_t>(*this),
1008 static_cast<hid_t>(dtpl),
1009 offset.data(),
1010 &filter_mask,
1011 dataspace::ptr(data))<0)
1012 {
1013 std::stringstream ss;
1014 ss<<"Failure to read chunk data from dataset ["<<link().path()<<"]!";
1016 }
1017#elif H5_VERSION_GE(1,10,3)
1018 if(H5Dread_chunk(static_cast<hid_t>(*this),
1019 static_cast<hid_t>(dtpl),
1020 offset.data(),
1021 &filter_mask,
1022 dataspace::ptr(data))<0)
1023 {
1024 std::stringstream ss;
1025 ss<<"Failure to read chunk data from dataset ["<<link().path()<<"]!";
1027 }
1028#else
1029 if(H5DOread_chunk(static_cast<hid_t>(*this),
1030 static_cast<hid_t>(dtpl),
1031 offset.data(),
1032 &filter_mask,
1033 dataspace::ptr(data))<0)
1034 {
1035 std::stringstream ss;
1036 ss<<"Failure to read chunk data from dataset ["<<link().path()<<"]!";
1038 }
1039#endif
1040 }
1041 else
1042 {
1043 std::stringstream ss;
1044 ss<<"Failure to read non-integer chunk data from dataset ["<<link().path()<<"]!";
1046 }
1047 return filter_mask;
1048}
1049
1050
1051template<typename T>
1052std::uint32_t Dataset::read_chunk(T &data,
1053 size_t byte_size,
1054 const datatype::Datatype &mem_type,
1055 std::vector<hsize_t> & offset,
1056 const property::DatasetTransferList &dtpl) const
1057{
1058 std::uint32_t filter_mask = 0;
1059 if(mem_type.get_class() == datatype::Class::Integer)
1060 {
1061#if H5_VERSION_GE(2,0,0)
1062 if(H5Dread_chunk(static_cast<hid_t>(*this),
1063 static_cast<hid_t>(dtpl),
1064 offset.data(),
1065 &filter_mask,
1066 dataspace::ptr(data), &byte_size)<0)
1067 {
1068 std::stringstream ss;
1069 ss<<"Failure to read chunk data from dataset ["<<link().path()<<"]!";
1071 }
1072#else
1073 if(chunk_storage_size(offset) <= byte_size){
1074 read_chunk(data, mem_type, offset, dtpl);
1075 }
1076 else {
1077 std::stringstream ss;
1078 ss<<"Failure to read chunk data from dataset ["<<link().path()<<"]!";
1080 }
1081#endif
1082 }
1083 else
1084 {
1085 std::stringstream ss;
1086 ss<<"Failure to read non-integer chunk data from dataset ["<<link().path()<<"]!";
1088 }
1089 return filter_mask;
1090}
1091
1092
1093#endif
1094
1095template<typename T>
1096void Dataset::write(const T &data,const property::DatasetTransferList &dtpl)
1097{
1098 hdf5::dataspace::DataspaceHolder mem_space_holder(space_pool);
1099 if(file_type_.get_class() == datatype::Class::String)
1100 {
1101 write_reshape(data, file_type_, mem_space_holder.get(data), dtpl);
1102 }
1103 else
1104 {
1105 hdf5::datatype::DatatypeHolder mem_type_holder;
1106 write_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), dtpl);
1107 }
1108}
1109
1110template<typename T>
1111void Dataset::write(const T &data,const property::DatasetTransferList &dtpl) const
1112{
1113 hdf5::dataspace::DataspaceHolder mem_space_holder;
1114 if(file_type_.get_class() == datatype::Class::String)
1115 {
1116 write_reshape(data, file_type_, mem_space_holder.get(data), dtpl);
1117 }
1118 else
1119 {
1120 hdf5::datatype::DatatypeHolder mem_type_holder;
1121 write_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), dtpl);
1122 }
1123}
1124
1125template<typename T>
1126void Dataset::write_reshape(const T &data,
1127 const datatype::Datatype &mem_type,
1128 const dataspace::Dataspace &mem_space,
1129 const property::DatasetTransferList &dtpl) const
1130{
1131 auto file_space = dataspace();
1132 file_space.selection.all();
1133
1134 if (file_space.size() == mem_space.size() &&
1135 mem_space.type() == dataspace::Type::Simple &&
1136 file_space.type() == dataspace::Type::Simple){
1137 const dataspace::Simple & mem_simple_space = dataspace::Simple(mem_space);
1138 const dataspace::Simple & file_simple_space = dataspace::Simple(file_space);
1139 if(file_simple_space.rank() > 1 && mem_simple_space.rank() == 1){
1140 return write(data,mem_type,file_space,file_space,dtpl);
1141 }
1142 }
1143 write(data,mem_type,mem_space,file_space,dtpl);
1144}
1145
1146
1147template<typename T>
1148void Dataset::read(T &data,const datatype::Datatype &mem_type,
1149 const dataspace::Dataspace &mem_space,
1150 const dataspace::Dataspace &file_space,
1151 const property::DatasetTransferList &dtpl) const
1152{
1153 if(file_type_class == datatype::Class::VarLength)
1154 {
1155 read_variable_length_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
1156 }
1157 else if(file_type_class == datatype::Class::String)
1158 {
1159 datatype::String string_type(file_type_);
1160 if(string_type.is_variable_length())
1161 {
1162 read_variable_length_string_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
1163 }
1164 else
1165 {
1166 read_fixed_length_string_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
1167 }
1168 }
1169 else
1170 {
1171 read_contiguous_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
1172 }
1173
1174}
1175
1176template<typename T>
1177void Dataset::read(T &data,const dataspace::Selection &selection,
1179{
1180 hdf5::datatype::DatatypeHolder mem_type_holder;
1181 hdf5::dataspace::DataspaceHolder mem_space_holder(space_pool);
1182 read_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), selection, dtpl);
1183}
1184
1185template<typename T>
1186void Dataset::read(T &data,const dataspace::Selection &selection,
1187 const property::DatasetTransferList &dtpl) const
1188{
1189 hdf5::datatype::DatatypeHolder mem_type_holder;
1190 hdf5::dataspace::DataspaceHolder mem_space_holder;
1191 read_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), selection, dtpl);
1192}
1193
1194template<typename T>
1196 const datatype::Datatype &mem_type,
1197 const dataspace::Dataspace &mem_space,
1198 const dataspace::Selection &selection,
1199 const property::DatasetTransferList &dtpl) const
1200{
1201 dataspace::Dataspace file_space = dataspace();
1202 file_space.selection(dataspace::SelectionOperation::Set,selection);
1203 if (selection.type() != dataspace::SelectionType::Hyperslab)
1204 return read(data,mem_type,mem_space,file_space,dtpl);
1205 try{
1206 const dataspace::Hyperslab & hyper = dynamic_cast<const dataspace::Hyperslab &>(selection);
1207 if(hyper.rank() > 1) {
1208 dataspace::Simple selected_space(hyper.dimensions());
1209 if (selected_space.size() == mem_space.size()) {
1210 // reads to the reshaped memory data buffer
1211 return read(data,mem_type,selected_space,file_space,dtpl);
1212 }
1213 }
1214 }
1215 catch(const std::bad_cast&) { }
1216 read(data,mem_type,mem_space,file_space,dtpl);
1217}
1218
1219template<typename T>
1220void Dataset::read(T &data,
1221 const datatype::Datatype &memory_type,
1222 const dataspace::Dataspace &memory_space,
1223 const dataspace::Selection &file_selection,
1224 const property::DatasetTransferList &dtpl) const
1225{
1226 dataspace::Dataspace file_space = dataspace();
1227 file_space.selection(dataspace::SelectionOperation::Set,file_selection);
1228 read(data,memory_type,memory_space,file_space,dtpl);
1229}
1230
1231template<typename T>
1232void Dataset::read(T &data,
1233 const datatype::Datatype &memory_type,
1234 const dataspace::Dataspace &memory_space,
1235 const property::DatasetTransferList &dtpl) const
1236{
1237 dataspace::Dataspace file_space = dataspace();
1238 file_space.selection.all();
1239 read(data,memory_type,memory_space,file_space,dtpl);
1240}
1241
1242template<typename T>
1243void Dataset::write(const T &data,const dataspace::Selection &selection,
1245{
1246 hdf5::datatype::DatatypeHolder mem_type_holder;
1247 hdf5::dataspace::DataspaceHolder mem_space_holder(space_pool);
1248 write_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), selection, dtpl);
1249}
1250
1251template<typename T>
1252void Dataset::write(const T &data,const dataspace::Selection &selection,
1253 const property::DatasetTransferList &dtpl) const
1254{
1255 hdf5::datatype::DatatypeHolder mem_type_holder;
1256 hdf5::dataspace::DataspaceHolder mem_space_holder;
1257 write_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), selection, dtpl);
1258}
1259
1260template<typename T>
1261void Dataset::write_reshape(const T &data,
1262 const datatype::Datatype &mem_type,
1263 const dataspace::Dataspace &mem_space,
1264 const dataspace::Selection &selection,
1265 const property::DatasetTransferList &dtpl) const
1266{
1267 dataspace::Dataspace file_space = dataspace();
1268 file_space.selection(dataspace::SelectionOperation::Set,selection);
1269 if (mem_space.type() != dataspace::Type::Simple
1270 || selection.type() != dataspace::SelectionType::Hyperslab)
1271 return write(data,mem_type,mem_space,file_space,dtpl);
1272 try{
1273 const dataspace::Hyperslab & hyper = dynamic_cast<const dataspace::Hyperslab &>(selection);
1274 if(hyper.rank() > 1) {
1275 const dataspace::Simple & mem_simple_space = dataspace::Simple(mem_space);
1276 dataspace::Simple selected_space(hyper.dimensions());
1277 if(selected_space.rank() > 1 &&
1278 mem_simple_space.rank() == 1 &&
1279 selected_space.size() == mem_space.size()) {
1280 // writes from the reshaped memory data buffer
1281 return write(data,mem_type,selected_space,file_space,dtpl);
1282 }
1283 }
1284 }
1285 catch(const std::bad_cast&) { }
1286 write(data,mem_type,mem_space,file_space,dtpl);
1287}
1288
1289
1290template<typename T>
1292{
1293 hdf5::dataspace::DataspaceHolder mem_space_holder(space_pool);
1294 if(file_type_class == datatype::Class::String){
1295 // in hdf5 1.12.1 UFT8 data cannot be read to an ASCII buffer
1296 read_reshape(data, file_type_, mem_space_holder.get(data), dtpl);
1297 }
1298 else {
1299 hdf5::datatype::DatatypeHolder mem_type_holder;
1300 read_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), dtpl);
1301 }
1302}
1303
1304template<typename T>
1305void Dataset::read(T &data,const property::DatasetTransferList &dtpl) const
1306{
1307 hdf5::datatype::DatatypeHolder mem_type_holder;
1308 hdf5::dataspace::DataspaceHolder mem_space_holder;
1309 read_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), dtpl);
1310}
1311
1312
1313template<typename T>
1315 const datatype::Datatype &mem_type,
1316 const dataspace::Dataspace &mem_space,
1317 const property::DatasetTransferList &dtpl) const
1318{
1319 auto file_space = dataspace();
1320 file_space.selection.all();
1321
1322 if (file_space.size() == mem_space.size()){
1323 read(data,mem_type,file_space,file_space,dtpl);
1324 }
1325 else{
1326 read(data,mem_type,mem_space,file_space,dtpl);
1327 }
1328
1329}
1330
1331
1344DLL_EXPORT void resize_by(const Dataset &dataset,size_t dimension_index,ssize_t delta);
1345
1346
1347
1348} // namespace node
1349} // namespace hdf5
path to a node object
Definition path.hpp:54
data space object holder
Definition type_trait.hpp:124
const Dataspace & get(const T &v)
factory holder method for getting reference of data spaces
Definition type_trait.hpp:158
data space object pool
Definition pool.hpp:42
dataspace base class
Definition dataspace.hpp:41
SelectionManager selection
access to selection manager
Definition dataspace.hpp:144
Type type() const
get the type of the dataspace
virtual hssize_t size() const
number of elements in the dataspace
hyperslab selection class
Definition hyperslab.hpp:52
size_t rank() const noexcept
get rank
virtual Dimensions dimensions() const override
get current dimensions
scalar dataspace
Definition scalar.hpp:44
void all() const
select everything
selection base class
Definition selection.hpp:42
virtual SelectionType type() const =0
get the selection type
simple multidimensional dataspace
Definition simple.hpp:43
size_t rank() const
get number of dimensions
data type object holder
Definition factory.hpp:54
const Datatype & get(const T &v=T{})
factory holder method for getting reference of data types
Definition factory.hpp:74
base class for all data types
Definition datatype.hpp:42
Class get_class() const
returns the datatypes class
virtual size_t size() const
get size of type in bytes
string datatype
Definition string.hpp:40
bool is_variable_length() const
return true if type is a variable length string
static Singleton & instance()
reference to singleton
Definition error.hpp:59
void throw_with_stack(const std::string &message)
throws an exception, potentially nested with error stack
utility container for external filters
Definition external_filter.hpp:123
Definition dataset.hpp:60
hsize_t chunk_storage_size(std::vector< hsize_t > offset) const
read dataset chunk
void write(const T &data, const datatype::Datatype &mem_type, const dataspace::Dataspace &mem_space, const dataspace::Dataspace &file_space, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
write data to the dataset
Definition dataset.hpp:867
void read_reshape(T &data, const datatype::Datatype &mem_type, const dataspace::Dataspace &mem_space, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
Definition dataset.hpp:1314
void write(const char *data, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
dataspace::Dataspace dataspace() const
get dataspace of dataset
void extent(const Dimensions &dims) const
set extent of the dataset
void refresh() const
refresh the dataset (since hdf5 1.10.0)
property::DatasetAccessList access_list() const
get the dataset access property list for the instance
void write_chunk(const T &data, std::vector< hsize_t > offset, std::uint32_t filter_mask=0, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get())
write dataset chunk
Definition dataset.hpp:909
Dataset()=default
default constructor
void resize(const Dimensions &dims) const
resize the dataset
std::uint32_t read_chunk(T &data, const datatype::Datatype &mem_type, std::vector< hsize_t > &offset, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
read dataset chunk
datatype::Datatype datatype() const
get datatype of dataset
Dataset(const Node &node)
construct
void extent(size_t dim, ssize_t delta_elements) const
void write(const char *data, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get())
write entire dataset
Dataset(const Group &base, const Path &path, const datatype::Datatype &type, const dataspace::Dataspace &space=dataspace::Scalar(), const property::LinkCreationList &lcpl=property::LinkCreationList(), const property::DatasetCreationList &dcpl=property::DatasetCreationList(), const property::DatasetAccessList &dapl=property::DatasetAccessList())
constructor
property::DatasetCreationList creation_list() const
get the dataset creation property list used for creation
std::uint32_t read_chunk(T &data, std::vector< hsize_t > offset, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
read dataset chunk (since hdf5 1.10.2)
void read(T &data, const datatype::Datatype &mem_type, const dataspace::Dataspace &mem_space, const dataspace::Dataspace &file_space, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
read data from the dataset
Definition dataset.hpp:1148
filter::ExternalFilters filters() const
get the dataset external filters for the instance
std::uint32_t read_chunk(T &data, size_t byte_size, const datatype::Datatype &mem_type, std::vector< hsize_t > &offset, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
read dataset chunk
void write_reshape(const T &data, const datatype::Datatype &mem_type, const dataspace::Dataspace &mem_space, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
write entire dataset
Definition dataset.hpp:1126
std::uint32_t read_chunk(T &data, size_t byte_size, std::vector< hsize_t > offset, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
read dataset chunk (since hdf5 1.10.2)
Definition group.hpp:51
Definition node.hpp:40
const Link & link() const
return link to object
dataset access property list
Definition dataset_access.hpp:76
dataset creation property list
Definition dataset_creation.hpp:111
class for a dataset transfer property list
Definition dataset_transfer.hpp:57
Definition link_creation.hpp:35
void * ptr(T &value)
Definition type_trait.hpp:111
const void * cptr(const T &value)
Definition type_trait.hpp:116
Class
Definition types.hpp:53
@ String
indicates a string type
@ VarLength
indicates a variable length type
@ Integer
indicates an integer type
void resize_by(const Dataset &dataset, size_t dimension_index, ssize_t delta)
resize a dataset by a particular offset
void link(const Node &target, const Group &link_base, const Path &link_path, const property::LinkCreationList &lcpl=property::LinkCreationList(), const property::LinkAccessList &lapl=property::LinkAccessList())
Create a soft or external link.
top-level namespace of the entire library
Definition attribute.hpp:45
std::vector< hsize_t > Dimensions
Definition types.hpp:32
std::vector< hvl_t > VarLengthDataBuffer
Definition types.hpp:34
variable length buffer trait
Definition types.hpp:44
#define DLL_EXPORT
Definition windows.hpp:29