Fix last() behavior for history: use constant-time getLast() in case of LinkedList

This commit is contained in:
Grigorii Kirgizov 2019-07-30 14:04:17 +03:00 committed by Fedor Isakov
parent c072097fb6
commit 9b472f5c15
2 changed files with 7 additions and 3 deletions

View File

@ -46,7 +46,7 @@ interface MatchJournal : MutableIterable<MatchJournal.Chunk> {
/**
* Indicates whether journal's pos points to its end.
*/
fun isFront(): Boolean = currentPos().chunk == this.last()
fun isFront(): Boolean
/**

View File

@ -47,13 +47,13 @@ internal open class MatchJournalImpl(
if (view == null) {
nextChunkId = 0
val initChunk = MatchChunk(nextChunkId++, InitRuleMatch)
hist = IteratorMutableList(ArrayList<Chunk>().apply { add(initChunk) })
hist = IteratorMutableList(LinkedList<Chunk>().apply { add(initChunk) })
} else {
// assert that initial chunk is present
with (view.chunks.first()) {
assert(this is MatchChunk && match is InitRuleMatch)
}
hist = IteratorMutableList(ArrayList(view.chunks as List<Chunk>))
hist = IteratorMutableList(LinkedList(view.chunks as List<Chunk>))
nextChunkId = view.nextChunkId
}
}
@ -91,6 +91,8 @@ internal open class MatchJournalImpl(
override fun currentPos(): MatchJournal.Pos = current.toPos()
override fun isFront(): Boolean = current == hist.last
override fun resetPos() {
posPtr = hist.listIterator()
}
@ -165,6 +167,8 @@ internal open class MatchJournalImpl(
override fun iterator(): MutableIterator<E> = l.iterator()
override fun listIterator(): MutableListIterator<E> = l.listIterator()
override fun listIterator(index: Int): MutableListIterator<E> = l.listIterator(index)
val last: E get() = if (l is LinkedList) l.last else l.last()
}
private class IndexImpl(chunks: Iterable<Chunk>): MatchJournal.Index