www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - allocate hash table manually

reply Mildred <mildred593 online.fr> writes:
Hi,

I'm a new user od D language but immediately, I felt that was the
language I always looking for. So I'm very happy to use it.
I'm trying to convert my recent project to C (using a garbage collector
as library) to D. It's a script language with a lisp syntax.

My question is how do I use new to allocate a hash table. My code is as
follows :

class Value {
	...
	protected Value[char[]] _environment = null;
	...
	public void env_make(Stack S){
		_environment = new Value[char[]];
	}
	...
}

But the compiler says : need size of rightmost array, not type char[]

My problem is that I want some Value objects to have an environment,
and I want others objects without environment (so I use null).
How can I do that ?

Thanks
Mildred

-- 
Mildred       <xmpp:mildred jabber.fr> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Sep 05 2006
parent reply Johan Granberg <lijat.meREM OVEgmail.com> writes:
Mildred wrote:
 Hi,
 
 I'm a new user od D language but immediately, I felt that was the
 language I always looking for. So I'm very happy to use it.
 I'm trying to convert my recent project to C (using a garbage collector
 as library) to D. It's a script language with a lisp syntax.
 
 My question is how do I use new to allocate a hash table. My code is as
 follows :
 
 class Value {
 	...
 	protected Value[char[]] _environment = null;
 	...
 	public void env_make(Stack S){
 		_environment = new Value[char[]];
 	}
 	...
 }
 
 But the compiler says : need size of rightmost array, not type char[]
 
 My problem is that I want some Value objects to have an environment,
 and I want others objects without environment (so I use null).
 How can I do that ?
 
 Thanks
 Mildred
 
I think this is what I have used. class Value { ... protected Value[char[]] _environment = null; ... public void env_make(Stack S){ Value[char[]] e; _environment = e; } ... }
Sep 05 2006
parent reply Mildred <mildred593 online.fr> writes:
Le mar 05/09/2006 __ 20:01 Johan Granberg __ __crit:
 I think this is what I have used.
 
 class Value {
 	...
 	protected Value[char[]] _environment = null;
 	...
 	public void env_make(Stack S){
 		Value[char[]] e;
 		_environment = e;
 	}
 	...
 }
Thanks, that helps me. Isn't there a solution with the new keyword, it is the normal way to allocate on the heap, no ? -- Mildred <xmpp:mildred jabber.fr> <http://mildred632.free.fr/> Clef GPG : <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key> Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Sep 06 2006
parent reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Mildred wrote:
 Le mar 05/09/2006 __ 20:01 Johan Granberg __ __crit:
 I think this is what I have used.

 class Value {
 	...
 	protected Value[char[]] _environment = null;
 	...
 	public void env_make(Stack S){
 		Value[char[]] e;
 		_environment = e;
 	}
 	...
 }
Thanks, that helps me. Isn't there a solution with the new keyword, it is the normal way to allocate on the heap, no ?
An empty hash is already "null" by default. Basically you're doing something stupid here - see this example: template Hash(K,V) { V[K] Hash() { V[K] tmp; return tmp; } } void main() { int[int] a; assert(a is null); a[1] = 1; assert(a !is null); a = Hash!(int, int); assert(a is null); a[1] = 1; assert(a !is null); a = Hash!(int, int); assert(a is null); } The compiler indicates a hash is empty by returning a null. Here Hash! creates an empty hash and returns it - hash array literals can be implemented in the same fashion.
Sep 06 2006
next sibling parent =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Jari-Matti Mäkelä wrote:
 An empty hash is already "null" by default. Basically you're doing
 something stupid here
<snip> Sorry, I was a bit rude. I always rely on the implicit initialization a bit too much. I hope the Hash-template will give the functionality you were looking for.
Sep 06 2006
prev sibling parent =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Jari-Matti Mäkelä wrote:
 main() {
         int[int] a;
         assert(a is null);
         a[1] = 1;
         assert(a !is null);
         a = Hash!(int, int);
         assert(a is null);
         a[1] = 1;
         assert(a !is null);
         a = Hash!(int, int);
         assert(a is null);
 }
Dammit, here's an optimized version (stupid me):
 void main() {
         int[int] a;
         assert(a is null);
         a[1] = 1;
         assert(a !is null);
         a = null;	// "creates" a new hash
         assert(a is null);
         a[1] = 1;
         assert(a !is null);
         a = null;	// "creates" yet another new hash
         assert(a is null);
 }
Sep 06 2006