Debian Codegraph @

src/3rdParty/salomesmesh/inc/SMESH_Array1.hxx

raw
1 // File: NCollection_Array1.hxx
2 // Created: 15.04.02 17:05:16
3 // Author: Alexander Kartomin (akm)
4 // <[email protected]>
5 // Copyright: Open Cascade 2002
6
7 #ifndef SMESH_Array1_HeaderFile
8 #define SMESH_Array1_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_BaseCollection.hxx>
17
18 #ifdef WNT
19 // Disable the warning "operator new unmatched by delete"
20 #pragma warning (push)
21 #pragma warning (disable:4291)
22 #endif
23
24 // *********************************************** Template for Array1 class
25
26 /**
27 * Purpose: The class Array1 represents unidimensional arrays
28 * of fixed size known at run time.
29 * The range of the index is user defined.
30 * An array1 can be constructed with a "C array".
31 * This functionality is useful to call methods expecting
32 * an Array1. It allows one to carry the bounds inside the arrays.
33 *
34 * Examples: Item tab[100]; // An example with a C array
35 * Array1OfItem ttab (tab[0],1,100);
36 *
37 * Array1OfItem tttab (ttab(10),10,20); // a slice of ttab
38 *
39 * If you want to reindex an array from 1 to Length do :
40 *
41 * Array1 tab1(tab(tab.Lower()),1,tab.Length());
42 *
43 * Warning: Programs client of such a class must be independent
44 * of the range of the first element. Then, a C++ for
45 * loop must be written like this
46 *
47 * for (i = A.Lower(); i <= A.Upper(); i++)
48 *
49 * Changes: In comparison to TCollection the flag isAllocated was
50 * renamed into myDeletable (alike in the Array2). For naming
51 * compatibility the method IsAllocated remained in class along
52 * with IsDeletable.
53 */
54 template <class TheItemType> class SMESH_Array1
55 : public NCollection_BaseCollection<TheItemType>
56 {
57
58 public:
59 //! Implementation of the Iterator interface.
60 class Iterator : public NCollection_BaseCollection<TheItemType>::Iterator
61 {
62 public:
63 //! Empty constructor - for later Init
64 Iterator (void) :
65 myCurrent (0),
66 myArray (NULL) {}
67 //! Constructor with initialisation
68 Iterator (const SMESH_Array1& theArray) :
69 myCurrent (theArray.Lower()),
70 myArray ((SMESH_Array1<TheItemType> *) &theArray) {}
71 //! Initialisation
72 void Init (const SMESH_Array1& theArray)
73 {
74 myCurrent = theArray.Lower();
75 myArray = (SMESH_Array1 *) &theArray;
76 }
77 //! Check end
78 virtual Standard_Boolean More (void) const
79 { return (myCurrent<=myArray->Upper()); }
80 //! Make step
81 virtual void Next (void)
82 { myCurrent++; }
83 //! Constant value access
84 virtual const TheItemType& Value (void) const
85 { return myArray->Value(myCurrent); }
86 //! Variable value access
87 virtual TheItemType& ChangeValue (void) const
88 { return myArray->ChangeValue(myCurrent); }
89 //! Operator new for allocating iterators
90 void* operator new(size_t theSize,
91 const Handle(NCollection_BaseAllocator)& theAllocator)
92 { return theAllocator->Allocate(theSize); }
93 private:
94 Standard_Integer myCurrent; //!< Index of the current item
95 SMESH_Array1* myArray; //!< Pointer to the array being iterated
96 }; // End of the nested class Iterator
97
98 public:
99 // ---------- PUBLIC METHODS ------------
100
101 //! Constructor
102 SMESH_Array1(const Standard_Integer theLower,
103 const Standard_Integer theUpper) :
104 NCollection_BaseCollection<TheItemType> (),
105 myLowerBound (theLower),
106 myUpperBound (theUpper),
107 myDeletable (Standard_True)
108 {
109 #if !defined No_Exception && !defined No_Standard_RangeError
110 if (theUpper < theLower)
111 Standard_RangeError::Raise ("SMESH_Array1::Create");
112 #endif
113 TheItemType* pBegin = new TheItemType[Length()];
114 #if !defined No_Exception && !defined No_Standard_OutOfMemory
115 if (!pBegin)
116 Standard_OutOfMemory::Raise ("SMESH_Array1 : Allocation failed");
117 #endif
118
119 myData = pBegin - theLower;
120 }
121
122 //! Copy constructor
123 SMESH_Array1 (const SMESH_Array1& theOther) :
124 NCollection_BaseCollection<TheItemType> (),
125 myLowerBound (theOther.Lower()),
126 myUpperBound (theOther.Upper()),
127 myDeletable (Standard_True)
128 {
129 TheItemType* pBegin = new TheItemType[Length()];
130 #if !defined No_Exception && !defined No_Standard_OutOfMemory
131 if (!pBegin)
132 Standard_OutOfMemory::Raise ("SMESH_Array1 : Allocation failed");
133 #endif
134 myData = pBegin - myLowerBound;
135
136 *this = theOther;
137 }
138
139 //! C array-based constructor
140 SMESH_Array1 (const TheItemType& theBegin,
141 const Standard_Integer theLower,
142 const Standard_Integer theUpper) :
143 NCollection_BaseCollection<TheItemType> (),
144 myLowerBound (theLower),
145 myUpperBound (theUpper),
146 myDeletable (Standard_False)
147 {
148 #if !defined No_Exception && !defined No_Standard_RangeError
149 if (theUpper < theLower)
150 Standard_RangeError::Raise ("SMESH_Array1::Array1");
151 #endif
152 myData = (TheItemType *) &theBegin - theLower;
153 }
154
155 //! Initialise the items with theValue
156 void Init (const TheItemType& theValue)
157 {
158 TheItemType *pCur = &myData[myLowerBound], *pEnd=&myData[myUpperBound];
159 for(; pCur <= pEnd; pCur++)
160 *pCur = (TheItemType&) theValue;
161 }
162
163 //! Size query
164 virtual Standard_Integer Size (void) const
165 { return Length(); }
166 //! Length query (the same)
167 Standard_Integer Length (void) const
168 { return (myUpperBound-myLowerBound+1); }
169
170 //! Lower bound
171 Standard_Integer Lower (void) const
172 { return myLowerBound; }
173 //! Upper bound
174 Standard_Integer Upper (void) const
175 { return myUpperBound; }
176
177 //! myDeletable flag
178 Standard_Boolean IsDeletable (void) const
179 { return myDeletable; }
180
181 //! IsAllocated flag - for naming compatibility
182 Standard_Boolean IsAllocated (void) const
183 { return myDeletable; }
184
185 //! Assign (any collection to this array)
186 // Copies items from the other collection into the allocated
187 // storage. Raises an exception when sizes differ.
188 virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther)
189 {
190 if (&theOther == this)
191 return;
192 #if !defined No_Exception && !defined No_Standard_DimensionMismatch
193 if (Length() != theOther.Size())
194 Standard_DimensionMismatch::Raise ("SMESH_Array1::Assign");
195 #endif
196 TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter2 =
197 theOther.CreateIterator();
198 TheItemType * const pEndItem = &myData[myUpperBound];
199 for (TheItemType * pItem = &myData[myLowerBound];
200 pItem <= pEndItem; anIter2.Next())
201 * pItem ++ = anIter2.Value();
202 }
203
204 //! operator= (array to array)
205 SMESH_Array1& operator= (const SMESH_Array1& theOther)
206 {
207 if (&theOther == this)
208 return *this;
209 #if !defined No_Exception && !defined No_Standard_DimensionMismatch
210 if (Length() != theOther.Length())
211 Standard_DimensionMismatch::Raise ("SMESH_Array1::operator=");
212 #endif
213 TheItemType * pMyItem = &myData[myLowerBound];
214 TheItemType * const pEndItem = &(theOther.myData)[theOther.myUpperBound];
215 TheItemType * pItem = &(theOther.myData)[theOther.myLowerBound];
216 while (pItem <= pEndItem) * pMyItem ++ = * pItem ++;
217 return *this;
218 }
219
220 //! Constant value access
221 const TheItemType& Value (const Standard_Integer theIndex) const
222 {
223 #if !defined No_Exception && !defined No_Standard_OutOfRange
224 if (theIndex < myLowerBound || theIndex > myUpperBound)
225 Standard_OutOfRange::Raise ("SMESH_Array1::Value");
226 #endif
227 return myData[theIndex];
228 }
229
230 //! operator() - alias to Value
231 const TheItemType& operator() (const Standard_Integer theIndex) const
232 { return Value (theIndex); }
233
234 //! Variable value access
235 TheItemType& ChangeValue (const Standard_Integer theIndex)
236 {
237 #if !defined No_Exception && !defined No_Standard_OutOfRange
238 if (theIndex < myLowerBound || theIndex > myUpperBound)
239 Standard_OutOfRange::Raise ("SMESH_Array1::ChangeValue");
240 #endif
241 return myData[theIndex];
242 }
243
244 //! operator() - alias to ChangeValue
245 TheItemType& operator() (const Standard_Integer theIndex)
246 { return ChangeValue (theIndex); }
247
248 //! Set value
249 void SetValue (const Standard_Integer theIndex,
250 const TheItemType& theItem)
251 {
252 #if !defined No_Exception && !defined No_Standard_OutOfRange
253 if (theIndex < myLowerBound || theIndex > myUpperBound)
254 Standard_OutOfRange::Raise ("SMESH_Array1::SetValue");
255 #endif
256 myData[theIndex] = theItem;
257 }
258
259 //! Destructor - releases the memory
260 ~SMESH_Array1 (void)
261 { if (myDeletable) delete [] &(myData[myLowerBound]); }
262
263 private:
264 // ----------- PRIVATE METHODS -----------
265
266 // ******** Creates Iterator for use on BaseCollection
267 virtual
268 TYPENAME NCollection_BaseCollection<TheItemType>::Iterator&
269 CreateIterator(void) const
270 { return *(new (this->IterAllocator()) Iterator(*this)); }
271
272 protected:
273 // ---------- PROTECTED FIELDS -----------
274 Standard_Integer myLowerBound;
275 Standard_Integer myUpperBound;
276 Standard_Boolean myDeletable; //!< Flag showing who allocated the array
277 TheItemType* myData; //!< Pointer to '0'th array item
278 };
279
280 #ifdef WNT
281 #pragma warning (pop)
282 #endif
283
284 #endif