Salome HOME
CoTech decision: move MEDWrapper from MED to SMESH
[modules/smesh.git] / src / MEDWrapper / Base / MED_SliceArray.hxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 #ifndef MED_SliceArray_HeaderFile
24 #define MED_SliceArray_HeaderFile
25
26 #ifdef WIN32 // for correctly compiling "valarray" in modules, which are includes this file
27   #undef max
28   #undef min
29 #endif
30
31 #include <valarray>
32 #include <stdexcept>
33
34 //#if defined(_DEBUG_)
35 #  define MED_TCSLICE_CHECK_RANGE
36 //#endif
37
38 namespace MED
39 {
40   //---------------------------------------------------------------
41   //! This class intends to provide an uniform way to handle multy-dimention data (const version)
42   /*! 
43     It just contains pointer to real sequence and implement proper calcultion of its indexes.
44     This class deal with constant pointer to the sources data and provide const method to
45     read the them (data).
46    */
47   template<class TValueType> 
48   class TCSlice
49   {
50     const TValueType* myCValuePtr; //!< Reference to source multy-dimension data
51     size_t mySourceSize; //!< Size of the source multy-dimension data
52     std::slice mySlice; //!< Defines algorithm of index calculation
53
54   protected:
55     void
56     check_id(size_t theId) const
57     {
58       long int anId = -1;
59       if(theId < mySlice.size()){
60         anId = mySlice.start() + theId*mySlice.stride();
61         if(anId < (long int)mySourceSize)
62           return;
63       }
64       throw std::out_of_range("TCSlice::check_id");
65     }
66
67     //! Calculate internal index to get proper element from the source multy-dimension data
68     size_t
69     calculate_id(size_t theId) const
70     {
71       return mySlice.start() + theId*mySlice.stride();
72     }
73     
74     size_t
75     get_id(size_t theId) const
76     {
77 #ifdef MED_TCSLICE_CHECK_RANGE
78       check_id(theId);
79 #endif
80       return calculate_id(theId);
81     }
82     
83     size_t
84     get_id_at(size_t theId) const
85     {
86       check_id(theId);
87       return calculate_id(theId);
88     }
89
90   public:
91     typedef TValueType value_type;
92
93     //! Construct the class from bare pointer
94     TCSlice(const value_type* theValuePtr,
95             size_t theSourceSize,
96             const std::slice& theSlice): 
97       myCValuePtr(theValuePtr),
98       mySourceSize(theSourceSize),
99       mySlice(theSlice)
100     {}
101     
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()),
107       mySlice(theSlice)
108     {}
109     
110     //! Default constructor (dangerous)
111     TCSlice():
112       myCValuePtr(NULL)
113     {}
114
115     //! Get element by its number (const version)
116     const value_type& 
117     operator[](size_t theId) const
118     {
119       return *(myCValuePtr + get_id(theId));
120     }
121     
122     const value_type& 
123     at(size_t theId) const
124     {
125       return *(myCValuePtr + get_id_at(theId));
126     }
127     
128     //! Get range of the order numbers
129     size_t
130     size() const
131     {
132       return mySlice.size();
133     }
134   };
135   
136
137   //---------------------------------------------------------------
138   //! This class extend TCSlice functionality for non-constant case
139   template<class TValueType> 
140   class TSlice: public TCSlice<TValueType>
141   {
142     TValueType* myValuePtr;
143     
144   public:
145     typedef TValueType value_type;
146     typedef TCSlice<TValueType> TSupperClass;
147
148     //! Construct the class from bare pointer
149     TSlice(value_type* theValuePtr,
150            size_t theSourceSize,
151            const std::slice& theSlice): 
152       TSupperClass(theValuePtr, theSourceSize, theSlice),
153       myValuePtr(theValuePtr)
154     {}
155     
156     //! Construct the class from corresponding container
157     TSlice(TVector<value_type>& theContainer,
158            const std::slice& theSlice): 
159       TSupperClass(theContainer, theSlice),
160       myValuePtr(&theContainer[0])
161     {}
162     
163     //! Default constructor (dangerous)
164     TSlice():
165       myValuePtr(NULL)
166     {}
167
168     //! Get element by its number
169     value_type& 
170     operator[](size_t theId)
171     {
172       return *(myValuePtr + this->get_id(theId));
173     }
174
175     value_type& 
176     at(size_t theId)
177     {
178       return *(myValuePtr + this->get_id_at(theId));
179     }
180   };
181
182 }
183
184 #undef MED_TCSLICE_CHECK_RANGE
185
186 #endif