]> git.saurik.com Git - cycript.git/blobdiff - Stack.hpp
Support typedef and @encode with a void specifier.
[cycript.git] / Stack.hpp
index 4a3fc6e63bd08c4cdeffe9bb898010071b727ed1..f249109cf7c1a760344a3185e3870ac532166c65 100644 (file)
--- 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 {{{ */
 
 namespace cy {
 
+#if 0
+template <class Type_>
+class stack {
+  public:
+    typedef std::vector<Type_> 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 Type_>
 class stack {
   public:
     typedef std::reverse_iterator<Type_ *> 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<Type_ *>(::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<Type_ *>(::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 Type_, class Stack_ = stack<Type_> >
 class slice {