Skip to content

Commit

Permalink
Bitset
Browse files Browse the repository at this point in the history
  • Loading branch information
belyaev-mikhail committed Jul 26, 2022
1 parent 21ac4d1 commit 389f9c2
Show file tree
Hide file tree
Showing 8 changed files with 869 additions and 87 deletions.
759 changes: 759 additions & 0 deletions src/commonMain/kotlin/ru/spbstu/wheels/BitSet.kt

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/commonMain/kotlin/ru/spbstu/wheels/Functions.kt
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 src/commonMain/kotlin/ru/spbstu/wheels/collections/Adapters.kt

This file was deleted.

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)
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)
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)
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)
}

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)

0 comments on commit 389f9c2

Please sign in to comment.