Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ val interval: IntInterval = interval( 0, 10, isEndIncluded = false )
val areIncluded = 0 in interval && 5 in interval // true
val areExcluded = 10 !in interval && 15 !in interval // true
val size: UInt = interval.size // 10
val shifted = interval shr 10u // Shifted right by 10: [10, 20)
```

This protects against overflows (e.g. if `size > Int.MAX_VALUE`) but also offers better semantics.
Expand All @@ -24,6 +25,7 @@ val now = Clock.System.now()
val interval: InstantInterval = interval( now, now + 100.seconds )
val areIncluded = now + 50.seconds in interval // true
val size: Duration = interval.size // 100 seconds
val shifted = interval shr 24.hours // 100 seconds 24 hours from now
```

## Interval Unions
Expand All @@ -36,6 +38,7 @@ Since operations are generally defined on the base interface, you can easily cha
val start = interval( 0, 100 ) // Interval: [0, 100]
val areIncluded = 50 in start && 100 in start // true
val splitInTwo = start - interval( 25, 85 ) // Union: [[0, 25), (85, 100]]
val shiftBackAndForth = splitInTwo shr 100u shl 100u // == splitInTwo
val areExcluded = 50 !in splitInTwo && 85 !in splitInTwo // true
val unite = splitInTwo + interval( 10, 90 ) // Interval: [0, 100]
val backToStart = start == unite // true
Expand Down Expand Up @@ -63,18 +66,18 @@ Instead, new instances are returned if the operation results in a different set

The following operations are available for any `IntervalUnion<T, TSize>`:

| Operation | Description |
|:-------------------:|:--------------------------------------------------------------------------------------------:|
| `isEmpty()` | Determines whether this is an empty set. |
| `getBounds()` | Gets the upper and lower bound of the set. |
| `contains()` (`in`) | Determines whether a value lies in the set. |
| `minus()` (`-`) | Subtract an interval from the set. |
| `plus()` (`+`) | Add an interval to the set. |
| `shift()` | Move the interval by a specified offset. |
| `intersects()` | Determines whether another interval intersects with this set. |
| `setEquals()` | Determines whether a set represents the same values. |
| `iterator()` | Iterate over all intervals in the union, in order. |
| `toString()` | Output as a string using common interval notation, e.g., `[0, 10]` or `[[0, 10), (10, 20]]`. |
| Operation | Description |
|:-----------------------:|:--------------------------------------------------------------------------------------------:|
| `isEmpty()` | Determines whether this is an empty set. |
| `getBounds()` | Gets the upper and lower bound of the set. |
| `contains()` (`in`) | Determines whether a value lies in the set. |
| `minus()` (`-`) | Subtract an interval from the set. |
| `plus()` (`+`) | Add an interval to the set. |
| `shift()` (`shl`/`shr`) | Move the interval by a specified offset. |
| `intersects()` | Determines whether another interval intersects with this set. |
| `setEquals()` | Determines whether a set represents the same values. |
| `iterator()` | Iterate over all intervals in the union, in order. |
| `toString()` | Output as a string using common interval notation, e.g., `[0, 10]` or `[[0, 10), (10, 20]]`. |

The following operations are specific to `Interval<T, TSize>`:

Expand Down
2 changes: 2 additions & 0 deletions kotlinx.interval.datetime/src/commonTest/kotlin/Readme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package io.github.whathecode.kotlinx.interval.datetime
import kotlinx.datetime.Clock
import kotlin.test.*
import kotlin.time.Duration
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.seconds


Expand All @@ -17,5 +18,6 @@ class Readme
val interval: InstantInterval = interval( now, now + 100.seconds )
val areIncluded = now + 50.seconds in interval // true
val size: Duration = interval.size // 100 seconds
val shifted = interval shr 24.hours // 100 seconds 24 hours from now
}
}
14 changes: 14 additions & 0 deletions kotlinx.interval/src/commonMain/kotlin/IntervalUnion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ sealed interface IntervalUnion<T : Comparable<T>, TSize : Comparable<TSize>> : I
*/
fun shift( amount: TSize, invertDirection: Boolean = false ): ShiftResult<IntervalUnion<T, TSize>, TSize>

/**
* Returns an [IntervalUnion] offset to the right from this interval union by the specified [amount],
* or as much as possible before the minimum or maximum value that can be represented by [T] is reached.
* Use [shift] in case you want to verify whether the interval could be offset the full [amount].
*/
infix fun shr( amount: TSize ): IntervalUnion<T, TSize> = shift( amount ).shiftedInterval

/**
* Returns an [IntervalUnion] offset to the left from this interval union by the specified [amount],
* or as much as possible before the minimum or maximum value that can be represented by [T] is reached.
* Use [shift] in case you want to verify whether the interval could be offset the full [amount].
*/
infix fun shl( amount: TSize ): IntervalUnion<T, TSize> = shift( amount, invertDirection = true ).shiftedInterval

/**
* Determines whether [interval] has at least one value in common with this set.
*/
Expand Down
2 changes: 2 additions & 0 deletions kotlinx.interval/src/commonTest/kotlin/Readme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Readme
val areIncluded = 0 in interval && 5 in interval // true
val areExcluded = 10 !in interval && 15 !in interval // true
val size: UInt = interval.size // 10
val shifted = interval shr 10u // Shifted right by 10: [10, 20)
}

@Test
Expand All @@ -22,6 +23,7 @@ class Readme
val start = interval( 0, 100 ) // Interval: [0, 100]
val areIncluded = 50 in start && 100 in start // true
val splitInTwo = start - interval( 25, 85 ) // Union: [[0, 25), (85, 100]]
val shiftBackAndForth = splitInTwo shr 100u shl 100u // == splitInTwo
val areExcluded = 50 !in splitInTwo && 85 !in splitInTwo // true
val unite = splitInTwo + interval( 10, 90 ) // Interval: [0, 100]
val backToStart = start == unite // true
Expand Down
Loading