1 // Created on: 2001-01-29
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2001-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 _Sequence_Declare_HeaderFile
17 #define _Sequence_Declare_HeaderFile
19 #ifndef _Standard_Macro_HeaderFile
20 #include <Standard_Macro.hxx>
23 // Declaration of Sequence (numbered list) class.
24 // Remarks on the current implementation:
26 // 1. Methods First() and Last() added
27 // 2. The method InsertAt(anIndex) replaces InsertBefore and InsertAfter.
28 // This method never throws exception "OutOfRange". Its behaviour:
29 // anIndex <= 1 => equivalent to Prepend()
30 // anIndex > Length() => equivalent to Append()
31 // else => equivalent to InsertBefore.
33 // *******************************************************************
34 // use the following somewhere in a header file;
35 // ClassName - name of the list class to create
36 // Type - type of members of the list
37 // *******************************************************************
39 #define DECLARE_SEQUENCE( ClassName, Type ) \
43 inline ClassName (); \
44 inline ClassName (const ClassName& anOther); \
45 inline ClassName& operator= (const ClassName& anOther); \
46 inline Standard_Integer Length () const; \
47 inline const Type& First () const; \
48 inline const Type& Last () const; \
49 inline const Type& Value (const Standard_Integer)const;\
50 inline Type& ChangeValue (const Standard_Integer); \
51 inline const Type& operator () (const Standard_Integer)const;\
52 inline Type& operator () (const Standard_Integer); \
54 Standard_EXPORT virtual ~ClassName (); \
55 Standard_EXPORT void Append (const Type& aVal); \
56 Standard_EXPORT void Prepend (const Type& aVal); \
57 Standard_EXPORT void InsertAt (const Standard_Integer, \
59 Standard_EXPORT void Clear (); \
60 Standard_EXPORT void Remove (const Standard_Integer); \
69 Node (const Type& aValue, Node * aPrv, Node * aNxt) \
70 : myValue (aValue), myPrev (aPrv), myNext (aNxt) {} \
71 const Type& Value () const { return myValue; } \
72 Type& ChangeValue () { return myValue; } \
73 friend class ClassName; \
76 Standard_EXPORT const void * FindItem (const Standard_Integer)const;\
77 Standard_EXPORT void Assign (const ClassName& anOther); \
82 Standard_Integer myICur; \
83 Standard_Integer myLength; \
86 inline ClassName::ClassName () : \
93 inline ClassName::ClassName (const ClassName& anOther) : \
99 inline ClassName& ClassName::operator= (const ClassName& anOther) \
105 inline Standard_Integer ClassName::Length () const{ \
109 inline const Type& ClassName::First () const \
111 return myFirst -> Value (); /* exception if out of range */ \
114 inline const Type& ClassName::Last () const \
116 return myLast -> Value(); /* exception if out of range */ \
119 inline const Type& ClassName::Value (const Standard_Integer anI) const \
121 const Node * anItem = (const Node *) FindItem (anI); \
122 return anItem -> Value (); /* exception if out of range */ \
125 inline Type& ClassName::ChangeValue (const Standard_Integer anI) \
127 Node * anItem = (Node *) FindItem (anI); \
128 return anItem -> ChangeValue (); /* exception if out of range */ \
131 inline const Type& ClassName::operator() (const Standard_Integer anI) const \
133 return Value (anI); \
136 inline Type& ClassName::operator() (const Standard_Integer anI) \
138 return ChangeValue (anI); \
141 // *******************************************************************
142 // use the following in a translation unit (*.cxx);
144 // *******************************************************************
145 #define IMPLEMENT_SEQUENCE( ClassName, Type ) \
146 const void * ClassName::FindItem (const Standard_Integer anI) const \
148 if (anI < 1 || anI > myLength) return NULL; \
149 Standard_Integer aCounter; \
150 Node * aCurrent = (Node *) myCurrent; \
151 Standard_Boolean aDir (Standard_False); \
152 if (aCurrent == NULL) { \
153 aCurrent = myFirst; \
154 aCounter = anI - 1; \
155 aDir = Standard_True; \
157 aCounter = Abs (anI - myICur); \
158 if (anI <= aCounter) { \
159 aCurrent = myFirst; \
160 aCounter = anI - 1; \
161 aDir = Standard_True; \
162 }else if (myLength - anI < aCounter) { \
164 aCounter = myLength - anI; \
165 }else if (anI > myICur) \
166 aDir = Standard_True; \
169 while (aCounter--) aCurrent = aCurrent -> myNext; \
171 while (aCounter--) aCurrent = aCurrent -> myPrev; \
172 (Standard_Integer&) myICur = anI; \
173 (Node *&) myCurrent = aCurrent; \
177 ClassName::~ClassName () \
182 void ClassName::Append (const Type& aVal) \
184 Node * anItem = new Node (aVal, myLast, NULL); \
188 myLast -> myNext = anItem; \
193 void ClassName::Prepend (const Type& aVal) \
195 Node * anItem = new Node (aVal, NULL, myFirst); \
199 myFirst -> myPrev = anItem; \
202 if (myICur > 0) myICur++; \
205 void ClassName::InsertAt (const Standard_Integer anI, const Type& aVal) \
207 if (anI <= 1) Prepend (aVal); \
208 else if (anI > myLength) Append (aVal); \
209 else if (FindItem(anI)) { \
210 Node * anItem = new Node (aVal, myCurrent -> myPrev, myCurrent); \
211 myCurrent -> myPrev = anItem; \
212 if (anItem -> myPrev) anItem -> myPrev -> myNext = anItem; \
218 void ClassName::Clear () \
221 Node * aCurr = myFirst -> myNext; \
225 myFirst = myLast = myCurrent = NULL; \
230 void ClassName::Remove (const Standard_Integer anI) \
232 Node * anItem = (Node *) FindItem (anI); \
234 if (myCurrent -> myPrev) { \
235 myCurrent -> myPrev -> myNext = myCurrent -> myNext; \
237 if (myCurrent -> myNext) { \
238 myCurrent -> myNext -> myPrev = myCurrent -> myPrev; \
239 myCurrent = myCurrent -> myNext; \
241 myCurrent = myCurrent -> myPrev; \
244 if (myFirst == anItem) myFirst = myFirst -> myNext; \
245 if (myLast == anItem) myLast = myLast -> myPrev; \
251 void ClassName::Assign (const ClassName& anOther) \
254 if (anOther.Length () == 0) return; \
255 myFirst = new Node (anOther.First(), NULL, NULL); \
256 Node * aPrevious = myFirst; \
258 while (myLength < anOther.Length()) { \
260 Node * aCurrent = new Node (anOther.Value(myLength), aPrevious, NULL); \
261 aPrevious = aPrevious -> myNext = aCurrent; \
263 myLast = aPrevious; \