Skip to content

Commit de4b5c9

Browse files
committed
Added Octal and UOctal types.
1 parent d8436cc commit de4b5c9

File tree

2 files changed

+131
-0
lines changed
  • subprojects/user-defined-integrals-in-kotlin/src/main/kotlin/org/sdkotlin/integral

2 files changed

+131
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.sdkotlin.integral
2+
3+
@JvmInline
4+
value class Octal internal constructor(
5+
internal val data: Int,
6+
) : Integral<Octal> {
7+
8+
companion object {
9+
10+
val MIN_VALUE = Octal(Int.MIN_VALUE)
11+
val MAX_VALUE = Octal(Int.MAX_VALUE)
12+
val NEGATIVE_ONE = Octal(-1)
13+
val ZERO = Octal(0)
14+
val ONE = Octal(1)
15+
16+
internal fun octalRemainder(v1: Octal, v2: Octal): Octal =
17+
Octal((v1.toLong() % v2.toLong()).toInt())
18+
}
19+
20+
internal constructor(octalValue: String) : this(octalValue.toInt(8))
21+
22+
override val minValue: Octal get() = MIN_VALUE
23+
override val maxValue: Octal get() = MAX_VALUE
24+
override val zero: Octal get() = ZERO
25+
override val one: Octal get() = ONE
26+
27+
override operator fun plus(other: Octal): Octal =
28+
Octal(data.plus(other.data))
29+
30+
override operator fun minus(other: Octal): Octal =
31+
Octal(data.minus(other.data))
32+
33+
override operator fun unaryMinus(): Octal =
34+
Octal(-data)
35+
36+
override operator fun rem(other: Octal): Octal =
37+
octalRemainder(this, other)
38+
39+
fun mod(other: Octal): Octal = rem(other)
40+
41+
override fun compareTo(other: Octal): Int =
42+
data.compareTo(other.data)
43+
44+
operator fun rangeTo(other: Octal): ClosedRange<Octal> =
45+
IntegralRange(this, other)
46+
47+
operator fun rangeUntil(other: Octal): OpenEndRange<Octal> =
48+
IntegralRange(this, other - other.one)
49+
50+
fun toUInt(): UInt = data.toUInt()
51+
52+
fun toInt(): Int = data
53+
54+
fun toLong(): Long = data.toLong()
55+
56+
fun toUOctal(): UOctal = UOctal(data)
57+
58+
override fun toString() = "%o".format(data)
59+
}
60+
61+
fun Int.toOctal(): Octal = Octal(this)
62+
63+
fun String.toOctal(): Octal = Octal(this)
64+
65+
infix fun Octal.downTo(to: Octal): IntegralProgression<Octal> =
66+
IntegralProgression.fromClosedRange(this, to, Octal.NEGATIVE_ONE)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.sdkotlin.integral
2+
3+
@JvmInline
4+
value class UOctal internal constructor(
5+
internal val data: Int,
6+
) : Integral<UOctal> {
7+
8+
companion object {
9+
10+
val MIN_VALUE = UOctal(Int.MIN_VALUE)
11+
val MAX_VALUE = UOctal(Int.MAX_VALUE)
12+
val ZERO = UOctal(0)
13+
val ONE = UOctal(1)
14+
15+
internal fun uOctalRemainder(v1: UOctal, v2: UOctal): UOctal =
16+
UOctal((v1.toLong() % v2.toLong()).toInt())
17+
}
18+
19+
internal constructor(octalValue: String) : this(octalValue.toInt(8))
20+
21+
override val minValue: UOctal get() = MIN_VALUE
22+
override val maxValue: UOctal get() = MAX_VALUE
23+
override val zero: UOctal get() = ZERO
24+
override val one: UOctal get() = ONE
25+
26+
override operator fun plus(other: UOctal): UOctal =
27+
UOctal(data.plus(other.data))
28+
29+
override operator fun minus(other: UOctal): UOctal =
30+
UOctal(data.minus(other.data))
31+
32+
override operator fun unaryMinus(): UOctal =
33+
UOctal(-data)
34+
35+
override operator fun rem(other: UOctal): UOctal =
36+
uOctalRemainder(this, other)
37+
38+
fun mod(other: UOctal): UOctal = rem(other)
39+
40+
override fun compareTo(other: UOctal): Int =
41+
data.compareTo(other.data)
42+
43+
operator fun rangeTo(other: UOctal): OpenEndRange<UOctal> =
44+
IntegralRange(this, other)
45+
46+
operator fun rangeUntil(other: UOctal): OpenEndRange<UOctal> =
47+
IntegralRange(this, other - other.one)
48+
49+
fun toUInt(): UInt = data.toUInt()
50+
51+
fun toInt(): Int = data
52+
53+
fun toLong(): Long = data.toLong() and 0xFFFF_FFFF
54+
55+
fun toOctal(): Octal = data.toOctal()
56+
57+
override fun toString() = "%o".format(data)
58+
}
59+
60+
fun UInt.toUOctal(): UOctal = UOctal(this.toInt())
61+
62+
fun String.toUOctal(): UOctal = UOctal(this)
63+
64+
infix fun UOctal.downTo(to: UOctal): IntegralProgression<UOctal> =
65+
IntegralProgression.fromClosedRange(this, to, Octal.NEGATIVE_ONE.toUOctal())

0 commit comments

Comments
 (0)