Initalizing complex array types or some other problem ;/

Prudence via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 15 13:09:48 PDT 2015


How does one initialize a tuple type and are arrays initialized 
by default?


The code below doesn't work. I recently converted to use a tuple 
and that seemed to have broke it and I don't know why, when I 
changed the New function to use a ref, that made it worse cause 
now the array is all ways null. Debugging doesn't help much 
because, for some reason, VD won't show object information inside 
the mixin ObjectStore.


	alias StoreType = Tuple!(TValue, SingleStore!(TKey, 
TValue))[][TKey];
	public static StoreType Store;

I thought using the alias would help but it doesn't. Trying to 
init it in the static this doesn't seem to help me, as I can't 
get it to new propertly. e.g., new StoreType(); fails.

Any ideas?






import std.stdio;
import std.concurrency;



extern (C) int getch();
import std.string;
import std.concurrency;
import core.time;
import core.thread;
import std.container.array;
import std.typecons;







public class SingleStore(TKey, TValue)
{
	public TValue Value;
	public TKey Key;	
	public Tuple!(TValue, SingleStore!(TKey, TValue))[][TKey] Store;


	public auto Remove()
	{
		import std.algorithm;
		if (Value == null || Key == null) return;
		int count = 0;
		for(int i = 0; i < Store[Key].length; i++)
		{
			if (Store[Key][i][0] == Value && Store[Key][i][1] == this)
			{
					count++;
					Store[Key][i] = tuple(null, null);
					swap(Store[Key][i], Store[Key][max(0, Store[Key].length - 
count)]);
					i = i - 1;
			}


		}
		if (count == 1 && Store[Key].length == 1)
		{
			Store[Key] = null;
			Store.remove(Key);
		} else
			//Store[Key] = Store[Key][0..max(0,Store[Key].length-count)];
			Store[Key].length = Store[Key].length - count;

		Value = null;
		Key = null;

	}

	public static auto New(TKey k, TValue v, ref Tuple!(TValue, 
SingleStore!(TKey, TValue))[][TKey] s)
	{
		auto o = new SingleStore!(TKey, TValue)(k, v);	// GC
		o.Store = s;
		return o;
	}

	private this(TKey k, TValue v)
	{
		Key = k;
		Value = v;
	}
}


// Creates a static Associative Array that stores multiple values 
per key. The object returned by New can then be used to remove 
the key/value without having to remember them specifically.
public mixin template ObjectStore(TKey, TValue)
{
	alias StoreType = Tuple!(TValue, SingleStore!(TKey, 
TValue))[][TKey];
	public static StoreType Store;

	public static auto New(TKey k, TValue v)
	{
		auto r =  SingleStore!(TKey, TValue).New(k, v, Store);
		return r;
	}
}

alias dg = int delegate(int);

class MyStore
{
	mixin ObjectStore!(string, dg);
}

void main()
{

	auto k = "x";
	
	dg d1 = (int x) { return x; };
	dg d2 = (int x) { return x; };
	dg d3 = d1;
	dg d4 = (int x) { return 3*x; };

	auto s = MyStore.New(k, d1);
	writeln(MyStore.Store[k].length);
	auto s1 = MyStore.New(k, d2);
	writeln(MyStore.Store[k].length);
	auto s2 = MyStore.New(k, d3);
	writeln(MyStore.Store[k].length);
	auto s3 = MyStore.New(k, d4);
	writeln(MyStore.Store[k].length);	
	
	s1.Remove();
	writeln(MyStore.Store[k].length);
	s2.Remove();
	writeln(MyStore.Store[k].length);
	s.Remove();
	writeln(MyStore.Store[k].length);
	s3.Remove();
	


	getch();
}


More information about the Digitalmars-d-learn mailing list