DataStructures.jl icon indicating copy to clipboard operation
DataStructures.jl copied to clipboard

Reconsider behavior of inclusive()/exclusive() wrt before-start and past-end semitokens

Open tjgreen42 opened this issue 8 years ago • 3 comments

Hi all,

The current treatment of before-start and past-end semitokens as parameters to inclusive(sc, st1, st2) is confusing, and not entirely in sync with the documentation: an out-of-bounds exception is thrown if either st1 or st2 is one of these semitokens, unless st1 is the past-end semitoken (and st2 is not), or st2 is the before-start semitoken (and st1 is not).

I'm not sure what the rationale for this behavior is, but it seems to me it'd be natural to expect inclusive(sc, st1, st2) to iterate over all st in sc such that st is >= st1 and <= st2 (via compare). This is a clean and easy-to-understand semantics which relaxes the current implementation and leaves fewer corner cases for the library consumer to have to worry about. For example, inclusive(sc, beforestartsemitoken(sc), pastendsemitoken(sc)) would then exactly coincide with semitokens(sc).

If people buy this argument, I could volunteer to produce a patch. (And even if not, the documentation and implementation still need to be brought in sync; I could volunteer to do this too.)

Thanks,

TJ

tjgreen42 avatar Jan 24 '18 20:01 tjgreen42

I disagree with the proposed redefinition of inclusive(sc, beforestartsemitoken(sc), pastendsemitoken(sc)). The idea of inclusive(sc,t1,t2) is that a loop is generated that includes both the items pointed to by t1 and t2. Since the before-start and past-end semitokens do not point to anything, the proposed redefinition would introduce new and potentially confusing cases. Note that you can get the requested behavior in the current system using the first and last semitokens instead of the before-start and past-end tokens (provided the container is nonempty).

But I agree that the corner cases in which an error is thrown versus those in which the loop is not executed are confusing and possibly incorrect. This may have to do with the order in which start/done/next are checked when a for-loop is compiled. Assuming that the first part of your proposal (namely, to redefine inclusive(sc, beforestartsemitoken(sc), pastendsemitoken(sc))) is not adopted, then what do you think are appropriate cases for when an error should be thrown versus cases when the loop should not get executed? Maybe the simplest logic is that the use of either before-start or past-end in either argument positiion is automatically an error? Would this be a breaking change for many users?

StephenVavasis avatar Jan 24 '18 21:01 StephenVavasis

Thanks Stephen for the quick feedback. If the proposal is not adopted, then I think I agree with you that simply ruling out the use of before-start and past-end would be simplest. I can't speculate on the scope of breakage, except to note that the current behavior (as documented, if not as implemented) does seem to have been quite deliberate, so probably somebody is relying on it.

tjgreen42 avatar Jan 24 '18 22:01 tjgreen42

After further thought, I decided to leave the current operation of inclusive as is, but to clarify the documentation. This is in: https://github.com/JuliaCollections/DataStructures.jl/pull/384

To recap: the current behavior of inclusive(sc, st1, st2) is: it is an error if st1 is the before-start token or if st2 is the past-end token. All other cases are not erroneous, although they may lead to zero loop iterations.

The rationale for this behavior is as follows. It makes the code more complicated if st2 can be the past-end token because the first thing the code does is increment st2 to compute the loop termination condition. It also makes the code more complicated if st1 is the before-start token since the iteration is supposed to give back the item pointed to by st1 on its first iteration. But on the other hand, there ought to be a way to set up a case in which the loop is iterated zero times, because one purpose of the inclusive loop is to take two semitokens computed by searchsortedequalrange (that delimit a range of entries in a SortedMultiDict with the same key). It is possible that there are no entries with that key, in which case searchsortedequalrange returns the delimiters past-end-token, before-start-token.

StephenVavasis avatar May 22 '18 01:05 StephenVavasis