Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef MED_SliceArray_HeaderFile
00029 #define MED_SliceArray_HeaderFile
00030
00031 #ifdef WNT // for correctly compiling "valarray" in modules, which are includes this file
00032 #undef max
00033 #undef min
00034 #endif
00035
00036 #include <valarray>
00037 #include <stdexcept>
00038
00039
00040 # define MED_TCSLICE_CHECK_RANGE
00041
00042
00043 namespace MED
00044 {
00045
00047
00052 template<class TValueType>
00053 class TCSlice
00054 {
00055 const TValueType* myCValuePtr;
00056 size_t mySourceSize;
00057 std::slice mySlice;
00058
00059 protected:
00060 void
00061 check_id(size_t theId) const
00062 {
00063 long int anId = -1;
00064 if(theId < mySlice.size()){
00065 anId = mySlice.start() + theId*mySlice.stride();
00066 if(anId < (long int)mySourceSize)
00067 return;
00068 }
00069 throw std::out_of_range("TCSlice::check_id");
00070 }
00071
00073 size_t
00074 calculate_id(size_t theId) const
00075 {
00076 return mySlice.start() + theId*mySlice.stride();
00077 }
00078
00079 size_t
00080 get_id(size_t theId) const
00081 {
00082 #ifdef MED_TCSLICE_CHECK_RANGE
00083 check_id(theId);
00084 #endif
00085 return calculate_id(theId);
00086 }
00087
00088 size_t
00089 get_id_at(size_t theId) const
00090 {
00091 check_id(theId);
00092 return calculate_id(theId);
00093 }
00094
00095 public:
00096 typedef TValueType value_type;
00097
00099 TCSlice(const value_type* theValuePtr,
00100 size_t theSourceSize,
00101 const std::slice& theSlice):
00102 myCValuePtr(theValuePtr),
00103 mySourceSize(theSourceSize),
00104 mySlice(theSlice)
00105 {}
00106
00108 TCSlice(const TVector<value_type>& theContainer,
00109 const std::slice& theSlice):
00110 myCValuePtr(&theContainer[0]),
00111 mySourceSize(theContainer.size()),
00112 mySlice(theSlice)
00113 {}
00114
00116 TCSlice():
00117 myCValuePtr(NULL)
00118 {}
00119
00121 const value_type&
00122 operator[](size_t theId) const
00123 {
00124 return *(myCValuePtr + get_id(theId));
00125 }
00126
00127 const value_type&
00128 at(size_t theId) const
00129 {
00130 return *(myCValuePtr + get_id_at(theId));
00131 }
00132
00134 size_t
00135 size() const
00136 {
00137 return mySlice.size();
00138 }
00139 };
00140
00141
00142
00144 template<class TValueType>
00145 class TSlice: public TCSlice<TValueType>
00146 {
00147 TValueType* myValuePtr;
00148
00149 public:
00150 typedef TValueType value_type;
00151 typedef TCSlice<TValueType> TSupperClass;
00152
00154 TSlice(value_type* theValuePtr,
00155 size_t theSourceSize,
00156 const std::slice& theSlice):
00157 TSupperClass(theValuePtr, theSourceSize, theSlice),
00158 myValuePtr(theValuePtr)
00159 {}
00160
00162 TSlice(TVector<value_type>& theContainer,
00163 const std::slice& theSlice):
00164 TSupperClass(theContainer, theSlice),
00165 myValuePtr(&theContainer[0])
00166 {}
00167
00169 TSlice():
00170 myValuePtr(NULL)
00171 {}
00172
00174 value_type&
00175 operator[](size_t theId)
00176 {
00177 return *(myValuePtr + this->get_id(theId));
00178 }
00179
00180 value_type&
00181 at(size_t theId)
00182 {
00183 return *(myValuePtr + this->get_id_at(theId));
00184 }
00185 };
00186
00187 }
00188
00189 #undef MED_TCSLICE_CHECK_RANGE
00190
00191 #endif