|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Matthew Nelson |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | +package org.kotlincrypto.core |
| 17 | + |
| 18 | +import org.kotlincrypto.core.Counter.Bit32.Companion.MAX_INCREMENT |
| 19 | +import org.kotlincrypto.core.Counter.Bit64.Companion.MAX_INCREMENT |
| 20 | +import kotlin.jvm.JvmField |
| 21 | +import kotlin.jvm.JvmName |
| 22 | + |
| 23 | +/** |
| 24 | + * Utility for counting things. |
| 25 | + * */ |
| 26 | +public sealed class Counter private constructor() { |
| 27 | + |
| 28 | + /** |
| 29 | + * A counter that utilizes 32-bit numbers providing a maximum count of 2^64. |
| 30 | + * */ |
| 31 | + public abstract class Bit32: Counter { |
| 32 | + |
| 33 | + public companion object { |
| 34 | + |
| 35 | + /** |
| 36 | + * The maximum value for which [Bit32.incrementBy] can be set to. |
| 37 | + * |
| 38 | + * 1024 * 1024 >> 1048576 |
| 39 | + * */ |
| 40 | + public const val MAX_INCREMENT: Int = 1024 * 1024 // Never decrease, only increase. |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * The value to increment things by |
| 45 | + * */ |
| 46 | + @JvmField |
| 47 | + public val incrementBy: Int |
| 48 | + |
| 49 | + /** |
| 50 | + * The least significant bits of the number |
| 51 | + * */ |
| 52 | + @get:JvmName("lo") |
| 53 | + public var lo: Int |
| 54 | + private set |
| 55 | + |
| 56 | + /** |
| 57 | + * The most significant bits of the number |
| 58 | + * */ |
| 59 | + @get:JvmName("hi") |
| 60 | + public var hi: Int |
| 61 | + private set |
| 62 | + |
| 63 | + /** |
| 64 | + * Creates a new [Bit32] counter initialized to [lo] and [hi] |
| 65 | + * |
| 66 | + * @throws [IllegalArgumentException] when: |
| 67 | + * - [incrementBy] is less than or equal to 0 |
| 68 | + * - [incrementBy] is greater than [MAX_INCREMENT] |
| 69 | + * - [incrementBy] is not a factor of 8 |
| 70 | + * - [lo] is not a factor of [incrementBy] |
| 71 | + * */ |
| 72 | + public constructor(lo: Int, hi: Int, incrementBy: Int): super() { |
| 73 | + require(incrementBy > 0) { "incrementBy[$incrementBy] must be greater than 0" } |
| 74 | + require(incrementBy <= MAX_INCREMENT) { "incrementBy[$incrementBy] must be less than or equal to $MAX_INCREMENT" } |
| 75 | + require(incrementBy % 8 == 0) { "incrementBy[$incrementBy] must be a factor of 8" } |
| 76 | + require(lo % incrementBy == 0) { "lo must be a factor of incrementBy[$incrementBy]" } |
| 77 | + |
| 78 | + this.incrementBy = incrementBy |
| 79 | + this.lo = lo |
| 80 | + this.hi = hi |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Creates a new [Bit32] counter initialized to 0, 0 |
| 85 | + * |
| 86 | + * @throws [IllegalArgumentException] when [incrementBy] is: |
| 87 | + * - Less than or equal to 0 |
| 88 | + * - Greater than [MAX_INCREMENT] |
| 89 | + * - Not a factor of 8 |
| 90 | + * */ |
| 91 | + public constructor(incrementBy: Int): this(0, 0, incrementBy) |
| 92 | + |
| 93 | + /** |
| 94 | + * Creates a clone of [other] |
| 95 | + * */ |
| 96 | + public constructor(other: Bit32): super() { |
| 97 | + this.incrementBy = other.incrementBy |
| 98 | + this.lo = other.lo |
| 99 | + this.hi = other.hi |
| 100 | + } |
| 101 | + |
| 102 | + protected override fun increment() { |
| 103 | + lo += incrementBy |
| 104 | + if (lo == 0) hi++ |
| 105 | + } |
| 106 | + |
| 107 | + protected override fun reset() { |
| 108 | + lo = 0 |
| 109 | + hi = 0 |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * A counter that utilizes 64-bit numbers providing a maximum count of 2^128. |
| 115 | + * */ |
| 116 | + public abstract class Bit64: Counter { |
| 117 | + |
| 118 | + public companion object { |
| 119 | + |
| 120 | + /** |
| 121 | + * The maximum value for which [Bit64.incrementBy] can be set to. |
| 122 | + * |
| 123 | + * @see [Bit32.MAX_INCREMENT] |
| 124 | + * */ |
| 125 | + public const val MAX_INCREMENT: Long = Bit32.MAX_INCREMENT.toLong() |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * The value to increment things by |
| 130 | + * */ |
| 131 | + @JvmField |
| 132 | + public val incrementBy: Long |
| 133 | + |
| 134 | + /** |
| 135 | + * The least significant bits of the number |
| 136 | + * */ |
| 137 | + @get:JvmName("lo") |
| 138 | + public var lo: Long |
| 139 | + private set |
| 140 | + |
| 141 | + /** |
| 142 | + * The most significant bits of the number |
| 143 | + * */ |
| 144 | + @get:JvmName("hi") |
| 145 | + public var hi: Long |
| 146 | + private set |
| 147 | + |
| 148 | + /** |
| 149 | + * Creates a new [Bit64] counter initialized to [lo] and [hi] |
| 150 | + * |
| 151 | + * @throws [IllegalArgumentException] when: |
| 152 | + * - [incrementBy] is less than or equal to 0 |
| 153 | + * - [incrementBy] is greater than [MAX_INCREMENT] |
| 154 | + * - [incrementBy] is not a factor of 8 |
| 155 | + * - [lo] is not a factor of [incrementBy] |
| 156 | + * */ |
| 157 | + public constructor(lo: Long, hi: Long, incrementBy: Long): super() { |
| 158 | + require(incrementBy > 0L) { "incrementBy[$incrementBy] must be greater than 0" } |
| 159 | + require(incrementBy <= MAX_INCREMENT) { "incrementBy[$incrementBy] must be less than or equal to $MAX_INCREMENT" } |
| 160 | + require(incrementBy % 8 == 0L) { "incrementBy[$incrementBy] must be a factor of 8" } |
| 161 | + require(lo % incrementBy == 0L) { "lo must be a factor of incrementBy[$incrementBy]" } |
| 162 | + |
| 163 | + this.incrementBy = incrementBy |
| 164 | + this.lo = lo |
| 165 | + this.hi = hi |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Creates a new [Bit64] counter initialized to 0, 0 |
| 170 | + * |
| 171 | + * @throws [IllegalArgumentException] when [incrementBy] is: |
| 172 | + * - Less than or equal to 0 |
| 173 | + * - Greater than [MAX_INCREMENT] |
| 174 | + * - Not a factor of 8 |
| 175 | + * */ |
| 176 | + public constructor(incrementBy: Long): this(0, 0, incrementBy) |
| 177 | + |
| 178 | + /** |
| 179 | + * Creates a clone of [other] |
| 180 | + * */ |
| 181 | + public constructor(other: Bit64): super() { |
| 182 | + this.incrementBy = other.incrementBy |
| 183 | + this.lo = other.lo |
| 184 | + this.hi = other.hi |
| 185 | + } |
| 186 | + |
| 187 | + protected override fun increment() { |
| 188 | + lo += incrementBy |
| 189 | + if (lo == 0L) hi++ |
| 190 | + } |
| 191 | + |
| 192 | + protected override fun reset() { |
| 193 | + lo = 0L |
| 194 | + hi = 0L |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + protected abstract fun increment() |
| 199 | + protected abstract fun reset() |
| 200 | + |
| 201 | + private val code = Any() |
| 202 | + |
| 203 | + /** @suppress */ |
| 204 | + public final override fun equals(other: Any?): Boolean = other is Counter && other.hashCode() == hashCode() |
| 205 | + /** @suppress */ |
| 206 | + public final override fun hashCode(): Int = 17 * 31 + code.hashCode() |
| 207 | + /** @suppress */ |
| 208 | + public final override fun toString(): String = when (this) { |
| 209 | + is Bit32 -> "Bit32[lo=$lo, hi=$hi, incrementBy=$incrementBy]" |
| 210 | + is Bit64 -> "Bit64[lo=$lo, hi=$hi, incrementBy=$incrementBy]" |
| 211 | + }.let { value -> "Counter.$value@${hashCode()}" } |
| 212 | +} |
0 commit comments