Skip to content

Commit

Permalink
new kotlin + number methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdullinAM committed Nov 3, 2023
1 parent 922e350 commit 82e53c2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<artifactId>kt-helper</artifactId>
<groupId>org.vorpal.research</groupId>
<packaging>jar</packaging>
<version>0.1.12</version>
<version>0.1.13</version>

<properties>
<jvm.version>1.8</jvm.version>
Expand All @@ -17,7 +17,7 @@
<maven.compiler.source>${jvm.version}</maven.compiler.source>
<maven.compiler.target>${jvm.version}</maven.compiler.target>

<kotlin.version>1.8.21</kotlin.version>
<kotlin.version>1.9.20</kotlin.version>
<kotlin.code.style>official</kotlin.code.style>
<junit.version>4.13.2</junit.version>

Expand Down
42 changes: 39 additions & 3 deletions src/main/kotlin/org/vorpal/research/kthelper/numbers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ operator fun Number.compareTo(other: Number): Int = when (this) {
else -> unreachable("Unknown numeric type")
}

fun Number.shl(bits: Int): Number = when (this) {
infix fun Number.shl(bits: Int): Number = when (this) {
is Long -> this.toLong().shl(bits)
is Int -> this.toInt().shl(bits)
is Short -> this.toShort().shl(bits)
Expand All @@ -101,7 +101,7 @@ fun Number.shl(bits: Int): Number = when (this) {
else -> unreachable("Unknown numeric type")
}

fun Number.shr(bits: Int): Number = when (this) {
infix fun Number.shr(bits: Int): Number = when (this) {
is Long -> this.toLong().shr(bits)
is Int -> this.toInt().shr(bits)
is Short -> this.toShort().shr(bits)
Expand All @@ -111,7 +111,7 @@ fun Number.shr(bits: Int): Number = when (this) {
else -> unreachable("Unknown numeric type")
}

fun Number.ushr(bits: Int): Number = when (this) {
infix fun Number.ushr(bits: Int): Number = when (this) {
is Long -> this.toLong().ushr(bits)
is Int -> this.toInt().ushr(bits)
is Short -> this.toShort().ushr(bits)
Expand Down Expand Up @@ -150,3 +150,39 @@ infix fun Number.xor(other: Number): Number = when (this) {
is Float -> this.toFloat() xor other.toFloat()
else -> unreachable("Unknown numeric type")
}

fun minOf(vararg numbers: Number): Number {
if (numbers.isEmpty()) throw IllegalStateException()
var min = numbers.first()
for (i in 1..numbers.lastIndex) {
if (numbers[i] < min) min = numbers[i]
}
return min
}

fun maxOf(vararg numbers: Number): Number {
if (numbers.isEmpty()) throw IllegalStateException()
var max = numbers.first()
for (i in 1..numbers.lastIndex) {
if (numbers[i] > max) max = numbers[i]
}
return max
}

fun minOf(numbers: Collection<Number>): Number {
if (numbers.isEmpty()) throw IllegalStateException()
var min = numbers.first()
for (num in numbers) {
if (num < min) min = num
}
return min
}

fun maxOf(numbers: Collection<Number>): Number {
if (numbers.isEmpty()) throw IllegalStateException()
var max = numbers.first()
for (num in numbers) {
if (num > max) max = num
}
return max
}

0 comments on commit 82e53c2

Please sign in to comment.