Skip to content

Commit 52f99d9

Browse files
authored
Merge pull request #11 from Whathecode/develop
Release 1.0.0-alpha.2
2 parents 8c7139d + e243a90 commit 52f99d9

File tree

6 files changed

+138
-14
lines changed

6 files changed

+138
-14
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ _Values_ covered by the interval can be of a different type than _distances_ bet
1010
For example, `IntInterval` has `Int` values and `UInt` distances:
1111

1212
```kotlin
13-
val interval = IntInterval(
14-
start = 0,
15-
isStartIncluded = true,
16-
end = 10,
17-
isEndIncluded = false
18-
)
13+
val interval: IntInterval = interval( 0, 10, isEndIncluded = false )
14+
val areIncluded = 0 in interval && 5 in interval // true
15+
val areExcluded = 10 !in interval && 15 !in interval // true
1916
val size: UInt = interval.size // 10
2017
```
2118

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ plugins {
1414
}
1515

1616
group = "io.github.whathecode.kotlinx.interval"
17-
version = "1.0.0-alpha.1"
17+
version = "1.0.0-alpha.2"
1818

1919
repositories {
2020
mavenCentral()

src/commonMain/kotlin/BasicTypeIntervals.kt

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class ByteInterval( start: Byte, isStartIncluded: Boolean, end: Byte, isEndInclu
2020
}
2121
}
2222

23+
/**
24+
* Create a [ByteInterval] representing the set of all [Byte] values lying between [start] and [end].
25+
* To exclude endpoints, set [isStartIncluded] or [isEndIncluded] to false; a closed interval is created by default.
26+
*/
27+
fun interval( start: Byte, end: Byte, isStartIncluded: Boolean = true, isEndIncluded: Boolean = true ) =
28+
ByteInterval( start, isStartIncluded, end, isEndIncluded )
29+
30+
2331
/**
2432
* An [Interval] representing the set of all [Short] values lying between a provided [start] and [end] value.
2533
* The interval can be closed, open, or half-open, as determined by [isStartIncluded] and [isEndIncluded].
@@ -37,6 +45,14 @@ class ShortInterval( start: Short, isStartIncluded: Boolean, end: Short, isEndIn
3745
}
3846
}
3947

48+
/**
49+
* Create a [ShortInterval] representing the set of all [Short] values lying between [start] and [end].
50+
* To exclude endpoints, set [isStartIncluded] or [isEndIncluded] to false; a closed interval is created by default.
51+
*/
52+
fun interval( start: Short, end: Short, isStartIncluded: Boolean = true, isEndIncluded: Boolean = true ) =
53+
ShortInterval( start, isStartIncluded, end, isEndIncluded )
54+
55+
4056
/**
4157
* An [Interval] representing the set of all [Int] values lying between a provided [start] and [end] value.
4258
* The interval can be closed, open, or half-open, as determined by [isStartIncluded] and [isEndIncluded].
@@ -54,6 +70,14 @@ class IntInterval( start: Int, isStartIncluded: Boolean, end: Int, isEndIncluded
5470
}
5571
}
5672

73+
/**
74+
* Create a [IntInterval] representing the set of all [Int] values lying between [start] and [end].
75+
* To exclude endpoints, set [isStartIncluded] or [isEndIncluded] to false; a closed interval is created by default.
76+
*/
77+
fun interval( start: Int, end: Int, isStartIncluded: Boolean = true, isEndIncluded: Boolean = true ) =
78+
IntInterval( start, isStartIncluded, end, isEndIncluded )
79+
80+
5781
/**
5882
* An [Interval] representing the set of all [Long] values lying between a provided [start] and [end] value.
5983
* The interval can be closed, open, or half-open, as determined by [isStartIncluded] and [isEndIncluded].
@@ -71,6 +95,14 @@ class LongInterval( start: Long, isStartIncluded: Boolean, end: Long, isEndInclu
7195
}
7296
}
7397

98+
/**
99+
* Create a [LongInterval] representing the set of all [Long] values lying between [start] and [end].
100+
* To exclude endpoints, set [isStartIncluded] or [isEndIncluded] to false; a closed interval is created by default.
101+
*/
102+
fun interval( start: Long, end: Long, isStartIncluded: Boolean = true, isEndIncluded: Boolean = true ) =
103+
LongInterval( start, isStartIncluded, end, isEndIncluded )
104+
105+
74106
/**
75107
* An [Interval] representing the set of all [Float] values lying between a provided [start] and [end] value.
76108
* The interval can be closed, open, or half-open, as determined by [isStartIncluded] and [isEndIncluded].
@@ -84,6 +116,14 @@ class FloatInterval( start: Float, isStartIncluded: Boolean, end: Float, isEndIn
84116
}
85117
}
86118

119+
/**
120+
* Create a [FloatInterval] representing the set of all [Float] values lying between [start] and [end].
121+
* To exclude endpoints, set [isStartIncluded] or [isEndIncluded] to false; a closed interval is created by default.
122+
*/
123+
fun interval( start: Float, end: Float, isStartIncluded: Boolean = true, isEndIncluded: Boolean = true ) =
124+
FloatInterval( start, isStartIncluded, end, isEndIncluded )
125+
126+
87127
/**
88128
* An [Interval] representing the set of all [Double] values lying between a provided [start] and [end] value.
89129
* The interval can be closed, open, or half-open, as determined by [isStartIncluded] and [isEndIncluded].
@@ -99,6 +139,14 @@ class DoubleInterval( start: Double, isStartIncluded: Boolean, end: Double, isEn
99139
}
100140
}
101141

142+
/**
143+
* Create a [DoubleInterval] representing the set of all [Double] values lying between [start] and [end].
144+
* To exclude endpoints, set [isStartIncluded] or [isEndIncluded] to false; a closed interval is created by default.
145+
*/
146+
fun interval( start: Double, end: Double, isStartIncluded: Boolean = true, isEndIncluded: Boolean = true ) =
147+
DoubleInterval( start, isStartIncluded, end, isEndIncluded )
148+
149+
102150
/**
103151
* An [Interval] representing the set of all [UByte] values lying between a provided [start] and [end] value.
104152
* The interval can be closed, open, or half-open, as determined by [isStartIncluded] and [isEndIncluded].
@@ -112,6 +160,14 @@ class UByteInterval( start: UByte, isStartIncluded: Boolean, end: UByte, isEndIn
112160
}
113161
}
114162

163+
/**
164+
* Create a [UByteInterval] representing the set of all [UByte] values lying between [start] and [end].
165+
* To exclude endpoints, set [isStartIncluded] or [isEndIncluded] to false; a closed interval is created by default.
166+
*/
167+
fun interval( start: UByte, end: UByte, isStartIncluded: Boolean = true, isEndIncluded: Boolean = true ) =
168+
UByteInterval( start, isStartIncluded, end, isEndIncluded )
169+
170+
115171
/**
116172
* An [Interval] representing the set of all [UShort] values lying between a provided [start] and [end] value.
117173
* The interval can be closed, open, or half-open, as determined by [isStartIncluded] and [isEndIncluded].
@@ -125,6 +181,14 @@ class UShortInterval( start: UShort, isStartIncluded: Boolean, end: UShort, isEn
125181
}
126182
}
127183

184+
/**
185+
* Create a [UShortInterval] representing the set of all [UShort] values lying between [start] and [end].
186+
* To exclude endpoints, set [isStartIncluded] or [isEndIncluded] to false; a closed interval is created by default.
187+
*/
188+
fun interval( start: UShort, end: UShort, isStartIncluded: Boolean = true, isEndIncluded: Boolean = true ) =
189+
UShortInterval( start, isStartIncluded, end, isEndIncluded )
190+
191+
128192
/**
129193
* An [Interval] representing the set of all [UInt] values lying between a provided [start] and [end] value.
130194
* The interval can be closed, open, or half-open, as determined by [isStartIncluded] and [isEndIncluded].
@@ -138,6 +202,14 @@ class UIntInterval( start: UInt, isStartIncluded: Boolean, end: UInt, isEndInclu
138202
}
139203
}
140204

205+
/**
206+
* Create a [UIntInterval] representing the set of all [UInt] values lying between [start] and [end].
207+
* To exclude endpoints, set [isStartIncluded] or [isEndIncluded] to false; a closed interval is created by default.
208+
*/
209+
fun interval( start: UInt, end: UInt, isStartIncluded: Boolean = true, isEndIncluded: Boolean = true ) =
210+
UIntInterval( start, isStartIncluded, end, isEndIncluded )
211+
212+
141213
/**
142214
* An [Interval] representing the set of all [ULong] values lying between a provided [start] and [end] value.
143215
* The interval can be closed, open, or half-open, as determined by [isStartIncluded] and [isEndIncluded].
@@ -151,6 +223,14 @@ class ULongInterval( start: ULong, isStartIncluded: Boolean, end: ULong, isEndIn
151223
}
152224
}
153225

226+
/**
227+
* Create a [ULongInterval] representing the set of all [ULong] values lying between [start] and [end].
228+
* To exclude endpoints, set [isStartIncluded] or [isEndIncluded] to false; a closed interval is created by default.
229+
*/
230+
fun interval( start: ULong, end: ULong, isStartIncluded: Boolean = true, isEndIncluded: Boolean = true ) =
231+
ULongInterval( start, isStartIncluded, end, isEndIncluded )
232+
233+
154234
/**
155235
* An [Interval] representing the set of all [Char] values lying between a provided [start] and [end] value.
156236
* The interval can be closed, open, or half-open, as determined by [isStartIncluded] and [isEndIncluded].
@@ -163,3 +243,10 @@ class CharInterval( start: Char, isStartIncluded: Boolean, end: Char, isEndInclu
163243
internal val Operations = createIntervalTypeOperations<Char, UShort> { it.code.toUShort() }
164244
}
165245
}
246+
247+
/**
248+
* Create a [CharInterval] representing the set of all [Char] values lying between [start] and [end].
249+
* To exclude endpoints, set [isStartIncluded] or [isEndIncluded] to false; a closed interval is created by default.
250+
*/
251+
fun interval( start: Char, end: Char, isStartIncluded: Boolean = true, isEndIncluded: Boolean = true ) =
252+
CharInterval( start, isStartIncluded, end, isEndIncluded )

src/commonMain/kotlin/Interval.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ open class Interval<T : Comparable<T>, TSize : Comparable<TSize>>(
4444
/**
4545
* Determines whether the [start] of the interval is greater than the [end].
4646
*/
47-
inline val isReversed: Boolean get() = start > end
47+
inline val isReversed: Boolean get() = start > end
4848

4949
/**
5050
* The absolute difference between [start] and [end].
@@ -67,6 +67,25 @@ open class Interval<T : Comparable<T>, TSize : Comparable<TSize>>(
6767
}
6868
}
6969

70+
71+
/**
72+
* Checks whether [value] lies within this interval.
73+
*/
74+
operator fun contains( value: T ): Boolean
75+
{
76+
class Endpoint( val value: T, val isIncluded: Boolean )
77+
78+
val a = Endpoint( start, isStartIncluded )
79+
val b = Endpoint( end, isEndIncluded )
80+
val (smallest, greatest) = if ( isReversed ) b to a else a to b
81+
82+
val smallestComp = value.compareTo( smallest.value )
83+
val greatestComp = value.compareTo( greatest.value )
84+
85+
return ( smallestComp > 0 || (smallestComp == 0 && smallest.isIncluded) )
86+
&& ( greatestComp < 0 || (greatestComp == 0 && greatest.isIncluded) )
87+
}
88+
7089
/**
7190
* Determines whether this interval equals [other]'s constructor parameters exactly,
7291
* i.e., not whether they represent the same set of [T] values, such as matching inverse intervals.

src/commonTest/kotlin/IntervalTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,28 @@ abstract class IntervalTest<T : Comparable<T>, TSize : Comparable<TSize>>(
112112
sizeOperations.unsafeAdd( rangeBelowIdentity, rangeAboveIdentity )
113113
)
114114
}
115+
116+
@Test
117+
fun contains_for_values_within_interval()
118+
{
119+
val acIntervals = createAllInclusionTypeIntervals( a, c ) + createAllInclusionTypeIntervals( c, a )
120+
acIntervals.forEach { assertTrue( b in it ) }
121+
}
122+
123+
@Test
124+
fun contains_for_values_on_endpoints_of_half_open_intervals()
125+
{
126+
val onlyAIncluded = listOf(
127+
createInterval( a, true, b, false ),
128+
createInterval( b, false, a, true )
129+
)
130+
onlyAIncluded.forEach { assertTrue(a in it && b !in it ) }
131+
}
132+
133+
@Test
134+
fun contains_for_values_on_endpoints_of_open_intervals()
135+
{
136+
val openIntervals = listOf( createOpenInterval( a, b ), createOpenInterval( b, a ) )
137+
openIntervals.forEach { assertTrue( a !in it && b !in it ) }
138+
}
115139
}

src/commonTest/kotlin/Readme.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ class Readme
88
@Test
99
fun introduction_example()
1010
{
11-
val interval = IntInterval(
12-
start = 0,
13-
isStartIncluded = true,
14-
end = 10,
15-
isEndIncluded = false
16-
)
11+
val interval: IntInterval = interval( 0, 10, isEndIncluded = false )
12+
val areIncluded = 0 in interval && 5 in interval // true
13+
val areExcluded = 10 !in interval && 15 !in interval // true
1714
val size: UInt = interval.size // 10
1815
}
1916
}

0 commit comments

Comments
 (0)