Re: Constructive criticism

new topic     » goto parent     » topic index » view thread      » older message » newer message
SDPringle said...

How did you get push( s, 1 ) to work? When did you add pass-by-reference?

Pass-by-reference has not been added and is not needed for this implementation.

SDPringle said...

I think EUPHORIA's sequences are simple enough not to need stack routines. How about append() and t = s[$] s = s[1..$-1].

You are probably right, but push(s, x) and pop(s) are easy and you don't have to remember if the 'top-of-the-stack' is the first or the last item in the sequence.

The implementation is really very straight forward ...

include std/error.e 
 
/* private */ sequence stacks = {}  
 
/* private */ procedure validate(integer stack) 
   if stack < 1 or stack > length(stacks) then 
       crash("The stack identifier '%d' is invalid", stack) 
   end if 
end procedure 
 
public function new()  
    integer n 
     
    n = find(0, stacks) 
    if n != 0 then 
        stacks[n] = {} 
        return n 
    end if 
    stacks = append(stacks, {})  
    return length(stacks)  
end function  
 
public procedure delete(integer stack) 
    validate(stack) 
    stacks[stack] = 0 
end procedure 
 
public procedure push(integer stack, object item)  
    validate(stack) 
    stacks[stack] = append(stacks[stack], item) 
end procedure  
 
public function pop(integer stack)  
    object temp = {}  
     
    validate(stack) 
    if length(stacks[stack]) > 0 then  
        temp = {stacks[stack][$]}  
        stacks[stack] = stacks[stack][1 .. $-1]  
    end if  
    return  temp 
end function 
 
public function depth(integer stack) 
    validate(stack) 
    return length(stacks[stack]) 
end function 
 
public procedure dup(integer stack) 
    validate(stack) 
    if length(stacks[stack]) > 0 then 
        stacks[stack] = append(stacks[stack], stacks[stack][$]) 
    end if 
end procedure 
   
public procedure drop(integer stack) 
    validate(stack) 
    if length(stacks[stack]) > 0 then 
        stacks[stack] = stacks[stack][1 .. $-1]  
    end if 
end procedure 
    
public procedure swap(integer stack) 
    object temp 
 
    validate(stack) 
    if length(stacks[stack]) > 1 then 
        temp = stacks[stack][$] 
        stacks[stack][$] = stacks[stack][$-1] 
        stacks[stack][$-1] = temp 
    end if 
end procedure 
 
public procedure rot(integer stack) 
    object temp 
 
    validate(stack) 
    if length(stacks[stack]) > 2 then 
        temp = stacks[stack][$] 
        stacks[stack][$] = stacks[stack][$-1] 
        stacks[stack][$-1] = stacks[stack][$-2] 
        stacks[stack][$-2] = temp 
    end if 
end procedure 
 
public procedure rrot(integer stack) 
    object temp 
     
    validate(stack) 
    if length(stacks[stack]) > 2 then 
        temp = stacks[stack][$-2] 
        stacks[stack][$-2] = stacks[stack][$-1] 
        stacks[stack][$-1] = stacks[stack][$] 
        stacks[stack][$] = temp 
    end if 
end procedure 

And with the v4.0's inlining action, most of these small function calls are actually inlined, which removes the calling overheads.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu