X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/ddeb187697cfab9802f10cc09b9901a50afe88e9..0846ea04c5e644dba19e4f2555e4518540ec4e01:/Stack.hpp diff --git a/Stack.hpp b/Stack.hpp index 4a3fc6e..f249109 100644 --- a/Stack.hpp +++ b/Stack.hpp @@ -1,5 +1,5 @@ -/* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2015 Jay Freeman (saurik) +/* Cycript - The Truly Universal Scripting Language + * Copyright (C) 2009-2016 Jay Freeman (saurik) */ /* GNU Affero General Public License, Version 3 {{{ */ @@ -24,68 +24,131 @@ namespace cy { +#if 0 +template +class stack { + public: + typedef std::vector Data_; + typedef typename Data_::const_reverse_iterator const_iterator; + + private: + Data_ data_; + + public: + stack() { + data_.reserve(200); + } + + _finline Type_ &operator [](size_t i) { + return data_[data_.size() - 1 - i]; + } + + _finline const Type_ &operator [](size_t i) const { + return data_[data_.size() - 1 - i]; + } + + _finline void push(Type_ &t) { + data_.push_back(t); + } + + _finline void pop() { + data_.pop_back(); + } + + _finline void pop(unsigned int size) { + for (; size != 0; --size) + pop(); + } + + void clear() { + data_.clear(); + } + + _finline size_t size() const { + return data_.size(); + } + + _finline const_iterator begin() const { + return data_.rbegin(); + } + + _finline const_iterator end() const { + return data_.rend(); + } + + private: + stack(const stack &); + stack &operator =(const stack &); +}; +#else template class stack { public: typedef std::reverse_iterator const_iterator; private: - Type_ *data_; - size_t size_; - size_t capacity_; + Type_ *begin_; + Type_ *end_; + Type_ *capacity_; void destroy() { - data_ -= size_; - for (size_t i(0); i != size_; ++i) - data_[i].~Type_(); + for (Type_ *i(begin_); i != end_; ++i) + i->~Type_(); } - void reserve(size_t capacity) { - capacity_ = capacity; - Type_ *data(static_cast(::operator new(sizeof(Type_) * capacity_))); + void reserve() { + size_t capacity(capacity_ - begin_); + if (capacity == 0) + capacity = 200; + else + capacity *= 2; - data_ -= size_; - for (size_t i(0); i != size_; ++i) { - Type_ &old(data_[i]); + Type_ *data(static_cast(::operator new(sizeof(Type_) * capacity))); + + size_t size(end_ - begin_); + for (size_t i(0); i != size; ++i) { + Type_ &old(begin_[i]); new (data + i) Type_(old); old.~Type_(); } - ::operator delete(data_); - data_ = data + size_; + ::operator delete(begin_); + + begin_ = data; + end_ = data + size; + capacity_ = data + capacity; } public: stack() : - data_(NULL), - size_(0) + begin_(NULL), + end_(NULL), + capacity_(NULL) { - reserve(200); + reserve(); } ~stack() { destroy(); - ::operator delete(data_); + ::operator delete(begin_); } _finline Type_ &operator [](size_t i) { - return data_[-1 - i]; + return end_[-1 - i]; } _finline const Type_ &operator [](size_t i) const { - return data_[-1 - i]; + return end_[-1 - i]; } _finline void push(Type_ &t) { - if (size_ == capacity_) - reserve(capacity_ * 2); - new (data_++) Type_(t); - ++size_; + if (end_ == capacity_) + reserve(); + new (end_++) Type_(t); } _finline void pop() { - (--data_)->~Type_(); - --size_; + (--end_)->~Type_(); } _finline void pop(unsigned int size) { @@ -95,25 +158,26 @@ class stack { void clear() { destroy(); - size_ = 0; + end_ = begin_; } _finline size_t size() const { - return size_; + return end_ - begin_; } _finline const_iterator begin() const { - return const_iterator(data_); + return const_iterator(end_); } _finline const_iterator end() const { - return const_iterator(data_ - size_); + return const_iterator(begin_); } private: stack(const stack &); stack &operator =(const stack &); }; +#endif template > class slice {