1 // Created on: 2002-04-15
2 // Created by: Alexander Kartomin (akm)
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef NCollection_Array2_HeaderFile
17 #define NCollection_Array2_HeaderFile
19 #include <Standard_DimensionMismatch.hxx>
20 #include <Standard_OutOfMemory.hxx>
21 #include <Standard_OutOfRange.hxx>
23 #include <NCollection_DefineAlloc.hxx>
25 // *********************************************** Template for Array2 class
27 * Purpose: The class Array2 represents bi-dimensional arrays
28 * of fixed size known at run time.
29 * The ranges of indices are user defined.
31 * Warning: Programs clients of such class must be independant
32 * of the range of the first element. Then, a C++ for
33 * loop must be written like this
35 * for (i = A.LowerRow(); i <= A.UpperRow(); i++)
36 * for (j = A.LowerCol(); j <= A.UpperCol(); j++)
38 template <class TheItemType>
39 class NCollection_Array2
42 //! STL-compliant typedef for value type
43 typedef TheItemType value_type;
46 // **************** Implementation of the Iterator interface.
50 //! Empty constructor - for later Init
55 //! Constructor with initialisation
56 Iterator (const NCollection_Array2& theArray) :
58 mySize (theArray.Length()),
59 myArray ((NCollection_Array2 *) &theArray) {}
61 void Init (const NCollection_Array2& theArray)
64 mySize = theArray.Length();
65 myArray = (NCollection_Array2 *) &theArray;
68 Standard_Boolean More (void) const
69 { return (myCurrent < mySize); }
73 //! Constant value access
74 const TheItemType& Value (void) const
75 { return myArray->myStart[myCurrent]; }
76 //! Variable value access
77 TheItemType& ChangeValue (void) const
78 { return myArray->myStart[myCurrent]; }
80 Standard_Integer myCurrent; //!< Index of the current item
81 Standard_Integer mySize; //!< Total amount of items
82 NCollection_Array2* myArray; //!< Pointer to the array being iterated
83 }; // End of nested class Iterator
86 // ---------- PUBLIC METHODS ------------
89 NCollection_Array2(const Standard_Integer theRowLower,
90 const Standard_Integer theRowUpper,
91 const Standard_Integer theColLower,
92 const Standard_Integer theColUpper) :
93 myLowerRow (theRowLower),
94 myUpperRow (theRowUpper),
95 myLowerCol (theColLower),
96 myUpperCol (theColUpper),
97 myDeletable (Standard_True)
101 NCollection_Array2 (const NCollection_Array2& theOther) :
102 myLowerRow (theOther.LowerRow()),
103 myUpperRow (theOther.UpperRow()),
104 myLowerCol (theOther.LowerCol()),
105 myUpperCol (theOther.UpperCol()),
106 myDeletable (Standard_True)
112 //! C array-based constructor
113 NCollection_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 myLowerRow (theRowLower),
119 myUpperRow (theRowUpper),
120 myLowerCol (theColLower),
121 myUpperCol (theColUpper),
122 myDeletable (Standard_False)
124 myStart = (TheItemType *) &theBegin;
128 //! Initialise the values
129 void Init (const TheItemType& theValue)
131 TheItemType *pCur, *pEnd=myStart+Size();
132 for(pCur = myStart; pCur<pEnd; pCur++)
136 //! Size (number of items)
137 Standard_Integer Size (void) const
139 //! Length (number of items)
140 Standard_Integer Length (void) const
141 { return RowLength() * ColLength(); }
143 //! Returns length of the row, i.e. number of columns
144 Standard_Integer RowLength (void) const
145 { return (myUpperCol-myLowerCol+1); }
147 //! Returns length of the column, i.e. number of rows
148 Standard_Integer ColLength (void) const
149 { return (myUpperRow-myLowerRow+1); }
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; }
165 Standard_Boolean IsDeletable (void) const
166 { return myDeletable; }
169 NCollection_Array2& Assign (const NCollection_Array2& theOther)
171 if (&theOther == this)
173 Standard_DimensionMismatch_Raise_if (Length() != theOther.Length(), "NCollection_Array2::operator=");
174 TheItemType * pMyItem = myStart;
175 TheItemType * pItem = theOther.myStart;
176 const Standard_Integer iSize = Length();
177 for (Standard_Integer i=0; i < iSize; i++, pItem++, pMyItem++)
182 //! Assignment operator
183 NCollection_Array2& operator= (const NCollection_Array2& theOther)
185 return Assign (theOther);
188 //! Constant value access
189 const TheItemType& Value (const Standard_Integer theRow,
190 const Standard_Integer theCol) const
192 Standard_OutOfRange_Raise_if (theRow < myLowerRow || theRow > myUpperRow ||
193 theCol < myLowerCol || theCol > myUpperCol, "NCollection_Array2::Value");
194 return myData[theRow][theCol];
197 //! operator() - alias to ChangeValue
198 const TheItemType& operator() (const Standard_Integer theRow,
199 const Standard_Integer theCol) const
200 { return Value (theRow,theCol); }
202 //! Variable value access
203 TheItemType& ChangeValue (const Standard_Integer theRow,
204 const Standard_Integer theCol)
206 Standard_OutOfRange_Raise_if (theRow < myLowerRow || theRow > myUpperRow ||
207 theCol < myLowerCol || theCol > myUpperCol, "NCollection_Array2::ChangeValue");
208 return myData[theRow][theCol];
211 //! operator() - alias to ChangeValue
212 TheItemType& operator() (const Standard_Integer theRow,
213 const Standard_Integer theCol)
214 { return ChangeValue (theRow,theCol); }
217 void SetValue (const Standard_Integer theRow,
218 const Standard_Integer theCol,
219 const TheItemType& theItem)
221 Standard_OutOfRange_Raise_if (theRow < myLowerRow || theRow > myUpperRow ||
222 theCol < myLowerCol || theCol > myUpperCol, "NCollection_Array2::SetValue");
223 myData[theRow][theCol] = theItem;
226 //! Destructor - releases the memory
227 ~NCollection_Array2 (void)
229 if (myDeletable) delete [] myStart;
230 delete [] &(myData[myLowerRow]);
234 // ----------- PRIVATE METHODS -----------
236 //! Allocate memory for the array, set up indirection table
239 const Standard_Integer iRowSize = myUpperCol - myLowerCol + 1;
240 const Standard_Integer iColSize = myUpperRow - myLowerRow + 1;
241 Standard_RangeError_Raise_if (iRowSize <= 0 || iColSize <= 0, "NCollection_Array2::Allocate");
243 // allocation of the data in the array
244 myStart = new TheItemType[iRowSize * iColSize];
245 Standard_OutOfMemory_Raise_if (!myStart, "NCollection_Array2 : Allocation failed");
247 // else myStart is set to the beginning of the given array
248 TheItemType** pTable = new TheItemType* [iColSize];
249 Standard_OutOfMemory_Raise_if (!pTable, "NCollection_Array2 : Allocation failed");
251 // Items of pTable point to the '0'th items in the rows of the array
252 TheItemType* pRow = myStart - myLowerCol;
253 for (Standard_Integer i = 0; i < iColSize; i++)
259 // Set myData to the '0'th row pointer of the pTable
260 myData = pTable - myLowerRow;
264 // ---------- PROTECTED FIELDS -----------
265 Standard_Integer myLowerRow;
266 Standard_Integer myUpperRow;
267 Standard_Integer myLowerCol;
268 Standard_Integer myUpperCol;
270 TheItemType** myData; //!< Pointer to the row pointers table
271 TheItemType* myStart; //!< Pointer to the memory array
272 Standard_Boolean myDeletable; //!< Flag showing who allocated the array
274 // ----------- FRIEND CLASSES ------------
275 friend class Iterator;