Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Stack(T)
- {
- private SList!T container = null;
- private Address pointer = 0;
- private size_t length = 0;
- private string name = null;
- this(string name)
- {
- this.name = name;
- }
- void push(T value)
- {
- this.container.insertFront(value);
- this.pointer++;
- this.length++;
- }
- Nullable!T topSafe()
- {
- Nullable!T front;
- if (this.pointer > 0 && this.length != 0)
- front = this.container.front();
- return front;
- }
- T top()
- {
- auto front_nullable = topSafe();
- if (front_nullable.isNull)
- throw new StackFlowError("The " ~ this.name ~ " stack underflew", this.pointer);
- return front_nullable.get;
- }
- T popTop()
- {
- auto front_nullable = topSafe();
- if (front_nullable.isNull)
- throw new StackFlowError("The " ~ this.name ~ " stack underflew", this.pointer);
- this.container.removeFront();
- this.pointer--;
- this.length--;
- return front_nullable.get;
- }
- Nullable!T nthSafe(size_t n)
- {
- size_t n_prime = 0;
- Nullable!T found;
- foreach (elt; this.container.opSlice())
- {
- if (n_prime++ == n)
- {
- found = elt;
- return found;
- }
- }
- return found;
- }
- T nth(size_t n)
- {
- auto nth_nullable = nthSafe(n);
- if (nth_nullable.isNull)
- throw new StackFlowError("The " ~ this.name ~ " stack overflew", this.pointer);
- return nth_nullable.get;
- }
- T popAtPointer()
- {
- auto nth = nth(this.pointer);
- this.pointer--;
- return nth;
- }
- Address getPointer()
- {
- return this.pointer;
- }
- void setPointer(Address pointer)
- {
- this.pointer = pointer;
- }
- Address getLength()
- {
- return this.length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement