On indexes

new topic     » topic index » view thread      » older message » newer message

C'est un message de format MIME en plusieurs parties.

------=_NextPart_000_009D_01C24AA7.2D4B5220
	charset="iso-8859-1"

Offsets are an universal solution for the base index problem, I think.

But there may be a problem with variable depth indexing. Assume we want =
to manipulate arbitrary trees in Euphoria. Trees are two-terms =
sequences: one object as root node content and a sequence of 0 or more =
trees (the subtrees originating from root).

Accessing an individual statement using ordinary indexes requires to =
know, before run time, its depth. Since generally we don't, the indexing =
scheme should be dynamic, so it must be a sequence. How to get or set an =
arbitrary tree subtree or element (a 0-depth node), given a tree, an =
index sequence and a value?

Getting the element is not a problem

type tree (sequence t)
atom istree
istree=3D1
if length(t)!=3D2 or sequence(t[2])=3D0 then return 0 end if
for i=3D1 to length(t[2]) do=20
    istree=3Dtree(t[2][i])
    if istree=3D0 then exit end if
end for
return istree
end type

function tree_get(tree t,sequence index)
if index=3D"" then return t[1] end if
--Flexibilty is OK but...
elsif not(atom(index[1])) then return "" end if
elsif length(index)=3D1 then return(t[2][index[1]][1])
else return tree_get(t[2][index[1]],index[2..length(index)])
end function

(did not care about out-of-bound indexes)

What about setting an element?
The way I found is to duplicate the tree with the modified element and =
return it. It seems a terrible waste of machine cycles and memory. If =
somebody knows better, I'd appreciate. If the interpreter is so smart =
that it does not really duplicate the structure, this rare feature needs =
be more thoroughly documented.

function altercopy(sequence seqofsubt, sequence index,object val)
sequence modseq
modseq=3D{}
for i=3D1 to length(seqofsubt) do
    if i!=3Dindex[1] then modseq=3Dappend(modseq,seqofsubt[i])
    else modseq=3Dappend(modseq,
            =
call_func(st_id,{seqofsubt[i],index[2..length(index)],setval}
    end if
end for
return modseq
end function

--Returns a whole tree with desired elmement mset to *val*
function tree_set(tree t, sequence index, object setval)
--No problem here...
if index=3D"" then return {setval,t[2]} end if
--Flexibilty is OK but...
elsif not(atom(index[1])) then return ""=20
else return {t[1],altercopy(t[2],index,val)}
end if
end function

integer st_id
st_id=3Droutine_id(tree_set)

Sorry for the potential mistypes.
I am not asking for a t[index]=3Dsetval syntax to be available - too =
much specialized-.

Hav a good week-end.

Chris

------=_NextPart_000_009D_01C24AA7.2D4B5220
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4616.200" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Offsets are an universal solution for =
the base=20
index problem, I think.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>But there may be a problem with =
variable depth=20
indexing. Assume we want to manipulate arbitrary trees in Euphoria. =
Trees are=20
two-terms sequences: one object as root node content and a sequence of 0 =
or more=20
trees (the subtrees originating from root).</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Accessing an individual statement using =
ordinary=20
indexes requires to know, before run time, its depth. Since generally we =
don't,=20
the indexing scheme should be dynamic, so it must be a sequence. How to =
get or=20
set an arbitrary tree subtree or element (a 0-depth node), given a tree, =
an=20
index sequence and a value?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Getting the element is not a =
problem</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>type tree (sequence t)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>atom istree</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>istree=3D1</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>if length(t)!=3D2&nbsp;or </FONT><FONT =
face=3DArial=20
size=3D2>sequence(t[2])=3D0 then return 0 end if</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>for i=3D1 to length(t[2]) do =
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;=20
istree=3Dtree(t[2][i])</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; if istree=3D0 then =
exit end=20
if</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>end for</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>return istree</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>end type</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>function tree_get(tree t,sequence=20
index)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>if index=3D"" then return t[1] end =
if</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>--Flexibilty is OK but...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>elsif not(atom(index[1])) then return =
"" end=20
if</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>elsif length(index)=3D1 then=20
return(t[2][index[1]][1])</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>else return=20
tree_get(t[2][index[1]],index[2..length(index)])</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>end function</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>(did not care about out-of-bound=20
indexes)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>What about setting an =
element?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>The way I found is to duplicate the =
tree with the=20
modified element and return it. It seems a terrible waste of machine =
cycles and=20
memory. If somebody knows better, I'd appreciate. If the interpreter is =
so smart=20
that it does not really duplicate the structure, this rare =
feature&nbsp;needs be=20
more thoroughly documented.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>function altercopy(sequence seqofsubt, =
sequence=20
index,object val)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>sequence modseq</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>modseq=3D{}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>for i=3D1 to length(seqofsubt) =
do</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; if i!=3Dindex[1] =
then=20
modseq=3Dappend(modseq,seqofsubt[i])</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;else=20
modseq=3Dappend(modseq,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp;=20
call_func(st_id,{seqofsubt[i],index[2..length(index)],setval}</FONT></DIV=
>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; end if</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>end for</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>return modseq</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>end function</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>--Returns a whole tree with desired =
elmement mset=20
to *val*</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>function tree_set(tree t,&nbsp;sequence =
index,=20
object setval)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>--No problem here...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>if index=3D"" then return {setval,t[2]} =
end=20
if</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>--Flexibilty is OK but...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>elsif not(atom(index[1])) then return =
""=20
</FONT></DIV>
<DIV></FONT><FONT face=3DArial size=3D2>else return=20
{t[1],altercopy(t[2],index,val)}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>end if</FONT></DIV></DIV>
<DIV><FONT face=3DArial size=3D2>end function</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>integer st_id</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2>st_id=3Droutine_id(tree_set)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Sorry for the potential =
mistypes.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I am not asking for a t[index]=3Dsetval =
syntax to be=20
available - too much specialized-.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Hav a good week-end.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>

------=_NextPart_000_009D_01C24AA7.2D4B5220--

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu