digitalmars.D - Templates
- Lorenzo Villani (103/103) Jan 15 2008 Hi, i have a little problem with a simple class (please note that I'm a ...
- BCS (8/34) Jan 15 2008 classic D error, try this:
- Lorenzo Villani (5/19) Jan 15 2008 Didn't know that, too
- David L. Davis (10/10) Jan 15 2008 Hi Lorenzo Villani,
- Bjoern (7/10) Jan 15 2008 Why reinvent the wheel ?
- BCS (2/20) Jan 15 2008 How many hello worlds are written?
- Lorenzo Villani (4/7) Jan 16 2008 I know that there are some useful D libraries around but I wanted to wri...
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
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
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 :-)Didn't know that, too Thanks a lot for the tips! Lorenzo "Arbiter" VillaniPS: 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
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
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
Bjoern wrote:Lorenzo Villani schrieb:How many hello worlds are written?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
Bjoern Wrote:Why reinvent the wheel ? http://www.dsource.org/projects/indigoI 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
Lorenzo Villani schrieb:Bjoern Wrote: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 ! BjoernWhy reinvent the wheel ? http://www.dsource.org/projects/indigoI 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
Bjoern wrote: Uwe Salomon(the creator) gavehis 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
torhu schrieb:Bjoern wrote: Uwe Salomon(the creator) gaveYes 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 ? Bjoernhis 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 17 2008
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