cellseqs_v1.nim 1021 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2019 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # ------------------- cell seq handling ---------------------------------------
  10. type
  11. PCellArray = ptr UncheckedArray[PCell]
  12. CellSeq {.final, pure.} = object
  13. len, cap: int
  14. d: PCellArray
  15. proc contains(s: CellSeq, c: PCell): bool {.inline.} =
  16. for i in 0 ..< s.len:
  17. if s.d[i] == c:
  18. return true
  19. return false
  20. proc resize(s: var CellSeq) =
  21. s.cap = s.cap * 3 div 2
  22. let d = cast[PCellArray](alloc(s.cap * sizeof(PCell)))
  23. copyMem(d, s.d, s.len * sizeof(PCell))
  24. dealloc(s.d)
  25. s.d = d
  26. proc add(s: var CellSeq, c: PCell) {.inline.} =
  27. if s.len >= s.cap:
  28. resize(s)
  29. s.d[s.len] = c
  30. inc(s.len)
  31. proc init(s: var CellSeq, cap: int = 1024) =
  32. s.len = 0
  33. s.cap = cap
  34. s.d = cast[PCellArray](alloc0(cap * sizeof(PCell)))
  35. proc deinit(s: var CellSeq) =
  36. dealloc(s.d)
  37. s.d = nil
  38. s.len = 0
  39. s.cap = 0