Repository navigation
Category Deque
Home › All Categories › Deque
A deque (say "deck", short for double-ended queue) is a sequence you can add to and take from at both ends quickly — and, unlike a LinkedList, reach any element by its position with d[ i ].
#import "std/containers"
using containers
dim d as Deque( of ustring )
d.PushBack( "b" )
d.PushFront( "a" )
d.PushBack( "c" )
print d[ 0 ], d[ 2 ] ' a c
print d.PopFront( ) ' a
print d.PopBack( ) ' c
print d.Count( ) ' 1Index 0 is the front. for each walks front to back.
A brace list fills one at the declaration: dim d as Deque( of long ) = { 1, 2, 3 } calls PushBack once per element. See Collection Initializers.
- A queue (first in, first out):
PushBackto add,PopFrontto take. - A sliding window: push new items at the back, drop old ones from the front, and look at any of them by index.
- An undo history with a size limit: push at the back, drop the oldest from the front when full.
Every end operation is O(1) amortised, and indexing is O(1). The elements live in one ring-shaped block of memory that grows by doubling, so there is no allocation per element. Prefer it to LinkedList unless you need an iterator that stays valid across insertions.
-
Empty deque: the
Popforms return a default value (0,"");TryPopFront( v )andTryPopBack( v )returnfalseinstead. -
d[ i ],Front( ),Back( )need0 <= i < Count( ). Outside that range the value is unspecified; the deque does not grow to make the index valid. - A Push may move elements in memory, so do not hold a reference from
d[ i ]across a Push.
PushBack( byref v as T ) O(1) amortised
PushFront( byref v as T ) O(1) amortised
PopBack( ) as T O(1)
PopFront( ) as T O(1)
TryPopBack( byref v as T ) as boolean O(1)
TryPopFront( byref v as T ) as boolean O(1)
Front( ) byref as T O(1)
Back( ) byref as T O(1)
operator [] ( byval i as long ) byref as T O(1) 0 = front
At( byval i as long ) byref as T O(1)
Count( ) as long O(1)
Capacity( ) as long O(1) always a power of two
IsEmpty( ) as boolean O(1)
Clear( ) O(n) keeps the capacity
Reserve( byval n as long )
GetIterator( ) as DequeIterator( of T )tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing