SALOME - SMESH
SMESH_Array2.hxx
Go to the documentation of this file.
1 // File: NCollection_Array2.hxx
2 // Created: 15.04.02 17:05:16
3 // Author: Alexander Kartomin (akm)
4 // <a-kartomin@opencascade.com>
5 // Copyright: Open Cascade 2002
6 
7 #ifndef SMESH_Array2_HeaderFile
8 #define SMESH_Array2_HeaderFile
9 
10 #ifndef No_Exception
11 #include <Standard_DimensionMismatch.hxx>
12 #include <Standard_OutOfMemory.hxx>
13 #include <Standard_OutOfRange.hxx>
14 #endif
15 
16 #include <NCollection_Array2.hxx>
17 
18 #ifdef WNT
19 // Disable the warning "operator new unmatched by delete"
20 #pragma warning (disable:4291)
21 #endif
22 
23 // *********************************************** Template for Array2 class
36 template <class TheItemType> class SMESH_Array2
37  : public NCollection_Array2<TheItemType>
38 {
39  public:
40  // **************** Implementation of the Iterator interface.
41  class Iterator : public NCollection_Array2<TheItemType>::Iterator
42  {
43  public:
45  Iterator (void) :
46  myCurrent (0),
47  mySize (0),
48  myArray (NULL) {}
50  Iterator (const SMESH_Array2& theArray) :
51  myCurrent (0),
52  mySize (theArray.Length()),
53  myArray (const_cast<SMESH_Array2<TheItemType>*>(&theArray)) {}
55  void Init (const SMESH_Array2& theArray)
56  {
57  myCurrent = 0;
58  mySize = theArray.Length();
59  myArray = (SMESH_Array2 *) &theArray;
60  }
62  virtual Standard_Boolean More (void) const
63  { return (myCurrent < mySize); }
65  virtual void Next (void)
66  { myCurrent++; }
68  virtual const TheItemType& Value (void) const
69  { return myArray->myStart[myCurrent]; }
71  virtual TheItemType& ChangeValue (void) const
72  { return myArray->myStart[myCurrent]; }
74  void* operator new(size_t theSize,
75  const Handle(NCollection_BaseAllocator)& theAllocator)
76  { return theAllocator->Allocate(theSize); }
77  private:
78  Standard_Integer myCurrent;
79  Standard_Integer mySize;
81  }; // End of nested class Iterator
82 
83  public:
84  // ---------- PUBLIC METHODS ------------
85 
87  SMESH_Array2(const Standard_Integer theRowLower,
88  const Standard_Integer theRowUpper,
89  const Standard_Integer theColLower,
90  const Standard_Integer theColUpper) :
91  NCollection_Array2<TheItemType> (),
92  myLowerRow (theRowLower),
93  myUpperRow (theRowUpper),
94  myLowerCol (theColLower),
95  myUpperCol (theColUpper),
96  myDeletable (Standard_True)
97  { Allocate(); }
98 
100  SMESH_Array2 (const SMESH_Array2& theOther) :
101  NCollection_Array2<TheItemType> (),
102  myLowerRow (theOther.LowerRow()),
103  myUpperRow (theOther.UpperRow()),
104  myLowerCol (theOther.LowerCol()),
105  myUpperCol (theOther.UpperCol()),
106  myDeletable (Standard_True)
107  {
108  Allocate();
109  *this = theOther;
110  }
111 
113  SMESH_Array2(const TheItemType& theBegin,
114  const Standard_Integer theRowLower,
115  const Standard_Integer theRowUpper,
116  const Standard_Integer theColLower,
117  const Standard_Integer theColUpper) :
118  NCollection_Array2<TheItemType> (),
119  myLowerRow (theRowLower),
120  myUpperRow (theRowUpper),
121  myLowerCol (theColLower),
122  myUpperCol (theColUpper),
123  myDeletable (Standard_False)
124  {
125  myStart = (TheItemType *) &theBegin;
126  Allocate();
127  }
128 
130  void Init (const TheItemType& theValue)
131  {
132  TheItemType *pCur, *pEnd=myStart+Size();
133  for(pCur = myStart; pCur<pEnd; pCur++)
134  *pCur = theValue;
135  }
136 
138  virtual Standard_Integer Size (void) const
139  { return Length(); }
141  Standard_Integer Length (void) const
142  { return RowLength() * ColLength(); }
143 
145  Standard_Integer RowLength (void) const
146  { return (myUpperCol-myLowerCol+1); }
148  Standard_Integer ColLength (void) const
149  { return (myUpperRow-myLowerRow+1); }
150 
152  Standard_Integer LowerRow (void) const
153  { return myLowerRow; }
155  Standard_Integer UpperRow (void) const
156  { return myUpperRow; }
158  Standard_Integer LowerCol (void) const
159  { return myLowerCol; }
161  Standard_Integer UpperCol (void) const
162  { return myUpperCol; }
163 
165  Standard_Boolean IsDeletable (void) const
166  { return myDeletable; }
167 
169  // Copies items from the other collection into the allocated
170  // storage. Raises an exception when sizes differ.
171  virtual void Assign (const NCollection_Array2<TheItemType>& theOther)
172  {
173  if (&theOther == this)
174  return;
175 #if !defined No_Exception && !defined No_Standard_DimensionMismatch
176  if (Length() != theOther.Size())
177  Standard_DimensionMismatch::Raise ("SMESH_Array2::Assign");
178 #endif
179  TYPENAME NCollection_Array2<TheItemType>::Iterator& anIter2 =
180  theOther.CreateIterator();
181  const TheItemType* pEnd = myStart+Length();
182  for (TheItemType* pItem=myStart;
183  pItem < pEnd;
184  pItem++, anIter2.Next())
185  *pItem = anIter2.Value();
186  }
187 
190  {
191  if (&theOther == this)
192  return *this;
193 #if !defined No_Exception && !defined No_Standard_DimensionMismatch
194  if (Length() != theOther.Length())
195  Standard_DimensionMismatch::Raise ("SMESH_Array2::operator=");
196 #endif
197  TheItemType * pMyItem = myStart;
198  TheItemType * pItem = theOther.myStart;
199  const Standard_Integer iSize = Length();
200  for (Standard_Integer i=0; i < iSize; i++, pItem++, pMyItem++)
201  *pMyItem = *pItem;
202  return *this;
203  }
204 
206  const TheItemType& Value (const Standard_Integer theRow,
207  const Standard_Integer theCol) const
208  {
209 #if !defined No_Exception && !defined No_Standard_OutOfRange
210  if (theRow < myLowerRow || theRow > myUpperRow ||
211  theCol < myLowerCol || theCol > myUpperCol)
212  Standard_OutOfRange::Raise ("SMESH_Array2::Value");
213 #endif
214  return myData[theRow][theCol];
215  }
216 
218  const TheItemType& operator() (const Standard_Integer theRow,
219  const Standard_Integer theCol) const
220  { return Value (theRow,theCol); }
221 
223  TheItemType& ChangeValue (const Standard_Integer theRow,
224  const Standard_Integer theCol)
225  {
226 #if !defined No_Exception && !defined No_Standard_OutOfRange
227  if (theRow < myLowerRow || theRow > myUpperRow ||
228  theCol < myLowerCol || theCol > myUpperCol)
229  Standard_OutOfRange::Raise ("SMESH_Array2::ChangeValue");
230 #endif
231  return myData[theRow][theCol];
232  }
233 
235  TheItemType& operator() (const Standard_Integer theRow,
236  const Standard_Integer theCol)
237  { return ChangeValue (theRow,theCol); }
238 
240  void SetValue (const Standard_Integer theRow,
241  const Standard_Integer theCol,
242  const TheItemType& theItem)
243  {
244 #if !defined No_Exception && !defined No_Standard_OutOfRange
245  if (theRow < myLowerRow || theRow > myUpperRow ||
246  theCol < myLowerCol || theCol > myUpperCol)
247  Standard_OutOfRange::Raise ("SMESH_Array2::SetValue");
248 #endif
249  myData[theRow][theCol] = theItem;
250  }
251 
254  {
255  if (myDeletable) delete [] myStart;
256  delete [] &(myData[myLowerRow]);
257  }
258 
259  private:
260  // ----------- PRIVATE METHODS -----------
261 
263  void Allocate (void)
264  {
265  const Standard_Integer iRowSize = myUpperCol - myLowerCol + 1;
266  const Standard_Integer iColSize = myUpperRow - myLowerRow + 1;
267 #if !defined No_Exception && !defined No_Standard_RangeError
268  if (iRowSize <= 0 || iColSize <= 0)
269  Standard_RangeError::Raise ("SMESH_Array2::Allocate");
270 #endif
271  if (myDeletable) {
272  // allocation of the data in the array
273  myStart = new TheItemType[iRowSize * iColSize];
274 #if !defined No_Exception && !defined No_Standard_OutOfMemory
275  if (!myStart)
276  Standard_OutOfMemory::Raise ("SMESH_Array2 : Allocation failed");
277 #endif
278  }
279  // else myStart is set to the beginning of the given array
280  TheItemType** pTable = new TheItemType* [iColSize];
281 #if !defined No_Exception && !defined No_Standard_OutOfMemory
282  if (!pTable)
283  Standard_OutOfMemory::Raise ("SMESH_Array2 : Allocation failed");
284 #endif
285 
286  // Items of pTable point to the '0'th items in the rows of the array
287  TheItemType* pRow = myStart - myLowerCol;
288  for (Standard_Integer i = 0; i < iColSize; i++)
289  {
290  pTable[i] = pRow;
291  pRow += iRowSize;
292  }
293 
294  // Set myData to the '0'th row pointer of the pTable
295  myData = pTable - myLowerRow;
296  }
297 
300  CreateIterator(void) const
301  { return *(new (this->IterAllocator()) Iterator(*this)); }
302 
303  protected:
304  // ---------- PROTECTED FIELDS -----------
305  Standard_Integer myLowerRow;
306  Standard_Integer myUpperRow;
307  Standard_Integer myLowerCol;
308  Standard_Integer myUpperCol;
309 
310  TheItemType** myData;
311  TheItemType* myStart;
312  Standard_Boolean myDeletable;
313 
314  // ----------- FRIEND CLASSES ------------
315  friend class Iterator;
316 
317 };
318 
319 #ifdef WNT
320 #pragma warning (default:4291)
321 #endif
322 
323 #endif
Standard_Integer myUpperCol
SMESH_Array2 & operator=(const SMESH_Array2 &theOther)
operator= (array to array)
Standard_Integer myLowerCol
~SMESH_Array2(void)
Destructor - releases the memory.
virtual void Next(void)
Make step.
Standard_Integer UpperCol(void) const
UpperCol.
SMESH_Array2 * myArray
Pointer to the array being iterated.
Standard_Boolean IsDeletable(void) const
myDeletable flag
TheItemType * myStart
Pointer to the memory array.
virtual Standard_Boolean More(void) const
Check end.
void Init(const SMESH_Array2 &theArray)
Initialisation.
SMESH_Array2(const Standard_Integer theRowLower, const Standard_Integer theRowUpper, const Standard_Integer theColLower, const Standard_Integer theColUpper)
Constructor.
Standard_Integer myUpperRow
virtual TheItemType & ChangeValue(void) const
Variable value access.
const TheItemType & operator()(const Standard_Integer theRow, const Standard_Integer theCol) const
operator() - alias to ChangeValue
SMESH_Array2(const TheItemType &theBegin, const Standard_Integer theRowLower, const Standard_Integer theRowUpper, const Standard_Integer theColLower, const Standard_Integer theColUpper)
C array-based constructor.
Purpose: The class Array2 represents bi-dimensional arrays of fixed size known at run time...
Standard_Integer mySize
Total amount of items.
void Allocate(void)
Allocate memory for the array, set up indirection table.
Standard_Integer ColLength(void) const
ColLength.
TheItemType ** myData
Pointer to the row pointers table.
Standard_Integer RowLength(void) const
RowLength.
virtual Standard_Integer Size(void) const
Size (number of items)
SMESH_Array2(const SMESH_Array2 &theOther)
Copy constructor.
const TheItemType & Value(const Standard_Integer theRow, const Standard_Integer theCol) const
Constant value access.
virtual void Assign(const NCollection_Array2< TheItemType > &theOther)
Assign.
virtual const TheItemType & Value(void) const
Constant value access.
Standard_Integer LowerRow(void) const
LowerRow.
void SetValue(const Standard_Integer theRow, const Standard_Integer theCol, const TheItemType &theItem)
SetValue.
Standard_Boolean myDeletable
Flag showing who allocated the array.
virtual TYPENAME NCollection_Array2< TheItemType >::Iterator & CreateIterator(void) const
Creates Iterator for use on BaseCollection.
Standard_Integer Length(void) const
Length (number of items)
friend class Iterator
TheItemType & ChangeValue(const Standard_Integer theRow, const Standard_Integer theCol)
Variable value access.
Iterator(const SMESH_Array2 &theArray)
Constructor with initialisation.
Standard_Integer UpperRow(void) const
UpperRow.
Standard_Integer myCurrent
Index of the current item.
void Init(const TheItemType &theValue)
Initialise the values.
Standard_Integer myLowerRow
Iterator(void)
Empty constructor - for later Init.
Standard_Integer LowerCol(void) const
LowerCol.