Re: Missing in misc.e

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

I'll start benchmarking later today hopefully, but here is a new version of the
min/max part of say math.e, taking into account some of the earlier discussion:

global constant  FB_MIN = 0,
                 FB_MAX = 8,
                 FB_INDEX = 1,
                 FB_VALUE = 2,
                 FB_PAIR = 4
constant M_CRASH_MESSAGE = 37,
         FB_ALT_MIN=1,
         FB_ALT_MAX=-1

integer find_max, starting_point,return_mode
sequence fb_seq
function find_bound()
   object min_or_max,y

   if starting_point > length(fb_seq) or starting_point < 1 then
       machine_proc(M_CRASH_MESSAGE,
         "math.e: Cannot find bounds of an empty sequence.")
       ?1/0 -- force run time error
   end if
   min_or_max=fb_seq[starting_point]
   for i=starting_point+1 to length(fb_seq) do
      y=fb_seq[i]
      if compare(min_or_max,y)=find_max then
         min_or_max=y
         starting_point=i
      end if
   end for
   if and_bits(return_mode, FB_PAIR) then
       if and_bits(return_mode,FB_INDEX) then
           return {starting_point,min_or_max}
       else
           return {min_or_max,starting_point}
       end if
   elsif and_bits(return_mode,FB_INDEX) then
      return starting_point
   else
      return min_or_max
   end if      
end function
       
global function find_min(sequence s,integer start)
   find_bound_mode=FB_ALT_MIN
   return_mode=FB_INDEX
   fb_seq = s
   starting_point = start
   return find_bound()
end function

global function find_max(sequence s,integer start)
   find_max=FB_ALT_MAX
   return_mode=FB_INDEX
   starting_point = start
   fb_seq = s
   return find_bound()
end function

global function get_min(sequence s,integer start)
   find_alt_max = FB_ALT_MIN
   return_mode=FB_VALUE
   starting_point = start
   fb_seq = s
   return find_bound()
end function

global function get_max(sequence s,integer start)
   find_bound_mode=FB_ALT_MAX
   return_mode=FB_VALUE
   starting_point = start
   fb_seq = s
   return find_bound()
end function

global function find_sequence_bounds(sequence s,integer start,integer mode)
    return_mode=and_bits(mode,FB_INDEX+FB_VALUE+FB_PAIR)
    if not return_mode then
        machine_proc(M_CRASH_MESSAGE,
          "math.e: find_sequence_bounds() doesn not know what to return.")
        ?1/0
    end if
    if and_bits(mode,FB_MAX) then
        find_bound_mode=FB_ALT_MAX
    else
        find_bound_mode=FB_ALT_MIN
    end if
    starting_point = start
    fb_seq=s
    return find_bound()
end function

function min(object x,object y)
   if compare(x,y)=-1 then
      return x
   else
      return y
   end if
end function

function max(object x,object y)
   if compare(x,y)=1 then
      return x
   else
      return y
   end if
end function


The basic idea is that you can have speed by using canned forms, or flexibility
using the general form, but not both at the same time. And it is not fair to
force a specific choice on the coder or the user.

I don't like the double test on return from find_bounds(), I'll see if I can
optimise it - or if it just doesn't matter anyway. After all, it's the only place
there's always a test, so it may be acceptable.

CChris

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

Search



Quick Links

User menu

Not signed in.

Misc Menu