Skip to content

Commit 76b2f49

Browse files
committed
Use checkElementIndex and checkPositionIndex for common index checks
1 parent cd1f73f commit 76b2f49

File tree

4 files changed

+29
-45
lines changed

4 files changed

+29
-45
lines changed

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableList/PersistentVector.kt

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package kotlinx.collections.immutable.implementations.immutableList
1818

1919
import kotlinx.collections.immutable.PersistentList
20+
import kotlinx.collections.immutable.internal.ListImplementation.checkElementIndex
21+
import kotlinx.collections.immutable.internal.ListImplementation.checkPositionIndex
2022

2123
/**
2224
* Persistent vector made of a trie of leaf buffers entirely filled with [MAX_BUFFER_SIZE] elements and a tail having
@@ -87,9 +89,7 @@ internal class PersistentVector<E>(private val root: Array<Any?>,
8789

8890

8991
override fun add(index: Int, element: E): PersistentList<E> {
90-
if (index < 0 || index > size) {
91-
throw IndexOutOfBoundsException() // TODO: diagnostic message
92-
}
92+
checkPositionIndex(index, size)
9393
if (index == size) {
9494
return add(element)
9595
}
@@ -154,9 +154,7 @@ internal class PersistentVector<E>(private val root: Array<Any?>,
154154
}
155155

156156
override fun removeAt(index: Int): PersistentList<E> {
157-
if (index < 0 || index >= size) {
158-
throw IndexOutOfBoundsException()
159-
}
157+
checkElementIndex(index, size)
160158
val rootSize = rootSize()
161159
if (index >= rootSize) {
162160
return removeFromTailAt(root, rootSize, rootShift, index - rootSize)
@@ -276,9 +274,7 @@ internal class PersistentVector<E>(private val root: Array<Any?>,
276274
}
277275

278276
override fun listIterator(index: Int): ListIterator<E> {
279-
if (index < 0 || index > size) {
280-
throw IndexOutOfBoundsException()
281-
}
277+
checkPositionIndex(index, size)
282278
@Suppress("UNCHECKED_CAST")
283279
return PersistentVectorIterator(root, tail as Array<E>, index, size, rootShift / LOG_MAX_BUFFER_SIZE + 1)
284280
}
@@ -300,18 +296,14 @@ internal class PersistentVector<E>(private val root: Array<Any?>,
300296
}
301297

302298
override fun get(index: Int): E {
303-
if (index < 0 || index >= size) {
304-
throw IndexOutOfBoundsException()
305-
}
299+
checkElementIndex(index, size)
306300
val buffer = bufferFor(index)
307301
@Suppress("UNCHECKED_CAST")
308302
return buffer[index and MAX_BUFFER_SIZE_MINUS_ONE] as E
309303
}
310304

311305
override fun set(index: Int, element: E): PersistentList<E> {
312-
if (index < 0 || index >= size) {
313-
throw IndexOutOfBoundsException()
314-
}
306+
checkElementIndex(index, size)
315307
if (rootSize() <= index) {
316308
val newTail = tail.copyOf(MAX_BUFFER_SIZE)
317309
newTail[index and MAX_BUFFER_SIZE_MINUS_ONE] = element

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableList/PersistentVectorBuilder.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package kotlinx.collections.immutable.implementations.immutableList
1818

1919
import kotlinx.collections.immutable.PersistentList
20+
import kotlinx.collections.immutable.internal.ListImplementation.checkElementIndex
21+
import kotlinx.collections.immutable.internal.ListImplementation.checkPositionIndex
2022

2123
private class Marker
2224

@@ -129,9 +131,8 @@ class PersistentVectorBuilder<E>(private var vector: PersistentList<E>,
129131
}
130132

131133
override fun add(index: Int, element: E) {
132-
if (index < 0 || index > size) {
133-
throw IndexOutOfBoundsException()
134-
}
134+
checkPositionIndex(index, size)
135+
135136
if (index == size) {
136137
add(element)
137138
return
@@ -193,9 +194,8 @@ class PersistentVectorBuilder<E>(private var vector: PersistentList<E>,
193194
}
194195

195196
override fun get(index: Int): E {
196-
if (index < 0 || index >= size) {
197-
throw IndexOutOfBoundsException()
198-
}
197+
checkElementIndex(index, size)
198+
199199
val buffer = bufferFor(index)
200200
return buffer[index and MAX_BUFFER_SIZE_MINUS_ONE] as E
201201
}
@@ -214,9 +214,8 @@ class PersistentVectorBuilder<E>(private var vector: PersistentList<E>,
214214
}
215215

216216
override fun removeAt(index: Int): E {
217-
if (index < 0 || index >= size) {
218-
throw IndexOutOfBoundsException()
219-
}
217+
checkElementIndex(index, size)
218+
220219
modCount += 1
221220

222221
val rootSize = rootSize()
@@ -321,9 +320,7 @@ class PersistentVectorBuilder<E>(private var vector: PersistentList<E>,
321320
}
322321

323322
override fun set(index: Int, element: E): E {
324-
if (index < 0 || index >= size) {
325-
throw IndexOutOfBoundsException()
326-
}
323+
checkElementIndex(index, size)
327324
if (rootSize() <= index) {
328325
val mutableTail = makeMutable(tail)
329326
val oldElement = mutableTail[index and MAX_BUFFER_SIZE_MINUS_ONE]

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableList/SmallPersistentVector.kt

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package kotlinx.collections.immutable.implementations.immutableList
1818

1919
import kotlinx.collections.immutable.ImmutableList
2020
import kotlinx.collections.immutable.PersistentList
21+
import kotlinx.collections.immutable.internal.ListImplementation.checkElementIndex
22+
import kotlinx.collections.immutable.internal.ListImplementation.checkPositionIndex
2123
import kotlinx.collections.immutable.mutate
2224

2325
internal class SmallPersistentVector<E>(private val buffer: Array<Any?>) : ImmutableList<E>, AbstractPersistentList<E>() {
@@ -72,9 +74,7 @@ internal class SmallPersistentVector<E>(private val buffer: Array<Any?>) : Immut
7274
}
7375

7476
override fun addAll(index: Int, c: Collection<E>): PersistentList<E> {
75-
if (index < 0 || index > size) {
76-
throw IndexOutOfBoundsException() // TODO: message
77-
}
77+
checkPositionIndex(index, size)
7878
if (size + c.size <= MAX_BUFFER_SIZE) {
7979
val newBuffer = bufferOfSize(size + c.size)
8080
buffer.copyInto(newBuffer, endIndex = index)
@@ -89,9 +89,7 @@ internal class SmallPersistentVector<E>(private val buffer: Array<Any?>) : Immut
8989
}
9090

9191
override fun add(index: Int, element: E): PersistentList<E> {
92-
if (index < 0 || index > size) {
93-
throw IndexOutOfBoundsException() // TODO: message
94-
}
92+
checkPositionIndex(index, size)
9593
if (index == size) {
9694
return add(element)
9795
}
@@ -113,9 +111,7 @@ internal class SmallPersistentVector<E>(private val buffer: Array<Any?>) : Immut
113111
}
114112

115113
override fun removeAt(index: Int): PersistentList<E> {
116-
if (index < 0 || index >= size) {
117-
throw IndexOutOfBoundsException()
118-
}
114+
checkElementIndex(index, size)
119115
if (size == 1) {
120116
return EMPTY
121117
}
@@ -137,24 +133,18 @@ internal class SmallPersistentVector<E>(private val buffer: Array<Any?>) : Immut
137133
}
138134

139135
override fun listIterator(index: Int): ListIterator<E> {
140-
if (index < 0 || index > size) {
141-
throw IndexOutOfBoundsException() // TODO: Message
142-
}
136+
checkPositionIndex(index, size)
143137
return BufferIterator(buffer as Array<E>, index, size)
144138
}
145139

146140
override fun get(index: Int): E {
147141
// TODO: use elementAt(index)?
148-
if (index < 0 || index >= size) {
149-
throw IndexOutOfBoundsException() // TODO: Message (element index)
150-
}
142+
checkElementIndex(index, size)
151143
return buffer[index] as E
152144
}
153145

154146
override fun set(index: Int, element: E): PersistentList<E> {
155-
if (index < 0 || index >= size) {
156-
throw IndexOutOfBoundsException()
157-
}
147+
checkElementIndex(index, size)
158148
val newBuffer = buffer.copyOf()
159149
newBuffer[index] = element
160150
return SmallPersistentVector(newBuffer)

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/internal/ListImplementation.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@ package kotlinx.collections.immutable.internal
1818

1919
internal object ListImplementation {
2020

21+
@JvmStatic
2122
internal fun checkElementIndex(index: Int, size: Int) {
2223
if (index < 0 || index >= size) {
2324
throw IndexOutOfBoundsException("index: $index, size: $size")
2425
}
2526
}
2627

28+
@JvmStatic
2729
internal fun checkPositionIndex(index: Int, size: Int) {
2830
if (index < 0 || index > size) {
2931
throw IndexOutOfBoundsException("index: $index, size: $size")
3032
}
3133
}
3234

35+
@JvmStatic
3336
internal fun checkRangeIndexes(fromIndex: Int, toIndex: Int, size: Int) {
3437
if (fromIndex < 0 || toIndex > size) {
3538
throw IndexOutOfBoundsException("fromIndex: $fromIndex, toIndex: $toIndex, size: $size")
@@ -39,6 +42,7 @@ internal object ListImplementation {
3942
}
4043
}
4144

45+
@JvmStatic
4246
internal fun orderedHashCode(c: Collection<*>): Int {
4347
var hashCode = 1
4448
for (e in c) {
@@ -47,6 +51,7 @@ internal object ListImplementation {
4751
return hashCode
4852
}
4953

54+
@JvmStatic
5055
internal fun orderedEquals(c: Collection<*>, other: Collection<*>): Boolean {
5156
if (c.size != other.size) return false
5257

0 commit comments

Comments
 (0)