mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 14:00:35 +00:00
Added the ability to initialize a deque with an openArray (#15138)
* Add ability to initialize a deque with a sequence Example: var dq = initDeque[char](@['a', 'b', 'c']) * Update deques.nim * Optimized deque initialization * Sequence replaced by open array in deque initialization
This commit is contained in:
@@ -84,6 +84,12 @@ proc initDeque*[T](initialSize: int = 4): Deque[T] =
|
|||||||
## The length of a newly created deque will still be 0.
|
## The length of a newly created deque will still be 0.
|
||||||
result.initImpl(initialSize)
|
result.initImpl(initialSize)
|
||||||
|
|
||||||
|
proc initDeque*[T](arr: openArray[T]): Deque[T] =
|
||||||
|
## Create a new deque initialized with an open array
|
||||||
|
result.initImpl(nextPowerOfTwo(arr.len))
|
||||||
|
for i in 0 ..< arr.len:
|
||||||
|
result.addLast(arr[i])
|
||||||
|
|
||||||
proc len*[T](deq: Deque[T]): int {.inline.} =
|
proc len*[T](deq: Deque[T]): int {.inline.} =
|
||||||
## Return the number of elements of `deq`.
|
## Return the number of elements of `deq`.
|
||||||
result = deq.count
|
result = deq.count
|
||||||
|
|||||||
Reference in New Issue
Block a user