1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #ifndef MED_SliceArray_HeaderFile
24 #define MED_SliceArray_HeaderFile
26 #ifdef WIN32 // for correct compiling of "valarray" in modules, which include this file
34 //#if defined(_DEBUG_)
35 # define MED_TCSLICE_CHECK_RANGE
40 //---------------------------------------------------------------
41 //! This class intends to provide a uniform way to handle multidimensional data (const version)
43 It just contains a pointer to real sequence and implement proper calculation of its indexes.
44 This class deals with constant pointer to the sources data and provides const method to
47 template<class TValueType>
50 const TValueType* myCValuePtr; //!< Reference to source multidimensional data
51 size_t mySourceSize; //!< Size of the source multidimensional data
52 std::slice mySlice; //!< Defines algorithm of index calculation
56 check_id(size_t theId) const
59 if(theId < mySlice.size()){
60 anId = mySlice.start() + theId*mySlice.stride();
61 if(anId < (long int)mySourceSize)
64 throw std::out_of_range("TCSlice::check_id");
67 //! Calculate internal index to get proper element from the source multidimensional data
69 calculate_id(size_t theId) const
71 return mySlice.start() + theId*mySlice.stride();
75 get_id(size_t theId) const
77 #ifdef MED_TCSLICE_CHECK_RANGE
80 return calculate_id(theId);
84 get_id_at(size_t theId) const
87 return calculate_id(theId);
91 typedef TValueType value_type;
93 //! Construct the class from bare pointer
94 TCSlice(const value_type* theValuePtr,
96 const std::slice& theSlice):
97 myCValuePtr(theValuePtr),
98 mySourceSize(theSourceSize),
102 //! Construct the class from corresponding container
103 TCSlice(const TVector<value_type>& theContainer,
104 const std::slice& theSlice):
105 myCValuePtr(&theContainer[0]),
106 mySourceSize(theContainer.size()),
110 //! Default constructor (dangerous)
115 //! Get element by its number (const version)
117 operator[](size_t theId) const
119 return *(myCValuePtr + get_id(theId));
123 at(size_t theId) const
125 return *(myCValuePtr + get_id_at(theId));
128 //! Get range of the order numbers
132 return mySlice.size();
136 //---------------------------------------------------------------
137 //! This class extends TCSlice functionality for non-constant case
138 template<class TValueType>
139 class TSlice: public TCSlice<TValueType>
141 TValueType* myValuePtr;
144 typedef TValueType value_type;
145 typedef TCSlice<TValueType> TSupperClass;
147 //! Construct the class from bare pointer
148 TSlice(value_type* theValuePtr,
149 size_t theSourceSize,
150 const std::slice& theSlice):
151 TSupperClass(theValuePtr, theSourceSize, theSlice),
152 myValuePtr(theValuePtr)
155 //! Construct the class from corresponding container
156 TSlice(TVector<value_type>& theContainer,
157 const std::slice& theSlice):
158 TSupperClass(theContainer, theSlice),
159 myValuePtr(&theContainer[0])
162 //! Default constructor (dangerous)
167 //! Get element by its number
169 operator[](size_t theId)
171 return *(myValuePtr + this->get_id(theId));
177 return *(myValuePtr + this->get_id_at(theId));
182 #undef MED_TCSLICE_CHECK_RANGE
184 #endif // MED_SliceArray_HeaderFile