// Extremely simple implementation for those who are more accustomed to C++ vectors.
// 05/12/2017 - added setAt() and back()
// By Clay Hanson [15144]

function ArrayObject::onAdd(%this) {
	%this.elementCount = 0;
}
function ArrayObject::pushBack(%this, %string) {
	%this.data[-1 + %this.elementCount++] = %string;
}
function ArrayObject::popBack(%this) {
	%this.data[%this.elementCount--] = "";
}
function ArrayObject::push_back(%this, %string) {
	%this.pushBack(%string);
}
function ArrayObject::pop_back(%this) {
	%this.popBack();
}
function ArrayObject::erase(%this, %idx) {
	if(%idx >= %this.size() || %idx < 0) {
		error("ArrayObject::insert index out of range on " @ %this.getName() @ ". Index = " @ %index @ ", Size = " @ %this.size());
		return 0;
	}
	%this.data[%idx] = "";
	// Cleanup
	for(%i=%idx+1;%i<%this.size();%i++) {
		%this.data[%i-1] = %this.data[%i];
		%this.data[%i]   = "";
	}
	%this.elementCount--;
	return 1;
}
function ArrayObject::insert(%this, %idx, %val) {
	if(%idx > %this.size() || %idx < 0) {
		error("ArrayObject::insert index out of range on " @ %this.getName() @ ". Index = " @ %index @ ", Size = " @ %this.size());
		return 0;
	}
	for(%i=%this.size();%i>=%idx;%i--) {
		%this.data[%i+1] = %this.data[%i];
	}
	%this.data[%idx] = %val;
	%this.elementCount++;
	return 1;
}
function ArrayObject::empty(%this) {
	for(%i=0;%i<%this.size();%i++) %this.data[%i] = "";
	%this.elementCount = 0;
	return 1;
}
function ArrayObject::toString(%this, %start, %length, %delimiter) {
	if(%start $= "") %start = 0;
	if(%length $= "") %length = %this.size();
	if(%delimiter $= "") %delimiter = "\t";
	if(%start > %this.size() || %start < 0) {
		error("ArrayObject::toString start must be less than the size of this array and greater than 0 on " @ %this.getName() @ ". Start = " @ %start @ ", Length = " @ %length);
		return "";
	}
	if((%length-%start) > %this.size()) {
		error("ArrayObject::toString Length must be greater than start and less than the size of the array on " @ %this.getName() @ ". Start = " @ %start @ ", Length = " @ %length @ ", Size = " @ %this.size());
		return "";
	}
	for(%i=%start;%i<%this.size();%i++) {
		if(%start+(%length-%i) == 0) break;
		%toRet = (%toRet $= "" ? "" : %toRet @ %delimiter) @ %this.data[%i];
	}
	return %toRet;
}
function ArrayObject::at(%this, %idx) {
	if(%idx >= %this.size() || %idx < 0) {
		error("ArrayObject::at index out of range on " @ %this.getName() @ ". Index = " @ %idx @ ", Size = " @ %this.size());
		return "";
	}
	return %this.data[%idx];
}
function ArrayObject::setAt(%this, %idx, %val) {
	if(%idx >= %this.size() || %idx < 0) {
		error("ArrayObject::at index out of range on " @ %this.getName() @ ". Index = " @ %idx @ ", Size = " @ %this.size());
		return "";
	}
	%this.data[%idx] = %val;
}
function ArrayObject::back(%this) {
	return %this.at(%this.size()-1);
}
function ArrayObject::findIndex(%this, %string) {
	for(%i=0;%i<%this.size();%i++) if(%this.at(%i) $= %string) return %i;
	return -1;
}
function ArrayObject::size(%this) {
	return %this.elementCount;
}
function ArrayObject::dumpData(%this) {
	for(%i=0;%i<%this.elementCount;%i++) echo(%i @ ": \"" @ %this.data[%i] @ "\"");
}