2002-05-26 19:40:17 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
|
|
|
* \file limited_stack.h
|
2002-09-25 10:03:41 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-05-26 19:40:17 +00:00
|
|
|
*
|
2002-09-25 10:03:41 +00:00
|
|
|
* \author John Levon
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
2002-05-26 19:40:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LIMITED_STACK_H
|
|
|
|
#define LIMITED_STACK_H
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* limited_stack - a stack of limited size
|
|
|
|
*
|
2002-07-22 02:19:53 +00:00
|
|
|
* Like a normal stack, but elements falling out
|
|
|
|
* of the bottom are destructed.
|
2002-05-26 19:40:17 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class limited_stack {
|
|
|
|
public:
|
|
|
|
typedef std::list<T> container_type;
|
|
|
|
typedef typename container_type::value_type value_type;
|
|
|
|
typedef typename container_type::size_type size_type;
|
2002-09-25 10:03:41 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
/// limit is the maximum size of the stack
|
2002-08-08 15:42:54 +00:00
|
|
|
limited_stack(size_type limit = 100) {
|
2002-05-26 19:40:17 +00:00
|
|
|
limit_ = limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return the top element
|
|
|
|
value_type top() {
|
|
|
|
return c_.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// pop and throw away the top element
|
|
|
|
void pop() {
|
|
|
|
c_.pop_front();
|
|
|
|
}
|
2002-09-25 10:03:41 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
/// return true if the stack is empty
|
|
|
|
bool empty() const {
|
|
|
|
return c_.size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// clear all elements, deleting them
|
|
|
|
void clear() {
|
|
|
|
while (!c_.empty()) {
|
|
|
|
c_.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2002-09-25 10:03:41 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
/// push an item on to the stack, deleting the
|
|
|
|
/// bottom item on overflow.
|
|
|
|
void push(value_type const & v) {
|
|
|
|
c_.push_front(v);
|
|
|
|
if (c_.size() > limit_) {
|
2002-09-25 10:03:41 +00:00
|
|
|
c_.pop_back();
|
2002-05-26 19:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
2002-09-25 10:03:41 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
private:
|
|
|
|
/// internal contents
|
|
|
|
container_type c_;
|
|
|
|
/// the maximum number elements stored
|
|
|
|
size_type limit_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// make pointer type an error.
|
|
|
|
template <typename T>
|
|
|
|
class limited_stack<T*>;
|
2002-09-25 10:03:41 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
#endif // LIMITED_STACK_H
|