vector and vector test are finally working! Also found some bugs that I don't have time to fix before bed. Added file future_features.txt to keep track of bugs and features.
This commit is contained in:
@@ -2,10 +2,10 @@ __if_comp__ __C__ __simple_passthrough__ """
|
||||
#include <stdlib.h>
|
||||
"""
|
||||
|
||||
char* nullPtr = 0;
|
||||
/* we have a template versions so we don't have to cast (because we don't have that yet) */
|
||||
|
||||
char* malloc(int size) {
|
||||
char* memPtr = nullPtr;
|
||||
template <T> T* malloc(int size) {
|
||||
T* memPtr = 0;
|
||||
__if_comp__ __C__ {
|
||||
__simple_passthrough__ """
|
||||
memPtr = malloc(size);
|
||||
@@ -14,15 +14,6 @@ char* malloc(int size) {
|
||||
return memPtr;
|
||||
}
|
||||
|
||||
void free(char* memPtr) {
|
||||
__if_comp__ __C__ {
|
||||
__simple_passthrough__ """
|
||||
free(memPtr);
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
/* we have a template version so we don't have to cast */
|
||||
template <T> void free(T* memPtr) {
|
||||
__if_comp__ __C__ {
|
||||
__simple_passthrough__ """
|
||||
@@ -43,7 +34,7 @@ template <T> int sizeof() {
|
||||
}
|
||||
|
||||
template <T> T* new(int count) {
|
||||
return malloc( sizeof<T>() * count );
|
||||
return malloc<T>( sizeof<T>() * count );
|
||||
}
|
||||
|
||||
template <T> T* new() {
|
||||
@@ -55,6 +46,7 @@ template <T> void delete(T* toDelete, int itemCount) {
|
||||
delete<T>(toDelete);
|
||||
}
|
||||
|
||||
/* Calling this with itemCount = 0 allows you to delete destructable objects without calling their destructors. */
|
||||
template <T(Destructable)> void delete(T* toDelete, int itemCount) {
|
||||
for (int i = 0; i < itemCount; i++;)
|
||||
toDelete[i].destruct();
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
import mem;
|
||||
import util;
|
||||
import io;
|
||||
|
||||
typedef template<T> vector (Destructable) {
|
||||
T *data;
|
||||
int size;
|
||||
int available;
|
||||
bool destroyItems;
|
||||
|
||||
vector<T>* construct(bool destroyItemsIn) {
|
||||
destroyItems = destroyItemsIn;
|
||||
return construct();
|
||||
vector<T>* construct() {
|
||||
size = 0;
|
||||
available = 8;
|
||||
data = new<T>(8);
|
||||
@@ -17,17 +15,14 @@ typedef template<T> vector (Destructable) {
|
||||
}
|
||||
|
||||
void destruct() {
|
||||
if (destroyItems)
|
||||
delete<T>(data, size);
|
||||
else
|
||||
delete<T>(data);
|
||||
delete<T>(data);
|
||||
}
|
||||
|
||||
bool resize(int newSize) {
|
||||
T* newData = new<T>(newSize);
|
||||
if (!newData)
|
||||
return false;
|
||||
for (int i = 0; i < lesser(size, newSize); i++;)
|
||||
for (int i = 0; i < lesser<int>(size, newSize); i++;)
|
||||
newData[i] = data[i];
|
||||
delete<T>(data, 0);
|
||||
return true;
|
||||
@@ -38,8 +33,10 @@ typedef template<T> vector (Destructable) {
|
||||
}
|
||||
|
||||
T get(int index) {
|
||||
if (index < 0 || index >= size)
|
||||
return null;
|
||||
if (index < 0 || index >= size) {
|
||||
println("Vector access out of bounds! Retuning 0th element as sanest option");
|
||||
return data[0];
|
||||
}
|
||||
return data[index];
|
||||
}
|
||||
|
||||
@@ -53,6 +50,6 @@ typedef template<T> vector (Destructable) {
|
||||
size++;
|
||||
else
|
||||
resize(size*2);
|
||||
data[size-1];
|
||||
data[size-1] = dataIn;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user