Deque<T> structv0.1.0
pub struct Deque<T> {
// private fields
}
Move-only: it owns its elements and storage, prohibits copying, and destroys both with ~Deque.
Type parameters
| Name | Description |
|---|---|
T | the element type |
Constructors
Deque
pub func Deque(allocator: Allocator) -> Deque<T>
An empty deque that has not allocated anything.
allocator is borrowed for the call and kept. Costs nothing: a deque that is never pushed to never allocates.
Cannot fail. Runs in constant time.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
Returns: an empty collection that has taken no storage yet
Associated functions
WithCapacity
pub func WithCapacity(
allocator: Allocator,
capacity: uint
) -> Deque<T>
An empty deque with room for capacity elements already taken.
allocator is borrowed for the call and kept. The capacity actually taken is rounded up to a power of two,
since that is what the ring's masking needs. Terminates the program when the storage cannot be had.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
capacity | uint | how many elements to make room for |
Returns: an empty deque with room for that many elements already taken
FromSlice
pub func FromSlice(
allocator: Allocator,
items: T[..]
) -> Deque<T>
A deque holding a copy of every element of items, front to back.
allocator is borrowed for the call and kept; items is borrowed and read. Copies its elements, so a move-only
element type is built with PushBack instead. Terminates the program when the storage cannot be had.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
items | T[..] | the elements to take |
Returns: a deque holding a copy of those elements
Methods
Clone
pub func Clone(&self) -> Deque<T>
An independent deque holding a copy of every element of this one, in order.
self is borrowed and left alone. The copy starts contiguous whatever this one's elements were doing.
Elements are copied with =, so the element type must be plain data that Core::Copyable describes; an
owning element type is rejected while compiling, at the copy. Terminates the program when the storage cannot
be had.
Returns: an independent copy
Length
pub func Length(&self) -> uint
How many elements the deque holds.
self is borrowed.
Returns: how many elements it holds
Capacity
pub func Capacity(&self) -> uint
How many elements the ring could hold without growing.
self is borrowed. A power of two, or zero.
Returns: how many elements fit before the next growth
IsEmpty
pub func IsEmpty(&self) -> bool
Whether the deque holds no elements.
self is borrowed.
Returns: whether it holds nothing
Reserve
pub func Reserve(&var self, additional: uint)
Makes room for additional more elements on top of those already present.
self is borrowed mutably; the length does not change. Terminates the program when the storage cannot be
had.
| Name | Type | Description |
|---|---|---|
additional | uint | how many more elements to make room for |
ShrinkToFit
pub func ShrinkToFit(&var self)
Releases the capacity the length does not need.
self is borrowed mutably. The capacity stays a power of two, so this gives back what rounding leaves spare
rather than everything above the length. An empty deque gives up its storage entirely. Terminates the program
when the smaller block cannot be had.
Clear
pub func Clear(&var self)
Destroys every element, keeping the storage.
self is borrowed mutably. The front returns to physical zero, so the next run of additions does not wrap.
Runs in time proportional to the length.
PushFront
pub func PushFront(&var self, value: T)
Adds an element at the front, growing the ring when it is full.
self is borrowed mutably; value is moved in. Terminates the program when the ring cannot grow.
Amortized constant time.
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
PushBack
pub func PushBack(&var self, value: T)
Adds an element at the back, growing the ring when it is full.
self is borrowed mutably; value is moved in. Terminates the program when the ring cannot grow.
Amortized constant time.
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
PopFront
pub func PopFront(&var self) -> T?
Takes the front element out.
self is borrowed mutably. The element is moved out and handed to the caller rather than destroyed, so one
that owns something leaves intact. Reports none for an empty deque.
The storage is kept, so a deque emptied this way costs nothing to refill.
Returns: the element taken from the front, or none when empty
PopBack
pub func PopBack(&var self) -> T?
Takes the back element out.
self is borrowed mutably. The element is moved out and handed to the caller. Reports none for an empty
deque.
Returns: the element taken from the back, or none when empty
Front
pub func Front(&self) -> &T?
The front element, borrowed where it sits, or none for an empty deque.
self stays borrowed while the reference is in use, so the deque cannot grow, shrink, or be made contiguous
under it.
Returns: a reference to the first element, or none when empty
Back
pub func Back(&self) -> &T?
The back element, borrowed where it sits, or none for an empty deque.
self stays borrowed while the reference is in use.
Returns: a reference to the last element, or none when empty
Get
pub func Get(&self, index: uint) -> T?
The element at index, counting from the front, or none when there is no such index.
self is borrowed; the element is returned by copy. The index is logical: element zero is the front wherever
it happens to sit physically.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: a copy of the element, or none when the index is out of range
At
pub func At(&self, index: uint) -> &T?
The element at index, counting from the front, borrowed where it sits, or none when there is no such
index.
self stays borrowed while the reference is in use.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: a reference to the element, or none when the index is out of range
AtMutable
pub func AtMutable(&var self, index: uint) -> &var T?
The element at index, counting from the front, borrowed to be written where it sits, or none when there
is no such index.
self stays borrowed exclusively while the reference is in use.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: a writable reference to the element, or none when the index is out of range
Set
pub func Set(
&var self,
index: uint,
value: T
) throws CollectionError
Replaces the element at index with value, destroying the element that was there.
self is borrowed mutably; value is moved in. The old element is destroyed once the new one is in place,
so an element that owns something is released rather than leaked; Replace hands it back instead. Reports
IndexOutOfRange and changes nothing when there is no such index, and value is then destroyed here.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
value | T | the element to store |
Returns: nothing when the element was written, or IndexOutOfRange
Replace
pub func Replace(
&var self,
index: uint,
value: T
) -> T throws CollectionError
Replaces the element at index with value and hands back the element that was there.
self is borrowed mutably; value is moved in and the old element is moved out to the caller rather than
destroyed. Reports IndexOutOfRange and changes nothing when there is no such index, and value is then
destroyed here.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
value | T | the element to store |
Returns: the element that was at index, or IndexOutOfRange
IsContiguous
pub func IsContiguous(&self) -> bool
Whether the elements occupy one unbroken run.
self is borrowed. True for an empty deque and for one whose elements have not wrapped past the end of the
ring. This is what says whether AsSlice will move anything.
Returns: whether the elements sit in one run, which is what AsSlice needs
AsSlices
pub func AsSlices(&self) -> (T[..], T[..])
The elements as at most two runs, front run first, without moving anything.
self is borrowed. The second run is empty whenever the storage is already contiguous, so joining the two
always gives the elements in order.
This is the view that costs nothing. AsSlice gives one run instead, and pays a rotation for it.
Both views are valid until the deque grows, shrinks, or is made contiguous.
Returns: the first run of elements and the second, which is empty when the storage is contiguous
MakeContiguous
pub func MakeContiguous(&var self)
Moves the elements so they occupy one unbroken run starting at physical zero.
self is borrowed mutably. Allocates nothing: rotating the whole ring left by the head brings the front
element to zero and preserves cyclic order for every slot at once, so no element has to be tracked
individually.
Every pointer and view into the deque is invalidated, and so is any physical index a caller recorded.
AsSlice
pub func AsSlice(&var self) -> T[..]
Every element as one borrowed read-only view, moving them together first.
self is borrowed mutably — unlike a vector's, producing a single view can move the elements, which is why
this asks for a writable receiver rather than a read-only one.
The view is valid until the deque grows, shrinks, or is made contiguous again.
Returns: the elements, borrowed; empty unless the storage is contiguous
AsMutableSlice
pub func AsMutableSlice(&var self) -> var T[..]
Every element as one borrowed writable view, moving them together first.
self is borrowed mutably.
The view is valid until the deque grows, shrinks, or is made contiguous again.
Returns: the elements borrowed for writing; empty unless the storage is contiguous
Iterate
pub func Iterate(&var self) -> ValueIterator<T>
An iterator over the elements, front to back, by copy.
self is borrowed mutably and must outlive the iterator, and the elements are moved together first so one
run can be walked. This is the method a for loop finds.
The deque must not grow or shrink while the iterator is alive.
Returns: a cursor yielding copies of the elements, front to back
References
pub func References(&var self) -> ReferenceIterator<T>
An iterator over pointers to the elements, front to back.
self is borrowed mutably and must outlive the iterator, and the elements are moved together first.
Returns: a cursor yielding the elements' addresses, front to back
Destructor
func ~Deque(&var self)
Destroys every element and releases the storage. Runs when the deque's life ends. The elements go first and the block second, since an element destroyed after its storage was released would be read from freed memory.
Related
MinimumDequeCapacity
pub const MinimumDequeCapacity: uint = 4;
The capacity a deque takes when it first has to allocate. Four slots, and a power of two like every capacity after it.
WriteDequeDebug<T>
pub func WriteDequeDebug<T: Debug>(
writer: &var TextWriter,
value: &Deque<T>,
spec: FormatSpec
) throws FormatError
Writes a borrowed deque as [front, ..., back], front element first.
The order is the deque's own — what PopFront would yield, in the order it would yield it — whether or not the
ring has wrapped. A wrapped deque holds its elements in two runs, and both are walked inside one pair of
brackets, so where the ring happens to be split is not visible in the output and does not need to be.
Nothing is rearranged to write it: this is the reason the deque is borrowed rather than borrowed mutably, since
making the elements contiguous first would be a diagnostic quietly moving a caller's data around.
spec describes the elements, exactly as for a vector.
Reports whatever the writer or an element's implementation reported, stopping at the first refusal.
Type parameters
| Name | Description |
|---|---|
T: Debug | the element type, which must know how to show itself |
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
value | &Deque<T> | the deque to show, borrowed for the call |
spec | FormatSpec | what to show each element under |
Returns: success when the whole rendering was accepted