and in situations where execution speed is more important than minimal memory 
usage, something along these lines might come in handy:
    
    
    type
      ExpandingArray*[T] = object
        chunks: array[65536, ptr UncheckedArray[T]]
        len: int64
    
    proc `[]=`*[T](e: var ExpandingArray[T], index: int64, elem: sink T) =
      if unlikely(e.chunks[index.shr(16)] == nil):
        e.chunks[index.shr(16)] = cast[ptr 
UncheckedArray[char]](alloc0(T.sizeof*65536))
      e.chunks[index.shr(16)][index and 0x0000FFFF] = elem
      if index >= e.len: e.len = index + 1
    
    proc add*[T](e: var ExpandingArray[T], elem: sink T) = e[e.len] = elem
    
    proc `[]`*[T](e: ExpandingArray[T], index: int64): var T  =
      if likely(e.chunks[index.shr(16)] != nil): return 
e.chunks[index.shr(16)][index and 0x0000FFFF]
      else: raise newException(IndexDefect, "index out of bounds")
    
    
    Run

Reply via email to