www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Templates

reply Lorenzo Villani <arbiter arbiterlab.net> writes:
Hi, i have a little problem with a simple class (please note that I'm a D
newbie)

The folllowing is an initial implementation of a Vector class using templates,
modeled after Qt's QVector. As you might notice this is a class wrapper around
dynamic sized arrays.

(there's only a little part of the code implemented)

module std.Vector;

class Vector(T) {
private:
	T[] a;
public:
	this() {}

	void append(T value) {
		a.length = a.length + 1;
		a[a.length - 1] = value;
	}

	T at(int i) {
		assert(i >= 0 && i < a.length);
		return a[i];
	}

	int capacity() {
		return size();
	}

	void clear() {
		a.length = 0;
	}

	bool contains(/*const*/ T value) {
		bool c = false;
		foreach (element; a) {
			if (element == T) {
				c = true;
				break;
			}
		}
		return c;
	}

	int count(/*const*/ T value) {
		int c = 0;
		foreach (element; a) {
			if (element == T)
				c++;
		}

		return c;
	}

	int count() {
		return size();
	}

	T first() {
		return a[0];
	}

	int indexOf(/*const*/ T value, int from = 0) {
		int ret = -1;

		for (int i = from; i < a.length; i++) {
			if (a[i] == value)
				ret = i;
		}

		return ret;
	}

	bool isEmpty() {
		if (a.length > 0)
			return true;
		else
			return false;
	}

	T last() {
		return a[a.length];
	}
	
	void popBack() {}
	void popFront() {}
	void prepend(/*const*/ T value) {}

	void pushBack(/*const*/ T value) {}
	void pushFront(/*const*/ T value) {}

	void remove(int i) {
	}

	void remove(int i, int count) {
	}

	void replace(int i, /*const*/ T value) {}

	void resize(int size) {
		a.length = size;
	}

	int size() {
		return a.length;
	}

	T value(int i) {
		assert(i >= 0);
		if (i > a.length) {
			return T;
		} else {
			return a[i];
		}
	}
	
	// TODO: Implement operators
}

Now, if I build a very simple test program such as

import std.stdio;
import std.Vector;

int main() {
       Vector!(Object) myVec;
       myvec.size();
       return 0;
}


The application would compile fine but give a segfault when running. I'm
running Linux with Digital Mars D Compiler v1.015 on a Fedora 8 box.

PS: Can you tell me why the compiler doesn't let me use consts in functions
declarations?
PPS: I've noticed that the compiler sometimes doesn't tell me about evident
mistakes such as writing assertiii() instead of assert() when using CMakeD to
build the project...
Jan 15 2008
next sibling parent reply BCS <BCS pathlink.com> writes:
Lorenzo Villani wrote:
 Hi, i have a little problem with a simple class (please note that I'm a D
newbie)
 
 The folllowing is an initial implementation of a Vector class using templates,
modeled after Qt's QVector. As you might notice this is a class wrapper around
dynamic sized arrays.
 
 (there's only a little part of the code implemented)
 
 module std.Vector;
 
 class Vector(T) {

 }
 
 Now, if I build a very simple test program such as
 
 import std.stdio;
 import std.Vector;
 
 int main() {
        Vector!(Object) myVec;
        myvec.size();
        return 0;
 }
 

classic D error, try this: Vector!(Object) myVec = new Vector!(Object)(); All objects are by reference and are, by default set to null, NOT newed. If you want a stack allocated type use structs.
 
 The application would compile fine but give a segfault when running. I'm
running Linux with Digital Mars D Compiler v1.015 on a Fedora 8 box.
 
 PS: Can you tell me why the compiler doesn't let me use consts in functions
declarations?

const in 1.x is true const, compile time known values like #define does in C.
Jan 15 2008
parent Lorenzo Villani <arbiter arbiterlab.net> writes:
BCS Wrote:

 Lorenzo Villani wrote:
 [cut]


 classic D error, try this:
 
 Vector!(Object) myVec = new Vector!(Object)();
 
 All objects are by reference and are, by default set to null, NOT newed. 
 If you want a stack allocated type use structs.
 

Heh, C++ habits :-)
 
 PS: Can you tell me why the compiler doesn't let me use consts in functions
declarations?

const in 1.x is true const, compile time known values like #define does in C.

Didn't know that, too Thanks a lot for the tips! Lorenzo "Arbiter" Villani
Jan 15 2008
prev sibling next sibling parent David L. Davis <SpottedTiger yahoo.com> writes:
Hi Lorenzo Villani,

    The current D v1.x is v1.025, so it might be a good idea to use the newest
version, also would the "alias" and the "= new" additions below help your issue?

alias Vector!(Object) vector;

int main() 
{
       vector myVec = new vector;
       myVec.size();
       return 0;
} 

Regards David.
Jan 15 2008
prev sibling parent reply Bjoern <nanali nospam-wanadoo.fr> writes:
Lorenzo Villani schrieb:
 Hi, i have a little problem with a simple class (please note that I'm a D
newbie)
 
 The folllowing is an initial implementation of a Vector class using templates,
modeled after Qt's QVector. As you might notice this is a class wrapper around
dynamic sized arrays.

Why reinvent the wheel ? http://www.dsource.org/projects/indigo /Indigo consists of containers modelled after the Qt 4.0 container classes . (having an STL like interface too ...)/ but probabely you prefer to implement these classes by yourself :) Bjoern
Jan 15 2008
next sibling parent BCS <BCS pathlink.com> writes:
Bjoern wrote:
 Lorenzo Villani schrieb:
 
 Hi, i have a little problem with a simple class (please note that I'm 
 a D newbie)

 The folllowing is an initial implementation of a Vector class using 
 templates, modeled after Qt's QVector. As you might notice this is a 
 class wrapper around dynamic sized arrays.

Why reinvent the wheel ? http://www.dsource.org/projects/indigo /Indigo consists of containers modelled after the Qt 4.0 container classes . (having an STL like interface too ...)/ but probabely you prefer to implement these classes by yourself :) Bjoern

How many hello worlds are written?
Jan 15 2008
prev sibling parent reply Lorenzo Villani <arbiter arbiterlab.net> writes:
Bjoern Wrote:
 Why reinvent the wheel ?
 http://www.dsource.org/projects/indigo
 

I know that there are some useful D libraries around but I wanted to write this class only for didactic purpose. I'm just trying to find out if D is a good replacement or at least a complement to C++ (at least for me). Regarding that project its home page at dsource says: "As of May 2007, there are no plans for further development of Indigo. [...]" It's sad to see some D-related project dying (or at least become 'sleeping') in a few months...
Jan 16 2008
parent reply Bjoern <nanali nospam-wanadoo.fr> writes:
Lorenzo Villani schrieb:
 Bjoern Wrote:
 Why reinvent the wheel ?
 http://www.dsource.org/projects/indigo

I know that there are some useful D libraries around but I wanted to write this class only for didactic purpose. I'm just trying to find out if D is a good replacement or at least a complement to C++ (at least for me). Regarding that project its home page at dsource says: "As of May 2007, there are no plans for further development of Indigo. [...]" It's sad to see some D-related project dying (or at least become 'sleeping') in a few months...

Hi Lorenzo, yep, the Indigo lib is too good to ... I 'll pick it up and port it to Tango.*** Uwe Salomon(the creator) gave his okay to use and modify the Indigo sources to "whatever you want" Best thing : Uwe gave me the permission to publish it under a BSD style licence. *** ASAP, not today ! Bjoern
Jan 16 2008
parent reply torhu <no spam.invalid> writes:
Bjoern wrote:
Uwe Salomon(the creator) gave
 his okay to use and modify the Indigo sources to "whatever you want"
 Best thing :  Uwe gave me the permission to publish it under a BSD style 
 licence.

Did he allow you to publish Indigo under a different license than LGPL? That's cool, because he wasn't interested in changing the license when I asked him last spring. That could make Indigo interesting to more people. :)
Jan 16 2008
parent reply Bjoern <nanali nospam-wanadoo.fr> writes:
torhu schrieb:
 Bjoern wrote:
 Uwe Salomon(the creator) gave
 his okay to use and modify the Indigo sources to "whatever you want"
 Best thing :  Uwe gave me the permission to publish it under a BSD 
 style licence.

Did he allow you to publish Indigo under a different license than LGPL? That's cool, because he wasn't interested in changing the license when I asked him last spring. That could make Indigo interesting to more people. :)

Yes he did ! the original reply (in german, translation follows) ------------------------------------------------------------------- Also bitte, hiermit bekommst du meine erlaubnis, den gesamten container-code der Indigo-bibliothek (dies umfasst die module core.allocmore, core.random, und alle tools.*) für Tango zu adaptieren, umzuschreiben oder abzuschreiben wie es dir beliebt, und das ergebnis unter eine lizenz deiner wahl zu stellen. --------------------------------------------------------------------- Short translation : Okay, herewith you got my permission to adapt,to modify or copy the complete container code .... to port it to Tango. ... You can publish the result under a licence of your choice. You see, very generous. Porting Indigo to Tango is from what I know pretty simple. Probabely replacing his stream classes with the Tango stream implementation could use some time. Indigo is good documented, as you know, but some comments are in a very special kind of german. (beside, Uwe called his creation "Ultra Deutsch" :) ) Anyway, no problem for me to translate. Also some variable names are in german, so a bit refactoring is nessesary. Lars Ivar suggested to publish the port at Tango Scrabble, I mean a good place. Afaik, you are the current Indigo maintainer, right ? Bjoern
Jan 17 2008
parent torhu <no spam.invalid> writes:
Bjoern wrote:
...
 You see, very generous.
 Porting Indigo to Tango is from what I know pretty simple. Probabely 
 replacing his stream classes with the Tango stream implementation could 
 use some time.
 Indigo is good documented, as you know, but some comments are in a very 
 special kind of german.
 (beside, Uwe called his creation "Ultra Deutsch" :) )
 Anyway, no problem for me to translate.
 Also some variable names are in german, so a bit refactoring is nessesary.

Couldn't you remove the dependency on any kind of streams, and you're left with a nice container library that could work with both phobos and tango?
 Afaik, you are the current Indigo maintainer, right ?

Well, I put it up on dsource and updated it a bit. Let me know if you need commit access to the project or anything.
Jan 18 2008