view dict.rhope @ 180:c40e0faa1c92

Add Call to Dict and List
author Mike Pavone <pavone@retrodev.com>
date Sun, 19 Jun 2011 10:43:11 -0700
parents f3686f60985d
children
line wrap: on
line source


Blueprint Empty Dictionary
{
}

Blueprint Dictionary
{
	Left
	Right
	Straight
	Bits(UInt32,Naked)
}

Dictionary[:out]
{
	out <- Build[Empty Dictionary()]
}

_Index@Empty Dictionary[dict,index,bits,bitindex:out,not found]
{
	not found <- dict
}

Index@Empty Dictionary[dict,index:out,not found]
{
	not found <- dict
}

_Terminal Node[val:out]
{
	out <- [[[[Build[Dictionary()]
		]Left <<[Dictionary[]]
		]Right <<[Dictionary[]]
		]Straight <<[val]
		]Bits <<[0u32] 
}

_Set New[index,val,bits,bitindex:out]
{
	nbitindex <- [bitindex]+[1]
	[index]Dict Bits[nbitindex]
	{ straight <- _Set New[index,val,[~]+[1u32],nbitindex] }
	{ straight <- _Terminal Node[val] }
	out <- [[[[Build[Dictionary()]]Left <<[Dictionary[]]]Right <<[Dictionary[]]]Straight <<[straight]]Bits <<[bits]
}

_Set@Empty Dictionary[dict,index,val,bits,bitindex:out]
{
	If[[bits]=[0u32]]
	{
		out <- _Terminal Node[val]
	}{
		out <- _Set New[index,val,bits,bitindex]
	}
}

Set@Empty Dictionary[dict,index,val:out]
{
	out <- _Set New[index,val, [Dict Type ID[index]]+[1u32], -1]
}

First@Empty Dictionary[dict:first,not found]
{
	not found <- dict
}

Next@Empty Dictionary[dict,index:next,not found]
{
	not found <- dict
}


Index@Dictionary[dict,index:out,not found]
{
	
	out,not found <- [dict]_Index[index,[Dict Type ID[index]]+[1u32], -1]
}

_Index@Dictionary[dict,index,bits,bitindex:out,not found]
{
	If[[bits]=[[dict]Bits >>]]
	{	
		If[[bits]=[0u32]]
		{
			out <- Straight >>[dict]
		}{
			nbitindex <- [bitindex]+[1]
			[index]Dict Bits[nbitindex]
			{ nbits <- [~]+[1u32] }
			{ nbits <- 0u32 }
			out,not found <- [[dict]Straight >>]_Index[index,nbits,nbitindex]
		}
	}{
		If[[bits]<[[dict]Bits >>]]
		{
			go <- Left >>[dict]
		}{
			go <- Right >>[dict]
		}
		out,not found <- [go]_Index[index,bits,bitindex]
	}
}

Set@Dictionary[dict,index,val:out]
{
	out <- [dict]_Set[index,val,[Dict Type ID[index]]+[1u32], -1]
}

_Set@Dictionary[dict,index,val,bits,bitindex:out]
{
	If[[bits]=[[dict]Bits >>]]
	{
		If[[bits]=[0u32]]
		{
			out <- [dict]Straight <<[val]
		}{
			nbitindex <- [bitindex]+[1]
			[index]Dict Bits[nbitindex]
			{ nbits <- [~]+[1u32] }
			{ nbits <- 0u32 }
			out <- [dict]Straight <<[[[dict]Straight >>]_Set[index,val,nbits,nbitindex]]
		}
	}{
		If[[bits]<[[dict]Bits >>]]
		{
			go <- Left >>[dict]
			out <- [dict]Left <<[new]
		}{
			go <- Right >>[dict]
			out <- [dict]Right <<[new]
		}
		new <- [go]_Set[index,val,bits,bitindex]
	}
}

_First Type ID@Empty Dictionary[dict:typeid,node,none]
{
	none <- dict
}

_First Type ID@Dictionary[dict:typeid,node]
{
	typeid,node <- [[dict]Left >>]_First Type ID {} {}
	{
		typeid <- [[dict]Bits >>]-[1u32]
		node <- Straight >>[dict]
	}
}

_First@Empty Dictionary[dict,keylist:keyout,none]
{
	none <- dict
}

_First@Dictionary[dict,keylist:keyout]
{
	keyout <- [[dict]Left >>]_First[keylist] {}
	{
		If[[[dict]Bits >>] = [0u32]]
		{
			keyout <- keylist
		}{
			keyout <- [[dict]Straight >>]_First[[keylist]Append[[[dict]Bits >>]-[1u32]]]
		}
	}
}

First@Dictionary[dict:out,none]
{
	typeid, node <- [dict]_First Type ID
	rawkey <- [node]_First[List[]]
	out <- [Build[Blueprint From ID[typeid]]]From Dict Key[rawkey]
}

Next@Dictionary[dict,cur:out,none]
{
	[cur]Dict Type ID
	{ rawkey,typeid,none <- [dict]_Next Type ID[[~]+[1u32], cur] }
	
	out <- [Build[Blueprint From ID[typeid]]]From Dict Key[rawkey]
}

_Next@Empty Dictionary[dict:out,none]
{
	none <- dict
}

_Next Type ID@Empty Dictionary[dict:out,typeid,none]
{
	none <- dict
}

_Next Type ID@Dictionary[dict,curtypeid,key:out,typeid,none]
{
	If[[[dict]Bits >>]=[curtypeid]]
	{
		out <- [[dict]Straight >>]_Next[key,0,List[]]
		{ typeid <- [curtypeid]-[1u32] }
		{
			typeid,,none <- [[dict]Right >>]_First Type ID {}
			{ 
				l <- List[]
				out <- [~]_First[l] {}
			}
		}
	}{
		If[[curtypeid]<[[dict]Bits >>]]
		{
			out,typeid <- [[dict]Left >>]_Next Type ID[curtypeid,key] {} {}
			{
				typeid <- [curtypeid]-[1u32]
				out <- [[dict]Straight >>]_First[List[]]
			}
		}{
			out,typeid,none <- [[dict]Right >>]_Next Type ID[curtypeid,key]
		}
	}
}

_Next@Dictionary[dict,key,keyidx,newkey:out,none]
{		
	[key]Dict Bits[keyidx] 
	{ bits <- [~]+[1u32] }
	{ bits <- 0u32 }
	
	If[[[dict]Bits >>]=[bits]]
	{
		,goright <- If[bits]	
		{
			out,goright <- [[dict]Straight >>]_Next[key,[keyidx]+[1], [newkey]Append[[bits]-[1u32]]]
		}
		Val[goright]
		{
			out,none <- [[dict]Right >>]_First[newkey]
		}	
	}{
		If[[bits]<[[dict]Bits >>]]
		{
			out <- [[dict]Left >>]_Next[key, keyidx, newkey] {}
			{ out <- [[dict]Straight >>]_First[[newkey]Append[[[dict]Bits >>]-[1u32]]] }
		}{
			out,none <- [[dict]Right >>]_Next[key, keyidx, newkey]
		}
	}
}

_String Seq[str,val,key:out]
{
	out <- [[[[str
		]Append["\n\t"]
		]Append[String[key]]
		]Append[":\t"]
		]Append[String[val]]
}


String@Dictionary[dict:out]
{
	out <- Fold[_String Seq[?], "Dictionary", dict]
}

_Print Seq[dict,key:out]
{
	val <- [[String[[dict]Index[key]]]Split["\n"]]Join["\n\t"]
	Print[ [[["\t"]Append[String[key]]]Append[":\t"]]Append[val] ]
	{
		,out <- [dict]Next[key]
		{
			out <- _Print Seq[dict, ~]
		}
	}
}

Print@Dictionary[dict:out]
{
	Print["Dictionary"]
	{ out <- _Print Seq[dict, [dict]First] }
}

Print@Empty Dictionary[dict:out]
{
	out <- Print["Dictionary\n\t{Empty}"]
}

New Like@Dictionary[in:out]
{
	out <- Dictionary[]
}

New Like@Empty Dictionary[in:out]
{
	out <- in
}

Length@Empty Dictionary[dict:out]
{
	out <- 0
}

Length@Dictionary[dict:out]
{
	If[[dict]Bits >>]
	{
		out <- [Length[[dict]Straight >>]]+[[Length[[dict]Left >>]] + [Length[[dict]Right >>]]]
	}{
		out <- [1]+[[Length[[dict]Left >>]] + [Length[[dict]Right >>]]]
	}
}

_Combine[source,dest,key:out]
{
	new dest <- [dest]Set[key, [source]Index[key]]
	[source]Next[key]
	{
		out <- _Combine[source, new dest, ~]
	}{
		out <- Val[new dest]
	}
}

Combine[source,dest:out]
{
	[source]First
	{
		out <- _Combine[source, dest, ~]
	}{
		out <- dest
	}
}

Call@Dictionary[dict,index:out,notfound]
{
	out,notfound <- [dict]Index[index]
}

Call@Empty Dictionary[dict,index:out,notfound]
{
	notfound <- index
}