7fd59977 |
1 | // File: NCollection_Stack.hxx |
2 | // Created: 17.04.02 10:12:48 |
3 | // <a-kartomin@opencascade.com> |
4 | // Author: Alexander Kartomin (akm) |
5 | // Copyright: Open Cascade 2002 |
6 | |
7 | #ifndef NCollection_Stack_HeaderFile |
8 | #define NCollection_Stack_HeaderFile |
9 | |
10 | #include <NCollection_BaseCollection.hxx> |
11 | #include <NCollection_BaseList.hxx> |
12 | #include <NCollection_TListNode.hxx> |
13 | #include <NCollection_TListIterator.hxx> |
14 | #if !defined No_Exception && !defined No_Standard_NoSuchObject |
15 | #include <Standard_NoSuchObject.hxx> |
16 | #endif |
17 | |
7fd59977 |
18 | /** |
19 | * Purpose: A stack is a structure where item can be added and |
20 | * removed from the top. Like a stack of plates in a |
21 | * kitchen. The last entered item will be be the |
22 | * first removed. This is called a LIFO (last In First Out). |
23 | * Inherits BaseList, adding the data item to each node. |
24 | */ |
25 | template <class TheItemType> class NCollection_Stack |
26 | : public NCollection_BaseCollection<TheItemType>, |
27 | public NCollection_BaseList |
28 | { |
29 | public: |
30 | typedef NCollection_TListNode<TheItemType> StackNode; |
31 | typedef NCollection_TListIterator<TheItemType> Iterator; |
32 | |
33 | public: |
34 | // ---------- PUBLIC METHODS ------------ |
35 | |
36 | //! Constructor |
37 | NCollection_Stack(const Handle(NCollection_BaseAllocator)& theAllocator=0L) : |
38 | NCollection_BaseCollection<TheItemType>(theAllocator), |
39 | NCollection_BaseList() {} |
40 | |
41 | //! Copy constructor |
42 | NCollection_Stack (const NCollection_Stack& theOther) : |
43 | NCollection_BaseCollection<TheItemType>(theOther.myAllocator), |
44 | NCollection_BaseList() |
45 | { *this = theOther; } |
46 | |
47 | //! Size - Number of items |
48 | virtual Standard_Integer Size (void) const |
49 | { return Extent(); } |
50 | |
51 | //! Depth - Number of items |
52 | Standard_Integer Depth (void) const |
53 | { return Extent(); } |
54 | |
55 | //! Replace this list by the items of theOther collection |
56 | virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther) |
57 | { |
58 | if (this == &theOther) |
59 | return; |
60 | Clear(); |
61 | TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter = |
62 | theOther.CreateIterator(); |
63 | for (; anIter.More(); anIter.Next()) |
64 | { |
65 | StackNode* pNew = new (this->myAllocator) StackNode(anIter.Value()); |
66 | PAppend(pNew); |
67 | } |
68 | } |
69 | |
70 | //! Replace this list by the items of theOther Stack |
71 | NCollection_Stack& operator= (const NCollection_Stack& theOther) |
72 | { |
73 | if (this == &theOther) |
74 | return *this; |
75 | Clear (); |
76 | StackNode * pCur = (StackNode *) theOther.PFirst(); |
77 | while (pCur) |
78 | { |
79 | StackNode* pNew = new (this->myAllocator) StackNode(pCur->Value()); |
80 | PAppend(pNew); |
81 | pCur = (StackNode *) pCur->Next(); |
82 | } |
83 | return *this; |
84 | } |
85 | |
86 | //! Clear this stack |
87 | void Clear (void) |
88 | { PClear (StackNode::delNode, this->myAllocator); } |
89 | |
90 | //! Top item - constant |
91 | const TheItemType& Top (void) const |
92 | { |
93 | #if !defined No_Exception && !defined No_Standard_NoSuchObject |
94 | if (IsEmpty()) Standard_NoSuchObject::Raise ("NCollection_Stack::Top"); |
95 | #endif |
96 | return ((StackNode *) PFirst())->Value(); |
97 | } |
98 | |
99 | //! Top item - variable |
100 | TheItemType& ChangeTop (void) |
101 | { |
102 | #if !defined No_Exception && !defined No_Standard_NoSuchObject |
103 | if (IsEmpty()) Standard_NoSuchObject::Raise("NCollection_Stack::ChangeTop"); |
104 | #endif |
105 | return ((StackNode *) PFirst())->ChangeValue(); |
106 | } |
107 | |
108 | //! Push one item |
109 | void Push (const TheItemType& theItem) |
110 | { |
111 | StackNode * pNew = new (this->myAllocator) StackNode(theItem); |
112 | PPrepend(pNew); |
113 | } |
114 | |
115 | //! Pop top item |
116 | void Pop (void) |
117 | { PRemoveFirst (StackNode::delNode, this->myAllocator); } |
118 | |
119 | //! Destructor - clears the List |
120 | ~NCollection_Stack (void) |
121 | { Clear(); } |
122 | |
123 | private: |
124 | // ----------- PRIVATE METHODS ----------- |
125 | |
126 | //! Creates Iterator for use on BaseCollection |
127 | virtual TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& |
128 | CreateIterator(void) const |
129 | { return *(new (this->IterAllocator()) Iterator(*this)); } |
130 | |
131 | }; |
132 | |
7fd59977 |
133 | #endif |