2014-06-30 01:57:50 -07:00
|
|
|
import mem;
|
|
|
|
|
import util;
|
2014-07-28 01:52:15 -07:00
|
|
|
import io;
|
2014-06-30 01:57:50 -07:00
|
|
|
|
2014-07-23 02:23:21 -07:00
|
|
|
typedef template<T> vector (Destructable) {
|
2014-08-01 00:45:48 -07:00
|
|
|
|T*| data;
|
|
|
|
|
|int| size;
|
|
|
|
|
|int| available;
|
2014-07-06 23:42:25 -07:00
|
|
|
|
2014-08-01 00:45:48 -07:00
|
|
|
|vector<T>*| construct() {
|
2014-06-30 01:57:50 -07:00
|
|
|
size = 0;
|
|
|
|
|
available = 8;
|
|
|
|
|
data = new<T>(8);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-01 00:45:48 -07:00
|
|
|
|void| destruct() {
|
2014-07-28 01:52:15 -07:00
|
|
|
delete<T>(data);
|
2014-06-30 01:57:50 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-01 00:45:48 -07:00
|
|
|
|bool| resize(|int| newSize) {
|
|
|
|
|
|T*| newData = new<T>(newSize);
|
2014-06-30 01:57:50 -07:00
|
|
|
if (!newData)
|
|
|
|
|
return false;
|
2014-08-01 00:45:48 -07:00
|
|
|
for (|int| i = 0; i < lesser<int>(size, newSize); i++;)
|
2014-06-30 01:57:50 -07:00
|
|
|
newData[i] = data[i];
|
|
|
|
|
delete<T>(data, 0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-08-19 01:24:28 -04:00
|
|
|
|
|
|
|
|
|T| operator[](|int| index) {
|
|
|
|
|
return at(index);
|
2014-06-30 01:57:50 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-19 01:24:28 -04:00
|
|
|
|T| at(|int| index) {
|
2014-07-28 01:52:15 -07:00
|
|
|
if (index < 0 || index >= size) {
|
|
|
|
|
println("Vector access out of bounds! Retuning 0th element as sanest option");
|
|
|
|
|
return data[0];
|
|
|
|
|
}
|
2014-06-30 01:57:50 -07:00
|
|
|
return data[index];
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-01 00:45:48 -07:00
|
|
|
|void| set(|int| index, |T| dataIn) {
|
2014-07-03 01:52:44 -07:00
|
|
|
if (index < 0 || index >= size)
|
2014-06-30 01:57:50 -07:00
|
|
|
return;
|
|
|
|
|
data[index] = dataIn;
|
|
|
|
|
}
|
2014-08-01 00:45:48 -07:00
|
|
|
|void| addEnd(|T| dataIn) {
|
2014-06-30 01:57:50 -07:00
|
|
|
if (size < available)
|
|
|
|
|
size++;
|
|
|
|
|
else
|
|
|
|
|
resize(size*2);
|
2014-07-28 01:52:15 -07:00
|
|
|
data[size-1] = dataIn;
|
2014-06-30 01:57:50 -07:00
|
|
|
}
|
|
|
|
|
};
|