1 /* 2 * Copyright 2006-2008 The FLWOR Foundation. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 #ifndef ZORBA_UTILS_STACK 18 #define ZORBA_UTILS_STACK 19 20 #include <vector> 21 22 #include <zorba/config.h> 23 #include "zorbamisc/config/platform.h" 24 25 26 namespace zorba { 27 28 template <class T> 29 class ZORBA_DLL_PUBLIC Stack 30 { 31 protected: 32 ulong theTop; 33 std::vector<T> theStack; 34 35 public: 36 Stack(ulong initSize = 0) : theTop(0) 37 { 38 if (initSize > 0) 39 theStack.reserve(initSize); 40 } 41 ~Stack()42 ~Stack() { clear(); } 43 empty()44 bool empty() const 45 { 46 return theTop == 0; 47 } 48 size()49 ulong size() const 50 { 51 return theTop; 52 } 53 clear()54 void clear() 55 { 56 theTop = 0; 57 theStack.clear(); 58 } 59 top()60 const T& top() const 61 { 62 assert(!empty()); 63 return theStack[theTop-1]; 64 } 65 top()66 T& top() 67 { 68 assert(!empty()); 69 return theStack[theTop-1]; 70 } 71 72 void pop(ulong num = 1) 73 { 74 assert(theTop >= num); 75 theTop -= num; 76 //theStack.resize(theTop); 77 } 78 push(const T & val)79 void push(const T& val) 80 { 81 if (theTop < theStack.size()) 82 theStack[theTop] = val; 83 else 84 theStack.push_back(val); 85 86 theTop++; 87 } 88 89 const T& operator[](ulong i) const 90 { 91 assert(i < theTop); 92 return theStack[i]; 93 } 94 }; 95 96 97 } 98 #endif 99 /* vim:set et sw=2 ts=2: */ 100