class design question (inner classes)

Regan Heath regan at netmail.co.nz
Tue Sep 11 09:29:18 PDT 2007


coxalan wrote:
> Regan Heath Wrote:
> 
>>> The class SymmetricGroup should collect everything that all of
>>> its elements (the object of the class Permutation) have in
>>> common.
>> Like .. (list please).  Are they all data members or methods as
>> well.
> 
> Definitely methods, like uint order() which returns the number of the
> elements of the group.
> 
> For the SymmetricGroup there are no other data members, but for other
> Groups there could be arbitrary data members (like a big array where
> the multiplication results are stored).
> 
>> [...] Ok.  I think I have reached the limit of my usefulness here
>> :)
>> 
>> Someone else is bound to have some ideas.
> 
> It was my fear that I put too much technical mathematics stuff into
> my posting.
> 
> I already put this question (in C++ form) into a mathematical
> internet forum, but then the  OOP-design part of the discussion got
> stuck at a very basic level... :-)
> 
> 
> 
> Now I rethought my question and how I can put it on a more abstract
> level to get out the math part.
> 
> I guess my problem comes down to this point:
> 
> class Outer { class Inner { [...] } [...] }
> 
> main() { A a = new A(); [... initialize many a.Inner objects here
> ...] }
> 
> Once that "a" is initialized, te reference "a" stays constant all the
> time. So all further references/pointers to "a" are redundant.
> Especially, the references to "a" stored in "a.Inner" inner objects
> are redundant. So if there was a way to tell the compiler that the
> reference a will never change, the compiler could do the optimization
> and skip all these inner-class-references.
> 
> Now the question is: Is there currently a way to achieve a
> satisfying, equivalent result? If not: Should I make this a feature
> request?
> 
> coxalan

What about doing it manually/explicitly...

import std.stdio;
import std.string;
import std.c.stdlib;

class Outer
{
	Inner[] m_Data;
  	uint 	m_Degree;
  	
  	this(uint d) { m_Degree = d; }
  	
	Inner allocate(uint[] data)
	{
		m_Data ~= new Inner(this, data);
		return m_Data[$-1];
	}
	
	new(uint size)
	{
		writefln("Outer new %d", size);
		return malloc(size);
	}
	
	delete(void *p)
	{
		free(p);
	}
}

class Inner
{
	static Outer 	m_Outer;
	uint 		m_Data[];
	
	this(Outer o, uint[] data = null) {
		m_Outer = o;		
		m_Data.length = m_Outer.m_Degree;		
		if (data !is null)
		{
			m_Data[0..$] = data[0..m_Data.length];
		}
	}
	
	Inner opMul(Inner b)
	{
		Inner res = new Inner(m_Outer);
		foreach(int i, uint val; b.m_Data)
		{
			res.m_Data[i] = m_Data[i] * b.m_Data[i];
		}
		return res;
	}
	
	new(uint size)
	{
		writefln("Inner new %d", size);
		return malloc(size);
	}
	
	delete(void *p)
	{
		free(p);
	}
	
	string toString()
	{
		return format("%s", m_Data);
	}
}

void main()
{
	Outer o = new Outer(3);
	Inner a = o.allocate([0,1,2]);
	Inner b = o.allocate([1,2,3]);
	Inner c = a * b;
	writefln(c);
}

Of course, if you use Inner with another Outer class it will overwrite
the static m_Outer and fail horribly...

Regan



More information about the Digitalmars-d mailing list