www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [The Code] Linked errors with templates in several modules

Seems like the system has problems with attachment. If you cannot download the
attachment, here is the code in each file + the make file.

====================================
IteratorModule.d
====================================
public interface Iterator(ValueType) {
public bool hasNext();
public ValueType next();
}

====================================
CollectionModule.d
====================================
import IteratorModule;

public interface Collection(ValueType) {

public bool add(ValueType val);
public bool addAll(.Collection!(ValueType) col);
public void clear();
public bool contains(ValueType val);
public bool containsAll(.Collection!(ValueType) col);
public bool isEmpty();
public Iterator!(ValueType) iterator();
public bool remove(ValueType val);
public bool removeAll(.Collection!(ValueType) col);
public bool retainAll(.Collection!(ValueType) col);
public int size();
public ValueType[] toArray();
public void toArray(ValueType[] array);

}

====================================
ListModule.d
====================================
import CollectionModule;

public interface List(ValueType) : Collection!(ValueType) {

public void addAt(int index, ValueType val);
public void addAllAt(int index, Collection!(ValueType) col);
public ValueType get(int index);
public int indexOf(ValueType val);
public int lasIndexOf(ValueType val);
public ValueType removeAt(int index);
public ValueType set(int index, ValueType val);
public .List!(ValueType) subList(int fromIndex, int toIndex);

}

====================================
ArrayListModule.d
====================================
import ListModule;
import IteratorModule;

public class ArrayList(ValueType) : List!(ValueType) {
private int curSize;
private ValueType[] values; 
private int incExpcurSize;

public this() {
this(100, 100);
}

public this(int initCapacity) {
this(initCapacity, 100);
}

public this(int initCapacity, int incExpcurSize) {
curSize = 0;
values.length = initCapacity;
incExpcurSize = incExpcurSize;
}

public bool add(ValueType val) {
ensurecurSize(curSize + 1);
values[curSize++] = val;
return true;
}

public void addAt(int index, ValueType val) {
ensurecurSize(curSize + 1);
shiftRight(index, 1);
values[index] = val;
}

public bool addAll(Collection!(ValueType) col) {
Iterator!(ValueType) itor = col.iterator();
while(itor.hasNext()) {
add(itor.next());
}
return true;
}

public void addAllAt(int index, Collection!(ValueType) col) {
ensurecurSize(curSize + col.size);
shiftRight(index, col.size);
Iterator!(ValueType) itor = col.iterator();
for(int i = index; itor.hasNext(); ++i) {
values[i] = itor.next();
}
}

public void clear() {
curSize = 0;
}

public bool contains(ValueType val) {
return indexOf(val) != -1;
}

public bool containsAll(Collection!(ValueType) col) {
Iterator!(ValueType) itor = col.iterator();
while(itor.hasNext()) {
if(!contains(itor.next())) {
return false;
}
}
return true;
}

public bool isEmpty() {
return curSize == 0;
}

public Iterator!(ValueType) iterator() {
return new ArrayListIterator!(ValueType)(curSize, values);
// return new ArrayListIterator!(ValueType)(curSize, curSize);
}

public ValueType get(int index) {
return values[index];
}

public int indexOf(ValueType val) {
for(int i = 0; i < curSize; ++i) {
if(values[i] == val) {
return i;
}
}
return -1;
}

public int lasIndexOf(ValueType val) {
for(int i = curSize - 1; i >= 0; --i) {
if(values[i] == val) {
return i;
}
}
return -1;
}

public ValueType set(int index, ValueType val) {
ValueType oldval = values[index];
values[index] = val;
return oldval;
}

public int size() {
return curSize;
}

public List!(ValueType) subList(int fromIndex, int toIndex) {
int count = toIndex - fromIndex;
ArrayList!(ValueType) subl = new .ArrayList!(ValueType)(count);
subl.values[] = this.values[fromIndex..toIndex];
return subl;
}

public bool remove(ValueType val) {
int pos = indexOf(val);
if(pos != -1) {
shiftLeft(pos, 1);
--curSize;
return true;
}
return false;
}

public ValueType removeAt(int index) {
ValueType oldval = values[index];
shiftLeft(index, 1);
--curSize;
return oldval;
}

public bool removeAll(Collection!(ValueType) col) {
bool changed = false;
Iterator!(ValueType) itor = col.iterator();
while(itor.hasNext()) {
changed = remove(itor.next()) || changed;
}
return changed;
}

public bool retainAll(Collection!(ValueType) col) {
bool changed = false;
for(int index = 0; index < curSize; ) {
if(col.contains(values[index])) {
++index;
} else {
removeAt(index);
changed = true;
}
}
return changed;
}

public ValueType[] toArray() {
ValueType[] result = new ValueType[curSize];
result[] = values;
return result;
}

public void toArray(ValueType[] array) {
array[] = values;

}

private void ensurecurSize(int reqcurSize) {
if(reqcurSize > values.length) {
int newcurSize = reqcurSize - curSize <= incExpcurSize
? incExpcurSize : reqcurSize + incExpcurSize;
values.length = newcurSize;
}
}

private void shiftLeft(int startIndex, int offset) {
for(int index = startIndex; index < curSize; --index) {
values[index - offset] = values[index];
}
}

private void shiftRight(int startIndex, int offset) {
for(int index = curSize - 1; index < startIndex; --index) {
values[index + offset] = values[index];
}
}
}

public class ArrayListIterator(ValueType) : Iterator!(ValueType) {
private int curSize, index;
private ValueType[] data;

public this(int curSize, ValueType[] data) {
// public this(int curSize, int data) {
this.curSize = curSize;
this.data = data;
// this.curSize = data;
index = 0;
}

public bool hasNext() {
return index < curSize;
}

public ValueType next() {
return data[index++];
}
}

====================================
test.d
====================================
import ArrayListModule;

void main()
{
ArrayList!(int) iarray = new ArrayList!(int)();

}

====================================
Makefile
====================================
DMD = dmd
SRCS = \
IteratorModule.d				\
CollectionModule.d				\
ListModule.d						\
ArrayListModule.d 				
OBJS = $(SRCS:.d=.obj)
TEST_SRCS = test.d
TEST_OBJS = $(TEST_SRCS:.d=.obj)
LIB = stl.lib
TEST = test.exe

all: $(SRCS) $(LIB)

clean:
rm $(OBJS) $(TEST_OBJS) $(LIB) $(TEST) *.exe *.map

$(LIB): $(OBJS)
lib -c -n $  $(OBJS) 

%.obj: %.d
$(DMD) $< -of$  -c $(DMD_FLAGS)

unittest: $(SRCS) $(LIB) $(TEST)

$(TEST): $(OBJS) $(TEST_OBJS)
$(DMD) $(TEST_SRCS) -of$  $(OBJS) $(DMD_FLAGS)
Jun 25 2004