Native functions deque library

Native functions for double-ended queues

Note: these functions are proposed for the next firmware release and are not available in Thymio robots

The Deque native library provides functions for double-ended queue operations in an object-oriented style on specially-formatted arrays. The array for a deque object must be of size $2 + m \cdot k\,\,$, where $k$ is the size of the tuples1 in the deque, and $m$ is the maximum number of tuples to be stored. An index $i$ into a deque is between two elements: the integer $i$ counts the number of elements to the left of the index.

deque.size(Q,n)
Set $n$ to the number of elements in deque $Q$. If $n=0$ then $Q$ is empty. Note that $n$ must be divided by the tuple size to obtain the number of tuples in $Q$.
deque.push_front(Q,A)
Insert tuple $A$ before the first tuple of deque $Q$.
deque.push_back(Q,A)
Insert tuple $A$ after the last tuple in deque $Q$.
deque.pop_front(Q,A)
Remove the first length($A$) elements of deque $Q$ and place them in tuple $A$.
deque.pop_back(Q,A)
Remove the last length($A$) elements of deque $Q$ and place them in tuple $A$.
deque.get(Q,A,i)
Copy into tuple $A$, length($A$) elements from deque $Q$ starting from index $i$.
deque.set(Q,A,i)
Copy into deque $Q$ starting at index $i$, length($A$) elements from tuple $A$.
deque.insert(Q,A,i)
Shift right the suffix of deque $Q$ starting at index $i$ by length($A$) elements, then copy tuple $A$ into the deque $Q$ at that index.
deque.erase(Q,i,k)
Erase $k$ elements from deque $Q$ at index $i$ by shifting the suffix starting from $i+k$ left. Length $k$ should be the tuple size or a multiple.

Example

Here is a simple motion queue, that accepts operations defined by a time and motor speeds, and executes them first-in, first-out.

var operation[3] # Tuple of size 3
var Queue[2 + (3*40)] # Store up to 40 operation tuples
var n

sub motion_add
    call deque.push_back(Queue, event.args[0:2])

onevent timer0
    call deque.size(Queue, n)
    if n > 0 then
        call deque.pop_front(Queue, operation) 
        timer.period[0] = operation[0]
        motor.left.target = operation[1]
        motor.right.target = operation[2]
    end
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License