blob: 06e616d99f35c2b27fe31f76239408923baf9c7a (
plain)
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
|
// File: NCollection_Stack.hxx
// Created: 17.04.02 10:12:48
// <a-kartomin@opencascade.com>
// Author: Alexander Kartomin (akm)
// Copyright: Open Cascade 2002
#ifndef NCollection_Stack_HeaderFile
#define NCollection_Stack_HeaderFile
#include <NCollection_BaseCollection.hxx>
#include <NCollection_BaseList.hxx>
#include <NCollection_TListNode.hxx>
#include <NCollection_TListIterator.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: A stack is a structure where item can be added and
* removed from the top. Like a stack of plates in a
* kitchen. The last entered item will be be the
* first removed. This is called a LIFO (last In First Out).
* Inherits BaseList, adding the data item to each node.
*/
template <class TheItemType> class NCollection_Stack
: public NCollection_BaseCollection<TheItemType>,
public NCollection_BaseList
{
public:
typedef NCollection_TListNode<TheItemType> StackNode;
typedef NCollection_TListIterator<TheItemType> Iterator;
public:
// ---------- PUBLIC METHODS ------------
//! Constructor
NCollection_Stack(const Handle(NCollection_BaseAllocator)& theAllocator=0L) :
NCollection_BaseCollection<TheItemType>(theAllocator),
NCollection_BaseList() {}
//! Copy constructor
NCollection_Stack (const NCollection_Stack& theOther) :
NCollection_BaseCollection<TheItemType>(theOther.myAllocator),
NCollection_BaseList()
{ *this = theOther; }
//! Size - Number of items
virtual Standard_Integer Size (void) const
{ return Extent(); }
//! Depth - Number of items
Standard_Integer Depth (void) const
{ return Extent(); }
//! 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();
for (; anIter.More(); anIter.Next())
{
StackNode* pNew = new (this->myAllocator) StackNode(anIter.Value());
PAppend(pNew);
}
}
//! Replace this list by the items of theOther Stack
NCollection_Stack& operator= (const NCollection_Stack& theOther)
{
if (this == &theOther)
return *this;
Clear ();
StackNode * pCur = (StackNode *) theOther.PFirst();
while (pCur)
{
StackNode* pNew = new (this->myAllocator) StackNode(pCur->Value());
PAppend(pNew);
pCur = (StackNode *) pCur->Next();
}
return *this;
}
//! Clear this stack
void Clear (void)
{ PClear (StackNode::delNode, this->myAllocator); }
//! Top item - constant
const TheItemType& Top (void) const
{
#if !defined No_Exception && !defined No_Standard_NoSuchObject
if (IsEmpty()) Standard_NoSuchObject::Raise ("NCollection_Stack::Top");
#endif
return ((StackNode *) PFirst())->Value();
}
//! Top item - variable
TheItemType& ChangeTop (void)
{
#if !defined No_Exception && !defined No_Standard_NoSuchObject
if (IsEmpty()) Standard_NoSuchObject::Raise("NCollection_Stack::ChangeTop");
#endif
return ((StackNode *) PFirst())->ChangeValue();
}
//! Push one item
void Push (const TheItemType& theItem)
{
StackNode * pNew = new (this->myAllocator) StackNode(theItem);
PPrepend(pNew);
}
//! Pop top item
void Pop (void)
{ PRemoveFirst (StackNode::delNode, this->myAllocator); }
//! Destructor - clears the List
~NCollection_Stack (void)
{ Clear(); }
private:
// ----------- PRIVATE METHODS -----------
//! Creates Iterator for use on BaseCollection
virtual TYPENAME NCollection_BaseCollection<TheItemType>::Iterator&
CreateIterator(void) const
{ return *(new (this->IterAllocator()) Iterator(*this)); }
};
#ifdef WNT
#pragma warning (default:4291)
#endif
#endif
|