0024831: Make iterators of NCollection classes STL-compatible
[occt.git] / src / NCollection / NCollection_BaseList.hxx
CommitLineData
b311480e 1// Created on: 2002-04-17
2// Created by: Alexander Kartomin (akm)
973c2be1 3// Copyright (c) 2002-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16// Purpose: This is a base class for the List, Set, Queue and Stack
17// collections. It offers operations on abstract lists (of the
18// objects of class NCollection_ListNode).
19// Apart from this class being brand new (in TCollection said
20// collections were independent, only using the same class for
21// node representation), here is an important new feature -
22// the list length is continuously updated, so the method
23// Extent is quite quick.
7fd59977 24
25#ifndef NCollection_BaseList_HeaderFile
26#define NCollection_BaseList_HeaderFile
27
28#include <Standard_NoSuchObject.hxx>
29#include <NCollection_ListNode.hxx>
30
7fd59977 31typedef void (* NCollection_DelListNode)
32 (NCollection_ListNode*, Handle(NCollection_BaseAllocator)& theAl);
33
34// ********************************************************** BaseList class
35class NCollection_BaseList
36{
37 public:
38 class Iterator
39 {
40 public:
41 // ******** Empty constructor
42 Iterator (void) :
43 myCurrent (NULL),
44 myPrevious(NULL) {}
45 // ******** Constructor with initialisation
46 Iterator (const NCollection_BaseList& theList) :
47 myCurrent (theList.myFirst),
48 myPrevious(NULL) {}
49 // ******** Initialisation
50 void Init (const NCollection_BaseList& theList)
51 {
52 myCurrent = theList.myFirst;
53 myPrevious = NULL;
54 }
9991c9c2 55 // ******** Initialisation
56 void Initialize (const NCollection_BaseList& theList)
57 {
58 Init(theList);
59 }
7fd59977 60 // ******** More
61 Standard_Boolean More (void) const
62 { return (myCurrent!=NULL); }
63 // ******** Assignment operator
64 Iterator& operator= (const Iterator& theIt)
65 {
66 if (&theIt != this)
67 {
68 myCurrent = theIt.myCurrent;
69 myPrevious = theIt.myPrevious;
70 }
71 return * this;
72 }
73//skt----------------------------------------------------
74 // ******** Comparison operator
79a35943 75 Standard_Boolean operator== (const Iterator& theIt) const
7fd59977 76 {
77 return myCurrent == theIt.myCurrent;
78 }
79//-------------------------------------------------------
79a35943 80 //! Performs comparison of two iterators
81 virtual Standard_Boolean IsEqual (const Iterator& theOther) const
82 {
83 return *this == theOther;
84 }
7fd59977 85 protected:
86 void Init (const NCollection_BaseList& theList,
87 NCollection_ListNode * const thePrev)
88 {
89 myCurrent = thePrev ? thePrev -> Next() :
90 (NCollection_ListNode *)theList.PLast();
91 myPrevious = thePrev;
92 }
93 public:
94 NCollection_ListNode * myCurrent; // Pointer to the current node
95 NCollection_ListNode * myPrevious;// Pointer to the previous one
96 friend class NCollection_BaseList;
97 }; // End of nested class Iterator
98
99 public:
100 // ---------- PUBLIC METHODS ------------
101 // ******** Extent
102 // Purpose: Returns the number of nodes in the list
103 Standard_Integer Extent (void) const
104 { return myLength; }
105
106 // ******** IsEmpty
107 // Purpose: Query if the list is empty
108 Standard_Boolean IsEmpty (void) const
109 { return (myFirst == NULL); }
110
111 protected:
112 // --------- PROTECTED METHODS ----------
113
114 // ******** Constructor
115 // Purpose: Initializes an empty list
116 NCollection_BaseList(void) :
117 myFirst(NULL),
118 myLast(NULL),
119 myLength(0) {}
120
121 // ******** PClear
122 // Purpose: deletes all nodes
123 Standard_EXPORT void PClear (NCollection_DelListNode fDel,
124 Handle(NCollection_BaseAllocator)& theAllocator);
125
126 // ******** PFirst
127 // Purpose: Returns pointer to the first node
128 const NCollection_ListNode* PFirst (void) const
129 { return myFirst; }
130
131 // ******** PLast
132 // Purpose: Returns pointer to the last node
133 const NCollection_ListNode* PLast (void) const
134 { return myLast; }
135
136 // ******** PAppend
137 // Purpose: Appends theNode at the end
138 Standard_EXPORT void PAppend (NCollection_ListNode* theNode);
139
140 // ******** PAppend
141 // Purpose: Appends theNode at the end, returns iterator to the previous
142 void PAppend (NCollection_ListNode* theNode,
143 Iterator& theIt)
144 {
145 NCollection_ListNode * aPrev = myLast;
146 PAppend (theNode);
147 theIt.Init (* this, aPrev);
148 }
149
150 // ******** PAppend
151 // Purpose: Appends theOther list at the end (clearing it)
152 Standard_EXPORT void PAppend (NCollection_BaseList& theOther);
153
154 // ******** PPrepend
155 // Purpose: Prepends theNode at the beginning
156 Standard_EXPORT void PPrepend (NCollection_ListNode* theNode);
157
158 // ******** PPrepend
159 // Purpose: Prepends theOther list at the beginning (clearing it)
160 Standard_EXPORT void PPrepend (NCollection_BaseList& theOther);
161
162 // ******** PRemoveFirst
163 // Purpose: Removes first node
164 Standard_EXPORT void PRemoveFirst
165 (NCollection_DelListNode fDel,
166 Handle(NCollection_BaseAllocator)& theAllocator);
167
168 // ******** PRemove
169 // Purpose: Removes the node pointed by theIter[ator]
170 Standard_EXPORT void PRemove
171 (Iterator& theIter,
172 NCollection_DelListNode fDel,
173 Handle(NCollection_BaseAllocator)& theAllocator);
174
175 // ******** PInsertBefore
176 // Purpose: Inserts theNode before one pointed by theIter[ator]
177 Standard_EXPORT void PInsertBefore (NCollection_ListNode* theNode,
178 Iterator& theIter);
179
180 // ******** PInsertBefore
181 // Purpose: Inserts theOther list before the node pointed by theIter[ator]
182 Standard_EXPORT void PInsertBefore (NCollection_BaseList& theOther,
183 Iterator& theIter);
184
185 // ******** PInsertAfter
186 // Purpose: Inserts theNode after one pointed by theIter[ator]
187 Standard_EXPORT void PInsertAfter (NCollection_ListNode* theNode,
188 Iterator& theIter);
189
190 // ******** PInsertAfter
191 // Purpose: Inserts theOther list after the node pointed by theIter[ator]
192 Standard_EXPORT void PInsertAfter (NCollection_BaseList& theOther,
193 Iterator& theIter);
194
195 // ******** PReverse
196 // Purpose: Reverse the list
197 Standard_EXPORT void PReverse ();
198
199 protected:
200 // ------------ PRIVATE FIELDS ------------
201 NCollection_ListNode * myFirst; // Pointer to the head
202 NCollection_ListNode * myLast; // Pointer to the tail
203 Standard_Integer myLength; // Actual length
204
205 // ------------ FRIEND CLASSES ------------
206 friend class Iterator;
207};
208
209#endif