1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
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 Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef YY_CY_STACK_HH_INCLUDED
23 #define YY_CY_STACK_HH_INCLUDED
28 template <class Type_>
31 typedef std::vector<Type_> Data_;
32 typedef typename Data_::const_reverse_iterator const_iterator;
42 _finline Type_ &operator [](size_t i) {
43 return data_[data_.size() - 1 - i];
46 _finline const Type_ &operator [](size_t i) const {
47 return data_[data_.size() - 1 - i];
50 _finline void push(Type_ &t) {
58 _finline void pop(unsigned int size) {
59 for (; size != 0; --size)
67 _finline size_t size() const {
71 _finline const_iterator begin() const {
72 return data_.rbegin();
75 _finline const_iterator end() const {
81 stack &operator =(const stack &);
84 template <class Type_>
87 typedef std::reverse_iterator<Type_ *> const_iterator;
95 for (Type_ *i(begin_); i != end_; ++i)
100 size_t capacity(capacity_ - begin_);
106 Type_ *data(static_cast<Type_ *>(::operator new(sizeof(Type_) * capacity)));
108 size_t size(end_ - begin_);
109 for (size_t i(0); i != size; ++i) {
110 Type_ &old(begin_[i]);
111 new (data + i) Type_(old);
115 ::operator delete(begin_);
119 capacity_ = data + capacity;
133 ::operator delete(begin_);
136 _finline Type_ &operator [](size_t i) {
140 _finline const Type_ &operator [](size_t i) const {
144 _finline void push(Type_ &t) {
145 if (end_ == capacity_)
147 new (end_++) Type_(t);
150 _finline void pop() {
154 _finline void pop(unsigned int size) {
155 for (; size != 0; --size)
164 _finline size_t size() const {
165 return end_ - begin_;
168 _finline const_iterator begin() const {
169 return const_iterator(end_);
172 _finline const_iterator end() const {
173 return const_iterator(begin_);
177 stack(const stack &);
178 stack &operator =(const stack &);
182 template <class Type_, class Stack_ = stack<Type_> >
185 slice(const Stack_ &stack, unsigned int range) :
191 _finline const Type_ &operator [](unsigned int i) const {
192 return stack_[range_ - i];
196 const Stack_ &stack_;
202 #endif/*YY_CY_STACK_HH_INCLUDED*/