Repository: ionspin/kotlin-multiplatform-bignum (kotlin)

Issue:
Support Ranges
**Is your feature request related to a problem? Please describe.**
For convenience it would be nice to support Range's for BigInteger's, for example by providing the infix.

**Describe the solution you'd like**
Just for convenience, non-blocking

**Describe alternatives you've considered**
Convert to Int before setting the range.
I did not research if this is a easy job. Just wanted to add the feature request in case it is possible.

**Additional context**
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/index.html
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/until.html

Pull request:
Fixes #20, need to add to README later

Patch:
diff --git a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/BigInteger.kt b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/BigInteger.kt
index 4ffea582..6e3d6efd 100644
--- a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/BigInteger.kt
+++ b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/BigInteger.kt
@@ -53,6 +53,8 @@ enum class Sign {
     }
 }
 
+
+
 /**
  * Arbitrary precision integer arithmetic.
  *
@@ -78,6 +80,8 @@ class BigInteger internal constructor(wordArray: WordArray, val sign: Sign) : Bi
         return this
     }
 
+
+
     @ExperimentalUnsignedTypes
     companion object : BigNumber.Creator<BigInteger>, BigNumber.Util<BigInteger> {
         private val arithmetic: BigIntegerArithmetic<WordArray, Word> = chosenArithmetic
@@ -651,4 +655,28 @@ class BigInteger internal constructor(wordArray: WordArray, val sign: Sign) : Bi
         }
         return magnitude[0].toUShort()
     }
-}
\ No newline at end of file
+
+    operator fun rangeTo(other : BigInteger) = BigIntegerRange(this, other)
+
+    class BigIntegerRange(override val start : BigInteger, override val endInclusive : BigInteger) : ClosedRange<BigInteger>, Iterable<BigInteger> {
+
+        override fun iterator(): Iterator<BigInteger> {
+            return BigIntegerIterator(start, endInclusive)
+        }
+
+    }
+
+    class BigIntegerIterator(start : BigInteger, private val endInclusive : BigInteger) : Iterator<BigInteger> {
+
+        private var current = start
+
+        override fun hasNext(): Boolean {
+            return current <= endInclusive
+        }
+
+        override fun next(): BigInteger {
+            return current++
+        }
+
+    }
+}


Test patch:
diff --git a/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/BigIntegerTest.kt b/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/BigIntegerTest.kt
index b0094053..1ed31793 100644
--- a/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/BigIntegerTest.kt
+++ b/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/BigIntegerTest.kt
@@ -342,4 +342,25 @@ BigIntegerTest {
 
     }
 
-}
\ No newline at end of file
+    @Test
+    fun testClosedRange() {
+        assertTrue {
+            val range = 0.toBigInteger() .. 10.toBigInteger()
+            val sum = range.fold(BigInteger.ZERO) { acc, bigint ->
+                acc + bigint
+            }
+            sum == ((range.endInclusive - range.start + 1) * ((range.start + range.endInclusive)) / 2)
+
+        }
+
+        assertTrue {
+            val range = Long.MAX_VALUE.toBigInteger() .. (Long.MAX_VALUE.toBigInteger() + 1000.toBigInteger())
+            val sum = range.fold(BigInteger.ZERO) { acc, bigint ->
+                acc + bigint
+            }
+            sum == ((range.endInclusive - range.start + 1) * ((range.start + range.endInclusive)) / 2)
+
+        }
+    }
+
+}