February 03, 2004 Repost: bug in templates? | ||||
---|---|---|---|---|
| ||||
Since I am new here, I don't know what is the usual turn-around for questions, but since I see activity I am going to assume that my original question went unswered. I am to use D, I need to understand if the following code is a bug in D, in which case I will unpatiently wait for it to being fixed, or if I am making uncorrect assumptions. Below is my original post. Thanks is advance for your help and patience with re-explaining everything to someone new. Cheers, Andres ----- Here is a minimalist ArrayList implementation using templates. Everything compiles fine, until I throw the second test file in the mix. I tried using the same syntax from the Christopher E. Miller example of a linked list. The trouble seems to be the method 'addAll', because if I comment it out, eveything compiles. But the weird thing is that MyArrayList compiles fine without the test file at the end, is only after adding that file that I get the compilation error. I get the following error: > cd c:/dev/d/util/src/sri/util/test/ > make -k -f c:/dev/d/util/Makefile compile > \dmd\bin\dmd -c -od\dev\d\util\bin -I\dev\d\util\src -release \dev\d\util\src\sri\util\MyArrayList > \dmd\bin\dmd -c -od\dev\d\util\bin -I\dev\d\util\src -release \dev\d\util\src\sri\util\test\TestMyArrayList > template instance MyArrayList!(char[]) MyArrayList is not a template declaration > make: *** [sri-util-test] Error 1 > make: Target `compile' not remade because of errors. > > Compilation exited abnormally with code 2 at Sun Feb 01 08:40:29 Thanks in advance, Andres ***** File MyArrayList module sri.util.MyArrayList; public class MyArrayList(T) { //~ ATTRIBUTE(S) private int length = 0; private T[] data; //~ CONSTRUCTOR(S) public this() { } public this(MyArrayList!(T) c) { addAll(c); } public this(int initialCapacity) { data.length = initialCapacity; } //~ METHOD(S) public boolean add(T o) { ensureCapacity(length+1); data[length++] = o; return true; } public void add(int index, T element) { ensureCapacity(length+1); for (int i = length - 1; i > index; i--) data[i] = data[i-1]; data[index] = element; } public boolean addAll(MyArrayList!(T) c) { boolean modified = false; for (int i = 0; i < c.length; i++) { if (add(c.get(i))) modified = true; } return modified; } public void clear() { data.length = 0; } public boolean contains(T o) { for (int i = 0; i < length; i++) if (o == data[i]) return true; return false; } public void ensureCapacity(int minCapacity) { if (minCapacity > data.length) { data.length = (data.length == 0? 10 : 2 * data.length); } } public T get(int index) { return data[index]; } public boolean isEmpty() { return length == 0; } public T remove(int index) { T o = data[index]; length--; for (int i = index; i < length; i++) data[i] = data[i+1]; return o; } public boolean remove(T o) { int lag = 0; for (int i = 0; i < length - lag; i++) { if (o == data[i]) lag++; if (lag > 0) data[i] = data[i+lag]; } length -= lag; return lag > 0; } public T set(int index, T element) { return data[index] = element; } public int size() { return length; } } ***** File TestMyArrayList module sri.util.test.TestMyArrayList; import sri.util.MyArrayList; public class TestMyArrayList { protected void buildArrayList() { MyArrayList!(char[]) al = new MyArrayList!(char[]); al.add("a"); al.add("c"); al.add("u"); al.add("n"); al.add("i"); al.add("a"); al.add(null); } } |
February 03, 2004 Re: Repost: bug in templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andres Rodriguez | It seems to work fine if you use this:
public boolean addAll(MyArrayList c)
instead of
public boolean addAll(MyArrayList!(T) c)
Same with the constructor.
--
Christopher E. Miller
www.dprogramming.com
|
Copyright © 1999-2021 by the D Language Foundation