From 4db4a3f667e89e3119f5580297ac9e4ee66a75bc Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Sun, 10 Nov 2024 20:59:28 +0300 Subject: [PATCH] Int variables --- .../kotlin/utilities/int_conversions_size.kt | 181 ++++++++++++++++-- .../kotlin/utilities/int_variables.kt | 126 ++++++++++++ 2 files changed, 287 insertions(+), 20 deletions(-) create mode 100644 src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_variables.kt diff --git a/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_size.kt b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_size.kt index 7be88dd..ad8a5d4 100644 --- a/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_size.kt +++ b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_conversions_size.kt @@ -106,6 +106,147 @@ public inline fun Int.extendToLong(): Long = this.toLong() @JvmName("extendToULong") public inline fun UInt.extendToULong(): ULong = this.toULong() +/******************** force trims *******************************/ + + +/** + * Converts this [Short] value to [Byte]. + * + * If this value is in [Byte.MIN_VALUE]..[Byte.MAX_VALUE], the resulting [Byte] value represents + * the same numerical value as this [Short]. + * + * The resulting [Byte] value is represented by the least significant 8 bits of this [Short] value. + */ +@JvmName("trimToByte") +public inline fun Short.trimToByte(): Byte = this.toByte() + + +/** + * Converts this [UShort] value to [UByte]. + * + * If this value is in 0..[UByte.MAX_VALUE], the resulting [UByte] value represents + * the same numerical value as this [UShort]. + * + * The resulting [UByte] value is represented by the least significant 8 bits of this [UShort] value. + */ +@JvmName("trimToUByte") +public inline fun UShort.trimToUByte(): UByte = this.toUByte() + + +/** + * Converts this [Int] value to [Byte]. + * + * If this value is in [Byte.MIN_VALUE]..[Byte.MAX_VALUE], the resulting [Byte] value represents + * the same numerical value as this [Int]. + * + * The resulting [Byte] value is represented by the least significant 8 bits of this [Int] value. + */ +@JvmName("trimToByte") +public inline fun Int.trimToByte(): Byte = this.toByte() + +/** + * Converts this [Int] value to [Short]. + * + * If this value is in [Short.MIN_VALUE]..[Short.MAX_VALUE], the resulting [Short] value represents + * the same numerical value as this [Int]. + * + * The resulting [Short] value is represented by the least significant 16 bits of this [Int] value. + */ +@JvmName("trimToShort") +public inline fun Int.trimToShort(): Short = this.toShort() + + +/** + * Converts this [UInt] value to [UByte]. + * + * If this value is in 0..[UByte.MAX_VALUE], the resulting [UByte] value represents + * the same numerical value as this [UInt]. + * + * The resulting [UByte] value is represented by the least significant 8 bits of this [UInt] value. + */ +@JvmName("trimToUByte") +public inline fun UInt.trimToUByte(): UByte = this.toUByte() + +/** + * Converts this [Int] value to [Short]. + * + * If this value is in 0..[UShort.MAX_VALUE], the resulting [Short] value represents + * the same numerical value as this [Int]. + * + * The resulting [UShort] value is represented by the least significant 16 bits of this [UInt] value. + */ +@JvmName("trimToUShort") +public inline fun UInt.trimToUShort(): UShort = this.toUShort() + + +/** + * Converts this [Long] value to [Byte]. + * + * If this value is in [Byte.MIN_VALUE]..[Byte.MAX_VALUE], the resulting [Byte] value represents + * the same numerical value as this [Long]. + * + * The resulting [Byte] value is represented by the least significant 8 bits of this [Long] value. + */ +@JvmName("trimToByte") +public inline fun Long.trimToByte(): Byte = this.toByte() + +/** + * Converts this [Long] value to [Short]. + * + * If this value is in [Short.MIN_VALUE]..[Short.MAX_VALUE], the resulting [Short] value represents + * the same numerical value as this [Long]. + * + * The resulting [Short] value is represented by the least significant 16 bits of this [Long] value. + */ +@JvmName("trimToShort") +public inline fun Long.trimToShort(): Short = this.toShort() + +/** + * Converts this [Long] value to [Int]. + * + * If this value is in [Int.MIN_VALUE]..[Int.MAX_VALUE], the resulting [Int] value represents + * the same numerical value as this [Long]. + * + * The resulting [Int] value is represented by the least significant 32 bits of this [Long] value. + */ +@JvmName("trimToInt") +public inline fun Long.trimToInt(): Int = this.toInt() + + +/** + * Converts this [ULong] value to [UByte]. + * + * If this value is in 0..[UByte.MAX_VALUE], the resulting [UByte] value represents + * the same numerical value as this [ULong]. + * + * The resulting [UByte] value is represented by the least significant 8 bits of this [ULong] value. + */ +@JvmName("trimToUByte") +public inline fun ULong.trimToUByte(): UByte =this.toUByte() + +/** + * Converts this [ULong] value to [UShort]. + * + * If this value is in 0..[UShort.MAX_VALUE], the resulting [UShort] value represents + * the same numerical value as this [ULong]. + * + * The resulting [UShort] value is represented by the least significant 16 bits of this [ULong] value. + */ +@JvmName("trimToUShort") +public inline fun ULong.trimToUShort(): UShort = this.toUShort() + + +/** + * Converts this [ULong] value to [UInt]. + * + * If this value is in 0..[UInt.MAX_VALUE], the resulting [UInt] value represents + * the same numerical value as this [ULong]. + * + * The resulting [UInt] value is represented by the least significant 32 bits of this [ULong] value. + */ +@JvmName("trimToUInt") +public inline fun ULong.trimToUInt(): UInt = this.toUInt() + /******************************** trims (or null) ****************************/ @@ -118,7 +259,7 @@ public inline fun UInt.extendToULong(): ULong = this.toULong() * Otherwise `null` returned. */ @JvmName("toByteOrNull") -public inline fun Short.toByteOrNull(): Byte? = if (this in (Byte.MIN_VALUE.extendToShort())..(Byte.MAX_VALUE.extendToShort())) this.toByte() else null +public inline fun Short.toByteOrNull(): Byte? = if (this in (Byte.MIN_VALUE.extendToShort())..(Byte.MAX_VALUE.extendToShort())) this.trimToByte() else null /** @@ -129,7 +270,7 @@ public inline fun Short.toByteOrNull(): Byte? = if (this in (Byte.MIN_VALUE.exte * Otherwise `null` returned. */ @JvmName("toUByteOrNull") -public inline fun UShort.toUByteOrNull(): UByte? = if (this <= UByte.MAX_VALUE.extendToUShort()) this.toUByte() else null +public inline fun UShort.toUByteOrNull(): UByte? = if (this <= UByte.MAX_VALUE.extendToUShort()) this.trimToUByte() else null /** @@ -140,7 +281,7 @@ public inline fun UShort.toUByteOrNull(): UByte? = if (this <= UByte.MAX_VALUE.e * Otherwise `null` returned. */ @JvmName("toByteOrNull") -public inline fun Int.toByteOrNull(): Byte? = if (this in (Byte.MIN_VALUE.extendToInt())..(Byte.MAX_VALUE.extendToInt())) this.toByte() else null +public inline fun Int.toByteOrNull(): Byte? = if (this in (Byte.MIN_VALUE.extendToInt())..(Byte.MAX_VALUE.extendToInt())) this.trimToByte() else null /** * Converts this [Int] value to [Short]. @@ -150,28 +291,28 @@ public inline fun Int.toByteOrNull(): Byte? = if (this in (Byte.MIN_VALUE.extend * Otherwise `null` returned. */ @JvmName("toShortOrNull") -public inline fun Int.toShortOrNull(): Short? = if (this in (Short.MIN_VALUE.extendToInt())..(Short.MAX_VALUE.extendToInt())) this.toShort() else null +public inline fun Int.toShortOrNull(): Short? = if (this in (Short.MIN_VALUE.extendToInt())..(Short.MAX_VALUE.extendToInt())) this.trimToShort() else null /** * Converts this [UInt] value to [UByte]. * - * If this value is in [UByte.MIN_VALUE]..[UByte.MAX_VALUE], the resulting [UByte] value represents + * If this value is in 0..[UByte.MAX_VALUE], the resulting [UByte] value represents * the same numerical value as this [UInt]. * Otherwise `null` returned. */ @JvmName("toUByteOrNull") -public inline fun UInt.toUByteOrNull(): UByte? = if (this <= UByte.MAX_VALUE.extendToUInt()) this.toUByte() else null +public inline fun UInt.toUByteOrNull(): UByte? = if (this <= UByte.MAX_VALUE.extendToUInt()) this.trimToUByte() else null /** - * Converts this [Int] value to [Short]. + * Converts this [UInt] value to [Short]. * - * If this value is in [Short.MIN_VALUE]..[Short.MAX_VALUE], the resulting [Short] value represents - * the same numerical value as this [Int]. + * If this value is in 0..[UShort.MAX_VALUE], the resulting [UShort] value represents + * the same numerical value as this [UInt]. * Otherwise `null` returned. */ @JvmName("toUShortOrNull") -public inline fun UInt.toUShortOrNull(): UShort? = if (this <= UShort.MAX_VALUE.extendToUInt()) this.toUShort() else null +public inline fun UInt.toUShortOrNull(): UShort? = if (this <= UShort.MAX_VALUE.extendToUInt()) this.trimToUShort() else null /** @@ -182,7 +323,7 @@ public inline fun UInt.toUShortOrNull(): UShort? = if (this <= UShort.MAX_VALUE. * Otherwise `null` returned. */ @JvmName("toByteOrNull") -public inline fun Long.toByteOrNull(): Byte? = if (this in (Byte.MIN_VALUE.extendToLong())..(Byte.MAX_VALUE.extendToLong())) this.toByte() else null +public inline fun Long.toByteOrNull(): Byte? = if (this in (Byte.MIN_VALUE.extendToLong())..(Byte.MAX_VALUE.extendToLong())) this.trimToByte() else null /** * Converts this [Long] value to [Short]. @@ -192,50 +333,50 @@ public inline fun Long.toByteOrNull(): Byte? = if (this in (Byte.MIN_VALUE.exten * Otherwise `null` returned. */ @JvmName("toShortOrNull") -public inline fun Long.toShortOrNull(): Short? = if (this in (Short.MIN_VALUE.extendToLong())..(Short.MAX_VALUE.extendToLong())) this.toShort() else null +public inline fun Long.toShortOrNull(): Short? = if (this in (Short.MIN_VALUE.extendToLong())..(Short.MAX_VALUE.extendToLong())) this.trimToShort() else null /** * Converts this [Long] value to [Int]. * - * If this value is in [Short.MIN_VALUE]..[Short.MAX_VALUE], the resulting [Int] value represents + * If this value is in [Int.MIN_VALUE]..[Int.MAX_VALUE], the resulting [Int] value represents * the same numerical value as this [Long]. * Otherwise `null` returned. */ @JvmName("toIntOrNull") -public inline fun Long.toIntOrNull(): Int? = if (this in (Int.MIN_VALUE.extendToLong())..(Int.MAX_VALUE.extendToLong())) this.toInt() else null +public inline fun Long.toIntOrNull(): Int? = if (this in (Int.MIN_VALUE.extendToLong())..(Int.MAX_VALUE.extendToLong())) this.trimToInt() else null /** * Converts this [ULong] value to [UByte]. * - * If this value is in [UByte.MIN_VALUE]..[UByte.MAX_VALUE], the resulting [UByte] value represents + * If this value is in 0..[UByte.MAX_VALUE], the resulting [UByte] value represents * the same numerical value as this [ULong]. * Otherwise `null` returned. */ @JvmName("toUByteOrNull") -public inline fun ULong.toUByteOrNull(): UByte? = if (this in (UByte.MIN_VALUE.extendToULong())..(UByte.MAX_VALUE.extendToULong())) this.toUByte() else null +public inline fun ULong.toUByteOrNull(): UByte? = if (this in (UByte.MIN_VALUE.extendToULong())..(UByte.MAX_VALUE.extendToULong())) this.trimToUByte() else null /** * Converts this [ULong] value to [UShort]. * - * If this value is in [UShort.MIN_VALUE]..[UShort.MAX_VALUE], the resulting [UShort] value represents + * If this value is in 0..[UShort.MAX_VALUE], the resulting [UShort] value represents * the same numerical value as this [ULong]. * Otherwise `null` returned. */ @JvmName("toUShortOrNull") -public inline fun ULong.toUShortOrNull(): UShort? = if (this in (UShort.MIN_VALUE.extendToULong())..(UShort.MAX_VALUE.extendToULong())) this.toUShort() else null +public inline fun ULong.toUShortOrNull(): UShort? = if (this in (UShort.MIN_VALUE.extendToULong())..(UShort.MAX_VALUE.extendToULong())) this.trimToUShort() else null /** * Converts this [ULong] value to [UInt]. * - * If this value is in [UShort.MIN_VALUE]..[UShort.MAX_VALUE], the resulting [UInt] value represents + * If this value is in 0..[UInt.MAX_VALUE], the resulting [UInt] value represents * the same numerical value as this [ULong]. * Otherwise `null` returned. */ @JvmName("toUIntOrNull") -public inline fun ULong.toUIntOrNull(): UInt? = if (this in (UInt.MIN_VALUE.extendToULong())..(UInt.MAX_VALUE.extendToULong())) this.toUInt() else null +public inline fun ULong.toUIntOrNull(): UInt? = if (this in (UInt.MIN_VALUE.extendToULong())..(UInt.MAX_VALUE.extendToULong())) this.trimToUInt() else null /********************************************* Conversion error formatters ****************************************/ diff --git a/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_variables.kt b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_variables.kt new file mode 100644 index 0000000..3ff0841 --- /dev/null +++ b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/utilities/int_variables.kt @@ -0,0 +1,126 @@ +package ru.landrafhomyak.kotlin.utilities + +import kotlin.jvm.JvmField + +public class ByteVariable( + @Suppress("MemberVisibilityCanBePrivate") + @JvmField + public var value: Byte +) { + @Suppress("MemberVisibilityCanBePrivate") + public fun getAndSet(newValue: Byte): Byte { + val oldValue = this.value + this.value = newValue + return oldValue + } + + public fun getAndAdd(d: Byte): Byte = this.getAndSet((this.value + d).trimToByte()) + public fun getAndSub(d: Byte): Byte = this.getAndSet((this.value - d).trimToByte()) +} + +public class UByteVariable( + @Suppress("MemberVisibilityCanBePrivate") + public var value: UByte +) { + @Suppress("MemberVisibilityCanBePrivate") + public fun getAndSet(newValue: UByte): UByte { + val oldValue = this.value + this.value = newValue + return oldValue + } + + public fun getAndAdd(d: UByte): UByte = this.getAndSet((this.value + d).trimToUByte()) + public fun getAndSub(d: UByte): UByte = this.getAndSet((this.value - d).trimToUByte()) +} + +public class ShortVariable( + @Suppress("MemberVisibilityCanBePrivate") + @JvmField + public var value: Short +) { + @Suppress("MemberVisibilityCanBePrivate") + public fun getAndSet(newValue: Short): Short { + val oldValue = this.value + this.value = newValue + return oldValue + } + + public fun getAndAdd(d: Short): Short = this.getAndSet((this.value + d).trimToShort()) + public fun getAndSub(d: Short): Short = this.getAndSet((this.value - d).trimToShort()) +} + +public class UShortVariable( + @Suppress("MemberVisibilityCanBePrivate") + public var value: UShort +) { + @Suppress("MemberVisibilityCanBePrivate") + public fun getAndSet(newValue: UShort): UShort { + val oldValue = this.value + this.value = newValue + return oldValue + } + + public fun getAndAdd(d: UShort): UShort = this.getAndSet((this.value + d).trimToUShort()) + public fun getAndSub(d: UShort): UShort = this.getAndSet((this.value - d).trimToUShort()) +} + +public class IntVariable( + @Suppress("MemberVisibilityCanBePrivate") + @JvmField public var value: Int +) { + @Suppress("MemberVisibilityCanBePrivate") + public fun getAndSet(newValue: Int): Int { + val oldValue = this.value + this.value = newValue + return oldValue + } + + public fun getAndAdd(d: Int): Int = this.getAndSet(this.value + d) + public fun getAndSub(d: Int): Int = this.getAndSet(this.value - d) +} + +public class UIntVariable( + @Suppress("MemberVisibilityCanBePrivate") + public var value: UInt +) { + @Suppress("MemberVisibilityCanBePrivate") + public fun getAndSet(newValue: UInt): UInt { + val oldValue = this.value + this.value = newValue + return oldValue + } + + public fun getAndAdd(d: UInt): UInt = this.getAndSet(this.value + d) + public fun getAndSub(d: UInt): UInt = this.getAndSet(this.value - d) +} + +public class LongVariable( + @Suppress("MemberVisibilityCanBePrivate") + @JvmField + public var value: Long +) { + @Suppress("MemberVisibilityCanBePrivate") + public fun getAndSet(newValue: Long): Long { + val oldValue = this.value + this.value = newValue + return oldValue + } + + public fun getAndAdd(d: Long): Long = this.getAndSet(this.value + d) + public fun getAndSub(d: Long): Long = this.getAndSet(this.value - d) +} + +public class ULongVariable( + @Suppress("MemberVisibilityCanBePrivate") + public var value: ULong +) { + @Suppress("MemberVisibilityCanBePrivate") + public fun getAndSet(newValue: ULong): ULong { + val oldValue = this.value + this.value = newValue + return oldValue + } + + public fun getAndAdd(d: ULong): ULong = this.getAndSet(this.value + d) + public fun getAndSub(d: ULong): ULong = this.getAndSet(this.value - d) +} \ No newline at end of file