Thread overview | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 19, 2002 What's the Solution? | ||||
---|---|---|---|---|
| ||||
Just to highlight a point, I would like to request how you would tackle a problem like this in D. Classes Vertex - Base class Line : Vertex Triangle : Line Polygon : Triangle ect... What I would like to determine is how many Vertexes, lines, Triangles and polygons are selected (individually). So for example 2 Vertexes are selected 5 lines are selected 6 Triangles are selected 3 Polygons are selected So for example you may have these methords //Although these could be done as an property VertexObject.setSelected() //Sets this object to selected VertexObject.getSelected() //Gets if this object is selected LineObject.setSelected() //Sets this object to selected ect.. VertexClass.getSelected() //gets the total amount of selected vertexes for the vertex class LineClass.getSelected() //gets the total amount of selected vertexes for the line class ect.. Easy right, heres a couple of important ground rules to make things more difficult: 1. All code details must be contained within the class itself. 2. This code should only need to be written once in the BASE class. Subsequent classes should inherit this property. 3. Needs to be resonably simple because the task is resonably simple. |
July 19, 2002 Re: What's the Solution? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anderson | Lines are not Vertices. Polygons are not Triangles. You should be using HasA, not IsA relationships. So maybe they all derive from class GeometricPrimitive. Hiding this all in the base class is hard; what you'd have to do is have each subclass register itself with the base class and have some kind of virtual "GetType()" function to let the base class know what kind it is so it can keep a tally. Probably: class GeometricPrimitive { private: static int[] countSelected; // how many of each type are selected bool selected; // if this is selected protected: int getType() { assert(false); } // how do you make pure virtual functions in D? public: void setSelected() { selected = true; countSelected.length = max(countSelected.length, getType()+1); // does D have builtin min/max/range/swap ? ++countSelected[getType()]; } bool getSelected() { return selected; } int getTotalSelectedOfType() { return countSelected(getType()); } } class Vertex : GeometricPrimitive { int getType() { return 0; } } class Line : GeometricPrimitive { int getType() { return 1; } } Something like that perhaps. Sean "anderson" <anderson@firestar.com.au> wrote in message news:ah82lm$16km$1@digitaldaemon.com... > Just to highlight a point, I would like to request how you would tackle a problem like this in D. > > Classes > Vertex - Base class > Line : Vertex > Triangle : Line > Polygon : Triangle > ect... > > What I would like to determine is how many Vertexes, lines, Triangles and polygons are selected (individually). So for example > > 2 Vertexes are selected > 5 lines are selected > 6 Triangles are selected > 3 Polygons are selected > > So for example you may have these methords > > //Although these could be done as an property > VertexObject.setSelected() //Sets this object to selected > VertexObject.getSelected() //Gets if this object is selected > LineObject.setSelected() //Sets this object to selected > ect.. > > VertexClass.getSelected() //gets the total amount of selected vertexes for > the vertex class > LineClass.getSelected() //gets the total amount of selected vertexes for the > line class > ect.. > > Easy right, heres a couple of important ground rules to make things more > difficult: > 1. All code details must be contained within the class itself. > 2. This code should only need to be written once in the BASE class. > Subsequent classes should inherit this property. > 3. Needs to be resonably simple because the task is resonably simple. |
July 20, 2002 Re: What's the Solution? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | "Sean L. Palmer" <seanpalmer@earthlink.net> wrote in message news:ah8k4f$1oop$1@digitaldaemon.com... > Lines are not Vertices. Polygons are not Triangles. Thanks for your reply, but it doesn't meet the requirements. Have you ever created a drawing package? Polygons are made of triangles, triangles of Vertices ect... I didn't mention that each vertex (line, triangle) is a group. Anyhow that's beside the point. I don't care what names are used, it's just for example purposes only (I'm not actually implementing this code) as long as its hyracical in nature. > > You should be using HasA, not IsA relationships. > So maybe they all derive from class GeometricPrimitive. > > Hiding this all in the base class is hard; what you'd have to do is have each subclass register itself with the base class and have some kind of virtual "GetType()" function to let the base class know what kind it is so it can keep a tally. > > Probably: > > class GeometricPrimitive > { > private: > static int[] countSelected; // how many of each type are selected > bool selected; // if this is selected > protected: > int getType() { assert(false); } // how do you make pure virtual > functions in D? > public: > void setSelected() > { > selected = true; > countSelected.length = max(countSelected.length, getType()+1); // > does D have builtin min/max/range/swap ? > ++countSelected[getType()]; > } > bool getSelected() > { > return selected; > } > int getTotalSelectedOfType() > { > return countSelected(getType()); > } > } > > class Vertex : GeometricPrimitive > { > int getType() { return 0; } > } > > class Line : GeometricPrimitive > { > int getType() { return 1; } > } > > Something like that perhaps. This breaks rule 2. These rules are not for breaking. Code should only be written in the base class. Writing it in the other classes not only means extra work for other programmers, but extra documentation. How do you suppose numbers are issued in your suggestion. One project could have many users that may not even be in the same company. Even I've solved that problem (using pointers) using a simular technique. I've use code like this all the time, it's not the question. I have an idea how D could be improved to deal with these problems. I think that other will draw to that some conclusion, so I won't mention it now. |
July 20, 2002 Re: What's the Solution? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anderson | You didn't specify that NO code could be added to the derived classes. Only that the getSelected methods weren't to be implemented in the derived classes. ;) I've run into that same problem before and the best solution is to use the classinfo as the index somehow. I just wasn't sure of D's classinfo syntax and naming so I didn't put it into the example. Sean "anderson" <anderson@firestar.com.au> wrote in message news:ahaq8u$ub4$1@digitaldaemon.com... > > "Sean L. Palmer" <seanpalmer@earthlink.net> wrote in message news:ah8k4f$1oop$1@digitaldaemon.com... > > Lines are not Vertices. Polygons are not Triangles. > > Thanks for your reply, but it doesn't meet the requirements. > > Have you ever created a drawing package? Polygons are made of triangles, triangles of Vertices ect... I didn't mention that each vertex (line, triangle) is a group. Anyhow that's beside the point. I don't care what names are used, it's just for example purposes only (I'm not actually implementing this code) as long as its hyracical in nature. > > > > > You should be using HasA, not IsA relationships. > > So maybe they all derive from class GeometricPrimitive. > > > > Hiding this all in the base class is hard; what you'd have to do is have > > each subclass register itself with the base class and have some kind of virtual "GetType()" function to let the base class know what kind it is so > > it can keep a tally. > > > > Probably: > > > > class GeometricPrimitive > > { > > private: > > static int[] countSelected; // how many of each type are selected > > bool selected; // if this is selected > > protected: > > int getType() { assert(false); } // how do you make pure virtual > > functions in D? > > public: > > void setSelected() > > { > > selected = true; > > countSelected.length = max(countSelected.length, getType()+1); // > > does D have builtin min/max/range/swap ? > > ++countSelected[getType()]; > > } > > bool getSelected() > > { > > return selected; > > } > > int getTotalSelectedOfType() > > { > > return countSelected(getType()); > > } > > } > > > > class Vertex : GeometricPrimitive > > { > > int getType() { return 0; } > > } > > > > class Line : GeometricPrimitive > > { > > int getType() { return 1; } > > } > > > > Something like that perhaps. > > This breaks rule 2. These rules are not for breaking. Code should only be written in the base class. Writing it in the other classes not only means extra work for other programmers, but extra documentation. How do you suppose numbers are issued in your suggestion. One project could have many users that may not even be in the same company. Even I've solved that problem (using pointers) using a simular technique. I've use code like this > all the time, it's not the question. I have an idea how D could be improved > to deal with these problems. I think that other will draw to that some conclusion, so I won't mention it now. > > > > |
July 21, 2002 Re: What's the Solution? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | "Sean L. Palmer" <seanpalmer@earthlink.net> wrote in message news:ahckrq$77p$1@digitaldaemon.com... > You didn't specify that NO code could be added to the derived classes. Only > that the getSelected methods weren't to be implemented in the derived classes. ;) Well that's what I ment by 2. 2. This code should only should be written once in the BASE class. Subsequent classes should inherit this property. > > I've run into that same problem before and the best solution is to use the classinfo as the index somehow. I just wasn't sure of D's classinfo syntax > and naming so I didn't put it into the example. That still doesn't solve 2. > Sean |
July 21, 2002 Re: What's the Solution? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anderson | "anderson" <anderson@firestar.com.au> wrote in message news:ahd447$lu1$1@digitaldaemon.com... > > "Sean L. Palmer" <seanpalmer@earthlink.net> wrote in message news:ahckrq$77p$1@digitaldaemon.com... > > You didn't specify that NO code could be added to the derived classes. > Only > > that the getSelected methods weren't to be implemented in the derived classes. ;) > > Well that's what I ment by 2. > > 2. This code should only should be written once in the BASE class. Subsequent classes should inherit this property. > > > > > I've run into that same problem before and the best solution is to use the > > classinfo as the index somehow. I just wasn't sure of D's classinfo > syntax > > and naming so I didn't put it into the example. > > That still doesn't solve 2. Sure it does. Just replace getType() with int(&classinfo(*this)) or something similar. It would be nice if classinfo had some generated int identifier or perhaps enum? that could be used for this purpose. Haven't looked into it much. Sean |
July 21, 2002 Re: What's the Solution? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | On Fri, 19 Jul 2002 02:02:49 -0700 "Sean L. Palmer" <seanpalmer@earthlink.net> wrote:
> int getType() { assert(false); } // how do you make pure virtual
> functions in D?
abstract int getType();
|
July 21, 2002 Re: What's the Solution? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | On Sun, 21 Jul 2002 09:36:30 -0700 "Sean L. Palmer" <seanpalmer@earthlink.net> wrote:
> Sure it does. Just replace getType() with int(&classinfo(*this)) or
> something similar.
Better yet, use an associative array:
int[ClassInfo] countSelected;
Then, no need for getType(). Just use .classinfo of object.
|
July 22, 2002 Re: What's the Solution? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | What type of numbers does ClassInfo return? Will the algorithm need a hash table? "Pavel Minayev" <evilone@omen.ru> wrote in message news:CFN374589713982292@news.digitalmars.com... On Sun, 21 Jul 2002 09:36:30 -0700 "Sean L. Palmer" <seanpalmer@earthlink.net> wrote: > Sure it does. Just replace getType() with int(&classinfo(*this)) or > something similar. Better yet, use an associative array: int[ClassInfo] countSelected; Then, no need for getType(). Just use .classinfo of object. |
July 22, 2002 Re: What's the Solution? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:CFN374589713982292@news.digitalmars.com... On Sun, 21 Jul 2002 09:36:30 -0700 "Sean L. Palmer" <seanpalmer@earthlink.net> wrote: > Sure it does. Just replace getType() with int(&classinfo(*this)) or > something similar. > Better yet, use an associative array: > int[ClassInfo] countSelected; > Then, no need for getType(). Just use .classinfo of object. Which leads me to my point. So to call, " int getTotalSelectedOfType() { return countSelected(this.classinfo()); } " I'd have to be virutal and static right? Otherwise I'd have to have an existing object to call it. Also doesn't this denote the need for an object? I'd like to quarie the class. |
Copyright © 1999-2021 by the D Language Foundation