Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

Stack.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/basic.h>
36 #include <ogdf/basic/memory.h>
37 
38 
39 namespace ogdf {
40 
41 
43 
51 template<class E> class StackPure
52 {
53  struct Element {
54  Element(Element *pNext, const E &x) : m_next(pNext), m_x(x) { }
55 
56  template<class ... Args>
57  Element(Element *pNext, Args && ... args) : m_next(pNext), m_x(std::forward<Args>(args)...) { }
58 
60  E m_x;
62  };
63 
65 
66 public:
68  StackPure() { m_head = nullptr; }
69 
71  StackPure(std::initializer_list<E> initList) : m_head(nullptr) {
72  for (const E &x : initList)
73  push(x);
74  }
75 
77  StackPure(const StackPure<E> &S) {
78  m_head = nullptr;
79  copy(S);
80  }
81 
83 
86  StackPure(StackPure<E> &&S) : m_head(S.m_head) {
87  S.m_head = nullptr;
88  }
89 
92  clear();
93  }
94 
96  bool empty() const { return m_head == nullptr; }
97 
99  const E &top() const {
100  return m_head->m_x;
101  }
102 
104  E &top() {
105  return m_head->m_x;
106  }
107 
110  clear();
111  copy(S);
112  return *this;
113  }
114 
116 
120  clear();
121  m_head = S.m_head;
122  S.m_head = nullptr;
123  return *this;
124  }
125 
127  void push(const E &x) {
128  m_head = new Element(m_head,x);
129  }
130 
132 
135  template<class ... Args>
136  void emplace(Args && ... args) {
137  m_head = new Element(m_head, std::forward<Args>(args)...);
138  }
139 
141  E pop() {
142  OGDF_ASSERT(m_head != nullptr);
143  Element *pX = m_head;
144  m_head = m_head->m_next;
145  E x = pX->m_x;
146  delete pX;
147  return x;
148  }
149 
151  void clear() {
152  while(m_head) {
153  Element *pX = m_head;
154  m_head = m_head->m_next;
155  delete pX;
156  }
157  }
158 
159  void print(ostream &os, char delim = ' ') const
160  {
161  Element *pX = m_head;
162  if (pX != nullptr) {
163  os << pX->m_x;
164  for(pX = pX->m_next; pX != nullptr; pX = pX->m_next)
165  os << delim << pX->m_x;
166  }
167  }
168 
169 private:
170  void copy(const StackPure<E> &S) {
171  Element **p = &m_head;
172 
173  for(Element *q = S.m_head; q != nullptr; q = q->m_next) {
174  *p = new Element(nullptr, q->m_x);
175  p = &(*p)->m_next;
176  }
177  }
178 
180 }; // class StackPure
181 
182 
184 
192 template<class E> class Stack : private StackPure<E>
193 {
194  size_t m_count;
195 
196 public:
197  using size_type = size_t;
198 
200  Stack() { m_count = 0; }
201 
203  Stack(std::initializer_list<E> initList) : StackPure<E>(initList) {
204  m_count = initList.size();
205  }
206 
208  Stack(const Stack<E> &S) : StackPure<E>(S) { m_count = S.m_count; }
209 
211 
214  Stack(Stack<E> &&S) : StackPure<E>(std::move(S)), m_count(S.m_count) {
215  S.m_count = 0;
216  }
217 
219  ~Stack() { }
220 
222  bool empty() const { return StackPure<E>::empty(); }
223 
225  size_t size() const { return m_count; }
226 
228  const E &top() const {
229  return StackPure<E>::top();
230  }
231 
233  E &top() {
234  return StackPure<E>::top();
235  }
236 
240  m_count = S.m_count;
241  return *this;
242  }
243 
245 
250  m_count = S.m_count;
251  S.m_count = 0;
252  return *this;
253  }
254 
256  void push(const E &x) {
257  ++m_count;
259  }
260 
262 
265  template<class ... Args>
266  void emplace(Args && ... args) {
267  ++m_count;
268  StackPure<E>::emplace(std::forward<Args>(args)...);
269  }
270 
272  E pop() {
273  --m_count;
274  return StackPure<E>::pop();
275  }
276 
278  void clear() {
280  m_count = 0;
281  }
282 
283  void print(ostream &os, char delim = ' ') const {
284  StackPure<E>::print(os,delim);
285  }
286 
288 }; // class Stack
289 
290 
291 
292 template<class E>
293 ostream &operator<<(ostream &os, const StackPure<E> &S)
294 {
295  S.print(os);
296  return os;
297 }
298 
299 
300 template<class E>
301 ostream &operator<<(ostream &os, const Stack<E> &S)
302 {
303  S.print(os);
304  return os;
305 }
306 
307 } // end namespace ogdf
void emplace(Args &&... args)
Adds a new element at as top-most element to the stack.
Definition: Stack.h:266
StackPure< E > & operator=(const StackPure< E > &S)
Assignment operator.
Definition: Stack.h:109
Element(Element *pNext, const E &x)
Definition: Stack.h:54
Declaration of memory manager for allocating small pieces of memory.
Stack(const Stack< E > &S)
Constructs a stack that is a copy of S.
Definition: Stack.h:208
void print(ostream &os, char delim=' ') const
Definition: Stack.h:283
StackPure(StackPure< E > &&S)
Constructs a stack containing the elements of S (move semantics).
Definition: Stack.h:86
StackPure< E > & operator=(StackPure< E > &&S)
Assignment operator (move semantics).
Definition: Stack.h:119
Element * m_next
Definition: Stack.h:59
void print(ostream &os, char delim=' ') const
Definition: Stack.h:159
const E & top() const
Returns a reference to the top element.
Definition: Stack.h:228
Stack(Stack< E > &&S)
Constructs a stack containing the elements of S (move semantics).
Definition: Stack.h:214
Stack()
Constructs an empty stack.
Definition: Stack.h:200
bool empty() const
Returns true iff the stack is empty.
Definition: Stack.h:222
void emplace(Args &&... args)
Adds a new element at as top-most element to the stack.
Definition: Stack.h:136
Stack< E > & operator=(Stack< E > &&S)
Assignment operator (move semantics).
Definition: Stack.h:248
size_t m_count
Definition: Stack.h:194
StackPure(std::initializer_list< E > initList)
Constructs a stack and pushes the elements in initList on it (last element will be on top)...
Definition: Stack.h:71
void push(const E &x)
Adds element x as top-most element to the stack.
Definition: Stack.h:127
StackPure(const StackPure< E > &S)
Constructs a stack that is a copy of S.
Definition: Stack.h:77
E pop()
Removes the top-most element from the stack and returns it.
Definition: Stack.h:272
const E & top() const
Returns a reference to the top element.
Definition: Stack.h:99
List-based stacks.
Definition: SList.h:39
void copy(const StackPure< E > &S)
Definition: Stack.h:170
size_t size() const
Returns the number of elements contained in the stack.
Definition: Stack.h:225
void push(const E &x)
Adds element x as top-most element to the stack.
Definition: Stack.h:256
Element(Element *pNext, Args &&... args)
Definition: Stack.h:57
Element * m_head
Definition: Stack.h:64
void clear()
Makes the stack empty.
Definition: Stack.h:151
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
E & top()
Returns a reference to the top element.
Definition: Stack.h:104
StackPure()
Constructs an empty stack.
Definition: Stack.h:68
const T & move(const T &v)
Definition: backward.hpp:243
E pop()
Removes the top-most element from the stack and returns it.
Definition: Stack.h:141
Basic declarations, included by all source files.
~Stack()
Destruction.
Definition: Stack.h:219
Stack< E > & operator=(const Stack< E > &S)
Assignment operator.
Definition: Stack.h:238
~StackPure()
Destruction.
Definition: Stack.h:91
void clear()
Makes the stack empty.
Definition: Stack.h:278
Stack(std::initializer_list< E > initList)
Constructs a stack and pushes the elements in initList on it (last element will be on top)...
Definition: Stack.h:203
The namespace for all OGDF objects.
bool empty() const
Returns true iff the stack is empty.
Definition: Stack.h:96
E & top()
Returns a reference to the top element.
Definition: Stack.h:233
size_t size_type
The number of elements in the list.
Definition: Stack.h:197
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
The parameterized class Stack<E> implements list-based stacks.
Definition: Stack.h:192