The file SetLike.scala
has a misleading comment; it would be great to fix this.
I stumbled upon it using IntelliJ’s goto definition features.
Here is the function in question with comment. The problem is that the comment claims that the empty Set {} IS NOT included in the set of subsets. However, my experimentation with the function indicates that it IS included.
/** An Iterator including all subsets containing exactly len elements.
* If the elements in 'This' type is ordered, then the subsets will also be in the same order.
* ListSet(1,2,3).subsets => {{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}}
*
* @author Eastsun
* @date 2010.12.6
*/
private class SubsetsItr(elms: IndexedSeq[A], len: Int) extends AbstractIterator[This] {
private val idxs = Array.range(0, len+1)
private var _hasNext = true
idxs(len) = elms.size
def hasNext = _hasNext
def next(): This = {
if (!hasNext) Iterator.empty.next()
val buf = self.newBuilder
idxs.slice(0, len) foreach (idx => buf += elms(idx))
val result = buf.result()
var i = len - 1
while (i >= 0 && idxs(i) == idxs(i+1)-1) i -= 1
if (i < 0) _hasNext = false
else {
idxs(i) += 1
for (j <- (i+1) until len)
idxs(j) = idxs(j-1) + 1
}
result
}
}