View previous topic :: View next topic |
Author |
Message |
pqnelson
Joined: 03 Jun 2007 Posts: 13 Location: Davis
|
Posted: Wed Oct 31, 2007 5:31 pm Post subject: Problems with a box, array, and a class |
|
|
I don't understand this problem I'm having, it says that there is an array out of bounds error when I'm using a dynamic array.
I'm compiling this on ubuntu 7.10 gutsy with the GDC pre-release 0.25; and it still uses boxes, which are supposed to be replaced with variants in the future.
I tried switching boxes for Objects, and I still have the same problem. Is it something with arrays or am I just drunk?
Below is the code I'm working with. If anyone is wondering why I'm using boxes, I'm trying to write a toy python bytecode interpreter in D.
Quote: | import std.stdio;
import std.boxer;
struct cardboard
{
Box box;
}
class Array
{
private cardboard[] data;
public void add(...)
{
data[data.length].box = box(_arguments[0], cast(void*) _argptr);
}
}
void main()
{
bool larry;
TypeInfo t = typeid(typeof(larry));
Array ar = new Array();
ar.add(t);
ar.add(larry);
}
|
|
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Thu Nov 01, 2007 7:42 am Post subject: |
|
|
The problem is in Array.add(). The array's length needs to be explicitly modified, or in the case of pushing to the end (which you're doing) you can use the concatenation operator. In straight terms, change that one-liner to this one-liner:
Code: | data ~= cardboard(box(_arguments[0], cast(void*) _argptr)); |
Depending on what you're doing, you might even try making Array.add() a template, since you're only using one argument in the body:
Code: | public void add (T) (T arg) {
data ~= cardboard(box(typeid(T), arg)) ;
} |
There may be a shortcut for boxes as well... honestly, I haven't used them myself. _________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
pqnelson
Joined: 03 Jun 2007 Posts: 13 Location: Davis
|
Posted: Thu Nov 01, 2007 12:12 pm Post subject: |
|
|
Thanks for the help, I managed to rewrite this so I avoided templates altogether.
I basically cloned the Java ArrayList api, I have copied it below.
A few differences is that this does not use templates or Objects, it uses instead void pointers which may not be kosher in the D programming style. I needed to use this and have it be as efficient as possible, so I decided to use void pointers rather than templates.
Arguably, this was kindof in vain since I am using a dynamic array anyways. But I figured "Oh well, what the hell...".
Note that this compiles using GDC pre-release version 0.25, it may or may not compile on the DMC.
(As for licensing, I am really - quite honestly - lost, so I am going to just put this in public domain.)
Code: | module ArrayList;
import std.stdio;
import std.c.string;
public class ArrayList
{
private void*[] data;
private uint length;
public this()
{
length = 0;
}
public void add(...)
{
for(int a=0; _arguments.length > a; a++)
{
_add(_argptr);
_argptr += _arguments[a].tsize();
}
}
protected void _add(void* data)
{
this.data ~= data;
length++;
}
public void clear()
{
delete data;
}
public Object clone()
{
Object ret;
std.c.string.memcpy(cast(void*)ret,cast(void*)this,this.sizeof);
return ret;
}
public bool contains(...)
{
if(_arguments.length == 1)
{
for(int a=0; length>a; a++)
if(data[a] == _argptr)
return true;
}
return false;
}
public void ensureCapacity(uint minCapacity)
{
for(int a=length; minCapacity>a; a++)
add(null);
}
public void* get(uint index)
{
if(index < length)
{
throw new Exception("ArrayList out of bounds.");
}
else
{
return data[index];
}
}
public uint indexOf(...)
{
if(_arguments.length == 1)
{
for(int a=0; a<length; a++)
if(_argptr == data[a])
return a;
}
throw new Exception("ArrayList.indexOf(): Object not found!");
}
public bool isEmpty()
{
return (length == 0);
}
public uint lastIndexOf(...)
{
if(_arguments.length == 1)
{
for(int a=length-1; 0<a; a--)
if(_argptr == data[a])
return a;
}
throw new Exception("ArrayList.indexOf(): Object not found!");
}
public void* remove(uint index)
{
void* ret = null;
if(length > index)
{
ret = data[index];
for(int a=index; length>a; a++)
data[a] = data[a+1];
data[this.length-1] = null;
this.length--;
}
return ret;
}
public bool remove(...)
{
if(_arguments.length == 1)
for(int a=0; length>a; a++)
if(_argptr == data[a])
{
for(int b=a; length>a; b++)
data[b] = data[b+1];
data[this.length-1] = null;
return true;
}
return false;
}
protected void removeRange(uint fromIndex, uint toIndex)
{
for(int a=fromIndex; toIndex>a; a++)
remove(cast(uint)a);
}
public void set(uint index, ...)
{
if(index < length)
{
data[index] = _argptr;
}
else
{
for(int a=length; index>a; a++)
add(null);
_add(_argptr);
}
}
public uint size()
{
return length;
}
public Object[] toArray()
{
Object[] ret;
for(int a=0; a<length; a++)
ret ~= cast(Object)(this.get(a));
return ret;
}
} |
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|