std::collections::stack (v0.2.2)

Last-in first-out container.

Latest

Overview

std::collections::stack is a generic LIFO container backed by contiguous storage.

push adds to the top, pop removes from the top, and peek reads the top without removing it. dropStack releases the storage.

Source: thrustc/std/v0.2.2/collections/stack.thrust

Public Signatures

Exported declarations for this module snapshot.

struct Stack [T] @public {
fn newStack [T] () Stack[T] @public;
fn withCapacity [T] (capacity: usize) Stack[T] @public;
fn dropStack [T] (stack: ptr[Stack[T]]) void @public @deallocator;
fn getLength [T] (stack: ptr[Stack[T]]) usize @public;
fn getCapacity [T] (stack: ptr[Stack[T]]) usize @public;
fn isEmpty [T] (stack: ptr[Stack[T]]) bool @public;
fn push [T] (stack: ptr[Stack[T]], value: T) void @public;
fn pop [T] (stack: ptr[Stack[T]]) T @public;
fn peek [T] (stack: ptr[Stack[T]]) T @public;
fn clear [T] (stack: ptr[Stack[T]]) void @public;
fn reserveCapacity [T] (stack: ptr[Stack[T]], capacity: usize) void @public;
fn shrinkToFit [T] (stack: ptr[Stack[T]]) void @public;

Behavior and Use

push adds an element to the top; pop removes and returns the top element; peek reads it without removal.

Call dropStack when the stack is no longer needed to release the backing storage.

Examples

import std::collections::stack;
import std::io;

// Peek at the most recent action in an undo stack.
fn main() s32 @public {
    var undo := stack::newStack[s32]();

    stack::push[s32](ref undo, 5);
    stack::push[s32](ref undo, 6);

    var top: s32 = stack::peek[s32](ref undo);

    io::print("last action: %d\n", top);

    stack::dropStack[s32](ref undo);

    return 0;
}
import std::collections::stack;
import std::io;

// Undo the last action and report what was removed.
fn main() s32 @public {
    var undo := stack::newStack[s32]();

    stack::push[s32](ref undo, 5);
    stack::push[s32](ref undo, 6);

    var removed: s32 = stack::pop[s32](ref undo);

    io::print("removed: %d\n", removed);

    stack::dropStack[s32](ref undo);

    return 0;
}
import std::collections::stack;
import std::io;

// Report how many actions are pending before clearing the stack.
fn main() s32 @public {
    var undo := stack::newStack[s32]();

    stack::push[s32](ref undo, 1);

    io::print("pending: %d\n", stack::getLength[s32](ref undo));

    stack::dropStack[s32](ref undo);

    return 0;
}

Notes

Back to std index