-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
21ac4d1
commit 389f9c2
Showing
8 changed files
with
869 additions
and
87 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,6 @@ | ||
package ru.spbstu.wheels | ||
|
||
inline fun <T> (() -> T).runAtMostOnce(): () -> T { | ||
val lazyRes = lazy(LazyThreadSafetyMode.SYNCHRONIZED, this) | ||
return { lazyRes.value } | ||
} |
87 changes: 0 additions & 87 deletions
87
src/commonMain/kotlin/ru/spbstu/wheels/collections/Adapters.kt
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
src/commonMain/kotlin/ru/spbstu/wheels/collections/adapters/CharArrayAsCharSequence.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,36 @@ | ||
package ru.spbstu.wheels.collections.adapters | ||
|
||
import ru.spbstu.wheels.collections.ICharSequence | ||
import ru.spbstu.wheels.collections.checkBounds | ||
|
||
class CharArrayAsCharSequence(val array: CharArray, | ||
val offset: Int = 0, | ||
val limit: Int = array.size): ICharSequence.Impl() { | ||
private fun checkIndex(index: Int) = index.also { | ||
checkBounds(index in array.indices) | ||
checkBounds(index < limit) | ||
} | ||
private fun checkLimit(limit: Int) = limit.also { | ||
checkBounds(limit in 0..array.size) | ||
} | ||
|
||
init { | ||
checkIndex(offset) | ||
checkLimit(limit) | ||
} | ||
|
||
override val length: Int | ||
get() = limit - offset | ||
|
||
override fun get(index: Int): Char = array[checkIndex(index + offset)] | ||
|
||
override fun subSequence(startIndex: Int, endIndex: Int): CharSequence { | ||
checkBounds(startIndex in 0..endIndex) | ||
checkBounds(startIndex < length) | ||
checkBounds(endIndex <= length) | ||
return CharArrayAsCharSequence(array, offset + startIndex, offset + endIndex) | ||
} | ||
} | ||
|
||
fun CharArray.asCharSequence(offset: Int = 0, limit: Int = size): CharSequence = | ||
CharArrayAsCharSequence(this, offset, limit) |
12 changes: 12 additions & 0 deletions
12
src/commonMain/kotlin/ru/spbstu/wheels/collections/adapters/CharSequenceAsList.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,12 @@ | ||
package ru.spbstu.wheels.collections.adapters | ||
|
||
import ru.spbstu.wheels.collections.IAbstractList | ||
|
||
class CharSequenceAsList(val charSequence: CharSequence): IAbstractList.Impl<Char>() { | ||
override val size: Int | ||
get() = charSequence.length | ||
|
||
override fun get(index: Int): Char = charSequence[index] | ||
} | ||
|
||
fun CharSequence.asList(): List<Char> = CharSequenceAsList(this) |
12 changes: 12 additions & 0 deletions
12
src/commonMain/kotlin/ru/spbstu/wheels/collections/adapters/ListAsCharSequence.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,12 @@ | ||
package ru.spbstu.wheels.collections.adapters | ||
|
||
import ru.spbstu.wheels.collections.ICharSequence | ||
|
||
class ListAsCharSequence(val list: List<Char>): ICharSequence.Impl() { | ||
override val length: Int | ||
get() = list.size | ||
|
||
override fun get(index: Int): Char = list[index] | ||
} | ||
|
||
fun List<Char>.asCharSequence(): CharSequence = ListAsCharSequence(this) |
15 changes: 15 additions & 0 deletions
15
src/commonMain/kotlin/ru/spbstu/wheels/collections/adapters/MapAsList.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,15 @@ | ||
package ru.spbstu.wheels.collections.adapters | ||
|
||
import ru.spbstu.wheels.collections.IAbstractList | ||
import ru.spbstu.wheels.collections.IAbstractMutableList | ||
|
||
class MapAsList<T>(val inner: Map<Int, T>): IAbstractList.Impl<T?>() { | ||
override val size: Int | ||
get() = when(inner.size) { | ||
0 -> 0 | ||
else -> inner.maxOf { it.key } + 1 | ||
} | ||
|
||
override fun get(index: Int): T? = inner.get(index) | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
src/commonMain/kotlin/ru/spbstu/wheels/collections/adapters/StringBuilderAsList.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,29 @@ | ||
package ru.spbstu.wheels.collections.adapters | ||
|
||
import ru.spbstu.wheels.collections.IAbstractMutableList | ||
|
||
class StringBuilderAsList(val sb: StringBuilder): IAbstractMutableList.Impl<Char>() { | ||
override val size: Int | ||
get() = sb.length | ||
|
||
override fun get(index: Int): Char = sb.get(index) | ||
override fun add(index: Int, element: Char) { sb.insert(index, element) } | ||
override fun removeAt(index: Int): Char { | ||
val cur = get(index) | ||
sb.deleteAt(index) | ||
return cur | ||
} | ||
|
||
override fun set(index: Int, element: Char): Char { | ||
val cur = get(index) | ||
sb.set(index, element) | ||
return cur | ||
} | ||
|
||
override fun clear() { | ||
sb.clear() | ||
} | ||
} | ||
|
||
fun StringBuilder.asList(): MutableList<Char> = StringBuilderAsList(this) | ||
|