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
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* 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
|
|
|
|
|
2003-04-24 15:52:01 +00:00
|
|
|
#include <deque>
|
2002-05-26 19:40:17 +00:00
|
|
|
|
|
|
|
/**
|
2003-05-06 09:34:56 +00:00
|
|
|
* limited_stack - A stack of limited size.
|
2002-05-26 19:40:17 +00:00
|
|
|
*
|
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:
|
2003-04-24 15:52:01 +00:00
|
|
|
typedef std::deque<T> container_type;
|
2002-05-26 19:40:17 +00:00
|
|
|
typedef typename container_type::value_type value_type;
|
|
|
|
typedef typename container_type::size_type size_type;
|
2003-06-17 15:33:49 +00:00
|
|
|
typedef typename container_type::const_iterator const_iterator;
|
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;
|
|
|
|
}
|
|
|
|
|
2003-05-06 09:34:56 +00:00
|
|
|
/// Return the top element.
|
2003-06-04 07:14:05 +00:00
|
|
|
value_type & top() {
|
2002-05-26 19:40:17 +00:00
|
|
|
return c_.front();
|
|
|
|
}
|
|
|
|
|
2003-05-06 09:34:56 +00:00
|
|
|
/// Pop and throw away the top element.
|
2002-05-26 19:40:17 +00:00
|
|
|
void pop() {
|
|
|
|
c_.pop_front();
|
|
|
|
}
|
2002-09-25 10:03:41 +00:00
|
|
|
|
2003-05-06 09:34:56 +00:00
|
|
|
/// Return true if the stack is empty.
|
2002-05-26 19:40:17 +00:00
|
|
|
bool empty() const {
|
2003-05-06 09:34:56 +00:00
|
|
|
return c_.empty();
|
2002-05-26 19:40:17 +00:00
|
|
|
}
|
|
|
|
|
2003-05-06 09:34:56 +00:00
|
|
|
/// Clear all elements, deleting them.
|
2002-05-26 19:40:17 +00:00
|
|
|
void clear() {
|
2003-05-06 09:34:56 +00:00
|
|
|
c_.clear();
|
2002-05-26 19:40:17 +00:00
|
|
|
}
|
2002-09-25 10:03:41 +00:00
|
|
|
|
2003-05-06 09:34:56 +00:00
|
|
|
/// Push an item on to the stack, deleting the
|
2002-05-26 19:40:17 +00:00
|
|
|
/// 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
|
|
|
|
2003-05-06 09:34:56 +00:00
|
|
|
/// Direct read access to intermediate elements.
|
2003-04-24 15:52:01 +00:00
|
|
|
T const & operator[](size_type pos) const {
|
|
|
|
return c_[pos];
|
|
|
|
}
|
|
|
|
|
2003-05-06 09:34:56 +00:00
|
|
|
/// Read access to used size.
|
2003-04-24 15:52:01 +00:00
|
|
|
size_type size() const {
|
|
|
|
return c_.size();
|
|
|
|
}
|
2003-06-17 15:33:49 +00:00
|
|
|
|
|
|
|
const_iterator begin() const {
|
|
|
|
return c_.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator end() const {
|
|
|
|
return c_.end();
|
|
|
|
}
|
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
private:
|
2003-05-06 09:34:56 +00:00
|
|
|
/// Internal contents.
|
2002-05-26 19:40:17 +00:00
|
|
|
container_type c_;
|
2003-05-06 09:34:56 +00:00
|
|
|
/// The maximum number elements stored.
|
2002-05-26 19:40:17 +00:00
|
|
|
size_type limit_;
|
|
|
|
};
|
|
|
|
|
2003-05-06 09:34:56 +00:00
|
|
|
// Make pointer type an error.
|
2002-05-26 19:40:17 +00:00
|
|
|
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
|