From 6bd6f20d2d2796335e624b77db2839e7dbab9d15 Mon Sep 17 00:00:00 2001 From: Calcitem Date: Mon, 7 Oct 2019 20:39:22 +0800 Subject: [PATCH] =?UTF-8?q?stack:=20=E5=A4=9A=E5=8A=A0=E5=87=A0=E4=B8=AA?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=A4=87=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/stack.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/src/base/stack.h b/src/base/stack.h index 1cd89bd6..3f5f667f 100644 --- a/src/base/stack.h +++ b/src/base/stack.h @@ -22,16 +22,56 @@ #ifndef STACK_H #define STACK_H -template +#include + +template class Stack { public: + Stack &operator= (const Stack &other) + { + memcpy(arr, other.arr, length()); + p = other.p; + return *this; + } + + bool operator== (const T &other) const + { + return (p == other.p && + memcmp(arr, other.arr, size())); + } + +#if 0 + T operator*(const Stack &obj) + { + return (obj.arr); + }; +#endif + + T &operator[](int i) + { + return arr[i]; + } + + const T &operator[](int i) const + { + return arr[i]; + } + inline void push(const T &obj) { p++; memcpy(arr + p, &obj, sizeof(T)); - assert(p < size); + assert(p < capacity); + } + + inline void push_back(const T &obj) + { + p++; + arr[p] = obj; + + assert(p < capacity); } inline void pop() @@ -44,9 +84,39 @@ public: return arr[p]; } + inline int size() + { + return p + 1; + } + + inline size_t length() + { + return (sizeof(T) * size()); + } + + inline T &begin() + { + return arr[0]; + } + + inline T &end() + { + return arr[p + 1]; + } + + inline bool empty() + { + return (p < 0); + } + + inline void clear() + { + p = -1; + } + private: int p { -1 }; - T arr[size]; + T arr[capacity]; }; #endif // STACK_H