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