Add *Range.isEmpty(), *Progression.isEmpty()

This commit is contained in:
Alexander Udalov
2014-06-16 18:50:16 +04:00
parent 07378a74d3
commit 751f062f23
8 changed files with 90 additions and 0 deletions
+14
View File
@@ -29,6 +29,8 @@ public class ByteProgression(
override fun iterator(): ByteIterator = ByteProgressionIterator(start, end, increment)
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is ByteProgression && start == other.start && end == other.end && increment == other.increment
@@ -48,6 +50,8 @@ public class CharProgression(
override fun iterator(): CharIterator = CharProgressionIterator(start, end, increment)
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is CharProgression && start == other.start && end == other.end && increment == other.increment
@@ -67,6 +71,8 @@ public class ShortProgression(
override fun iterator(): ShortIterator = ShortProgressionIterator(start, end, increment)
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is ShortProgression && start == other.start && end == other.end && increment == other.increment
@@ -86,6 +92,8 @@ public class IntProgression(
override fun iterator(): IntIterator = IntProgressionIterator(start, end, increment)
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is IntProgression && start == other.start && end == other.end && increment == other.increment
@@ -105,6 +113,8 @@ public class LongProgression(
override fun iterator(): LongIterator = LongProgressionIterator(start, end, increment)
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is LongProgression && start == other.start && end == other.end && increment == other.increment
@@ -125,6 +135,8 @@ public class FloatProgression(
override fun iterator(): FloatIterator = FloatProgressionIterator(start, end, increment)
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is FloatProgression && java.lang.Float.compare(start, other.start) == 0 && java.lang.Float.compare(end, other.end) == 0 && java.lang.Float.compare(increment, other.increment) == 0
@@ -145,6 +157,8 @@ public class DoubleProgression(
override fun iterator(): DoubleIterator = DoubleProgressionIterator(start, end, increment)
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is DoubleProgression && java.lang.Double.compare(start, other.start) == 0 && java.lang.Double.compare(end, other.end) == 0 && java.lang.Double.compare(increment, other.increment) == 0
+2
View File
@@ -23,5 +23,7 @@ public trait Range<T : Comparable<T>> {
public fun contains(item: T): Boolean
public fun isEmpty(): Boolean = start > end
override fun toString(): String = "$start..$end"
}
+14
View File
@@ -26,6 +26,8 @@ public class ByteRange(public override val start: Byte, public override val end:
override fun iterator(): ByteIterator = ByteProgressionIterator(start, end, 1)
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is ByteRange && start == other.start && end == other.end
@@ -44,6 +46,8 @@ public class CharRange(public override val start: Char, public override val end:
override fun iterator(): CharIterator = CharProgressionIterator(start, end, 1)
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is CharRange && start == other.start && end == other.end
@@ -62,6 +66,8 @@ public class ShortRange(public override val start: Short, public override val en
override fun iterator(): ShortIterator = ShortProgressionIterator(start, end, 1)
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is ShortRange && start == other.start && end == other.end
@@ -80,6 +86,8 @@ public class IntRange(public override val start: Int, public override val end: I
override fun iterator(): IntIterator = IntProgressionIterator(start, end, 1)
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is IntRange && start == other.start && end == other.end
@@ -98,6 +106,8 @@ public class LongRange(public override val start: Long, public override val end:
override fun iterator(): LongIterator = LongProgressionIterator(start, end, 1)
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is LongRange && start == other.start && end == other.end
@@ -116,6 +126,8 @@ public class FloatRange(public override val start: Float, public override val en
override fun iterator(): FloatIterator = FloatProgressionIterator(start, end, 1.0f)
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is FloatRange && java.lang.Float.compare(start, other.start) == 0 && java.lang.Float.compare(end, other.end) == 0
@@ -134,6 +146,8 @@ public class DoubleRange(public override val start: Double, public override val
override fun iterator(): DoubleIterator = DoubleProgressionIterator(start, end, 1.0)
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is DoubleRange && java.lang.Double.compare(start, other.start) == 0 && java.lang.Double.compare(end, other.end) == 0