Abstract Datastructure Standard Module


The Abstract Datastructures module contains several classes that implement datastructures like a stack class. All the source code of the classes is written using the Trinc-Prolog object-oriented extensions.

Defined classes:

The stack class

The stack class can be used for storing all kinds of elements, all the elements are stored in FIFO order, First-In-First-Out. This means that the last element pushed onto the stack is the first to be popped from the stack. An example of using the stack class is:

    X new_obj stack.
    stack(A, S)<-init.
    stack(A, S)<-push(a).
    stack(A, S)<-push([b,s]).
    stack(A, S)<-pop(T). %The element retrieved will be [b,s]
    del_obj stack.

The interface of the stack class is:

init/0

func Initialise the stack, after the stack has been initialised elements can be pushed on and popped of the stack.
pre TRUE
post The list of the stack has become empty and the element counter has been set to 0.

init(List, Contains)/2

func Initialise the stack, the initial elements of the stack and the element counter can be set.
pre The List variable must contain a list, an empty list is possible and the variable Contains must contain an integer value.
post The list and element counter attributes of the stack instance were assigned the values of the parameters.

push(Element)/1

func Push a single element onto the stack.
pre The stack instance must have been initialised.
post The element was pushed onto the stack and the element counter was incremented.

pop(Output)/1

func Pop a single element from the stack.
pre The stack instance must have been initialised.
post An element was popped from the stack and the element counter was decremented. If the stack instance was empty before this method was called the method fails, else it is exited.

contains(Output)/1

func Get the number of items on the stack.
pre The stack instance must have been initialised.
post The number of items on the stack was assigned to the variable Output.

list(Output)/1

func Get the list of elements of the stack.
pre The stack instance must have been initialised.
post The element list of the stack was assigned to the variable Output.

The queue class

As with the stack class the queue class can also be used for storing all kinds of elements, the elements are inserted at the back of the queue and elements are removed from the front of the queue (LIFO, Last-In First-Out).
Because instances of the queue class must save their state after the proving of a goal the current implementation does not use an advanced storage technique like difference lists. An example of using the queue class is:

    X new_obj queue.
    queue(A, S)<-init.
    queue(A, S)<-enter(a).
    queue(A, S)<-enter([b,s]).
    queue(A, S)<-serve(T).
    del_obj queue.

The interface of the queue class is:

init/0

func Initialise the queue, after the queue has been initialised elements can be inserted and removed from the queue.
pre TRUE
post The list of the queue has become empty and the element counter has been set to 0.

init(List, Contains)/2

func Initialise the queue, the initial elements of the queue and the element counter can be set.
pre The List variable must contain a list, an empty list is possible and the variable Contains must contain an integer value.
post The list and element counter attributes of the queue instance were assigned the values of the parameters.

enter(Element)/1

func Insert an element at the back of the queue.
pre The queue instance must have been initialised.
post The element was inserted at the back of the queue and the element counter was incremented.

serve(Element)/1

func Remove an element from the front of the queue.
pre The queue instance must have been initialised.
post The element was removed from the front of the queue and the element counter was decremented. If the queue was empty before this method was called the methods fails.

contains(Output)/1

func Get the number of items in the queue.
pre The queue instance must have been initialised.
post The number of items on the queue was assigned to the variable Output.

list(Output)/1

func Get the list of elements of the queue.
pre The queue instance must have been initialised.
post The element list of the queue was assigned to the variable Output.

[top] [up]

 

info@trinc-prolog.com