# # # Nim's Runtime Library # (c) Copyright 2017 Nim contributors # # See the file "copying.txt", included in this # distribution, for details about the copyright. # import typetraits # strs already imported allocators for us. ## Default seq implementation used by Nim's core. type NimSeqPayload {.core.}[T] = object cap: int region: Allocator data: UncheckedArray[T] NimSeqV2*[T] = object len: int p: ptr NimSeqPayload[T] const nimSeqVersion {.core.} = 2 template payloadSize(cap): int = cap * sizeof(T) + sizeof(int) + sizeof(Allocator) # XXX make code memory safe for overflows in '*' when false: # this is currently not part of Nim's type bound operators and so it's # built into the tracing proc generation just like before. proc `=trace`[T](s: NimSeqV2[T]) = for i in 0 ..< s.len: `=trace`(s.data[i]) proc `=destroy`[T](s: var seq[T]) = var x = cast[ptr NimSeqV2[T]](addr s) var p = x.p if p != nil: when not supportsCopyMem(T): for i in 0.. 0: copyMem(unsafeAddr a.p.data[0], unsafeAddr b.p.data[0], a.len * sizeof(T)) else: for i in 0..= x.cap: resize(x) result = addr(x.data[x.len]) inc x.len template add*[T](x: var NimSeqV2[T]; y: T) = reserveSlot(x)[] = y template `[]`*[T](x: NimSeqV2[T]; i: Natural): T = assert i < x.len x.data[i] template `[]=`*[T](x: NimSeqV2[T]; i: Natural; y: T) = assert i < x.len x.data[i] = y proc `@`*[T](elems: openArray[T]): NimSeqV2[T] = result.cap = elems.len result.len = elems.len result.data = cast[type(result.data)](alloc(result.cap * sizeof(T))) when supportsCopyMem(T): copyMem(result.data, unsafeAddr(elems[0]), result.cap * sizeof(T)) else: for i in 0..