rtarrays.nim 861 B

123456789101112131415161718192021222324252627282930313233343536
  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. template usesSeqPart(x): untyped = x.L > ArrayPartSize
  19. proc initRtArray*[T](len: Natural): RtArray[T] =
  20. result.L = len
  21. if usesSeqPart(result):
  22. newSeq(result.spart, len)
  23. proc getRawData*[T](x: var RtArray[T]): ptr UncheckedArray[T] =
  24. if usesSeqPart(x): cast[ptr UncheckedArray[T]](addr(x.spart[0]))
  25. else: cast[ptr UncheckedArray[T]](addr(x.apart[0]))
  26. #proc len*[T](x: RtArray[T]): int = x.L