view dict.rhope @ 89:5a195ee08eac

Fix memory leak and bug that was preventing First@Dictionary from working properly
author Mike Pavone <pavone@retrodev.com>
date Sat, 31 Jul 2010 00:19:15 -0400
parents 3c4325e6298f
children c25d75c2440b
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 <<[4294967295u32] //Maximum 32-bit unsigned int value
}

_Set New[index,val,bits,bitindex:out]
{
	nbitindex <- [bitindex]+[1]
	[index]Dict Bits[nbitindex]
	{ straight <- _Set New[index,val,~,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]
{
	out <- _Set New[index,val,bits,bitindex]
}

Set@Empty Dictionary[dict,index,val:out]
{
	out <- _Set New[index,val, Dict Type ID[index], -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], -1]
}

_Index@Dictionary[dict,index,bits,bitindex:out,not found]
{
	If[[bits]=[[dict]Bits >>]]
	{
		If[[bits]=[4294967295u32]]
		{
			out <- Straight >>[dict]
		}{
			nbitindex <- [bitindex]+[1]
			nbits <- [index]Dict Bits[nbitindex] {}
			{ nbits <- 4294967295u32 }
			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], -1]
}

_Set@Dictionary[dict,index,val,bits,bitindex:out]
{
	If[[bits]=[[dict]Bits >>]]
	{
		If[[bits]=[4294967295u32]]
		{
			out <- [dict]Straight <<[val]
		}{
			nbitindex <- [bitindex]+[1]
			nbits <- [index]Dict Bits[nbitindex] {}
			{ nbits <- 4294967295u32 }
			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 >>
		node <- dict
	}
}

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

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

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