lights FAQ Forum
This package

Terra Standard Libraries


Dynamic array type for Terra


	Dynamic array type for Terra.
	Written by Cosmin Apreutesei. Public domain.

	A dynamic array is a typed interface over realloc().

	When the array owns its elements (own_elements ~= false, the default),
	elem:free() is called on each element that gets removed or replaced.

	local A = arr{T=,...}                       create a type from Lua
	local A = arr(T, [size_t=int])              create a type from Lua
	var a =   arr{T=,...}                       create a value from Terra
	var a =   arr(T, [size_t=int])              create a value from Terra
	var a =   arr(T, elements, len[,...])       create a value from Terra
	var a = A(nil)                              nil-cast (for use in global())
	var a = A(&v)                               copy constructor from view
	var a = A(&a)                               copy constructor from array

	a:init() | fill(&a)                         initialize (for struct members)

	a:free()                                    free the elements and free the array
	a:setcapacity(n) -> ok?                     `a.capacity = n` with error checking

	a.view                                      (read/only) arr's arrayview
	a.elements                                  (read/only) array elements
	a.len                                       (read/write) array length
	a:setlen(len) -> new_elems                  set length and return new elements view
	a:setlen(len,empty_t)                       set length and set new elements
	a.capacity                                  (read/write) array capacity
	a.min_len                                   (write/only) grow array
	a.min_capacity                              (write/only) grow capacity

	a:set(i,t) -> &t                            replace value
	a:set(i) -> &t                              free value and get its address
	a:set(i,t,empty_t) -> &t                    grow array and set value or replace value
	a:getat(i) -> &t, new_elems                 grow array or get address
	a:getat(i,empty_t) -> &t                    grow array or get address

	a:push|add() -> &t                          a:insert(self.len)
	a:push|add(t) -> i                          a:insert(self.len, t)
	a:push|add(&t,n) -> i                       a:insert(self.len, &t, n)
	a:push|add(v) -> i                          a:insert(self.len, &v)
	a:push|add(&a) -> i                         a:insert(self.len, &a)
	a:pop() -> t                                remove top value and return a copy

	a:insertn(i,n)                              make room for n elements at i
	a:insertn(i,n,empty_t)                      grow array or make room for n elements
	a:insert(i) -> &t                           make room at i and return address
	a:insert(i,t)                               insert element at i
	a:insert(i,&t,n)                            insert buffer at i
	a:insert(i,v)                               insert arrayview at i
	a:insert(i,&a)                              insert dynarray at i
	a:remove|leak() -> i                        (free and) remove top element
	a:remove|leak(i[,n]) -> n                   (free and) remove n elements starting at i
	a:remove|leak(&t) -> i                      (free and) remove element at address

	a:copy() -> &a                              copy to new array
	a:move(i0,i1)                               move element to new position

	a:METHOD(...) -> a.view:METHOD(...)         call a method of a.view through a


See the source code for more info.

Pkg type:Terra
Version: dcc1e83
Last commit:
Author: Cosmin Apreutesei
License: Public Domain

Requires: terra.arrayview  terra.low  glue  luajit  pp 

Required by: terra.arrayview  terra.layer