1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
// File: NCollection_SList.hxx
// Created: 17.04.02 10:12:48
// Author: Alexander Kartomin (akm)
// Copyright: Open Cascade 2002
#ifndef NCollection_SList_HeaderFile
#define NCollection_SList_HeaderFile
#include <NCollection_BaseCollection.hxx>
#if !defined No_Exception && !defined No_Standard_NoSuchObject
#include <Standard_NoSuchObject.hxx>
#endif
#ifdef WNT
// Disable the warning "operator new unmatched by delete"
#pragma warning (disable:4291)
#endif
/**
* Purpose: An SList is a LISP like list of Items.
* An SList is :
* . Empty.
* . Or it has a Value and a Tail which is an other SList.
*
* The Tail of an empty list is an empty list.
* SList are shared. It means that they can be
* modified through other lists.
* SList may be used as Iterators. They have Next,
* More, and value methods. To iterate on the content
* of the list S just do.
*
* SList Iterator;
* for (Iterator = S; Iterator.More(); Iterator.Next())
* X = Iterator.Value();
*
* Memory usage is automatically managed for SLists
* (using reference counts).
*
* Example:
* If S1 and S2 are SLists :
* if S1.Value() is X.
*
* And the following is done :
* S2 = S1;
* S2.SetValue(Y);
*
* S1.Value() becomes also Y. So SList must be used
* with care. Mainly the SetValue() method is
* dangerous.
*/
template <class TheItemType> class NCollection_SList
: public NCollection_BaseCollection<TheItemType>,
public NCollection_BaseCollection<TheItemType>::Iterator
{
public:
//! The node of SList
class SListNode
{
private:
//! Constructor
SListNode (const TheItemType& theItem,
const NCollection_SList& theTail) :
myCount(1),
myValue(theItem)
{ myTail = new (theTail.myAllocator) NCollection_SList(theTail); }
//! Tail
NCollection_SList& Tail (void)
{ return (*myTail); }
//! Value
TheItemType& Value (void)
{ return myValue; }
//! Clear
void Clear (void)
{
myTail->Clear();
myTail->myAllocator->Free(myTail);
}
//! Operator new for allocating nodes
void* operator new(size_t theSize,
const Handle(NCollection_BaseAllocator)& theAllocator)
{ return theAllocator->Allocate(theSize); }
//! news to avoid warnings on hiding - not for use
void* operator new(size_t theSize)
{ return Standard::Allocate(theSize); }
void* operator new(size_t /*theSize*/, void* theAddress)
{ return theAddress; }
private:
// ---------- PRIVATE FIELDS ------------
Standard_Integer myCount; //!< Reference count
NCollection_SList * myTail; //!< The tail
TheItemType myValue; //!< Datum
// Everything above is private. Only SList has an access
friend class NCollection_SList<TheItemType>;
}; // End of nested class SListNode
public:
// ---------- PUBLIC METHODS ------------
//! Empty constructor
NCollection_SList(const Handle(NCollection_BaseAllocator)& theAllocator=0L) :
NCollection_BaseCollection<TheItemType>(theAllocator),
myNode(NULL) {}
//! Constructor
NCollection_SList(const TheItemType& theItem,
const NCollection_SList& theTail) :
NCollection_BaseCollection<TheItemType>(theTail.myAllocator)
{ myNode = new (theTail.myAllocator) SListNode(theItem,theTail); }
//! Copy constructor
NCollection_SList (const NCollection_SList& theOther) :
NCollection_BaseCollection<TheItemType>(theOther.myAllocator)
{
myNode = theOther.myNode;
if (myNode)
myNode->myCount++;
}
//! Operator new for creating 'iterator'
void* operator new(size_t theSize,
const Handle(NCollection_BaseAllocator)& theAllocator)
{ return theAllocator->Allocate(theSize); }
//! Clear the items out
void Clear (void)
{
if (!myNode)
return;
myNode->myCount--;
if (myNode->myCount < 1)
{
myNode->Clear();
this->myAllocator->Free(myNode);
}
myNode = NULL;
}
//! Make this list identical to theOther
NCollection_SList& operator= (const NCollection_SList& theOther)
{
if (myNode != theOther.myNode)
{
if (theOther.myNode)
theOther.myNode->myCount++;
Clear();
this->myAllocator = theOther.myAllocator;
myNode = theOther.myNode;
}
return *this;
}
//! Replace this list by the items of theOther collection
virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther)
{
if (this == &theOther)
return;
Clear();
TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter =
theOther.CreateIterator();
if (!anIter.More())
return;
SListNode *aNode, *aPrevNode=NULL;
for (; anIter.More(); anIter.Next())
{
aNode = new (this->myAllocator) SListNode
(anIter.Value(), NCollection_SList(this->myAllocator));
if (IsEmpty())
myNode = aNode;
else
aPrevNode->Tail().myNode = aNode;
aPrevNode = aNode;
}
}
//! IsEmpty query
Standard_Boolean IsEmpty (void) const
{ return (myNode==NULL); }
//! Value - constant access
virtual const TheItemType& Value (void) const
{
#if !defined No_Exception && !defined No_Standard_NoSuchObject
if (IsEmpty())
Standard_NoSuchObject::Raise ("NCollection_SList::Value");
#endif
return myNode->Value();
}
//! ChangeValue - variable access
virtual TheItemType& ChangeValue (void) const
{
#if !defined No_Exception && !defined No_Standard_NoSuchObject
if (IsEmpty())
Standard_NoSuchObject::Raise ("NCollection_SList::ChangeValue");
#endif
return myNode->Value();
}
//! SetValue
void SetValue (const TheItemType& theItem)
{
#if !defined No_Exception && !defined No_Standard_NoSuchObject
if (IsEmpty())
Standard_NoSuchObject::Raise ("NCollection_SList::SetValue");
#endif
myNode->Value() = theItem;
}
//! Tail
const NCollection_SList& Tail (void) const
{
if (!IsEmpty())
return myNode->Tail();
else
return *this;
}
//! ChangeTail
NCollection_SList& ChangeTail (void)
{
if (!IsEmpty())
return myNode->Tail();
else
return *this;
}
//! SetTail
void SetTail (NCollection_SList& theList)
{
if (!IsEmpty())
myNode->Tail() = theList;
else
*this = theList;
}
//! Construct
void Construct(const TheItemType& theItem)
{ *this = NCollection_SList (theItem, *this); }
//! Constructed
NCollection_SList Constructed(const TheItemType& theItem) const
{ return NCollection_SList (theItem, *this); }
//! ToTail
void ToTail (void)
{ *this = Tail(); }
//! Initialize (~Assign)
void Initialize (const NCollection_SList& theOther)
{ *this = theOther; }
//! Init (virtual method of base iterator)
void Init (const NCollection_SList& theOther)
{ *this = theOther; }
//! More (~!IsEmpty)
virtual Standard_Boolean More (void) const
{ return !IsEmpty(); }
//! Next (~ToTail)
virtual void Next (void)
{ ToTail(); }
//! Size - Number of items
virtual Standard_Integer Size (void) const
{ return (IsEmpty() ? 0 : 1+myNode->Tail().Size()); }
//! Destructor - clears the SList
~NCollection_SList (void)
{ Clear(); }
private:
// ----------- PRIVATE METHODS -----------
//! Creates Iterator for use on BaseCollection
virtual TYPENAME NCollection_BaseCollection<TheItemType>::Iterator&
CreateIterator(void) const
{ return *(new (this->IterAllocator()) NCollection_SList(*this)); }
private:
// ---------- PRIVATE FIELDS ------------
SListNode* myNode;
friend class SListNode;
};
#ifdef WNT
#pragma warning (default:4291)
#endif
#endif
|