stack.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* ScummVM Tools
  2. *
  3. * ScummVM Tools is the legal property of its developers, whose
  4. * names are too numerous to list here. Please refer to the
  5. * COPYRIGHT file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #ifndef DEC_STACK_H
  22. #define DEC_STACK_H
  23. #include <deque>
  24. #include <iostream>
  25. /**
  26. * Stack class based on a deque.
  27. */
  28. template<typename T>
  29. class Stack {
  30. private:
  31. std::deque<T> _stack; ///< Container used for the stack.
  32. public:
  33. /**
  34. * Returns whether or not the stack is empty.
  35. *
  36. * @return true if the stack is empty, false if it is not.
  37. */
  38. bool empty() const { return _stack.empty(); }
  39. /**
  40. * Push an item onto the stack.
  41. *
  42. * @param item The item to push.
  43. */
  44. void push(const T &item) { _stack.push_front(item); }
  45. /**
  46. * Pop an item from the stack and return it.
  47. *
  48. * @return The value popped from the stack.
  49. */
  50. T pop() {
  51. T retval = _stack.front();
  52. _stack.pop_front();
  53. return retval;
  54. }
  55. /**
  56. * Return the topmost item on the stack without removing it.
  57. *
  58. * @return The topmost item on the stack.
  59. */
  60. T &peek() { return _stack.front(); }
  61. /**
  62. * Return the topmost item on the stack without removing it.
  63. *
  64. * @return The topmost item on the stack.
  65. */
  66. const T &peek() const { return _stack.front(); }
  67. /**
  68. * Return the item on the specificed stack position without removing it.
  69. *
  70. * @param pos The number of items to skip on the stack.
  71. * @return The desired item from the stack.
  72. */
  73. T &peekPos(size_t pos) {
  74. if (pos >= _stack.size())
  75. std::cerr << "WARNING: Looking outside stack\n";
  76. return _stack.at(pos);
  77. }
  78. /**
  79. * Return the item on the specificed stack position without removing it.
  80. *
  81. * @param pos The number of items to skip on the stack.
  82. * @return The desired item from the stack.
  83. */
  84. const T &peekPos(size_t pos) const {
  85. if (pos >= _stack.size())
  86. std::cerr << "WARNING: Looking outside stack\n";
  87. return _stack.at(pos);
  88. }
  89. };
  90. #endif