Just for the proof of concept...
/*
The whole script relies on a globally declared string array and int array. You can NOT simply
declare additional string matrices and use them with the functions declared in THIS script.
If you need more matrices just copy the script and replace all occurences of the string
"stringMatrix" (in functions and variables) with "stringMatrix2" or so. Then call
the individual functions for the individual string matrix to get access. You don't need to
declare anything, just use the functions, the neccessary variables will be handled for you.
The size of the virtual rows and columns of the string matrix can of course not be specified,
they change dynamically during use and will be managed by the procedures.
FUNCTIONS:
// Bounds:
int stringMatrixGetRowSize(int $rowId)
Returns the current amount of columns in a individual
row regardless if the columns are empty ("") or filled.
int stringMatrixGetValidRowSize(int $rowId)
Returns the current amount of columns in a individual
row holding values, means empty columns doesn't count.
int stringMatrixGetRowCount()
Returns the actual amount of rows the string matrix
contains regardless if they are empty or not.
int stringMatrixGetUpperBound()
Returns the Id of the highest row containing valid
strings. RETURNS -1 IF THE MATRIX IS EMPTY.
int stringMatrixGetValidSize()
Returns the amount of non empty strings inside the
whole matrix.
// Element Access:
string stringMatrixGetElement(int $rowId, int $colId)
Returns the string value at the given coordinate.
string[] stringMatrixGetRow(int $rowId)
Returns all string values regardles if empty or not
of the given row as string array.
string[] stringMatrixGetValidRow(int $rowId)
Returns all non empty string values at the given row
as string array.
string[] stringMatrixToStringArray()
Returns all non empty strings inside the matrix as
string array.
stringMatrixSetElement(string $element, int $rowId, int $colId)
Sets the given string value at the given coordinate of
the matrix. This function creates new rows and columns
if neccessary. Newly created rows in between are
completely empty, created columns in between are
filled with empty strings ("").
int stringMatrixAddElement(string $element, int $rowId)
Adds a given string value to the end of a given row
and returns the column id at this entry. If a not
already existing row is given, it will be created.
stringMatrixInsertElement(string $element, int $rowId, int $colId)
Inserts a given string value at the given coordinate.
Rows will be created or expanded if neccessary.
int stringMatrixAddRow(string $rowStrArray[])
Adds a given string array as new row at the end of the
matrix. Returns the row Id at this entry.
stringMatrixInsertRow(string $rowStrArray[], int $rowId)
Inserts a given string array as row at the given row Id.
All rows at higher entries move up one step.
stringMatrixRemoveElement(int $rowId, int $colId)
If the given entry exists, it will be removed. Rows
will be downsized but not deleted if they are empty
after removal.
stringMatrixRemoveRow(int $rowId)
If the given row exists, it will be removed. All rows
at higher entries move down one step.
// Operations:
int stringMatrixDeflateRow(int $rowId)
Sets the row bound to the last valid string in the
given row and returns the new row size.
stringMatrixDeflate()
Removes all unused rows above the current upper bound.
stringMatrixRemoveAll()
Removes all the elements from the matrix.
*/
proc stringMatrixSetElement(string $element, int $rowId, int $colId)
{
global string $stringMatrix[];
global int $customIndex[];
if($rowId < `stringMatrixGetRowCount` && $colId < `stringMatrixGetRowSize $rowId`)
{
$stringMatrix[($customIndex[$rowId] + $colId)] = $element;
}
else stringMatrixInsertElement $element $rowId $colId;
};
proc stringMatrixRemoveRow(int $rowId)
{
global string $stringMatrix[];
global int $customIndex[];
int $rowSize = stringMatrixGetRowSize $rowId;
int $rowCount = stringMatrixGetRowCount;
for($i=($rowSize - 1);$i>=0;$i--)
{
stringArrayRemoveAtIndex ($customIndex[$rowId] + $i) $stringMatrix;
};
int $newIndex[];
for($i=0;$i<=$rowCount;$i++)
{
if($i <= $rowId) $newIndex[$i] = $customIndex[$i];
else if($i < $rowCount) $newIndex[$i] = $customIndex[($i + 1)] - $rowSize;
};
$customIndex = $newIndex;
};
proc stringMatrixRemoveElement(int $rowId, int $colId)
{
global string $stringMatrix[];
global int $customIndex[];
if($rowId < `stringMatrixGetRowCount` && $colId < `stringMatrixGetRowSize $rowId`)
{
stringArrayRemoveAtIndex ($customIndex[$rowId] + $colId) $stringMatrix;
for($i=($rowId + 1);$i<`size $customIndex`;$i++) $customIndex[$i] -= 1;
};
};
proc stringMatrixRemoveAll()
{
global string $stringMatrix[];
global int $customIndex[];
clear $stringMatrix;
$customIndex = {0};
};
proc stringMatrixInsertRow(string $rowStrArray[], int $rowId)
{
global string $stringMatrix[];
global int $customIndex[];
if(!size($customIndex)) stringMatrixRemoveAll();
int $size = size $customIndex;
int $numElements = size $rowStrArray;
if(($size - 1) < $rowId)
{
int $count = $customIndex[($size - 1)];
for($i=$size;$i<=$rowId;$i++) $customIndex[$i] = $count;
for($element in $rowStrArray) $stringMatrix[size $stringMatrix] = $element;
$customIndex[size $customIndex] = $count + $numElements;
}
else
{
int $count = $customIndex[$rowId];
for($i=0;$i<$numElements;$i++) stringArrayInsertAtIndex(($count + $i), $stringMatrix, $rowStrArray[$i]);
int $newIndex[];
for($i=0;$i<=size $customIndex;$i++)
{
if($i <= $rowId) $newIndex[$i] = $customIndex[$i];
else $newIndex[$i] += $customIndex[($i - 1)] + $numElements;
};
$customIndex = $newIndex;
};
};
proc stringMatrixInsertElement(string $element, int $rowId, int $colId)
{
global string $stringMatrix[];
global int $customIndex[];
int $rowSize = stringMatrixGetRowSize $rowId;
int $size = size $customIndex;
if($rowId >= stringMatrixGetRowCount())
{
string $tmp[];
$tmp[$colId] = $element;
stringMatrixInsertRow $tmp $rowId;
}
else if($colId >= $rowSize)
{
int $count = $colId - $rowSize;
string $tmp[];
$tmp[$count] = $element;
for($i=0;$i<=$count;$i++) stringMatrixAddElement $tmp[$i] $rowId;
}
else
{
stringArrayInsertAtIndex(($customIndex[$rowId] + $colId), $stringMatrix, $element);
for($i=0;$i<size $customIndex;$i++)
{
if($i > $rowId) $customIndex[$i] += 1;
};
};
};
proc int stringMatrixGetValidSize()
{
global string $stringMatrix[];
int $counter = 0;
for($i=0;$i<size $stringMatrix;$i++)
{
if($stringMatrix[$i] != "") $counter++;
};
return $counter;
};
proc int stringMatrixGetValidRowSize(int $rowId)
{
global string $stringMatrix[];
global int $customIndex[];
int $rowSize = stringMatrixGetRowSize $rowId;
int $counter = 0;
for($i=0;$i<$rowSize;$i++)
{
if($stringMatrix[($customIndex[$rowId] + $i)] != "") $counter++;
};
return $counter;
};
proc string[] stringMatrixGetValidRow(int $rowId)
{
global string $stringMatrix[];
global int $customIndex[];
string $tmp[];
for($i=$customIndex[$rowId];$i<$customIndex[($rowId + 1)];$i++)
{
if($stringMatrix[$i] != "") $tmp[size $tmp] = $stringMatrix[$i];
};
return $tmp;
};
proc int stringMatrixGetUpperBound()
{
global string $stringMatrix[];
global int $customIndex[];
int $matrixSize = size $stringMatrix;
int $index = -1;
for($i=($matrixSize - 1);$i>=0;$i--)
{
if($stringMatrix[$i] != "")
{
$index = $i;
break;
};
};
if($index > 0)
{
int $indexSize = size $customIndex;
for($i=($indexSize - 1);$i>=0;$i--)
{
if($index >= $customIndex[$i])
{
return $i;
break;
};
};
}
else return $index;
};
proc int stringMatrixGetRowSize(int $rowId)
{
global int $customIndex[];
if(!size($customIndex)) stringMatrixRemoveAll();
return ($customIndex[($rowId + 1)] - $customIndex[$rowId]);
};
proc int stringMatrixGetRowCount()
{
global int $customIndex[];
if(!size($customIndex)) stringMatrixRemoveAll();
return (size $customIndex - 1);
};
proc int stringMatrixGetRowCount()
{
global int $customIndex[];
if(!size($customIndex)) stringMatrixRemoveAll();
return (size $customIndex - 1);
};
proc string stringMatrixGetElement(int $rowId, int $colId)
{
global string $stringMatrix[];
global int $customIndex[];
return $stringMatrix[($customIndex[$rowId] + $colId)];
};
proc int stringMatrixDeflateRow(int $rowId)
{
global string $stringMatrix[];
global int $customIndex[];
int $rowSize = stringMatrixGetRowSize $rowId;
for($i=($rowSize - 1);$i>=0;$i--)
{
if($stringMatrix[($customIndex[$rowId] + $i)] == "")
{
stringMatrixRemoveElement $rowId $i;
}
else break;
};
return stringMatrixGetRowSize $rowId;
};
proc stringMatrixDeflate()
{
global string $stringMatrix[];
global int $customIndex[];
int $matrixSize = size $stringMatrix;
int $validAt= -1;
for($i=($matrixSize - 1);$i>=0;$i--)
{
if($stringMatrix[$i] != "")
{
$validAt = $i;
break;
};
};
if($validAt == -1) stringMatrixRemoveAll();
else
{
int $validRow;
int $switch = 1;
int $newCustomIndex[];
int $indexSize = size $customIndex;
for($i=($indexSize - 1);$i>=0;$i--)
{
if($switch && $validAt >= $customIndex[$i])
{
$validRow = $i;
$newCustomIndex[($i + 1)] = $customIndex[($i + 1)];
$newCustomIndex[$i] = $customIndex[$i];
$switch = 0;
}
else if(!$switch) $newCustomIndex[$i] = $customIndex[$i];
};
int $rowSize = stringMatrixGetRowSize $validRow;
string $newStringMatrix[];
appendStringArray $newStringMatrix $stringMatrix ($customIndex[$validRow] + $rowSize);
stringMatrixRemoveAll();
$stringMatrix = $newStringMatrix;
$customIndex = $newCustomIndex;
};
};
proc int stringMatrixAddRow(string $rowStrArray[])
{
global string $stringMatrix[];
global int $customIndex[];
if(!size($customIndex)) stringMatrixRemoveAll();
for($element in $rowStrArray) $stringMatrix[size $stringMatrix] = $element;
int $size = size $customIndex;
$customIndex[$size] = $customIndex[($size - 1)] + size($rowStrArray);
return ($size - 1);
};
proc int stringMatrixAddElement(string $element, int $rowId)
{
global string $stringMatrix[];
global int $customIndex[];
if(!size($customIndex)) stringMatrixRemoveAll();
if($rowId < (`size $customIndex` - 1))
{
stringArrayInsertAtIndex($customIndex[($rowId + 1)], $stringMatrix, $element);
for($i=0;$i<`size $customIndex`;$i++)
{
if($i > $rowId) $customIndex[$i] += 1;
};
return ($customIndex[($rowId + 1)] - $customIndex[$rowId] - 1);
}
else
{
stringMatrixInsertRow {$element} $rowId;
return 0;
};
};
proc string[] stringMatrixToStringArray()
{
global string $stringMatrix[];
string $tmp[];
int $counter = 0;
for($i=0;$i<size $stringMatrix;$i++)
{
if($stringMatrix[$i] != "") $tmp[$counter++] = $stringMatrix[$i];
};
return $tmp;
};
// have a nice day