1. feature req: limited stack

For example:

include std/stack.e
stack MyStack = new(FILO, 20) 
for i = 1 to 21 do 
  push(MyStack, i) 
end for 
? size(MyStack) 
? last(MyStack) 
? top(MyStack)

Output:

20 
2 
21 

It is useful for programming cached structures. May be, better to create my own type "cache"? Something similar to map, but with timestamps of last access.

new topic     » topic index » view message » categorize

2. Re: feature req: limited stack

My fast, but not very useful decision:

export function new(integer Size) 
  return repeat({{}, {}}, Size) 
end function 
 
export function cram(sequence Cache, object Key, object Value) 
  return append(Cache[2..$], {Key, Value}) 
end function 
 
export function stir(sequence Cache, object Key) 
  integer i = length(Cache) 
  loop do 
    if equal(Cache[i][1], Key) then 
      Cache = append(remove(Cache, i), Cache[i]) 
      exit 
    end if 
    i -= 1 
    until i = 0 
  end loop 
  return Cache 
end function 
 
export function lick(sequence Cache) 
  return Cache[$][2] 
end function

Usage/Testing:

include std/unittest.e
include cache.e 
 
test_equal("new", {{{}, {}}, {{}, {}}, {{}, {}}}, new(3)) 
 
constant NUMBERS = { 
"odin", "dva", "tri", "chetyre", "pyat", "shest", "sem", "vosem", "devyat", "desyat", "odinnadtzat" 
} 
 
sequence Cache = new(10) 
for i = 1 to 11 do 
  Cache = cram(Cache, NUMBERS[i], i) 
end for 
 
Cache = stir(Cache, "odin") 
test_not_equal("eject", 1, lick(Cache)) 
 
Cache = stir(Cache, "tri") 
test_equal("pull", 3, lick(Cache)) 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu