[219] | 1 | #ifndef __XMLIO_CArray_impl__ |
---|
| 2 | #define __XMLIO_CArray_impl__ |
---|
| 3 | |
---|
| 4 | #include "array_mac.hpp" |
---|
[300] | 5 | #include "buffer_in.hpp" |
---|
| 6 | #include "buffer_out.hpp" |
---|
[219] | 7 | |
---|
[335] | 8 | namespace xios |
---|
[219] | 9 | { |
---|
| 10 | |
---|
| 11 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 12 | |
---|
| 13 | template <typename ValueType, StdSize NumDims, typename Allocator> |
---|
| 14 | template <typename ExtentList> |
---|
| 15 | CArray<ValueType, NumDims, Allocator>::CArray(const ExtentList & sizes) |
---|
| 16 | : boost::multi_array<ValueType, NumDims, Allocator> |
---|
| 17 | (sizes, boost::fortran_storage_order()) |
---|
| 18 | { /* Ne rien faire de plus */ } |
---|
| 19 | |
---|
| 20 | template <typename ValueType, StdSize NumDims, typename Allocator> |
---|
[300] | 21 | CArray<ValueType, NumDims, Allocator>::CArray() |
---|
| 22 | : boost::multi_array<ValueType, NumDims, Allocator> |
---|
| 23 | (getExtentNull<NumDims>(), boost::fortran_storage_order()) |
---|
| 24 | { /* Ne rien faire de plus */ } |
---|
| 25 | |
---|
| 26 | template <typename ValueType, StdSize NumDims, typename Allocator> |
---|
[219] | 27 | template <typename ExtentList> |
---|
| 28 | CArray<ValueType, NumDims, Allocator>::CArray |
---|
| 29 | (const ExtentList & sizes, const boost::general_storage_order<NumDims> & store) |
---|
| 30 | : boost::multi_array<ValueType, NumDims, Allocator> (sizes, store) |
---|
| 31 | { /* Ne rien faire de plus */ } |
---|
| 32 | |
---|
| 33 | template <typename ValueType, StdSize NumDims, typename Allocator> |
---|
| 34 | CArray<ValueType, NumDims, Allocator>::~CArray(void) |
---|
| 35 | { /* Ne rien faire de plus */ } |
---|
| 36 | |
---|
| 37 | //---------------------------------------------------------------- |
---|
| 38 | |
---|
| 39 | template <typename ValueType, StdSize NumDims, typename Allocator> |
---|
| 40 | StdOStream & operator << (StdOStream & os, |
---|
| 41 | const CArray<ValueType, NumDims, Allocator> & array) |
---|
| 42 | { |
---|
[300] | 43 | os << "CArray (" ; |
---|
[219] | 44 | for (StdSize i = 1; i < array.num_dimensions(); i++) |
---|
| 45 | os << ", " << array.shape()[i]; |
---|
[300] | 46 | os << ") = " ; |
---|
| 47 | for (StdSize i = 0; i < array.num_elements(); i++) os << (array.data()[i])<<" "; |
---|
[219] | 48 | return (os); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | //---------------------------------------------------------------- |
---|
| 52 | |
---|
| 53 | template <typename ValueType, StdSize NumDims, typename Allocator> |
---|
| 54 | StdOStream & operator << |
---|
| 55 | (StdOStream & os, const boost::shared_ptr<CArray<ValueType, NumDims, Allocator> > & array) |
---|
| 56 | { |
---|
| 57 | os << *array; |
---|
| 58 | return (os); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | //---------------------------------------------------------------- |
---|
| 62 | |
---|
| 63 | template <typename ValueType> void FromBinary |
---|
| 64 | (StdIStream & is, ARRAY(ValueType, 1) & array) |
---|
| 65 | { |
---|
| 66 | ARRAY_ASSIGN(array, ValueType, 1, [1]); |
---|
| 67 | array->fromBinary(is); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | template <typename ValueType> void FromBinary |
---|
| 71 | (StdIStream & is, ARRAY(ValueType, 2) & array) |
---|
| 72 | { |
---|
| 73 | ARRAY_ASSIGN(array, ValueType, 2, [1][1]); |
---|
| 74 | array->fromBinary(is); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | template <typename ValueType> void FromBinary |
---|
| 78 | (StdIStream & is, ARRAY(ValueType, 3) & array) |
---|
| 79 | { |
---|
| 80 | ARRAY_ASSIGN(array, ValueType, 3, [1][1][1]); |
---|
| 81 | array->fromBinary(is); |
---|
| 82 | } |
---|
| 83 | //---------------------------------------------------------------- |
---|
| 84 | |
---|
| 85 | template <typename ValueType, StdSize NumDims, typename Allocator> |
---|
| 86 | void CArray<ValueType, NumDims, Allocator>::toBinary (StdOStream & os) const |
---|
| 87 | { |
---|
| 88 | typedef boost::multi_array_types::size_type LSize; |
---|
| 89 | LSize nelem = this->num_elements(); |
---|
| 90 | LSize ndim = this->num_dimensions(); |
---|
| 91 | const LSize * shape = this->shape(); |
---|
| 92 | const ValueType * data = this->data(); |
---|
| 93 | |
---|
| 94 | os.write (reinterpret_cast<const char*>(&ndim) , sizeof(LSize)); |
---|
| 95 | for (LSize i = 0; i < ndim; i++ ) |
---|
| 96 | os.write (reinterpret_cast<const char*>(&(shape[i])), sizeof(LSize)); |
---|
| 97 | os.write (reinterpret_cast<const char*>(&nelem), sizeof(LSize)); |
---|
| 98 | os.write (reinterpret_cast<const char*>(data), nelem * sizeof(ValueType)); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | //---------------------------------------------------------------- |
---|
| 102 | |
---|
| 103 | template <typename ValueType, StdSize NumDims, typename Allocator> |
---|
| 104 | void CArray<ValueType, NumDims, Allocator>::fromBinary(StdIStream & is) |
---|
| 105 | { |
---|
| 106 | typedef boost::multi_array_types::size_type LSize; |
---|
| 107 | LSize ndim = 0, nelem = 0, temp = 0; |
---|
| 108 | std::vector<LSize> shape; |
---|
| 109 | is.read (reinterpret_cast<char*>(&ndim) , sizeof(LSize)); |
---|
| 110 | for (LSize i = 0; i < ndim; i++ ) |
---|
| 111 | { |
---|
| 112 | is.read (reinterpret_cast<char*>(&temp) , sizeof(LSize)); |
---|
| 113 | shape.push_back(temp); |
---|
| 114 | } |
---|
| 115 | this->resize(shape); |
---|
| 116 | is.read (reinterpret_cast<char*>(&nelem), sizeof(LSize)); |
---|
| 117 | is.read (reinterpret_cast<char*>(this->data()), nelem * sizeof(ValueType)); |
---|
| 118 | } |
---|
[300] | 119 | |
---|
| 120 | template <typename ValueType, StdSize NumDims, typename Allocator> |
---|
| 121 | size_t CArray<ValueType, NumDims, Allocator>::getSize(void) const |
---|
| 122 | { |
---|
| 123 | typedef boost::multi_array_types::size_type LSize; |
---|
| 124 | LSize nelem = this->num_elements(); |
---|
| 125 | LSize ndim = this->num_dimensions(); |
---|
| 126 | const LSize * shape = this->shape(); |
---|
| 127 | const ValueType * data = this->data(); |
---|
| 128 | size_t ret ; |
---|
| 129 | ret=sizeof(ndim) ; |
---|
| 130 | for (LSize i = 0; i < ndim; i++ ) ret+=sizeof(shape[i]) ; |
---|
| 131 | ret+=sizeof(nelem) ; |
---|
| 132 | ret+=sizeof(ValueType)*nelem ; |
---|
| 133 | return ret ; |
---|
| 134 | } |
---|
[219] | 135 | |
---|
[300] | 136 | template <typename ValueType, StdSize NumDims, typename Allocator> |
---|
| 137 | bool CArray<ValueType, NumDims, Allocator>::toBuffer(CBufferOut& buffer) const |
---|
| 138 | { |
---|
| 139 | typedef boost::multi_array_types::size_type LSize; |
---|
| 140 | |
---|
| 141 | LSize nelem = this->num_elements(); |
---|
| 142 | LSize ndim = this->num_dimensions(); |
---|
| 143 | const LSize* shape = this->shape(); |
---|
| 144 | const ValueType* data = this->data(); |
---|
| 145 | bool ret ; |
---|
| 146 | |
---|
| 147 | ret=buffer.put(ndim) ; |
---|
| 148 | for (LSize i = 0; i < ndim; i++ ) ret&=buffer.put(shape[i]) ; |
---|
| 149 | ret&=buffer.put(nelem) ; |
---|
| 150 | ret&=buffer.put(data,nelem) ; |
---|
| 151 | return ret ; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | template <typename ValueType, StdSize NumDims, typename Allocator> |
---|
| 155 | bool CArray<ValueType, NumDims, Allocator>::fromBuffer(CBufferIn& buffer) |
---|
| 156 | { |
---|
| 157 | typedef boost::multi_array_types::size_type LSize; |
---|
| 158 | LSize ndim = 0, nelem = 0, temp = 0; |
---|
| 159 | std::vector<LSize> shape; |
---|
| 160 | bool ret ; |
---|
| 161 | |
---|
| 162 | ret=buffer.get(ndim) ; |
---|
| 163 | for (LSize i = 0; i < ndim; i++ ) |
---|
| 164 | { |
---|
| 165 | ret&=buffer.get(temp) ; |
---|
| 166 | shape.push_back(temp); |
---|
| 167 | } |
---|
| 168 | this->resize(shape); |
---|
| 169 | ret&=buffer.get(nelem) ; |
---|
| 170 | ret&=buffer.get(this->data(),nelem) ; |
---|
| 171 | return ret ; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
[219] | 175 | ///--------------------------------------------------------------- |
---|
| 176 | |
---|
[335] | 177 | } // namespace xios |
---|
[219] | 178 | |
---|
| 179 | #endif // __XMLIO_CArray_impl__ |
---|