# # # Nim's Runtime Library # (c) Copyright 2017 Nim contributors # # See the file "copying.txt", included in this # distribution, for details about the copyright. # import allocators, typetraits ## Default seq implementation used by Nim's core. type seq*[T] = object len, cap: int data: ptr UncheckedArray[T] const nimSeqVersion {.core.} = 2 template frees(s) = dealloc(s.data, s.cap * sizeof(T)) # XXX make code memory safe for overflows in '*' when defined(nimHasTrace): proc `=trace`[T](s: seq[T]; a: Allocator) = for i in 0 ..< s.len: `=trace`(s.data[i], a) proc `=destroy`[T](x: var seq[T]) = if x.data != nil: when not supportsCopyMem(T): for i in 0..= x.cap: resize(x) result = addr(x.data[x.len]) inc x.len template add*[T](x: var seq[T]; y: T) = reserveSlot(x)[] = y proc shrink*[T](x: var seq[T]; newLen: int) = assert newLen <= x.len assert newLen >= 0 when not supportsCopyMem(T): for i in countdown(x.len - 1, newLen - 1): `=destroy`(x.data[i]) x.len = newLen proc grow*[T](x: var seq[T]; newLen: int; value: T) = if newLen <= x.len: return assert newLen >= 0 if x.cap == 0: x.cap = newLen else: x.cap = max(newLen, (x.cap * 3) shr 1) x.data = cast[type(x.data)](realloc(x.data, x.cap * sizeof(T))) for i in x.len..