Skip to content

Commit 3a9f8af

Browse files
committed
- upgraded to kotlin 1.1.1
- restructuration complete
1 parent f98c3d1 commit 3a9f8af

File tree

12 files changed

+418
-406
lines changed

12 files changed

+418
-406
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
gradleScriptKotlin()
66
}
77
dependencies {
8-
classpath(kotlinModule("gradle-plugin", "1.1.0"))
8+
classpath(kotlinModule("gradle-plugin", "1.1.1"))
99
// classpath("com.github.jengelman.gradle.plugins:shadow:1.2.3")
1010
}
1111
}
@@ -22,7 +22,7 @@ repositories {
2222
}
2323

2424
dependencies {
25-
compile(kotlinModule("stdlib", "1.1.0"))
25+
compile(kotlinModule("stdlib", "1.1.1"))
2626
testCompile("com.github.elect86:kotlintest:d8878d6da0944ec6bcbcdad6a1540bba021d768d")
2727
}
2828

src/main/kotlin/unsigned/BigInteger.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ package unsigned
77
import java.math.BigInteger
88

99

10-
infix fun BigInteger.ushr(bitCount: Int) = shiftRight(bitCount)
10+
infix fun BigInteger.ushr(bitCount: Int): BigInteger = shiftRight(bitCount)

src/main/kotlin/unsigned/Byte.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import kotlin.experimental.xor
1414
fun Byte.toUShort() = toUInt().toShort()
1515
fun Byte.toUInt() = toInt() and 0xff
1616
fun Byte.toULong() = toUInt().toLong()
17-
fun Byte.toBigInt() = BigInteger.valueOf(toULong())
1817

1918
fun Byte.toUbyte() = Ubyte(this)
2019
fun Byte.toUshort() = Ushort(toUInt())
2120
fun Byte.toUint() = Uint(toUInt())
2221
fun Byte.toUlong() = Ulong(toUInt())
2322

23+
fun Byte.toBigInt(): BigInteger = BigInteger.valueOf(toLong())
24+
fun Byte.toUBigInt(): BigInteger = BigInteger.valueOf(toULong())
25+
2426
val Byte.ub
2527
get() = toUbyte()
2628
val Byte.ui

src/main/kotlin/unsigned/Int.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import java.math.BigInteger
1111
fun Int.toUByte() = toByte()
1212
fun Int.toUShort() = toShort()
1313
fun Int.toULong() = toLong() and 0xffffffffL
14-
fun Int.toBigInt() = BigInteger.valueOf(toULong())
1514

1615
fun Int.toUbyte() = Ubyte(this)
1716
fun Int.toUshort() = Ushort(this)
1817
fun Int.toUint() = Uint(this)
1918
fun Int.toUlong() = Ulong(toULong())
2019

20+
fun Int.toBigInt(): BigInteger = BigInteger.valueOf(toLong())
21+
fun Int.toUBigInt(): BigInteger = BigInteger.valueOf(toULong())
22+
2123
val Int.ub
2224
get() = toUbyte()
2325
val Int.ui
@@ -40,7 +42,7 @@ infix fun Int.ushr(b: Short) = (toULong() ushr b.toUInt()).toInt()
4042
infix fun Int.udiv(b: Int) = (toULong() / b.toULong()).toInt()
4143
infix fun Int.urem(b: Int) = (toULong() % b.toULong()).toInt()
4244
infix fun Int.ucmp(b: Int) = toULong().compareTo(b.toULong())
43-
infix fun Int.ushr(b: Int) = (toULong() ushr b).toInt()
45+
// Int.ushr is already offered by the Kotlin lib
4446

4547
infix fun Int.udiv(b: Long) = (toBigInt() / b.toBigInt()).toInt()
4648
infix fun Int.urem(b: Long) = (toBigInt() % b.toBigInt()).toInt()

src/main/kotlin/unsigned/Long.kt

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import java.math.BigInteger
88
fun Long.toUByte() = toByte()
99
fun Long.toUShort() = toShort()
1010
fun Long.toUInt() = toInt()
11-
fun Long.toBigInt() = BigInteger(java.lang.Long.toUnsignedString(this))
1211

1312
fun Long.toUbyte() = Ubyte(this)
1413
fun Long.toUshort() = Ushort(this)
1514
fun Long.toUint() = Uint(this)
1615
fun Long.toUlong() = Ulong(this)
1716

17+
fun Long.toBigInt(): BigInteger = BigInteger.valueOf(this)
18+
fun Long.toUBigInt(): BigInteger = BigInteger(java.lang.Long.toUnsignedString(this))
19+
1820
val Long.ub
1921
get() = toUbyte()
2022
val Long.ui
@@ -24,24 +26,24 @@ val Long.ul
2426
val Long.us
2527
get() = toUshort()
2628

27-
infix fun Long.udiv(b: Byte) = (toBigInt() / b.toBigInt()).toLong()
28-
infix fun Long.urem(b: Byte) = (toBigInt() % b.toBigInt()).toLong()
29-
infix fun Long.ucmp(b: Byte) = toBigInt().compareTo(b.toBigInt())
30-
infix fun Long.ushr(b: Byte) = (toBigInt() ushr b.toUInt()).toLong()
29+
infix fun Long.udiv(b: Byte) = (toUBigInt() / b.toUBigInt()).toLong()
30+
infix fun Long.urem(b: Byte) = (toUBigInt() % b.toUBigInt()).toLong()
31+
infix fun Long.ucmp(b: Byte) = toUBigInt().compareTo(b.toUBigInt())
32+
infix fun Long.ushr(b: Byte) = (toUBigInt() ushr b.toUInt()).toLong()
3133

32-
infix fun Long.udiv(b: Short) = (toBigInt() / b.toBigInt()).toLong()
33-
infix fun Long.urem(b: Short) = (toBigInt() % b.toBigInt()).toLong()
34-
infix fun Long.ucmp(b: Short) = toBigInt().compareTo(b.toBigInt())
35-
infix fun Long.ushr(b: Short) = (toBigInt() ushr b.toUInt()).toLong()
34+
infix fun Long.udiv(b: Short) = (toUBigInt() / b.toUBigInt()).toLong()
35+
infix fun Long.urem(b: Short) = (toUBigInt() % b.toUBigInt()).toLong()
36+
infix fun Long.ucmp(b: Short) = toUBigInt().compareTo(b.toUBigInt())
37+
infix fun Long.ushr(b: Short) = (toUBigInt() ushr b.toUInt()).toLong()
3638

37-
infix fun Long.udiv(b: Int) = (toBigInt() / b.toBigInt()).toLong()
38-
infix fun Long.urem(b: Int) = (toBigInt() % b.toBigInt()).toLong()
39-
infix fun Long.ucmp(b: Int) = toBigInt().compareTo(b.toBigInt())
39+
infix fun Long.udiv(b: Int) = (toUBigInt() / b.toUBigInt()).toLong()
40+
infix fun Long.urem(b: Int) = (toUBigInt() % b.toUBigInt()).toLong()
41+
infix fun Long.ucmp(b: Int) = toUBigInt().compareTo(b.toUBigInt())
4042
// Long.ushr(b: Int) offered by Kotlin lib
4143

42-
infix fun Long.udiv(b: Long) = (toBigInt() / b.toBigInt()).toLong()
43-
infix fun Long.urem(b: Long) = (toBigInt() % b.toBigInt()).toLong()
44-
infix fun Long.ucmp(b: Long) = toBigInt().compareTo(b.toBigInt())
44+
infix fun Long.udiv(b: Long) = (toUBigInt() / b.toUBigInt()).toLong()
45+
infix fun Long.urem(b: Long) = (toUBigInt() % b.toUBigInt()).toLong()
46+
infix fun Long.ucmp(b: Long) = toUBigInt().compareTo(b.toUBigInt())
4547
// no Long ushr Long
4648

4749

@@ -53,9 +55,9 @@ infix fun Long.or(b: Ubyte) = this or b.toLong()
5355
infix fun Long.xor(b: Ubyte) = this xor b.toLong()
5456
infix fun Long.shl(b: Ubyte) = this shl b.toInt()
5557

56-
infix fun Long.udiv(b: Ubyte) = (toBigInt() / b.toBigInt()).toLong()
57-
infix fun Long.urem(b: Ubyte) = (toBigInt() % b.toBigInt()).toLong()
58-
infix fun Long.ucmp(b: Ubyte) = toBigInt().compareTo(b.toBigInt())
58+
infix fun Long.udiv(b: Ubyte) = (toUBigInt() / b.toBigInt()).toLong()
59+
infix fun Long.urem(b: Ubyte) = (toUBigInt() % b.toBigInt()).toLong()
60+
infix fun Long.ucmp(b: Ubyte) = toUBigInt().compareTo(b.toBigInt())
5961
infix fun Long.ushr(b: Ubyte) = this ushr b.toInt()
6062

6163

@@ -67,10 +69,10 @@ infix fun Long.or(b: Ushort) = this or b.toLong()
6769
infix fun Long.xor(b: Ushort) = this xor b.toLong()
6870
infix fun Long.shl(b: Ushort) = this shl b.toInt()
6971

70-
infix fun Long.udiv(b: Ushort) = (toBigInt() / b.toBigInt()).toLong()
71-
infix fun Long.urem(b: Ushort) = (toBigInt() % b.toBigInt()).toLong()
72-
infix fun Long.ucmp(b: Ushort) = toBigInt().compareTo(b.toBigInt())
73-
infix fun Long.ushr(b: Ushort) = (toBigInt() ushr b.toInt()).toLong()
72+
infix fun Long.udiv(b: Ushort) = (toUBigInt() / b.toBigInt()).toLong()
73+
infix fun Long.urem(b: Ushort) = (toUBigInt() % b.toBigInt()).toLong()
74+
infix fun Long.ucmp(b: Ushort) = toUBigInt().compareTo(b.toBigInt())
75+
infix fun Long.ushr(b: Ushort) = (toUBigInt() ushr b.toInt()).toLong()
7476

7577

7678
operator fun Long.plus(b: Uint) = this + b.v
@@ -81,10 +83,10 @@ infix fun Long.or(b: Uint) = this or b.toLong()
8183
infix fun Long.xor(b: Uint) = this xor b.toLong()
8284
infix fun Long.shl(b: Uint) = this shl b.v
8385

84-
infix fun Long.udiv(b: Uint) = (toBigInt() / b.toBigInt()).toLong()
85-
infix fun Long.urem(b: Uint) = (toBigInt() % b.toBigInt()).toLong()
86-
infix fun Long.ucmp(b: Uint) = toBigInt().compareTo(b.toBigInt())
87-
infix fun Long.ushr(b: Uint) = (toBigInt() ushr b.toInt()).toLong()
86+
infix fun Long.udiv(b: Uint) = (toUBigInt() / b.toBigInt()).toLong()
87+
infix fun Long.urem(b: Uint) = (toUBigInt() % b.toBigInt()).toLong()
88+
infix fun Long.ucmp(b: Uint) = toUBigInt().compareTo(b.toBigInt())
89+
infix fun Long.ushr(b: Uint) = (toUBigInt() ushr b.toInt()).toLong()
8890

8991

9092
operator fun Long.plus(b: Ulong) = this + b.v
@@ -95,7 +97,7 @@ infix fun Long.or(b: Ulong) = this or b.toLong()
9597
infix fun Long.xor(b: Ulong) = this xor b.toLong()
9698
infix fun Long.shl(b: Ulong) = this shl b.toInt()
9799

98-
infix fun Long.udiv(b: Ulong) = (toBigInt() / b.toBigInt()).toLong()
99-
infix fun Long.urem(b: Ulong) = (toBigInt() % b.toBigInt()).toLong()
100-
infix fun Long.ucmp(b: Ulong) = toBigInt().compareTo(b.toBigInt())
100+
infix fun Long.udiv(b: Ulong) = (toUBigInt() / b.toBigInt()).toLong()
101+
infix fun Long.urem(b: Ulong) = (toUBigInt() % b.toBigInt()).toLong()
102+
infix fun Long.ucmp(b: Ulong) = toUBigInt().compareTo(b.toBigInt())
101103
// no Int ushr Ulong

src/main/kotlin/unsigned/Short.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import kotlin.experimental.xor
1414
fun Short.toUByte() = toByte()
1515
fun Short.toUInt() = toInt() and 0xffff
1616
fun Short.toULong() = toUInt().toLong()
17-
fun Short.toBigInt() = BigInteger.valueOf(toULong())
1817

1918
fun Short.toUbyte() = Ubyte(this)
2019
fun Short.toUshort() = Ushort(this)
2120
fun Short.toUint() = Uint(toUInt())
2221
fun Short.toUlong() = Ulong(toUInt())
2322

23+
fun Short.toBigInt(): BigInteger = BigInteger.valueOf(toLong())
24+
fun Short.toUBigInt(): BigInteger = BigInteger.valueOf(toULong())
25+
2426
val Short.ub
2527
get() = toUbyte()
2628
val Short.ui

src/main/kotlin/unsigned/Ubyte.kt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package unsigned
2+
3+
import java.math.BigInteger
4+
import kotlin.experimental.and
5+
import kotlin.experimental.inv
6+
7+
/**
8+
* Created by GBarbieri on 20.03.2017.
9+
*/
10+
11+
data class Ubyte(var v: Byte = 0) : Number() {
12+
13+
companion object {
14+
15+
/** A constant holding the minimum value an <code>unsigned byte</code> can have, 0. */
16+
const val MIN_VALUE = 0x00
17+
/** A constant holding the maximum value an <code>unsigned byte</code> can have, 2<sup>8</sup>-1. */
18+
const val MAX_VALUE = 0xff
19+
20+
fun checkSigned(v: Number) = v.toInt() in MIN_VALUE..MAX_VALUE
21+
}
22+
23+
constructor(number: Number) : this(number.toByte())
24+
@JvmOverloads constructor(string: String, base: Int = 10) : this(Integer.parseUnsignedInt(string.filter { it != '_' && it != '\'' }, base).toShort())
25+
26+
override fun toByte() = v
27+
override fun toShort() = v.toUInt().toShort()
28+
override fun toInt() = v.toUInt()
29+
override fun toLong() = v.toULong()
30+
31+
fun toBigInt(): BigInteger = BigInteger.valueOf(toLong())
32+
33+
override fun toDouble() = toInt().toDouble()
34+
override fun toFloat() = toInt().toFloat()
35+
36+
override fun toChar() = toInt().toChar()
37+
38+
fun toUbyte() = this
39+
fun toUshort() = Ushort(toInt())
40+
fun toUint() = Uint(toInt())
41+
fun toUlong() = Ulong(toInt())
42+
43+
operator fun inc() = Ubyte(v + 1)
44+
operator fun dec() = Ubyte(v - 1)
45+
46+
infix operator fun plus(b: Ubyte) = Ubyte(toInt() + b.toInt())
47+
infix operator fun plus(b: Byte) = Ubyte(toInt() + b.toUInt())
48+
infix operator fun plus(b: Int) = Ubyte(toInt() + b)
49+
50+
infix operator fun minus(b: Ubyte) = Ubyte(toInt() - b.toInt())
51+
infix operator fun minus(b: Byte) = Ubyte(toInt() - b.toUInt())
52+
infix operator fun minus(b: Int) = Ubyte(toInt() - b)
53+
54+
infix operator fun times(b: Ubyte) = Ubyte(toInt() * b.toInt())
55+
infix operator fun times(b: Byte) = Ubyte(toInt() * b.toUInt())
56+
infix operator fun times(b: Int) = Ubyte(toInt() * b)
57+
58+
infix operator fun div(b: Ubyte) = Ubyte(toInt() / b.toInt())
59+
infix operator fun div(b: Byte) = Ubyte(toInt() / b.toUInt())
60+
infix operator fun div(b: Int) = Ubyte(Integer.divideUnsigned(toInt(), b))
61+
62+
infix operator fun rem(b: Ubyte) = Ubyte(toInt() % b.toInt())
63+
infix operator fun rem(b: Byte) = Ubyte(toInt() % b.toUInt())
64+
infix operator fun rem(b: Int) = Ubyte(Integer.remainderUnsigned(toInt(), b))
65+
66+
// TODO add counterparts with res
67+
68+
infix fun and(b: Ubyte) = Ubyte(v and b.v)
69+
infix fun and(b: Byte) = Ubyte(v and b)
70+
infix fun and(b: Int) = Ubyte(toInt() and b)
71+
72+
infix fun or(b: Ubyte) = Ubyte(toInt() or b.toInt())
73+
infix fun or(b: Byte) = Ubyte(toInt() or b.toUInt())
74+
infix fun or(b: Int) = Ubyte(toInt() or b)
75+
76+
infix fun xor(b: Ubyte) = Ubyte(toInt() xor b.toInt())
77+
infix fun xor(b: Byte) = Ubyte(toInt() xor b.toUInt())
78+
infix fun xor(b: Int) = Ubyte(toInt() xor b)
79+
80+
infix fun shl(b: Ubyte) = Ubyte(toInt() shl b.toInt())
81+
infix fun shl(b: Byte) = Ubyte(toInt() shl b.toUInt())
82+
infix fun shl(b: Int) = Ubyte(toInt() shl b)
83+
84+
infix fun shr(b: Ubyte) = Ubyte(toInt() ushr b.toInt())
85+
infix fun shr(b: Byte) = Ubyte(toInt() ushr b.toUInt())
86+
infix fun shr(b: Int) = Ubyte(toInt() ushr b)
87+
88+
fun inv() = Ubyte(v.inv())
89+
90+
operator fun compareTo(b: Ubyte) = Integer.compareUnsigned(toInt(), b.toInt())
91+
operator fun compareTo(b: Byte) = Integer.compareUnsigned(toInt(), b.toUInt())
92+
operator fun compareTo(b: Int) = Integer.compareUnsigned(toInt(), b)
93+
}

src/main/kotlin/unsigned/Uint.kt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package unsigned
2+
3+
import java.math.BigInteger
4+
5+
/**
6+
* Created by GBarbieri on 20.03.2017.
7+
*/
8+
9+
data class Uint(var v: Int = 0) : Number() {
10+
11+
companion object {
12+
13+
/** A constant holding the minimum value an <code>unsigned int</code> can have, 0. */
14+
const val MIN_VALUE = 0x00000000
15+
16+
/** A constant holding the maximum value an <code>unsigned int</code> can have, 2<sup>32</sup>-1. */
17+
const val MAX_VALUE = 0xffffffffL
18+
19+
fun checkSigned(v: Number) = v.toLong() in MIN_VALUE..MAX_VALUE
20+
}
21+
22+
23+
constructor(number: Number) : this(number.toInt())
24+
@JvmOverloads constructor(string: String, base: Int = 10) : this(Integer.parseUnsignedInt(string.filter { it != '_' && it != '\'' }, base))
25+
26+
override fun toByte() = v.toByte()
27+
override fun toShort() = v.toShort()
28+
override fun toInt() = v
29+
override fun toLong() = v.toULong()
30+
31+
fun toBigInt(): BigInteger = BigInteger.valueOf(toLong())
32+
33+
override fun toDouble() = toLong().toDouble()
34+
override fun toFloat() = toLong().toFloat()
35+
36+
override fun toChar() = v.toChar()
37+
38+
operator fun inc() = Uint(v + 1)
39+
operator fun dec() = Uint(v - 1)
40+
41+
infix operator fun plus(b: Uint) = Uint(v + b.v)
42+
infix operator fun plus(b: Int) = Uint(v + b)
43+
44+
infix operator fun minus(b: Uint) = Uint(v - b.v)
45+
infix operator fun minus(b: Int) = Uint(v - b)
46+
47+
infix operator fun times(b: Uint) = Uint(v * b.v)
48+
infix operator fun times(b: Int) = Uint(v * b)
49+
50+
infix operator fun div(b: Uint) = Uint(Integer.divideUnsigned(v, b.toInt()))
51+
infix operator fun div(b: Int) = Uint(Integer.divideUnsigned(v, b))
52+
53+
infix operator fun rem(b: Uint) = Uint(Integer.remainderUnsigned(v, b.toInt()))
54+
infix operator fun rem(b: Int) = Uint(Integer.remainderUnsigned(v, b))
55+
56+
infix fun and(b: Uint) = Uint(v and b.toInt())
57+
infix fun and(b: Int) = Uint(v and b)
58+
59+
infix fun or(b: Uint) = Uint(v or b.toInt())
60+
infix fun or(b: Int) = Uint(v or b)
61+
62+
infix fun xor(b: Uint) = Uint(v xor b.toInt())
63+
infix fun xor(b: Int) = Uint(v xor b)
64+
65+
infix fun shl(b: Uint) = Uint(v shl b.toInt())
66+
infix fun shl(b: Int) = Uint(v shl b)
67+
68+
infix fun shr(b: Uint) = Uint(v ushr b.toInt())
69+
infix fun shr(b: Int) = Uint(v ushr b)
70+
71+
fun inv() = Uint(v.inv())
72+
73+
operator fun compareTo(b: Uint) = Integer.compareUnsigned(v, b.toInt())
74+
operator fun compareTo(b: Int) = Integer.compareUnsigned(v, b)
75+
76+
// TODO long?
77+
}

0 commit comments

Comments
 (0)