More all-rounded implementation of the new objecty features, several bugfixes, and updates to the standard library to behave. Vector still needs a bit more work for some operations
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import vector:*;
|
||||
import io:*;
|
||||
|
||||
typedef matrix (Destructable) {
|
||||
typedef matrix (Object) {
|
||||
var data: vector<double>;
|
||||
var rows: int;
|
||||
var cols: int;
|
||||
|
||||
|
||||
///******************************
|
||||
// Constructors
|
||||
///*****************************/
|
||||
@@ -18,16 +18,16 @@ typedef matrix (Destructable) {
|
||||
data.construct();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
//Constructor with single argument
|
||||
//Creates an N x N matrix
|
||||
fun construct(size: int): matrix* {
|
||||
rows = size;
|
||||
cols = size;
|
||||
data.construct(rows*cols);
|
||||
return this;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
//Constructor with two arguments
|
||||
//Creates an N x M matrix
|
||||
fun construct(r: int, c: int): matrix* {
|
||||
@@ -36,16 +36,16 @@ typedef matrix (Destructable) {
|
||||
data.construct(rows*cols);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
///****************************
|
||||
// Utility Functions
|
||||
///***************************/
|
||||
|
||||
|
||||
//Test using indexing at 0
|
||||
fun test0(i: int, j: int): bool {
|
||||
|
||||
|
||||
var index = i*rows + j;
|
||||
|
||||
|
||||
if(index > (rows * cols - 1) ) {
|
||||
print("Index (");
|
||||
print(i);
|
||||
@@ -59,15 +59,15 @@ typedef matrix (Destructable) {
|
||||
println(").");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//Test using indexing at 1
|
||||
fun test1(i: int, j: int): bool {
|
||||
|
||||
|
||||
var index = (i-1)*rows + (j-1);
|
||||
|
||||
|
||||
if(index > (rows * cols - 1) ) {
|
||||
print("Index (");
|
||||
print(i);
|
||||
@@ -81,24 +81,24 @@ typedef matrix (Destructable) {
|
||||
println(").");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//Access matrix element
|
||||
fun at(i: int, j: int): double {
|
||||
|
||||
|
||||
var index = i*rows + j;
|
||||
|
||||
if(test0(i,j))
|
||||
return data.at(index);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//Set matrix element
|
||||
fun set(i: int, j: int, num: double): void {
|
||||
|
||||
|
||||
var index = i*rows + j;
|
||||
|
||||
if(test0(i,j))
|
||||
@@ -106,11 +106,11 @@ typedef matrix (Destructable) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
fun printMatrix(): void {
|
||||
|
||||
|
||||
for(var i: int = 0; i < rows; i++;)
|
||||
{
|
||||
{
|
||||
for(var j: int = 0; j < cols; j++;)
|
||||
{
|
||||
print(at(i,j));
|
||||
@@ -121,11 +121,11 @@ typedef matrix (Destructable) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
///**************************
|
||||
// Linear Algebra Functions
|
||||
//**************************/
|
||||
|
||||
|
||||
fun transpose(): void {
|
||||
var val1: double;
|
||||
var val2: double;
|
||||
@@ -138,7 +138,7 @@ typedef matrix (Destructable) {
|
||||
set(n, m, val2);
|
||||
set(m, n, val1);
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import io:*
|
||||
|
||||
__if_comp__ __C__ simple_passthrough """
|
||||
#include <stdlib.h>
|
||||
"""
|
||||
@@ -41,24 +43,36 @@ fun new<T>(): T* {
|
||||
return new<T>(1);
|
||||
}
|
||||
|
||||
/* We specilize on the trait Destructable to decide on whether or not the destructor should be called */
|
||||
/* We specilize on the trait Object to decide on whether or not the destructor should be called */
|
||||
fun delete<T>(toDelete: T*, itemCount: int): void {
|
||||
delete<T>(toDelete);
|
||||
}
|
||||
|
||||
/* Calling this with itemCount = 0 allows you to delete destructable objects without calling their destructors. */
|
||||
fun delete<T(Destructable)>(toDelete: T*, itemCount: int): void {
|
||||
for (var i: int = 0; i < itemCount; i++;)
|
||||
fun delete<T(Object)>(toDelete: T*, itemCount: int): void {
|
||||
// start at one because the actual delete will call the destructor of the first one as it
|
||||
// finishes the pointer
|
||||
for (var i: int = 1; i < itemCount; i++;)
|
||||
toDelete[i].destruct();
|
||||
delete<T>(toDelete);
|
||||
}
|
||||
|
||||
/* We specilize on the trait Destructable to decide on whether or not the destructor should be called */
|
||||
/* We specilize on the trait Object to decide on whether or not the destructor should be called */
|
||||
fun delete<T>(toDelete: T*): void {
|
||||
free<T>(toDelete);
|
||||
}
|
||||
|
||||
fun delete<T(Destructable)>(toDelete: T*): void {
|
||||
fun delete<T(Object)>(toDelete: T*): void {
|
||||
toDelete->destruct();
|
||||
free<T>(toDelete);
|
||||
}
|
||||
|
||||
// a wrapper for copy constructing if it has the Object trait
|
||||
fun maybe_copy_construct<T>(to:T*, from:T*):void {
|
||||
*to = *from
|
||||
}
|
||||
|
||||
fun maybe_copy_construct<T(Object)>(to:T*, from:T*):void {
|
||||
to->copy_construct(from)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import vector;
|
||||
import mem;
|
||||
|
||||
obj string (Destructable) {
|
||||
obj string (Object) {
|
||||
var data: vector::vector<char>;
|
||||
fun construct(): string* {
|
||||
data.construct();
|
||||
|
||||
@@ -2,7 +2,7 @@ import mem:*;
|
||||
import util:*;
|
||||
import io:*;
|
||||
|
||||
obj vector<T> (Destructable) {
|
||||
obj vector<T> (Object) {
|
||||
var data: T*;
|
||||
var size: int;
|
||||
var available: int;
|
||||
@@ -23,14 +23,14 @@ obj vector<T> (Destructable) {
|
||||
}
|
||||
|
||||
fun copy_construct(old: vector<T>*): void {
|
||||
construct(old->size)
|
||||
for (var i = 0; i < size; i++;)
|
||||
set(i, old->get(i))
|
||||
construct()
|
||||
for (var i = 0; i < old->size; i++;)
|
||||
addEnd(old->get(i))
|
||||
}
|
||||
|
||||
fun destruct(): void {
|
||||
if (data)
|
||||
delete<T>(data);
|
||||
delete(data, size);
|
||||
data = 0
|
||||
}
|
||||
|
||||
@@ -41,11 +41,11 @@ obj vector<T> (Destructable) {
|
||||
}
|
||||
|
||||
fun operator+(other:vector<T>):vector<T> {
|
||||
var newVec.construct(size + other.size):vector<T>
|
||||
var newVec.construct():vector<T>
|
||||
for (var i = 0; i < size; i++;)
|
||||
newVec.set(i, get(i))
|
||||
newVec.addEnd(get(i))
|
||||
for (var i = 0; i < other.size; i++;)
|
||||
newVec.set(i+size, other.get(i))
|
||||
newVec.addEnd(other.get(i))
|
||||
return newVec
|
||||
}
|
||||
|
||||
@@ -55,9 +55,9 @@ obj vector<T> (Destructable) {
|
||||
}
|
||||
|
||||
fun clone(): vector<T> {
|
||||
var newVec.construct(size): vector<T>
|
||||
var newVec.construct(): vector<T>
|
||||
for (var i = 0; i < size; i++;)
|
||||
newVec.set(i, data[i])
|
||||
newVec.addEnd(data[i])
|
||||
return newVec
|
||||
}
|
||||
|
||||
@@ -66,8 +66,8 @@ obj vector<T> (Destructable) {
|
||||
if (!newData)
|
||||
return false;
|
||||
for (var i: int = 0; i < lesser<int>(size, newSize); i++;)
|
||||
newData[i] = data[i];
|
||||
delete<T>(data, 0);
|
||||
maybe_copy_construct(&newData[i], &data[i]);
|
||||
delete(data, size);
|
||||
data = newData;
|
||||
available = newSize;
|
||||
size = lesser(size, newSize)
|
||||
@@ -101,7 +101,7 @@ obj vector<T> (Destructable) {
|
||||
size++;
|
||||
if (size >= available)
|
||||
resize(size*2);
|
||||
data[size-1] = dataIn;
|
||||
maybe_copy_construct(&data[size-1], &dataIn);
|
||||
}
|
||||
fun do(func: fun(T):void):void {
|
||||
for (var i = 0; i < size; i++;)
|
||||
@@ -112,9 +112,9 @@ obj vector<T> (Destructable) {
|
||||
data[i] = func(data[i])
|
||||
}
|
||||
fun map<U>(func: fun(T):U):vector<U> {
|
||||
var newVec.construct(size): vector<U>
|
||||
var newVec.construct(): vector<U>
|
||||
for (var i = 0; i < size; i++;)
|
||||
newVec.set(i, func(data[i]))
|
||||
newVec.addEnd(func(data[i]))
|
||||
return newVec
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user