view list.rhope @ 130:147dfc703161

Add String method to List
author Mike Pavone <pavone@retrodev.com>
date Fri, 05 Nov 2010 02:42:45 +0000
parents f4fc0a98088a
children 50c97b448f44
line wrap: on
line source


Blueprint List Leaf
{
	Buffer
}

Index@List Leaf[list,index:out,not found]
{
	out, not found <- [[list]Buffer >>]Index[index]
}

Set@List Leaf[list,index,value:out,invalid index]
{
	If[[index] < [0]]
	{
		rev index <- [[[list]Buffer >>]Length]+[index]
		invalid index <- If[[rev index] < [0]] {}
		{
			out,invalid index <- [list]Set[rev index, value]
		}
			
	}{
		len <- [[list]Buffer >>]Length
		If[[index] > [len]]
		{
			makeleft <- Yes
		}{
			If[[[index] > [7]] And [[index] >= [len]]]
			{
				makeleft <- Yes
			}{
				out <- [list]Buffer <<[ [[list]Buffer >>]Set[index, value] ]
			}
		}
	}

	Val[makeleft]
	{
		out <- [[[[[[Build[List()]
			]Buffer << [[Array[]]Append[value]]
			]Left << [list]
			]Right << [List[]]
			]Offset << [index]
			]Right Offset <<[[index]+[8i32]]
			]Length << [ [[list]Length]+[1] ]
	}
}

_Right Set@List Leaf[list,index,val:out,didn't set]
{
	len <- [[list]Buffer >>]Length
	do it <- If[[index] < [len]] {}
	{
		,didn't set <- If[[index]=[len]]
		{
			didn't set,do it <- If[[index]>[7]]
		}
	}
	Val[do it]
	{
		out <- [list]Set[index,val]
	}
}

Length@List Leaf[list:out]
{
	out <- [[list]Buffer >>]Length
}

Last@List Leaf[list:out,none]
{
	len <- [[list]Buffer >>]Length
	,none <-If[len]
	{
		out <- [len]-[1]
	}
}

Append@List Leaf[list,val:out]
{
	[list]Last
	{ index <- [~]+[1] }
	{ index <- 0 }
	out <- [list]Set[index, val]
}

First@List Leaf[list:out,none]
{
	[[list]Buffer >>]Index[0]
	{ out <- 0 }
	{ none <- Yes }
}

Next@List Leaf[list,index:next,none]
{
	pos next <- [index]+[1]
	,none <- If[[pos next] < [[list]Length]]
	{
		next <- Val[pos next]
	}
}

Blueprint List
{
	Buffer
	Left
	Right
	Offset(Int32,Naked)
	Right Offset(Int32,Naked)
	Length(Int32,Naked)
}

List[:out(List)]
{
	out <- [Build[List Leaf()]]Buffer <<[Array[]]
}

Index@List[list,index:out,not found]
{
	If[[index]<[[list]Offset >>]]
	{
		out, not found <- [[list]Left >>]Index[index]
	}{
		If[[index] < [[list]Right Offset >>]]
		{
			out, not found <- [[list]Buffer >>]Index[[index]-[[list]Offset >>]]
		}{
			out, not found <- [[list]Right >>]Index[[index]-[[list]Right Offset >>]]
		}
	}
}

Length@List[list:out]
{
	out <- [list]Length >>
}

Set@List[list,index,val:out,invalid index]
{
	If[[index] < [0]]
	{
		, invalid index <- [list]Last
		{ rev index <- [[~]+[1]]+[index] }
		invalid index <- If[[rev index] < [0]] {}
		{ out,invalid index <- [list]Set[rev index, val] }
		
	}{
		If[[index]<[[list]Offset >>]]
		{
			lsize <- [[list]Left >>]Length
			[[list]Left >>]Set[index, val]
			{
				out <- [[list]Left <<[~]
					]Length <<[ [[list]Length >>]+[[[~]Length]-[lsize]] ]
			}
		}{	
			
			If[[index]<[[list]Right Offset >>]]
			{
				off index <- [index]-[[list]Offset >>]
				bsize <- [[list]Buffer >>]Length
				If[[off index]>[bsize]]
				{
					If[[[list]Right >>]Length]
					{
						my end <- [[list]Offset >>]+[[[list]Buffer >>]Length]
						nroffset <- [[[index]-[my end]]/[2]]+[my end]
						nright <- out <- [[[[[[Build[List()]
							]Buffer << [[Array[]]Append[val]]
							]Left << [List[]]
							]Right << [[list]Right >>]
							]Offset << [[index]-[nroffset]]
							]Right Offset <<[ [[list]Right Offset>>]-[nroffset] ]
							]Length << [ [[[list]Right >>]Length]+[1] ]

						out <- [[[list]Right <<[nright]
							]Length <<[ [[list]Length >>]+[1] ]
							]Right Offset <<[nroffset]
					}{
						out <- [[[list]Right <<[ [[list]Right >>]Set[0, val] ]
							]Right Offset <<[index]
							]Length <<[ [[list]Length >>]+[1] ]
					}
				}{
					[[list]Buffer >>]Set[off index, val]
					{
						out <- [[list]Buffer <<[~]
							]Length <<[ [[list]Length >>]+[[[~]Length >>]-[bsize]] ]
					}
				}
			}{
				rsize <- [[list]Right>>]Length
				adj ind <- [index]-[[list]Right Offset>>]
				If[[[[list]Left>>]Length] > [rsize]]
				{
					[[list]Right >>]Set[adj ind, val]
					{
						out <- [[list]Right <<[~]
							]Length <<[ [[list]Length >>]+[[[~]Length]-[rsize]] ]
					}
				}{
					[[list]Right >>]_Right Set[adj ind, val]
					{
						out <- [[list]Right <<[~]
							]Length <<[ [[list]Length >>]+[[[~]Length]-[rsize]] ]
					}{
						out <- [[[[[[Build[List()]
							]Buffer << [[Array[]]Append[val]]
							]Left << [list]
							]Right << [List[]]
							]Offset << [index]
							]Right Offset <<[[index]+[8i32]]
							]Length << [ [[list]Length]+[1] ]
					}
				}
			}
		}
	}
}

_Right Set@List[list,index,val:out,didn't set]
{
	If[[[list]Right Offset >>]>[index]]
	{
		out <- [list]Set[index, val]
	}{
		,didn't set <- [[list]Right >>]_Right Set[[index]-[[list]Right Offset >>], val]
		{ out <- [list]Right <<[~] }
	}
}

Last@List[list:out,none]
{
	[[list]Right >>]Last 
	{ out <- [~]+[[list]Right Offset >>] }
	{ out <- [[[[list]Buffer >>]Length]-[1]]+[[list]Offset >>] }
}

Append@List[list,val:out]
{
	[list]Last
	{ index <- [~]+[1] }
	{ index <- 0 }
	out <- [list]Set[index, val]
}

First@List[list:out,none]
{
	If[[[list]Left >>]Length]
	{
		out <- [[list]Left >>]First
	}{
		out <- [list]Offset >>
	}
}

Next@List[list,index:next,none]
{
	If[[index] < [[[list]Offset >>]-[1]]]
	{
		next <- [[list]Left >>]Next[index] {}
		{ next <- Offset >>[list] }
	}{
		If[[index] < [[list]Right Offset >>]]
		{
			pos next <- [index]+[1]
			If[[pos next] < [[[[list]Buffer >>]Length]+[[list]Offset >>]]]
			{
				next <- Val[pos next]
			}{
				,none <- [[list]Right >>]First
				{ next <- [~]+[[list]Right Offset >>] }
			}
		}{
			,none <- [[list]Right >>]Next[[index]-[[list]Right Offset >>]]
			{ next <- [~]+[[list]Right Offset >>] }
		}
	}
}

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

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

//TODO: Implement a more efficent version of this
_Tail[list, cur, dest:out]
{
	ndest <- [dest]Append[[list]Index[cur]]
	[list]Next[cur]
	{
		out <- _Tail[list, ~, ndest]
	}{
		out <- Val[ndest]
	}
}
Tail[list,start:out]
{
	newlist <- New Like[list]
	[list]Index[start]
	{
		out <- _Tail[list, start, newlist]
	}{
		out <- Val[newlist]
	}
}

Concatenate[left,right:out]
{
	out <- Fold[Append[?], left, right]
}

Print@List Leaf[list:out]
{
	If[[[list]Buffer >>]Length]
	{
		Print["List"]
		{ out <- _Print Seq[list, [list]First] }
	}{
		out <- Print["List\n\t{Empty}"]
	}	
}

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

String@List Leaf[list:out]
{
	If[[[list]Buffer >>]Length]
	{
		out <- Fold[_String Seq[?], "List", list]
	}{
		out <- "List\n\t{Empty}"
	}
}

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

Print@List[list:out]
{
	Print["List"]
	{ out <- _Print Seq[list, [list]First] }
}


Peek@List[list:out,empty]
{
	[list]Last
	{
		out <- [list]Index[~]
	}{
		empty <- list
	}
}

Peek@List Leaf[list:out,empty]
{
	[list]Last
	{
		out <- [list]Index[~]
	}{
		empty <- list
	}
}

_Check Present[check in,val,index:out]
{
	[check in]Index[index]
	{
		If[[~]=[val]]
		{ out <- No }
		{ out <- Yes }
	}{ 
		out <- Yes
	}
}

_=List[a,b:out]
{
	,out <- If[[[a]Length]=[[b]Length]]
	{
		[a]Find[_Check Present[b,?]]
		{
			out <- No
		}{
			out <- Yes
		}
	}
}

=@List Leaf[a,b:out]
{
	out <- _=List[a,b]
}

=@List[a,b:out]
{
	out <- _=List[a,b]
}