#pragma once #include "allocators.h" namespace tp { extern ModuleManifest gModuleContainer; template class Stack { public: template class Node { public: Node* below; NodeType data; public: Node(NodeType data, Node* below) { this->below = below; this->data = data; } }; template struct Iterator { Node* mIter; IterType& operator->() { return mIter->data; } const Iterator& operator*() { return *this; } inline void operator++() { mIter = mIter->below; } bool operator==(alni end) { return mIter == NULL; } }; alni length; Node* top; Allocator alloc; Stack() { MODULE_SANITY_CHECK(gModuleContainer); length = 0; top = nullptr; } ~Stack() { free(); } // pushes the node on top of the stack void push(Type data) { Node* NewNode = new (alloc) Node(data, top); top = NewNode; length++; } // pops the top node of the stack void pop() { assert(top); Node* del = top; top = top->below; delete del; length--; } Node* last() { return top; } // deletes all nodes void free() { Node* del = top; for (alni i = 0; i < length; i++) { Node* below = del->below; delete del; del = below; } length = 0; top = NULL; } alni sizeAllocatedMem() { alni out = 0; out += sizeof(Node*); out += sizeof(alni); out += sizeof(Node) * length; return out; } alni sizeUsedMem() { return sizeAllocatedMem(); } Iterator begin() const { return {top}; } alni end() const { return NULL; } }; template class ConstSizeStack { Type mBuff[tSize]; Index mLoadIdx = 0; public: ConstSizeStack() {} Index loadLen() const { return mLoadIdx; } void push(const Type& in) { assert(mLoadIdx >= 0 && mLoadIdx < tSize && "out of range"); mBuff[mLoadIdx] = in; mLoadIdx++; } void pop() { mLoadIdx--; } Type& last() { assert(mLoadIdx >= 0 && mLoadIdx < tSize && "out of range"); return mBuff[mLoadIdx - 1]; } const Type& last() const { assert(mLoadIdx >= 0 && mLoadIdx < tSize && "out of range"); return mBuff[mLoadIdx - 1]; } Type* buff() { return mBuff; } Type& operator[] (Index idx) { assert(idx >= 0 && idx < tSize && "out of range"); return mBuff[idx]; } const Type* buff() const { return mBuff; } const Type& operator[] (Index idx) const { assert(idx >= 0 && idx < tSize && "out of range"); return mBuff[idx]; } constexpr Index len() { return tSize; } }; };