Skip to content

Commit 7fcb699

Browse files
Introduce ConcatenatedListView for efficient list concatenation in internal datetime parsing logic.
1 parent b8bbed8 commit 7fcb699

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019-2025 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.internal.format.parser
7+
8+
internal class ConcatenatedListView<T>(val list1: List<T>, val list2: List<T>) : AbstractList<T>() {
9+
override val size: Int
10+
get() = list1.size + list2.size
11+
12+
override fun get(index: Int): T = if (index < list1.size) list1[index] else list2[index - list1.size]
13+
14+
override fun iterator(): Iterator<T> = ConcatenatedListViewIterator()
15+
16+
private inner class ConcatenatedListViewIterator : Iterator<T> {
17+
private val iterators: List<Iterator<T>> = buildList {
18+
collectIterators(list1)
19+
collectIterators(list2)
20+
}
21+
private var index = 0
22+
23+
private fun MutableList<Iterator<T>>.collectIterators(list: List<T>) {
24+
if (list is ConcatenatedListView<T>) {
25+
collectIterators(list.list1)
26+
collectIterators(list.list2)
27+
} else {
28+
add(list.iterator())
29+
}
30+
}
31+
32+
override fun hasNext(): Boolean {
33+
while (index < iterators.size && !iterators[index].hasNext()) {
34+
index++
35+
}
36+
return index < iterators.size
37+
}
38+
39+
override fun next(): T = iterators[index].next()
40+
}
41+
}

0 commit comments

Comments
 (0)