Array - mfichman/jogo GitHub Wiki

An array is an integer-indexed, dynamically resizable container. Items can be accessed by their index in linear time. Every index in the array is considered valid, with empty indices returning nil. The 'count' property is simply the greatest index of any element in the array, plus one.

@init(capacity Int)

Creates a new buffer with capacity for 'capacity' elements. Regardless of the initial capacity, the number of elements in the array will always be zero after construction.

@destroy()

Releases memory used by the array.

@index(index Int) :a

Returns the element at 'index'. If 'index' is greater than 'count', this function return nil.

@insert(index Int, element :a)

Sets the element at 'index' to 'element'. If 'index' is greater than 'count', then the array will be resized to accomodate 'index'.

@add(other Array[:a]) Array[:a]

Concatenates this array with 'other', and returns the result

count=(count Int)

Sets the number of elements in the array. If 'count' is greater than the size of the array, then the size of the array is increased, with new array buckets being set to nil. If 'count' is less than the size of the array, then the size of the array is reduced, and elements belonging to removed buckets are freed.

capacity=(capacity Int)

Reserves at least storage for at least 'capacity' elements. If 'capacity' is smaller than the current capacity, the array will shrink to the number of elements in the array. Setting capacity to a value less than 'count' will result in 'capacity' being set equal to 'count'.

shift(element :a)

Adds 'element' to the beginning of the array, and shifts all other elements up by one index.

unshift() :a

Removes the first element from the array, and shifts all other elements down by one index.

push(element :a)

Adds 'element' to the end of the array.

append(element :a)

Adds 'element' to the end of the array (synonym for 'push')

pop() :a

Removes the element at the end of the array, and reduces the count.

first?() :a

Returns the element at index 0

last?() :a

Returns the last element in the array.

prefix(end Int) Array[:a]

Returns a new array containing all of the elements from the beginning of the array to the element before 'end'

suffix(begin Int) Array[:a]

Returns a new array containing elements starting at 'begin' and continuing to the end of this string.

slice(begin Int, end Int) Array[:a]

Returns a new array containing the elements starting at 'begin' and ending at the element before 'end'.

compact!()

Removes any nil elements in the array, and shifts the remaining elements down into a contiguous block.

empty?() Bool

Returns true if the array is empty

clear()

Clears all the items from this array.

iter() ArrayIter[:a]

Returns an iterator over the elements in this array

capacity?() Int

No comment

count?() Int

No comment