I'd like to check if a range is within another ran...
# stdlib
e
I'd like to check if a range is within another range, ie
i-2..i+2 in wd.indices
e
there's two possible answers, both reasonable for different purposes:
Copy code
this.start <= other.endInclusive && this.endInclusive >= other.start
this.start >= other.start && this.endInclusive <= other.endInclusive
plus additional considerations around what the result should be with empty ranges
e
I don't see any reason why one would interpret this as the first option
r
I - 2 in wd.indices && I + 2 in wd.indices
e
I'm doing exactly this atm
l
Thinking how operator
in
is implemented in Kotlin, it seems to imply that some element is contained in some *set*(collection), not subset. In java, there is
containsAll
, not
contains
.
k
This would work but would currently be too expensive:
Copy code
wd.indices.toSet().containsAll((i-2..i+2).toSet())
That's only because toSet() creates a LinkedHashSet. Perhaps it could be optimized to create a specialized Set that just keeps the range as its only property and whose
contains
method just forwards to the range, and defines a suitably specialized
containsAll
. Then the above code would have good performance. Or perhaps just a new
containsAll
method on ranges.
l
Yes, sorry for not being clear. I meant
contains
(which corresponds to
in
) does not work for set-to-set. (like
Set.contains(Set)
)