Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

BoundedStack.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/exceptions.h>
35 
36 namespace ogdf {
37 
39 
46 template<class E, class INDEX = int> class BoundedStack {
47 
48  E *m_pTop;
49  E *m_pStart;
50  E *m_pStop;
51 
52 public:
54 
59  m_pTop = m_pStart = m_pStop = nullptr;
60  }
61 
63  explicit BoundedStack(INDEX n) {
64  OGDF_ASSERT(n >= 1);
65  m_pStart = new E[n];
66  if (m_pStart == nullptr) OGDF_THROW(InsufficientMemoryException);
67  m_pTop = m_pStart - 1;
68  m_pStop = m_pStart+n;
69  }
70 
73  copy(S);
74  }
75 
77 
82  m_pTop = S.m_pTop;
83  m_pStart = S.m_pStart;
84  m_pStop = S.m_pStop;
85  S.m_pTop = S.m_pStart = S.m_pStop = nullptr;
86  }
87 
90  delete[] m_pStart;
91  }
92 
94  const E &top() const {
95  OGDF_ASSERT(m_pTop != m_pStart-1);
96  return *m_pTop;
97  }
98 
100  E &top() {
101  OGDF_ASSERT(m_pTop != m_pStart-1);
102  return *m_pTop;
103  }
104 
106  INDEX size() const { return (m_pStart != nullptr) ? (INDEX)(m_pTop+1 - m_pStart) : 0; }
107 
109  bool empty() { return m_pTop == (m_pStart-1); }
110 
112  bool full() { return m_pTop == (m_pStop-1); }
113 
115  bool valid() const { return m_pStart != nullptr; }
116 
118  INDEX capacity() const { return (INDEX)(m_pStop - m_pStart); }
119 
121  void init() {
122  delete[] m_pStart;
123  m_pTop = m_pStart = m_pStart = nullptr;
124  }
125 
127  void init(INDEX n) {
128  OGDF_ASSERT(n >= 1);
129 
130  delete[] m_pStart;
131 
132  m_pStart = new E[n];
133  if (m_pStart == nullptr) OGDF_THROW(InsufficientMemoryException);
134  m_pTop = m_pStart - 1;
135  m_pStop = m_pStart+n;
136  }
137 
140  delete[] m_pStart;
141  copy(S);
142  return *this;
143  }
144 
146 
151  delete[] m_pStart;
152 
153  m_pTop = S.m_pTop;
154  m_pStart = S.m_pStart;
155  m_pStop = S.m_pStop;
156  S.m_pTop = S.m_pStart = S.m_pStop = nullptr;
157 
158  return *this;
159  }
160 
162  void push(const E &x) {
163  OGDF_ASSERT(m_pTop != m_pStop-1);
164  *++m_pTop = x;
165  }
166 
168  E pop() {
169  OGDF_ASSERT(m_pTop != (m_pStart-1));
170  return *m_pTop--;
171  }
172 
174  void clear() { m_pTop = m_pStart-1; }
175 
177  void print(ostream &os, char delim = ' ') const
178  {
179  if(m_pStart != nullptr) {
180  for (const E *pX = m_pStart; pX <= m_pTop; ++pX)
181  os << *pX << delim;
182  }
183  }
184 
185 private:
186  void copy(const BoundedStack<E> &S)
187  {
188  if(!S.valid()) {
189  m_pTop = m_pStart = m_pStop = nullptr;
190  } else {
191  INDEX sz = S.m_pStop - S.m_pStart;
192  m_pStart = new E[sz+1];
193  if (m_pStart == nullptr) OGDF_THROW(InsufficientMemoryException);
194  m_pStop = m_pStart + sz;
195  m_pTop = m_pStart-1;
196  for (E *pX = S.m_pStart-1; pX != S.m_pTop; )
197  *++m_pTop = *++pX;
198  }
199  }
200 }; // class BoundedStack
201 
202 
203 
204 // output operator
205 template<class E, class INDEX>
206 ostream &operator<<(ostream &os, const BoundedStack<E,INDEX> &S)
207 {
208  S.print(os);
209  return os;
210 }
211 
212 } // end namespace ogdf
BoundedStack(BoundedStack< E > &&S)
Constructs a bounded stack containing the elements of S (move semantics).
Definition: BoundedStack.h:81
void clear()
Makes the stack empty.
Definition: BoundedStack.h:174
BoundedStack< E > & operator=(const BoundedStack< E > &S)
Assignment operator.
Definition: BoundedStack.h:139
E & top()
Returns top element.
Definition: BoundedStack.h:100
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:60
E * m_pTop
Pointer to top element.
Definition: BoundedStack.h:48
bool full()
Returns true iff the stack is full.
Definition: BoundedStack.h:112
~BoundedStack()
Destruction.
Definition: BoundedStack.h:89
BoundedStack(INDEX n)
Constructs an empty bounded stack for at most n elements.
Definition: BoundedStack.h:63
Definition of exception classes.
void init()
Reinitializes the stack for no elements at all (actually frees memory).
Definition: BoundedStack.h:121
The parameterized class BoundedStack implements stacks with bounded size.
Definition: BoundedStack.h:46
BoundedStack< E > & operator=(BoundedStack< E > &&S)
Assignment operator (move semantics).
Definition: BoundedStack.h:150
E * m_pStart
Pointer to first element.
Definition: BoundedStack.h:49
Exception thrown when not enough memory is available to execute an algorithm.
Definition: exceptions.h:235
void copy(const BoundedStack< E > &S)
Definition: BoundedStack.h:186
BoundedStack()
Constructs an empty bounded stack for no elements at all.
Definition: BoundedStack.h:58
const E & top() const
Returns top element.
Definition: BoundedStack.h:94
void init(INDEX n)
Reinitializes the stack for n elements.
Definition: BoundedStack.h:127
INDEX size() const
Returns current size of the stack.
Definition: BoundedStack.h:106
E pop()
Removes the top-most element from the stack and returns it.
Definition: BoundedStack.h:168
void print(ostream &os, char delim=' ') const
Prints the stack to output stream os with the seperator delim.
Definition: BoundedStack.h:177
INDEX capacity() const
Returns the capacity of the bounded stack.
Definition: BoundedStack.h:118
bool valid() const
Returns true iff the stack was initialized.
Definition: BoundedStack.h:115
The namespace for all OGDF objects.
BoundedStack(const BoundedStack< E > &S)
Constructs a bounded stack that is a copy of S.
Definition: BoundedStack.h:72
E * m_pStop
Pointer to one past last element.
Definition: BoundedStack.h:50
bool empty()
Returns true iff the stack is empty.
Definition: BoundedStack.h:109
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
void push(const E &x)
Adds element x as top-most element to the stack.
Definition: BoundedStack.h:162