RE: On indexes

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

-----Original Message-----
From: christian.cuvier at education.gouv.fr
[mailto:christian.cuvier at education.gouv.fr]

> 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?

> 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.

This came up a while back.  After some help from Jiri, I came up with the
following iterative fetch and recursive store functions.  It should only
copy the most deeply nested sequence within b.  Mostly, only pointers are
copied (internally).  Pass c = 0 to simply assign b = a.  c = {1,0} = {1}.
I use this in my database code to fetch and store nested fields.

-- return a[b...]
function seq_fetch(object a, sequence b)
    
    for i = 1 to length(b) do
    
        a = a[b[i]]
        
    end for
    
    return a
end function

-- store a in b at subcript c
function seq_store(object a, object b, object c)
    integer len
    
    if atom(c) then
        c = {c}
    end if
    
    len = length(c)
    
    -- now it will insert a new element!
    if c[1] = -1 or length(b) + 1 = c[1] then
        return b & { a }
    elsif len > 1 then
        -- recursively go into the sequence
        
        b[c[1]] = seq_store(a, b[c[1]], c[2..len] )
        return b
    end if
    
    -- get the index
    c = c[1]
    
    if c then
        b[c] = a
    else
        b = a    
    end if
    
    return b
end function


Matt Lewis

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

Search



Quick Links

User menu

Not signed in.

Misc Menu