Re: binary search with reference to append

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

Derek,

This is the last I will say on this as it is getting personal.

What I objected to in the first place was the logical prelude in binary_search which:

1 was erroneous.

2 was making inferences about the call which went beyond what was reasonable.

It seems to me that the prelude code is defensive programming with a vengeance. It seems to be trying to make sense of start_points and end_points which are invalid, returning positions which do not make good sense and so on.

Your code is not correct, sorry.

I think what Jaygade wrote or the first of mine (with sp and ep) are both simpler and better.

sp = start_point  
    if end_point <= 0 then  
        -- End point specified is relative to the end of the sequence.  
        ep = length(haystack) + end_point  
    else  
        ep = end_point             -- ep=3 => length?? (G) 
    end if  
      
    if ep > length(haystack) then  -- this cannot happen (G) 
        ep = length(haystack)  
    end if  
    if sp < 1 then  
        sp = 1  
    end if  
    if sp > ep then  
        if sp = ep + 1 then        -- if ep = length??   (G) 
            return -sp  -- Obviously the needle is never in an empty sequence/slice.  
                        -- if sp > ep then why assume the slice starts at sp? 
                        -- with ep = length -sp is out of range. (G) 
        end if  
        return 0  -- Slice starts after the end.  
    end if  

Look it's hard to write this kind of code because you are forever faced with questions:

What do I do in this particular situation?,
Have I covered every possible case?,
What is the meaning of all this?
Should I even try to cover this?

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

Search



Quick Links

User menu

Not signed in.

Misc Menu