From 4736b8d6e7f47063dcaac9b244b581f5538051eb Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Tue, 22 Jan 2013 23:17:39 +0400 Subject: [PATCH] Introduced sequence classes. --- compiler/frontend/src/jet/Ranges.jet | 70 ++++++++++++++++++++++++++ runtime/src/jet/ByteSequence.java | 55 ++++++++++++++++++++ runtime/src/jet/CharacterSequence.java | 55 ++++++++++++++++++++ runtime/src/jet/DoubleSequence.java | 55 ++++++++++++++++++++ runtime/src/jet/FloatSequence.java | 55 ++++++++++++++++++++ runtime/src/jet/IntSequence.java | 55 ++++++++++++++++++++ runtime/src/jet/LongSequence.java | 55 ++++++++++++++++++++ runtime/src/jet/NumberSequence.java | 26 ++++++++++ runtime/src/jet/ShortSequence.java | 55 ++++++++++++++++++++ 9 files changed, 481 insertions(+) create mode 100644 runtime/src/jet/ByteSequence.java create mode 100644 runtime/src/jet/CharacterSequence.java create mode 100644 runtime/src/jet/DoubleSequence.java create mode 100644 runtime/src/jet/FloatSequence.java create mode 100644 runtime/src/jet/IntSequence.java create mode 100644 runtime/src/jet/LongSequence.java create mode 100644 runtime/src/jet/NumberSequence.java create mode 100644 runtime/src/jet/ShortSequence.java diff --git a/compiler/frontend/src/jet/Ranges.jet b/compiler/frontend/src/jet/Ranges.jet index f46f320b0fc..e7b2dc614c8 100644 --- a/compiler/frontend/src/jet/Ranges.jet +++ b/compiler/frontend/src/jet/Ranges.jet @@ -97,3 +97,73 @@ public class DoubleRange(public val start : Double, public val size : Double) : public val EMPTY: DoubleRange } } + + +public trait NumberSequence: Iterable { + public val start: N + public val end: N + public val increment: Number +} + +public class IntSequence( + public override val start: Int, + public override val end: Int, + public override val increment: Int +): NumberSequence { + + override fun iterator(): IntIterator +} + +public class LongSequence( + public override val start: Long, + public override val end: Long, + public override val increment: Long +): NumberSequence { + + override fun iterator(): LongIterator +} + +public class ByteSequence( + public override val start: Byte, + public override val end: Byte, + public override val increment: Int +): NumberSequence { + + override fun iterator(): ByteIterator +} + +public class ShortSequence( + public override val start: Short, + public override val end: Short, + public override val increment: Int +): NumberSequence { + + override fun iterator(): ShortIterator +} + +public class CharacterSequence( + public override val start: Char, + public override val end: Char, + public override val increment: Int +): NumberSequence { + + override fun iterator(): CharIterator +} + +public class FloatSequence( + public override val start: Float, + public override val end: Float, + public override val increment: Float +): NumberSequence { + + override fun iterator(): FloatIterator +} + +public class DoubleSequence( + public override val start: Double, + public override val end: Double, + public override val increment: Double +): NumberSequence { + + override fun iterator(): DoubleIterator +} diff --git a/runtime/src/jet/ByteSequence.java b/runtime/src/jet/ByteSequence.java new file mode 100644 index 00000000000..f678ee74dbc --- /dev/null +++ b/runtime/src/jet/ByteSequence.java @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package jet; + +import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver; + +@AssertInvisibleInResolver +public class ByteSequence implements NumberSequence { + private final byte start; + private final byte end; + private final int increment; + + public ByteSequence(byte start, byte end, int increment) { + if (increment == 0) { + throw new IllegalArgumentException("Increment must be non-zero: " + increment); + } + this.start = start; + this.end = end; + this.increment = increment; + } + + @Override + public Byte getStart() { + return start; + } + + @Override + public Byte getEnd() { + return end; + } + + @Override + public Integer getIncrement() { + return increment; + } + + @Override + public ByteIterator iterator() { + return new ByteSequenceIterator(start, end, increment); + } +} diff --git a/runtime/src/jet/CharacterSequence.java b/runtime/src/jet/CharacterSequence.java new file mode 100644 index 00000000000..65ad0e07f5f --- /dev/null +++ b/runtime/src/jet/CharacterSequence.java @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package jet; + +import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver; + +@AssertInvisibleInResolver +public class CharacterSequence implements NumberSequence { + private final char start; + private final char end; + private final int increment; + + public CharacterSequence(char start, char end, int increment) { + if (increment == 0) { + throw new IllegalArgumentException("Increment must be non-zero: " + increment); + } + this.start = start; + this.end = end; + this.increment = increment; + } + + @Override + public Character getStart() { + return start; + } + + @Override + public Character getEnd() { + return end; + } + + @Override + public Integer getIncrement() { + return increment; + } + + @Override + public CharIterator iterator() { + return new CharacterSequenceIterator(start, end, increment); + } +} diff --git a/runtime/src/jet/DoubleSequence.java b/runtime/src/jet/DoubleSequence.java new file mode 100644 index 00000000000..24a2aa35397 --- /dev/null +++ b/runtime/src/jet/DoubleSequence.java @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package jet; + +import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver; + +@AssertInvisibleInResolver +public class DoubleSequence implements NumberSequence { + private final double start; + private final double end; + private final double increment; + + public DoubleSequence(double start, double end, double increment) { + if (increment == 0.0 || increment == -0.0) { + throw new IllegalArgumentException("Increment must be non-zero: " + increment); + } + this.start = start; + this.end = end; + this.increment = increment; + } + + @Override + public Double getStart() { + return start; + } + + @Override + public Double getEnd() { + return end; + } + + @Override + public Double getIncrement() { + return increment; + } + + @Override + public DoubleIterator iterator() { + return new DoubleSequenceIterator(start, end, increment); + } +} diff --git a/runtime/src/jet/FloatSequence.java b/runtime/src/jet/FloatSequence.java new file mode 100644 index 00000000000..26224e9a462 --- /dev/null +++ b/runtime/src/jet/FloatSequence.java @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package jet; + +import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver; + +@AssertInvisibleInResolver +public class FloatSequence implements NumberSequence { + private final float start; + private final float end; + private final float increment; + + public FloatSequence(float start, float end, float increment) { + if (increment == 0.0f || increment == -0.0f) { + throw new IllegalArgumentException("Increment must be non-zero: " + increment); + } + this.start = start; + this.end = end; + this.increment = increment; + } + + @Override + public Float getStart() { + return start; + } + + @Override + public Float getEnd() { + return end; + } + + @Override + public Float getIncrement() { + return increment; + } + + @Override + public FloatIterator iterator() { + return new FloatSequenceIterator(start, end, increment); + } +} diff --git a/runtime/src/jet/IntSequence.java b/runtime/src/jet/IntSequence.java new file mode 100644 index 00000000000..e15a37c480b --- /dev/null +++ b/runtime/src/jet/IntSequence.java @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package jet; + +import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver; + +@AssertInvisibleInResolver +public class IntSequence implements NumberSequence { + private final int start; + private final int end; + private final int increment; + + public IntSequence(int start, int end, int increment) { + if (increment == 0) { + throw new IllegalArgumentException("Increment must be non-zero: " + increment); + } + this.start = start; + this.end = end; + this.increment = increment; + } + + @Override + public Integer getStart() { + return start; + } + + @Override + public Integer getEnd() { + return end; + } + + @Override + public Integer getIncrement() { + return increment; + } + + @Override + public IntIterator iterator() { + return new IntSequenceIterator(start, end, increment); + } +} diff --git a/runtime/src/jet/LongSequence.java b/runtime/src/jet/LongSequence.java new file mode 100644 index 00000000000..4972d588ca0 --- /dev/null +++ b/runtime/src/jet/LongSequence.java @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package jet; + +import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver; + +@AssertInvisibleInResolver +public class LongSequence implements NumberSequence { + private final long start; + private final long end; + private final long increment; + + public LongSequence(long start, long end, long increment) { + if (increment == 0) { + throw new IllegalArgumentException("Increment must be non-zero: " + increment); + } + this.start = start; + this.end = end; + this.increment = increment; + } + + @Override + public Long getStart() { + return start; + } + + @Override + public Long getEnd() { + return end; + } + + @Override + public Long getIncrement() { + return increment; + } + + @Override + public LongIterator iterator() { + return new LongSequenceIterator(start, end, increment); + } +} diff --git a/runtime/src/jet/NumberSequence.java b/runtime/src/jet/NumberSequence.java new file mode 100644 index 00000000000..09c06b8e3f4 --- /dev/null +++ b/runtime/src/jet/NumberSequence.java @@ -0,0 +1,26 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package jet; + +import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver; + +@AssertInvisibleInResolver +public interface NumberSequence extends Iterable { + N getStart(); + N getEnd(); + Number getIncrement(); +} diff --git a/runtime/src/jet/ShortSequence.java b/runtime/src/jet/ShortSequence.java new file mode 100644 index 00000000000..e2cac38b247 --- /dev/null +++ b/runtime/src/jet/ShortSequence.java @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package jet; + +import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver; + +@AssertInvisibleInResolver +public class ShortSequence implements NumberSequence { + private final short start; + private final short end; + private final int increment; + + public ShortSequence(short start, short end, int increment) { + if (increment == 0) { + throw new IllegalArgumentException("Increment must be non-zero: " + increment); + } + this.start = start; + this.end = end; + this.increment = increment; + } + + @Override + public Short getStart() { + return start; + } + + @Override + public Short getEnd() { + return end; + } + + @Override + public Integer getIncrement() { + return increment; + } + + @Override + public ShortIterator iterator() { + return new ShortSequenceIterator(start, end, increment); + } +}