rtarrays.nim 910 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Module that implements a fixed length array whose size
  10. ## is determined at runtime. Note: This is not ready for other people to use!
  11. const
  12. ArrayPartSize = 10
  13. type
  14. RtArray*[T] = object ##
  15. L: Natural
  16. spart: seq[T]
  17. apart: array[ArrayPartSize, T]
  18. UncheckedArray* {.unchecked.}[T] = array[0, T]
  19. template usesSeqPart(x): untyped = x.L > ArrayPartSize
  20. proc initRtArray*[T](len: Natural): RtArray[T] =
  21. result.L = len
  22. if usesSeqPart(result):
  23. newSeq(result.spart, len)
  24. proc getRawData*[T](x: var RtArray[T]): ptr UncheckedArray[T] =
  25. if usesSeqPart(x): cast[ptr UncheckedArray[T]](addr(x.spart[0]))
  26. else: cast[ptr UncheckedArray[T]](addr(x.apart[0]))
  27. #proc len*[T](x: RtArray[T]): int = x.L