rtarrays.nim 880 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. ##
  12. ## Unstable API.
  13. const
  14. ArrayPartSize = 10
  15. type
  16. RtArray*[T] = object ##
  17. L: Natural
  18. spart: seq[T]
  19. apart: array[ArrayPartSize, T]
  20. template usesSeqPart(x): untyped = x.L > ArrayPartSize
  21. proc initRtArray*[T](len: Natural): RtArray[T] =
  22. result.L = len
  23. if usesSeqPart(result):
  24. newSeq(result.spart, len)
  25. proc getRawData*[T](x: var RtArray[T]): ptr UncheckedArray[T] =
  26. if usesSeqPart(x): cast[ptr UncheckedArray[T]](addr(x.spart[0]))
  27. else: cast[ptr UncheckedArray[T]](addr(x.apart[0]))
  28. #proc len*[T](x: RtArray[T]): int = x.L