-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
209 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
selector/src/commonMain/kotlin/li/songe/selector/data/ConnectExpression.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package li.songe.selector.data | ||
|
||
import li.songe.selector.NodeSequenceFc | ||
|
||
sealed class ConnectExpression { | ||
abstract val isConstant: Boolean | ||
abstract val minOffset: Int | ||
|
||
internal abstract val traversal: NodeSequenceFc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
selector/src/commonMain/kotlin/li/songe/selector/data/TupleExpression.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package li.songe.selector.data | ||
|
||
import li.songe.selector.NodeSequenceFc | ||
import li.songe.selector.util.filterIndexes | ||
|
||
data class TupleExpression( | ||
val numbers: List<Int>, | ||
) : ConnectExpression() { | ||
override val isConstant = numbers.size == 1 | ||
override val minOffset = (numbers.firstOrNull() ?: 1) - 1 | ||
private val indexes = numbers.map { x -> x - 1 } | ||
override val traversal: NodeSequenceFc = object : NodeSequenceFc { | ||
override fun <T> invoke(sq: Sequence<T?>): Sequence<T?> { | ||
return sq.filterIndexes(indexes) | ||
} | ||
} | ||
|
||
override fun toString(): String { | ||
if (numbers.size == 1) { | ||
return if (numbers.first() == 1) { | ||
"" | ||
} else { | ||
numbers.first().toString() | ||
} | ||
} | ||
return "(${numbers.joinToString(",")})" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
selector/src/commonMain/kotlin/li/songe/selector/util/FilterIndexesSequence.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package li.songe.selector.util | ||
|
||
internal class FilterIndexesSequence<T>( | ||
private val sequence: Sequence<T>, | ||
private val indexes: List<Int>, | ||
) : Sequence<T> { | ||
override fun iterator() = object : Iterator<T> { | ||
val iterator = sequence.iterator() | ||
var seqIndex = 0 // sequence | ||
var i = 0 // indexes | ||
var nextItem: T? = null | ||
|
||
fun calcNext(): T? { | ||
if (seqIndex > indexes.last()) return null | ||
while (iterator.hasNext()) { | ||
val item = iterator.next() | ||
if (indexes[i] == seqIndex) { | ||
i++ | ||
seqIndex++ | ||
return item | ||
} | ||
seqIndex++ | ||
} | ||
return null | ||
} | ||
|
||
override fun next(): T { | ||
val result = nextItem | ||
nextItem = null | ||
return result ?: calcNext() ?: throw NoSuchElementException() | ||
} | ||
|
||
override fun hasNext(): Boolean { | ||
nextItem = nextItem ?: calcNext() | ||
return nextItem != null | ||
} | ||
} | ||
} | ||
|
||
internal fun <T> Sequence<T>.filterIndexes(indexes: List<Int>): Sequence<T> { | ||
return FilterIndexesSequence(this, indexes) | ||
} |