Changes to README

This commit is contained in:
Nocturn9x 2022-03-16 14:08:39 +01:00
parent e2e66db49b
commit 49d7ae6c8e
1 changed files with 22 additions and 12 deletions

View File

@ -19,26 +19,26 @@ import nimdeque
queue = newLinkedDeque[int]()
# Appends at the end
# Appends to the tail
queue.add(1)
queue.add(2)
queue.add(3)
# Prepends at the beginning
# Prepends to the head
queue.addLeft(0)
queue.addLeft(-1)
queue.addLeft(-2)
# Pops the first element in O(1) time
# Pops and returns the first element in O(1) time
queue.pop()
# Pops the last element in O(1) time
queue.pop(queue.high())
# Pops and returns the last element in O(1) time
discard queue.pop(queue.high())
# This can also be written as
queue.pop(^1)
discard queue.pop(^1)
# Pops element at position n
queue.pop(n)
discard queue.pop(n)
# Supports iteration
for i, e in queue:
@ -49,15 +49,16 @@ for e in queue.reversed():
echo e
echo queue.len()
echo 5 in queue # false
echo 0 in queue # true
5 in queue # false
0 in queue # true
# Item accessing works just like regular sequence types in Nim.
# Note that the further the item is from either end of the
# queue, the higher the time it takes to retrieve it. For
# fast random access, seqs should be used instead
assert queue[0] == -1
assert queue[^1] == queue[queue.high()]
queue[0] # -1
queue[^1]
queue[queue.high()]
# It's possible to extend a deque with other deques or with seqs
# of compatible type
@ -67,6 +68,13 @@ other.add(10)
queue.extend(@[5, 6, 7, 8])
queue.extend(other)
# Finds the first occurrence of an
# item in the queue, returns -1 if not
# found
queue.find(9999) # -1, not found
queue.find(-1) # 0
# Clears the queue in O(1) time
queue.clear()
# Clears the queue in O(n) time
@ -100,7 +108,9 @@ some benchmarks as well as the test suite used to validate the behavior of the q
1. I was bored during my programming class
2. That only provides a deque based on `seq`s
3. The deque in that module is a value type
4. I was bored during my programming class
4. The deques in this module allow accessing at arbirary locations
5. More useful procs are implemented
1. I was bored during my programming class
## Performance against a regular seq