#!/bin/bash # If this file has already been sourced, just return test $(VARIABLES_QUEUE_SH+true) && return declare -g VARIABLES_QUEUE_SH=true source ${BASH_SOURCE%/*}/common.sh source ${BASH_SOURCE%/*}/logger.sh source ${BASH_SOURCE%/*}/variables.sh source ${BASH_SOURCE%/*}/variables.atom.sh source ${BASH_SOURCE%/*}/variables.arraylist.sh variable::type::define ArrayQueue ArrayList # == QUEUE == # # First In / First Out # # Queue commands act on a list data structure # proc variable::ArrayQueue::new { variable::new ArrayQueue $(@) } # # Adds an item to the queue # proc variable::ArrayQueue::enqueue { if [[ ${VARIABLES_DEBUG} == 1 ]] { stderr "variable::ArrayQueue::enqueue $(@)" ; } declare token="$(1)" if ! variable::type::instanceOf $(token) ArrayQueue { stderr "Variable [$(token)] is not of type ArrayQueue (actual type [$(RESULT)])" exit 1 } variable::ArrayList::append $(@) } # # Removes and returns the oldest item added to the queue # proc variable::ArrayQueue::dequeue { if [[ ${VARIABLES_DEBUG} == 1 ]] { stderr "variable::ArrayList::isEmpty_c $(@)" ; } declare token="$(1)" if ! variable::type::instanceOf $(token) ArrayQueue { stderr "Variable [$(token)] is not of type ArrayQueue (actual type [$(RESULT)])" exit 1 } if variable::ArrayList::isEmpty_c $(token) { stderr "Cannot dequeue from an empty queue" exit 1 } variable::ArrayQueue::peek $(token); declare result=$RESULT variable::type $token; declare type=$RESULT variable::ArrayList::rest $token; declare value=$RESULT variable::set $token $type $value setglobal RESULT = $(result) } # # Returns the oldest item added to the queue (does note remove it) # proc variable::ArrayQueue::peek { declare token="$(1)" if ! variable::type::instanceOf $(token) ArrayQueue { stderr "Variable [$(token)] is not of type ArrayQueue (actual type [$(RESULT)])" exit 1 } if variable::ArrayList::isEmpty_c $token { stderr "Cannot peek from an empty queue" exit 1 } # stderr "peeking at list [$(variable::value_p $token)] / first=$(variable::ArrayList::first_p $token)" variable::ArrayList::first $token; declare result=$RESULT setglobal RESULT = $(result) } proc _variable::ArrayQueue::peek_p { variable::ArrayQueue::peek $(@) echo $RESULT } # ====================================================== if test $0 != $BASH_SOURCE { return } # # QUEUE tests # variable::ArrayQueue::new ; setglobal vCode = $(RESULT) variable::new String "first" ; variable::ArrayQueue::enqueue $(vCode) $(RESULT) variable::new String "second" ; variable::ArrayQueue::enqueue $(vCode) $(RESULT) variable::new String "third" ; variable::ArrayQueue::enqueue $(vCode) $(RESULT) variable::ArrayQueue::peek $vCode ; variable::value $(RESULT) ; \ assert::equals "first" $RESULT "queue:peek first" variable::ArrayQueue::dequeue $vCode ; variable::value $(RESULT) ; \ assert::equals "first" $RESULT "queue::dequeue first" variable::ArrayQueue::peek $vCode ; variable::value $(RESULT) ; \ assert::equals "second" $RESULT "queue:peek second" variable::ArrayQueue::dequeue $vCode ; variable::value $(RESULT) ; \ assert::equals "second" $RESULT "queue::dequeue second" assert::report if test $(1+isset) && test $1 == "debug" { variable::printMetadata }