Skip to content

Containers

In this documentation, we consider a container to be any generic class that has the primary purpose of storing more than one value.

array

Methods

empty

Determine whether or not the array is empty.

  1. bool array::empty();

  2. bool array::is_empty();

Returns:

bool: true if the array is empty, false if not.

Remarks:

This method is functionally equivalent to array.length() == 0.

Example:
void main() {
	string[] items;
	bool insert = random_bool();
	if (insert)
		items.insert_last(1);
	alert("The array is", items.empty() ? "empty" : "not empty");
}

erase

Remove an item from the array at a particular index.

void array::erase(uint index);

Arguments:
Example:
void main() {
	string[] items = {"this", "is", "a", "test"};
	alert("The list currently contains", join(items, ", "));
	items.erase(random(0, 2));
	alert("The list currently contains", join(items, ", "));
}

insert_last

Appends an element to an array.

  1. void array::insert_last(const T&in);

  2. void array::push_back(const T&in);

Arguments
Remarks

This method adds an element to the end of an array.

Example:
void main() {
	string[] names = {"HTML", "CSS"};
	names.insert_last("Java Script");
	alert("Languages", join(names, ", "));
}

length

Returns the number of items in the array.

  1. uint array::length();

  2. uint array::size();

Returns:

uint: the number of items in the array.

Remarks:

This value is equal to the number of items in the array + 1.

Example:
void main() {
	int[] items;
	for (uint i = 0; i < random(1, 10); i++)
		items.insert_last(i);
	alert("The array contains", items.length() + " " + (items.length() == 1 ? "item" : "items"));
}

remove_last

Removes the last item from the array.

  1. void array::remove_last();
  2. void array::pop_back();

remove_range

Removes a group of items from an array, starting at a given position and removing the specified number of items after it.

void array::remove_range(uint start, uint count);

Arguments:
Example:
void main() {
	string[] items = {"there", "are", "a", "few", "items", "that","will", "disappear"};
	alert("The array currently is", join(items, ", "));
	items.remove_range(1, 3);
	alert("The array is now", join(items, ", "));
}

reserve

allocates the memory needed to hold the given number of items, but doesn't initialize them.

void array::reserve(uint length);

Arguments:

resize

Resizes the array to the specified size, and initializes all its elements to their default values.

void array::resize(uint length);

Arguments:

reverse

Reverses the array, so the last item becomes the first and vice versa.

void array::reverse();

Example:
void main() {
	string[] items = {"This", "is", "a", "test"};
	alert("The array is currently", join(items, ", "));
	items.reverse();
	alert("The reversed array is", join(items, ", "));
}

grid

This type is essentially just a more convenient 2d array. One place it's super useful is when representing a game board.

  1. grid();

  2. grid(uint width, uint height);

  3. grid({repeat {repeat_same T\}\});

Arguments (2):

Remarks:

One of the things that makes this class especially convenient is its opIndex overload. It's possible to index into a grid like:

my_grid[1, 2];

to access the value at (1, 2) on your grid.

Example:

void main() {
	grid<bool> game_board(10, 10); // a 10x10 grid of booleans.
	game_board[4, 4] = true; // set the center of the board to true.
	alert("The center of the board is", game_board[4, 4]);
}

Methods

height

Returns the current height of the grid.

uint grid::height();

Returns:

uint: the height of the grid.

Example:
void main() {
	grid<int> g(random(1, 10), random(1, 10));
	alert("Grid height is", g.height());
}

resize

Resize a grid to the given width and height.

void grid::resize(uint width, uint height);

Arguments:
Example:
void main() {
	grid<int> g(5, 5);
	alert("Original width and height", g.width() + ", " + g.height());
	g.resize(random(1, 100), random(1, 100));
	alert("New width and height", g.width() + ", " + g.height());
}

width

Returns the current width of the grid.

uint grid::width();

Returns:

uint: the width of the grid.

Example:
void main() {
	grid<int> g(random(1, 10), random(1, 10));
	alert("Grid width is", g.width());
}

Operators

opIndex

Get or set the value of a grid cell given an x and a y.

T& grid::opIndex(uint row, uint column);

Arguments:
Returns:

T&: a reference to the value at the given position. It is of whatever type your grid holds.

Remarks:

This function can throw index out of bounds errors, exactly like arrays can, so be careful.

Example:
void main() {
	grid<int> the_grid;
	the_grid.resize(5, 5);
	for (uint i = 0; i < 5; i++) {
		for (uint j = 0; j < 5; j++) {
			the_grid[i, j] = random(1, 100);
		}
	}
	alert("Info", "The center is " + the_grid[2, 2]);
}