1 // Created on: 2001-01-29
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2001-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #ifndef _Sequence_Declare_HeaderFile
22 #define _Sequence_Declare_HeaderFile
24 #ifndef _Standard_Macro_HeaderFile
25 #include <Standard_Macro.hxx>
28 // Declaration of Sequence (numbered list) class.
29 // Remarks on the current implementation:
31 // 1. Methods First() and Last() added
32 // 2. The method InsertAt(anIndex) replaces InsertBefore and InsertAfter.
33 // This method never throws exception "OutOfRange". Its behaviour:
34 // anIndex <= 1 => equivalent to Prepend()
35 // anIndex > Length() => equivalent to Append()
36 // else => equivalent to InsertBefore.
38 // *******************************************************************
39 // use the following somewhere in a header file;
40 // ClassName - name of the list class to create
41 // Type - type of members of the list
42 // *******************************************************************
44 #define DECLARE_SEQUENCE( ClassName, Type ) \
48 inline ClassName (); \
49 inline ClassName (const ClassName& anOther); \
50 inline ClassName& operator= (const ClassName& anOther); \
51 inline Standard_Integer Length () const; \
52 inline const Type& First () const; \
53 inline const Type& Last () const; \
54 inline const Type& Value (const Standard_Integer)const;\
55 inline Type& ChangeValue (const Standard_Integer); \
56 inline const Type& operator () (const Standard_Integer)const;\
57 inline Type& operator () (const Standard_Integer); \
59 Standard_EXPORT virtual ~ClassName (); \
60 Standard_EXPORT void Append (const Type& aVal); \
61 Standard_EXPORT void Prepend (const Type& aVal); \
62 Standard_EXPORT void InsertAt (const Standard_Integer, \
64 Standard_EXPORT void Clear (); \
65 Standard_EXPORT void Remove (const Standard_Integer); \
74 Node (const Type& aValue, Node * aPrv, Node * aNxt) \
75 : myValue (aValue), myPrev (aPrv), myNext (aNxt) {} \
76 const Type& Value () const { return myValue; } \
77 Type& ChangeValue () { return myValue; } \
78 friend class ClassName; \
81 Standard_EXPORT const void * FindItem (const Standard_Integer)const;\
82 Standard_EXPORT void Assign (const ClassName& anOther); \
87 Standard_Integer myICur; \
88 Standard_Integer myLength; \
91 inline ClassName::ClassName () : \
98 inline ClassName::ClassName (const ClassName& anOther) : \
104 inline ClassName& ClassName::operator= (const ClassName& anOther) \
110 inline Standard_Integer ClassName::Length () const{ \
114 inline const Type& ClassName::First () const \
116 return myFirst -> Value (); /* exception if out of range */ \
119 inline const Type& ClassName::Last () const \
121 return myLast -> Value(); /* exception if out of range */ \
124 inline const Type& ClassName::Value (const Standard_Integer anI) const \
126 const Node * anItem = (const Node *) FindItem (anI); \
127 return anItem -> Value (); /* exception if out of range */ \
130 inline Type& ClassName::ChangeValue (const Standard_Integer anI) \
132 Node * anItem = (Node *) FindItem (anI); \
133 return anItem -> ChangeValue (); /* exception if out of range */ \
136 inline const Type& ClassName::operator() (const Standard_Integer anI) const \
138 return Value (anI); \
141 inline Type& ClassName::operator() (const Standard_Integer anI) \
143 return ChangeValue (anI); \
146 // *******************************************************************
147 // use the following in a translation unit (*.cxx);
149 // *******************************************************************
150 #define IMPLEMENT_SEQUENCE( ClassName, Type ) \
151 const void * ClassName::FindItem (const Standard_Integer anI) const \
153 if (anI < 1 || anI > myLength) return NULL; \
154 Standard_Integer aCounter; \
155 Node * aCurrent = (Node *) myCurrent; \
156 Standard_Boolean aDir (Standard_False); \
157 if (aCurrent == NULL) { \
158 aCurrent = myFirst; \
159 aCounter = anI - 1; \
160 aDir = Standard_True; \
162 aCounter = Abs (anI - myICur); \
163 if (anI <= aCounter) { \
164 aCurrent = myFirst; \
165 aCounter = anI - 1; \
166 aDir = Standard_True; \
167 }else if (myLength - anI < aCounter) { \
169 aCounter = myLength - anI; \
170 }else if (anI > myICur) \
171 aDir = Standard_True; \
174 while (aCounter--) aCurrent = aCurrent -> myNext; \
176 while (aCounter--) aCurrent = aCurrent -> myPrev; \
177 (Standard_Integer&) myICur = anI; \
178 (Node *&) myCurrent = aCurrent; \
182 ClassName::~ClassName () \
187 void ClassName::Append (const Type& aVal) \
189 Node * anItem = new Node (aVal, myLast, NULL); \
193 myLast -> myNext = anItem; \
198 void ClassName::Prepend (const Type& aVal) \
200 Node * anItem = new Node (aVal, NULL, myFirst); \
204 myFirst -> myPrev = anItem; \
207 if (myICur > 0) myICur++; \
210 void ClassName::InsertAt (const Standard_Integer anI, const Type& aVal) \
212 if (anI <= 1) Prepend (aVal); \
213 else if (anI > myLength) Append (aVal); \
214 else if (FindItem(anI)) { \
215 Node * anItem = new Node (aVal, myCurrent -> myPrev, myCurrent); \
216 myCurrent -> myPrev = anItem; \
217 if (anItem -> myPrev) anItem -> myPrev -> myNext = anItem; \
223 void ClassName::Clear () \
226 Node * aCurr = myFirst -> myNext; \
230 myFirst = myLast = myCurrent = NULL; \
235 void ClassName::Remove (const Standard_Integer anI) \
237 Node * anItem = (Node *) FindItem (anI); \
239 if (myCurrent -> myPrev) { \
240 myCurrent -> myPrev -> myNext = myCurrent -> myNext; \
242 if (myCurrent -> myNext) { \
243 myCurrent -> myNext -> myPrev = myCurrent -> myPrev; \
244 myCurrent = myCurrent -> myNext; \
246 myCurrent = myCurrent -> myPrev; \
249 if (myFirst == anItem) myFirst = myFirst -> myNext; \
250 if (myLast == anItem) myLast = myLast -> myPrev; \
256 void ClassName::Assign (const ClassName& anOther) \
259 if (anOther.Length () == 0) return; \
260 myFirst = new Node (anOther.First(), NULL, NULL); \
261 Node * aPrevious = myFirst; \
263 while (myLength < anOther.Length()) { \
265 Node * aCurrent = new Node (anOther.Value(myLength), aPrevious, NULL); \
266 aPrevious = aPrevious -> myNext = aCurrent; \
268 myLast = aPrevious; \