Tape
final class Tape<Type>
Tape container. The container is stateful, maintaining a current position along a tape, providing constant-time operations at that and the previous position, but linear-time operations elsewhere (which require a seek).
- Type Element type. Must be default-constructible.
The data structure is arranged as two singly-linked lists about the current position, one recursing backward through elements behind the current position, the other recursing forward through elements at and ahead of the current position:
The call get(i)
may be used to retrieve any element, with complexity
linear in the offset from the current position.
Changing the current position in the list (i.e. seeking) is achieved with
the seek()
, backward()
, forward()
, rewind()
and fastForward()
member functions. It is not usually necessary to call these directly,
however, as the container seeks as necessary.
Note
As a recursive and singly-linked data structure, Tape has excellent performance properties under Birch's lazy deep copy mechanism.
Attention
Large recursive data structures can cause an execution stack overflow on destruction that usually manifests as a segmentation fault. Possible solutions include:
- Use an array (
Type[_]
) or Array instead. - Remove items one-by-one before the object goes out of scope.
- Increase the execution stack size with the shell command
ulimit
.
Slices
Name | Description |
---|---|
[...] | Reference to an element. |
Member Variables
Name | Description |
---|---|
ahead:TapeNode<Type>? | Elements at and ahead of the current position. |
behind:TapeNode<Type>? | Elements behind the current position. |
aheadCount:Integer | Number of elements at and ahead of the current position. |
behindCount:Integer | Number of elements behind the current positionl equivalent to current position minus 1. |
Member Functions
Name | Description |
---|---|
size | Number of elements. |
empty | Is this empty? |
clear | Clear all elements. |
front | Get the first element. |
back | Get the last element. |
get | Get an element. |
set | Set an element. |
pushFront | Insert an element at the front. |
pushFront | Insert a new default-constructed element at the front and return it. |
pushBack | Insert an element at the back. |
pushBack | Insert a new default-constructed element at the back and return it. |
popFront | Remove the first element. |
popBack | Remove the last element. |
insert | Insert a new element. |
erase | Erase an element. |
walk | Rewind and obtain an iterator. |
forward | Move the current position forward one. |
backward | Move the current position backward one. |
rewind | Rewind to one before the first element (that is set). |
fastForward | Fast-forward to one after the last element (that is set). |
seek | Seek to a new position. |
Member Slice Details
[i:Integer] -> Type
Reference to an element.
Member Function Details
back
function back() -> Type
Get the last element.
backward
function backward()
Move the current position backward one.
clear
function clear()
Clear all elements.
empty
function empty() -> Boolean
Is this empty?
erase
fastForward
function fastForward()
Fast-forward to one after the last element (that is set).
forward
function forward()
Move the current position forward one.
front
function front() -> Type
Get the first element.
get
insert
function insert(i:Integer, x:Type)
Insert a new element.
- i Position.
- x Value.
Inserts the new element immediately before the current element at
position i
. To insert at the back of the container, use a position that
is one more than the current size, or pushBack()
.
popBack
function popBack()
Remove the last element.
popFront
function popFront()
Remove the first element.
pushBack
function pushBack(x:Type)
Insert an element at the back.
- x Value.
function pushBack() -> Type
Insert a new default-constructed element at the back and return it.
pushFront
function pushFront(x:Type)
Insert an element at the front.
- x Value.
function pushFront() -> Type
Insert a new default-constructed element at the front and return it.
rewind
function rewind()
Rewind to one before the first element (that is set).
seek
function seek(k:Integer)
Seek to a new position.
- k New position.
Elements are default-constructed as necessary between the current position and the new position.
set
size
function size() -> Integer
Number of elements.
walk
function walk() -> Iterator<Type>
Rewind and obtain an iterator.
Returns an iterator across elements (that are set) from front to back.