nim-eth/eth/utp/growable_buffer.nim

97 lines
3.3 KiB
Nim
Raw Normal View History

# Copyright (c) 2021 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import
std/[options, math]
export options
# Buffer implementation similar to the one in in reference implementation.
# Main rationale for it, is to refer to items in buffer by their sequence number,
# and support out of order packets.
# Therefore it is super specific data structure, and it mostly usefull for
# utp implementation.
# Another alternative would be to use standard deque from deques module, and caluclate
# item indexes from their sequence numbers.
type
GrowableCircularBuffer*[A] = object
items: seq[Option[A]]
2022-03-24 14:56:00 +00:00
mask: uint32
2022-10-18 02:01:52 +00:00
when not defined(nimHasEffectsOfs):
template effectsOf(f: untyped) {.pragma.}
# provided size will always be adjusted to next power of two
2022-03-24 14:56:00 +00:00
proc init*[A](T: type GrowableCircularBuffer[A], size: uint32 = 16): T =
let powOfTwoSize = nextPowerOfTwo(int(size))
T(
2022-03-24 14:56:00 +00:00
items: newSeq[Option[A]](powOfTwoSize),
mask: uint32(powOfTwoSize - 1)
)
2022-03-24 14:56:00 +00:00
proc get*[A](buff: GrowableCircularBuffer[A], i: uint32): Option[A] =
buff.items[i and buff.mask]
2022-03-24 14:56:00 +00:00
proc putImpl[A](buff: var GrowableCircularBuffer[A], i: uint32, elem: Option[A]) =
buff.items[i and buff.mask] = elem
2022-03-24 14:56:00 +00:00
proc put*[A](buff: var GrowableCircularBuffer[A], i: uint32, elem: A) =
buff.putImpl(i, some(elem))
2022-03-24 14:56:00 +00:00
proc delete*[A](buff: var GrowableCircularBuffer[A], i: uint32) =
buff.putImpl(i, none[A]())
2022-03-24 14:56:00 +00:00
proc hasKey*[A](buff: GrowableCircularBuffer[A], i: uint32): bool =
buff.get(i).isSome()
2022-10-18 02:01:52 +00:00
proc exists*[A](buff: GrowableCircularBuffer[A], i: uint32,
check: proc (x: A): bool): bool {.gcsafe, effectsOf: check.} =
let maybeElem = buff.get(i)
if (maybeElem.isSome()):
let elem = maybeElem.unsafeGet()
2022-10-18 02:01:52 +00:00
{.gcsafe.}:
check(elem)
else:
false
2022-03-24 14:56:00 +00:00
proc `[]`*[A](buff: var GrowableCircularBuffer[A], i: uint32): var A =
## Returns contents of the `var GrowableCircularBuffer`. If it is not set, then an exception
## is thrown.
buff.items[i and buff.mask].get()
proc len*[A](buff: GrowableCircularBuffer[A]): int =
2022-03-24 14:56:00 +00:00
int(buff.mask) + 1
proc calculateNextMask(currentMask: uint32, index:uint32): uint32 =
# Increase mask,so that index will fit in buffer size i.e mask + 1
if currentMask == uint32.high:
return currentMask
var newSize = currentMask + 1
2022-03-24 14:56:00 +00:00
while true:
newSize = newSize * 2
if newSize == 0 or index < newSize:
break
return newSize - 1
# Item contains the element we want to make space for
# index is the index in the list.
2022-03-24 14:56:00 +00:00
proc ensureSize*[A](buff: var GrowableCircularBuffer[A], item: uint32, index: uint32) =
if (index > buff.mask):
2022-03-24 14:56:00 +00:00
let newMask = calculateNextMask(buff.mask, index)
var newSeq = newSeq[Option[A]](int(newMask) + 1)
var i = 0'u32
while i <= buff.mask:
let idx = item - index + i
newSeq[idx and newMask] = buff.get(idx)
inc i
buff.items = move(newSeq)
buff.mask = newMask
iterator items*[A](buff: GrowableCircularBuffer[A]): Option[A] =
for e in buff.items:
yield e