Skip to content

Commit 43d4f93

Browse files
committed
GROOVY-11577: Add withIndex/indexed methods for ArrayGroovyMethods
1 parent 3879953 commit 43d4f93

File tree

2 files changed

+237
-5
lines changed

2 files changed

+237
-5
lines changed

src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java

+198-5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.codehaus.groovy.util.DoubleArrayIterable;
5151
import org.codehaus.groovy.util.DoubleArrayIterator;
5252
import org.codehaus.groovy.util.DoubleDoubleArrayColumnIterator;
53+
import org.codehaus.groovy.util.FloatArrayIterable;
5354
import org.codehaus.groovy.util.FloatArrayIterator;
5455
import org.codehaus.groovy.util.FloatFloatArrayColumnIterator;
5556
import org.codehaus.groovy.util.IntArrayIterable;
@@ -4468,6 +4469,22 @@ public static Map<Integer, Long> indexed(long[] self) {
44684469
return indexed(self, 0);
44694470
}
44704471

4472+
/**
4473+
* Zips a float[] with indices in (index, value) order starting from index 0.
4474+
* <p/>
4475+
* Example usage:
4476+
* <pre class="groovyTestCase">
4477+
* float[] nums = [10.0f, 20.0f, 30.0f]
4478+
* assert [0: 10.0f, 1: 20.0f, 2: 30.0f] == nums.indexed()
4479+
* </pre>
4480+
*
4481+
* @see #indexed(float[], int)
4482+
* @since 5.0.0
4483+
*/
4484+
public static Map<Integer, Float> indexed(float[] self) {
4485+
return indexed(self, 0);
4486+
}
4487+
44714488
/**
44724489
* Zips a double[] with indices in (index, value) order starting from index 0.
44734490
* <p/>
@@ -4484,10 +4501,8 @@ public static Map<Integer, Double> indexed(double[] self) {
44844501
return indexed(self, 0);
44854502
}
44864503

4487-
//
4488-
44894504
/**
4490-
* Zips an int[] with indices in (index, value) order.
4505+
* Zips an int[] with indices in (index, value) order starting from a given index.
44914506
* <p/>
44924507
* Example usage:
44934508
* <pre class="groovyTestCase">
@@ -4507,7 +4522,7 @@ public static Map<Integer, Integer> indexed(int[] self, int offset) {
45074522
}
45084523

45094524
/**
4510-
* Zips a long[] with indices in (index, value) order.
4525+
* Zips a long[] with indices in (index, value) order starting from a given index.
45114526
* <p/>
45124527
* Example usage:
45134528
* <pre class="groovyTestCase">
@@ -4526,7 +4541,26 @@ public static Map<Integer, Long> indexed(long[] self, int offset) {
45264541
}
45274542

45284543
/**
4529-
* Zips a double[] with indices in (index, value) order.
4544+
* Zips a float[] with indices in (index, value) order starting from a given index.
4545+
* <p/>
4546+
* Example usage:
4547+
* <pre class="groovyTestCase">
4548+
* float[] nums = [10.0f, 20.0f, 30.0f]
4549+
* assert [5: 10.0f, 6: 20.0f, 7: 30.0f] == nums.indexed(5)
4550+
* </pre>
4551+
*
4552+
* @param self a float[]
4553+
* @param offset an index to start from
4554+
* @return a Map (since the keys/indices are unique) containing the elements from the iterable zipped with indices
4555+
* @see DefaultGroovyMethods#indexed(Iterable, int)
4556+
* @since 5.0.0
4557+
*/
4558+
public static Map<Integer, Float> indexed(float[] self, int offset) {
4559+
return DefaultGroovyMethods.indexed(new FloatArrayIterable(self), offset);
4560+
}
4561+
4562+
/**
4563+
* Zips a double[] with indices in (index, value) order starting from a given index.
45304564
* <p/>
45314565
* Example usage:
45324566
* <pre class="groovyTestCase">
@@ -9205,6 +9239,165 @@ public static <K, V> Map<K, V> withCollectedValues(K[] keys, Map<K, V> collector
92059239
return DefaultGroovyMethods.collectEntries(new ArrayIterator<>(keys), collector, Function.identity(), valueTransform);
92069240
}
92079241

9242+
//--------------------------------------------------------------------------
9243+
// withIndex
9244+
9245+
/**
9246+
* Zips an int array with indices in (value, index) order.
9247+
* <p/>
9248+
* Example usage:
9249+
* <pre class="groovyTestCase">
9250+
* int[] nums = [42, -1]
9251+
* assert [[42, 0], [-1, 1]] == nums.withIndex()
9252+
* assert ["0: 42", "1: -1"] == nums.withIndex().collect { n, idx {@code ->} "$idx: $n" }
9253+
* </pre>
9254+
*
9255+
* @param self an int array
9256+
* @return a zipped list with indices
9257+
* @see DefaultGroovyMethods#withIndex(Iterable)
9258+
* @since 5.0.0
9259+
*/
9260+
public static List<Tuple2<Integer, Integer>> withIndex(int[] self) {
9261+
return withIndex(self, 0);
9262+
}
9263+
9264+
/**
9265+
* Zips an int array with indices in (value, index) order starting from a given index.
9266+
* <p/>
9267+
* Example usage:
9268+
* <pre class="groovyTestCase">
9269+
* int[] nums = [42, -1]
9270+
* assert [[42, 5], [-1, 6]] == nums.withIndex(5)
9271+
* assert ["5: 42", "6: -1"] == nums.withIndex(5).collect { n, idx {@code ->} "$idx: $n" }
9272+
* </pre>
9273+
*
9274+
* @param self an int array
9275+
* @param offset an index to start from
9276+
* @return a zipped list with indices
9277+
* @see DefaultGroovyMethods#withIndex(Iterable, int)
9278+
* @since 5.0.0
9279+
*/
9280+
public static List<Tuple2<Integer, Integer>> withIndex(int[] self, int offset) {
9281+
return DefaultGroovyMethods.withIndex(new IntArrayIterable(self), offset);
9282+
}
9283+
9284+
/**
9285+
* Zips a long array with indices in (value, index) order.
9286+
* <p/>
9287+
* Example usage:
9288+
* <pre class="groovyTestCase">
9289+
* long[] nums = [42L, -1L]
9290+
* assert [[42L, 0], [-1L, 1]] == nums.withIndex()
9291+
* assert ["0: 42", "1: -1"] == nums.withIndex().collect { n, idx {@code ->} "$idx: $n" }
9292+
* </pre>
9293+
*
9294+
* @param self a long array
9295+
* @return a zipped list with indices
9296+
* @see DefaultGroovyMethods#withIndex(Iterable)
9297+
* @since 5.0.0
9298+
*/
9299+
public static List<Tuple2<Long, Integer>> withIndex(long[] self) {
9300+
return withIndex(self, 0);
9301+
}
9302+
9303+
/**
9304+
* Zips a long array with indices in (value, index) order starting from a given index.
9305+
* <p/>
9306+
* Example usage:
9307+
* <pre class="groovyTestCase">
9308+
* long[] nums = [42L, -1L]
9309+
* assert [[42L, 5], [-1L, 6]] == nums.withIndex(5)
9310+
* assert ["5: 42", "6: -1"] == nums.withIndex(5).collect { n, idx {@code ->} "$idx: $n" }
9311+
* </pre>
9312+
*
9313+
* @param self a long array
9314+
* @param offset an index to start from
9315+
* @return a zipped list with indices
9316+
* @see DefaultGroovyMethods#withIndex(Iterable)
9317+
* @since 5.0.0
9318+
*/
9319+
public static List<Tuple2<Long, Integer>> withIndex(long[] self, int offset) {
9320+
return DefaultGroovyMethods.withIndex(new LongArrayIterable(self), offset);
9321+
}
9322+
9323+
/**
9324+
* Zips a float array with indices in (value, index) order.
9325+
* <p/>
9326+
* Example usage:
9327+
* <pre class="groovyTestCase">
9328+
* float[] nums = [42.0f, -1.0f]
9329+
* assert [[42.0f, 0], [-1.0f, 1]] == nums.withIndex()
9330+
* assert ["0: 42.0", "1: -1.0"] == nums.withIndex().collect { n, idx {@code ->} "$idx: $n" }
9331+
* </pre>
9332+
*
9333+
* @param self a float array
9334+
* @return a zipped list with indices
9335+
* @see DefaultGroovyMethods#withIndex(Iterable)
9336+
* @since 5.0.0
9337+
*/
9338+
public static List<Tuple2<Float, Integer>> withIndex(float[] self) {
9339+
return withIndex(self, 0);
9340+
}
9341+
9342+
/**
9343+
* Zips a float array with indices in (value, index) order starting from a given index.
9344+
* <p/>
9345+
* Example usage:
9346+
* <pre class="groovyTestCase">
9347+
* float[] nums = [42.0f, -1.0f]
9348+
* assert [[42.0f, 5], [-1.0f, 6]] == nums.withIndex(5)
9349+
* assert ["5: 42.0", "6: -1.0"] == nums.withIndex(5).collect { n, idx {@code ->} "$idx: $n" }
9350+
* </pre>
9351+
*
9352+
* @param self a float array
9353+
* @param offset an index to start from
9354+
* @return a zipped list with indices
9355+
* @see DefaultGroovyMethods#withIndex(Iterable)
9356+
* @since 5.0.0
9357+
*/
9358+
public static List<Tuple2<Float, Integer>> withIndex(float[] self, int offset) {
9359+
return DefaultGroovyMethods.withIndex(new FloatArrayIterable(self), offset);
9360+
}
9361+
9362+
/**
9363+
* Zips a double array with indices in (value, index) order.
9364+
* <p/>
9365+
* Example usage:
9366+
* <pre class="groovyTestCase">
9367+
* double[] nums = [42.0d, -1.0d]
9368+
* assert [[42.0d, 0], [-1.0d, 1]] == nums.withIndex()
9369+
* assert ["0: 42.0", "1: -1.0"] == nums.withIndex().collect { n, idx {@code ->} "$idx: $n" }
9370+
* </pre>
9371+
*
9372+
* @param self a double array
9373+
* @return a zipped list with indices
9374+
* @see DefaultGroovyMethods#withIndex(Iterable)
9375+
* @since 5.0.0
9376+
*/
9377+
public static List<Tuple2<Double, Integer>> withIndex(double[] self) {
9378+
return withIndex(self, 0);
9379+
}
9380+
9381+
/**
9382+
* Zips a double array with indices in (value, index) order starting from a given index.
9383+
* <p/>
9384+
* Example usage:
9385+
* <pre class="groovyTestCase">
9386+
* double[] nums = [42.0d, -1.0d]
9387+
* assert [[42.0d, 5], [-1.0d, 6]] == nums.withIndex(5)
9388+
* assert ["5: 42.0", "6: -1.0"] == nums.withIndex(5).collect { n, idx {@code ->} "$idx: $n" }
9389+
* </pre>
9390+
*
9391+
* @param self a double array
9392+
* @param offset an index to start from
9393+
* @return a zipped list with indices
9394+
* @see DefaultGroovyMethods#withIndex(Iterable)
9395+
* @since 5.0.0
9396+
*/
9397+
public static List<Tuple2<Double, Integer>> withIndex(double[] self, int offset) {
9398+
return DefaultGroovyMethods.withIndex(new DoubleArrayIterable(self), offset);
9399+
}
9400+
92089401
//--------------------------------------------------------------------------
92099402
// zip
92109403

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.codehaus.groovy.util;
20+
21+
import java.util.Iterator;
22+
23+
/**
24+
* Allows a primitive float array to be used where an Iterable is expected.
25+
*
26+
* @since 5.0.0
27+
*/
28+
public class FloatArrayIterable implements Iterable<Float> {
29+
private final float[] array;
30+
31+
public FloatArrayIterable(final float[] array) {
32+
this.array = array;
33+
}
34+
35+
@Override
36+
public Iterator<Float> iterator() {
37+
return new FloatArrayIterator(array);
38+
}
39+
}

0 commit comments

Comments
 (0)