class design question (inner classes)

coxalan coxalan at web.de
Tue Sep 11 07:14:53 PDT 2007


Regan Heath Wrote:

> Maybe template bolt-ins?  (I'm not 100% sure I know what you want to 
> achieve so this may be waaaay off base)
> 
> class SymmetricGroup
> {
>       uint degree;
> 
>       this(int degreeIn) {
>           degree = degreeIn;
>       }
> }
> 
> class Permutation(T, int D) : T
> {
>           uint data[];
> 
>           this() {
>               super(D);
>               data.length = degree;
>           }
> 
>           this(uint[] dataIn) {
>               super(D);
>               data = dataIn;
>           }
> 
>           Permutation opMul(Permutation b) {
> 		auto result = new Permutation;
> 		foreach(int i, uint val; b.data) {
> 		    result.data[i] = data[val] * b.data[val];
> 		}
> 		return result;
>          }
> }
> 
> alias Permutation!(SymmetricGroup, 3) SGP;
> 
> void main()
> {
> 	SGP a = new SGP();
> 	SGP b = new SGP();
> 	SGP c = a * b;
> }

With your code, the uint-value "degree" gets stored once for every instance of SGP. This is not what I want. The class SymmetricGroup should collect everything that all of its elements (the object of the class Permutation) have in common.

Just imagine the same for a class TableGroup, where the multiplication-values are stored in an huge array. This table definitely should not be stored for each single element of the group.

> In the above you don't actually need SymmetricGroup at all, your 
> template could just take "int D" and use that for degree, however I 
> suspect you want to add more to SymmetricGroup?

Yes, of course this is only an example which I made as short as possible.

There are two reasons for this class [these are the classes of kind 1) in my first posting]:

* I want to implement more kinds of groups, for example also matrix groups. All the groups should have a common interface which will be used in templated group algorithms.
* There are constructions which combine two or more groups into a new one. For the implementation of such combined groups, It will be very convenient to have classes of this kind 1).

coxalan




More information about the Digitalmars-d mailing list