Version: 9.12.0
MED_SliceArray.hxx
Go to the documentation of this file.
1 // Copyright (C) 2007-2023 CEA, EDF, 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, or (at your option) any later version.
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 correct compiling of "valarray" in modules, which include this file
27  #undef max
28  #undef min
29 #endif
30 
31 #include <valarray>
32 #include <stdexcept>
33 
34 # define MED_TCSLICE_CHECK_RANGE
35 
36 namespace MED
37 {
38  //---------------------------------------------------------------
40 
45  template<class TValueType>
46  class TCSlice
47  {
48  const TValueType* myCValuePtr;
49  size_t mySourceSize;
50  std::slice mySlice;
51 
52  protected:
53  void
54  check_id(size_t theId) const
55  {
56  long int anId = -1;
57  if(theId < mySlice.size()){
58  anId = mySlice.start() + theId*mySlice.stride();
59  if(anId < (long int)mySourceSize)
60  return;
61  }
62  throw std::out_of_range("TCSlice::check_id");
63  }
64 
66  size_t
67  calculate_id(size_t theId) const
68  {
69  return mySlice.start() + theId*mySlice.stride();
70  }
71 
72  size_t
73  get_id(size_t theId) const
74  {
75 #ifdef MED_TCSLICE_CHECK_RANGE
76  check_id(theId);
77 #endif
78  return calculate_id(theId);
79  }
80 
81  size_t
82  get_id_at(size_t theId) const
83  {
84  check_id(theId);
85  return calculate_id(theId);
86  }
87 
88  public:
89  typedef TValueType value_type;
90 
92  TCSlice(const value_type* theValuePtr,
93  size_t theSourceSize,
94  const std::slice& theSlice):
95  myCValuePtr(theValuePtr),
96  mySourceSize(theSourceSize),
97  mySlice(theSlice)
98  {}
99 
101  TCSlice(const TVector<value_type>& theContainer,
102  const std::slice& theSlice):
103  myCValuePtr(&theContainer[0]),
104  mySourceSize(theContainer.size()),
105  mySlice(theSlice)
106  {}
107 
110  myCValuePtr(NULL)
111  {}
112 
114  const value_type&
115  operator[](size_t theId) const
116  {
117  return *(myCValuePtr + get_id(theId));
118  }
119 
120  const value_type&
121  at(size_t theId) const
122  {
123  return *(myCValuePtr + get_id_at(theId));
124  }
125 
127  size_t
128  size() const
129  {
130  return mySlice.size();
131  }
132  };
133 
134  //---------------------------------------------------------------
136  template<class TValueType>
137  class TSlice: public TCSlice<TValueType>
138  {
139  TValueType* myValuePtr;
140 
141  public:
142  typedef TValueType value_type;
144 
146  TSlice(value_type* theValuePtr,
147  size_t theSourceSize,
148  const std::slice& theSlice):
149  TSupperClass(theValuePtr, theSourceSize, theSlice),
150  myValuePtr(theValuePtr)
151  {}
152 
155  const std::slice& theSlice):
156  TSupperClass(theContainer, theSlice),
157  myValuePtr(&theContainer[0])
158  {}
159 
162  myValuePtr(NULL)
163  {}
164 
166  value_type&
167  operator[](size_t theId)
168  {
169  return *(myValuePtr + this->get_id(theId));
170  }
171 
172  value_type&
173  at(size_t theId)
174  {
175  return *(myValuePtr + this->get_id_at(theId));
176  }
177  };
178 }
179 
180 #undef MED_TCSLICE_CHECK_RANGE
181 
182 #endif // MED_SliceArray_HeaderFile
This class intends to provide a uniform way to handle multidimensional data (const version)
Definition: MED_SliceArray.hxx:47
std::slice mySlice
Defines algorithm of index calculation.
Definition: MED_SliceArray.hxx:50
TCSlice(const value_type *theValuePtr, size_t theSourceSize, const std::slice &theSlice)
Construct the class from bare pointer.
Definition: MED_SliceArray.hxx:92
size_t get_id_at(size_t theId) const
Definition: MED_SliceArray.hxx:82
const TValueType * myCValuePtr
Reference to source multidimensional data.
Definition: MED_SliceArray.hxx:48
const value_type & at(size_t theId) const
Definition: MED_SliceArray.hxx:121
TCSlice()
Default constructor (dangerous)
Definition: MED_SliceArray.hxx:109
TCSlice(const TVector< value_type > &theContainer, const std::slice &theSlice)
Construct the class from corresponding container.
Definition: MED_SliceArray.hxx:101
size_t mySourceSize
Size of the source multidimensional data.
Definition: MED_SliceArray.hxx:49
size_t get_id(size_t theId) const
Definition: MED_SliceArray.hxx:73
size_t calculate_id(size_t theId) const
Calculate internal index to get proper element from the source multidimensional data.
Definition: MED_SliceArray.hxx:67
const value_type & operator[](size_t theId) const
Get element by its number (const version)
Definition: MED_SliceArray.hxx:115
TValueType value_type
Definition: MED_SliceArray.hxx:89
void check_id(size_t theId) const
Definition: MED_SliceArray.hxx:54
size_t size() const
Get range of the order numbers.
Definition: MED_SliceArray.hxx:128
This class extends TCSlice functionality for non-constant case.
Definition: MED_SliceArray.hxx:138
TValueType value_type
Definition: MED_SliceArray.hxx:142
value_type & operator[](size_t theId)
Get element by its number.
Definition: MED_SliceArray.hxx:167
TSlice()
Default constructor (dangerous)
Definition: MED_SliceArray.hxx:161
TSlice(value_type *theValuePtr, size_t theSourceSize, const std::slice &theSlice)
Construct the class from bare pointer.
Definition: MED_SliceArray.hxx:146
TValueType * myValuePtr
Definition: MED_SliceArray.hxx:139
value_type & at(size_t theId)
Definition: MED_SliceArray.hxx:173
TCSlice< TValueType > TSupperClass
Definition: MED_SliceArray.hxx:143
TSlice(TVector< value_type > &theContainer, const std::slice &theSlice)
Construct the class from corresponding container.
Definition: MED_SliceArray.hxx:154
Main purpose to introduce the class was to customize operator [].
Definition: MED_Vector.hxx:36
Definition: MED_Algorithm.cxx:28