groostav
08/29/2023, 2:34 PMvalue class CoolZeroCostSequence(val arr: IntArray) { ...}
if i wanted to write
val seq: CoolZeroCostSequence = makeArraySequenceThing()
for(int in seq){
}
and I wanted to avoid any boxing, which is to say, I do not want to create an instance of Iterator<Int>
because that would box up my elements, how can I do it?groostav
08/29/2023, 2:36 PMvalue class TraversalSequence(val array: IntArray){
// want similar behaviour to Array<NodeEncounter> but actually IntArray
}
value class NodeEncounter(val idx: Int){
val isLeave: Boolean get() = idx < 0
val index: Int get() = idx.let { if(it > 0) it else -1*it }
}
groostav
08/29/2023, 2:41 PMfor
needs an operator fun iterator(): Iterator<Int>
, but that requres boxing... can i give you back an object with an operator fun next()
on it instead?franztesca
08/29/2023, 2:56 PMYoussef Shoaib [MOD]
08/29/2023, 3:21 PMIterator
interface, they only need to have operator fun next()
and operator fun hasNext(): Boolean
. So what you can do is something like this:
class IntIterator(private val arr: IntArray, private var index: Int = 0) {
operator fun next(): Int = arr[index++]
operator fun hasNext(): Boolean = index < arr.length
}
operator fun CoolZeroCostSequence.iterator(): IntIterator = IntIterator(arr)
ephemient
08/29/2023, 10:07 PMgroostav
08/30/2023, 2:49 AMfor(elem in somethingThatsIntIterator)
?Youssef Shoaib [MOD]
08/30/2023, 6:59 AMephemient
08/30/2023, 7:09 AM