cellseqs_v1.nim 1001 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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-1:
  17. if s.d[i] == c: return true
  18. return false
  19. proc add(s: var CellSeq, c: PCell) {.inline.} =
  20. if s.len >= s.cap:
  21. s.cap = s.cap * 3 div 2
  22. var 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. # XXX: realloc?
  27. s.d[s.len] = c
  28. inc(s.len)
  29. proc init(s: var CellSeq, cap: int = 1024) =
  30. s.len = 0
  31. s.cap = cap
  32. s.d = cast[PCellArray](alloc0(cap * sizeof(PCell)))
  33. proc deinit(s: var CellSeq) =
  34. dealloc(s.d)
  35. s.d = nil
  36. s.len = 0
  37. s.cap = 0