Re: binary search with reference to append

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

This is an example of why you should require the caller validates the parameters of a called function.

While I agree with "you should require the caller validates the parameters of a called function", I don't think this is a good example.

gimlet said...

The function messes with its parameters.

No, it does not modify it's parameters.

gimlet said...

	lo = max(sp, 1)              -- lo could be > N 
	hi = min(N, ep)              -- hi could be < lo 
	hi = (lo > hi && N) ? N : hi -- hi could be > N  

Agreed, a sanity bounds check here would not hurt - but if sp or ep is out of bounds, it's an error. (Ideally, it'd be an exception or a flag, not an uncatchable run-time error.)

gimlet said...

	mid = sp                     -- this is unwise 

That is harmless, as in the case where lo <= hi, mid is immediately reset without the old value being used. So this is for the case where lo > hi, in which case the function returns immediately, stating that the record would be inserted into the first position. (Actually, maybe this only makes sense when dealing with the empty sequence?)

gimlet said...

	c = 0                        -- the match (set to 0 - found) 

Again, harmless. In the case where lo <= hi, c is immediately reset without the old value being used as well. Where lo > hi, c=0 is still harmless, it just means we don't adjust the offset returned by 1 (as we would do in the case where lo<=hi but the item was not found and the last object in the haystack search range < needle).

gimlet said...

--  at this point consider 
--     if lo > hi   we have mid = sp, c = 0, a return of -sp (sp may be -ve) 

What the heck is ve? Anyways, this case is designed for the empty sequence. I concede that if sp and ep are both longer than the length of the sequence, the return value here may be weird, causing logic errors or a run-time crash later on.

gimlet said...

--     if N = 0 then  
--        if lo <= hi     we have a crash. 

If both sp and ep are out of bounds. Conceded. Maybe this is the right thing to do, though, since we normally crash on out of bounds errors and it's better to fail early.

gimlet said...

--     if N != 0 then 
--           if lo > N     we have a crash. 
--           if lo <= N then 
--              if hi > N  we have a potential crash. 

So if either sp or ep are out of bounds, we could have a crash. I don't like the 'potential crash' here, I think if it's out of bounds, we should detect it early and report back on it somehow.

So, that's definitely a bug. Conceded.

gimlet said...

--     if N = 0 then  
--        if lo > hi      we have mid = sp, c = 0, a return of -sp (sp may be -ve) 

Didn't you already say this?

gimlet said...

Perhaps you may say this is all so remote because 'almost always' it suceeds

No, you've definitely found something here. 'almost always' is not good enough in this case.

gimlet said...

if A was null then any return was nonsense

"A was null" means "A = {}" ? Assuming that this is what you mean,

Yeah, I need to think about this one some more.

gimlet said...

if A was not empty was the range sp..ep valid?

Yes, that definitely needs to be checked. But what to do if there's an out-of-bounds error?

gimlet said...

Tell me, in how many ways is that function wrong?

Just one - lack of proper bounds checking.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu