Repository: giraph Updated Branches: refs/heads/trunk 43de1d8cd -> bc2babcc6
[GIRAPH-986] Add more stuff to TypeOps Summary: Add: createZero/createOne to NumericTypeOps unwrap, fill to BasicArrayList AndReduce and OrReduce Test Plan: mvn clean install Reviewers: majakabiljo, sergey.edunov, maja.kabiljo Reviewed By: maja.kabiljo Subscribers: laxman.dhulipala Differential Revision: https://reviews.facebook.net/D31731 Project: http://git-wip-us.apache.org/repos/asf/giraph/repo Commit: http://git-wip-us.apache.org/repos/asf/giraph/commit/bc2babcc Tree: http://git-wip-us.apache.org/repos/asf/giraph/tree/bc2babcc Diff: http://git-wip-us.apache.org/repos/asf/giraph/diff/bc2babcc Branch: refs/heads/trunk Commit: bc2babcc63ba482f7f15ff4820abf51339081e5f Parents: 43de1d8 Author: Igor Kabiljo <[email protected]> Authored: Fri Jan 16 16:30:29 2015 -0800 Committer: Maja Kabiljo <[email protected]> Committed: Fri Jan 16 16:31:12 2015 -0800 ---------------------------------------------------------------------- CHANGELOG | 2 + .../apache/giraph/reducers/impl/AndReduce.java | 56 +++++++ .../apache/giraph/reducers/impl/MaxReduce.java | 5 + .../apache/giraph/reducers/impl/MinReduce.java | 5 + .../apache/giraph/reducers/impl/OrReduce.java | 56 +++++++ .../apache/giraph/reducers/impl/SumReduce.java | 5 + .../apache/giraph/types/ops/BooleanTypeOps.java | 5 + .../apache/giraph/types/ops/ByteTypeOps.java | 44 +++++- .../apache/giraph/types/ops/DoubleTypeOps.java | 20 ++- .../apache/giraph/types/ops/FloatTypeOps.java | 20 ++- .../org/apache/giraph/types/ops/IntTypeOps.java | 24 ++- .../apache/giraph/types/ops/LongTypeOps.java | 24 ++- .../apache/giraph/types/ops/NumericTypeOps.java | 17 ++- .../giraph/types/ops/PrimitiveTypeOps.java | 6 + .../types/ops/collections/BasicArrayList.java | 152 ++++++++++++++++++- 15 files changed, 405 insertions(+), 36 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/CHANGELOG ---------------------------------------------------------------------- diff --git a/CHANGELOG b/CHANGELOG index ad94207..9adca87 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ Giraph Change Log Release 1.2.0 - unreleased + GIRAPH-986: Add more stuff to TypeOps (ikabiljo via majakabiljo) + GIRAPH-962: TextAggregatorWriter with frequency AT_THE_END writes in every superstep (mju via majakabiljo) GIRAPH-967: (Strongly Connected Components) bug fix (mhan via majakabiljo) http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/reducers/impl/AndReduce.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/reducers/impl/AndReduce.java b/giraph-core/src/main/java/org/apache/giraph/reducers/impl/AndReduce.java new file mode 100644 index 0000000..31a324c --- /dev/null +++ b/giraph-core/src/main/java/org/apache/giraph/reducers/impl/AndReduce.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.giraph.reducers.impl; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; + +import org.apache.giraph.reducers.OnSameReduceOperation; +import org.apache.hadoop.io.BooleanWritable; + +/** + * + * ReduceOperation for calculating the AND function over boolean values. + * The default value when nothing is aggregated is true. + * + */ +public class AndReduce extends OnSameReduceOperation<BooleanWritable> { + /** Instance */ + public static final AndReduce INSTANCE = new AndReduce(); + + @Override + public BooleanWritable createInitialValue() { + return new BooleanWritable(true); + } + + @Override + public BooleanWritable reduceSingle( + BooleanWritable curValue, BooleanWritable valueToReduce) { + curValue.set(curValue.get() && valueToReduce.get()); + return curValue; + } + + @Override + public void write(DataOutput out) throws IOException { + } + + @Override + public void readFields(DataInput in) throws IOException { + } +} http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/reducers/impl/MaxReduce.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/reducers/impl/MaxReduce.java b/giraph-core/src/main/java/org/apache/giraph/reducers/impl/MaxReduce.java index 9d603a1..febd9a4 100644 --- a/giraph-core/src/main/java/org/apache/giraph/reducers/impl/MaxReduce.java +++ b/giraph-core/src/main/java/org/apache/giraph/reducers/impl/MaxReduce.java @@ -23,10 +23,12 @@ import java.io.IOException; import org.apache.giraph.reducers.OnSameReduceOperation; import org.apache.giraph.types.ops.DoubleTypeOps; +import org.apache.giraph.types.ops.IntTypeOps; import org.apache.giraph.types.ops.LongTypeOps; import org.apache.giraph.types.ops.NumericTypeOps; import org.apache.giraph.types.ops.TypeOpsUtils; import org.apache.hadoop.io.DoubleWritable; +import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.WritableComparable; @@ -42,6 +44,9 @@ public class MaxReduce<T extends WritableComparable> /** LongWritable specialization */ public static final MaxReduce<LongWritable> LONG = new MaxReduce<>(LongTypeOps.INSTANCE); + /** IntWritable specialization */ + public static final MaxReduce<IntWritable> INT = + new MaxReduce<>(IntTypeOps.INSTANCE); /** Value type operations */ private NumericTypeOps<T> typeOps; http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/reducers/impl/MinReduce.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/reducers/impl/MinReduce.java b/giraph-core/src/main/java/org/apache/giraph/reducers/impl/MinReduce.java index 9972340..1b5cf03 100644 --- a/giraph-core/src/main/java/org/apache/giraph/reducers/impl/MinReduce.java +++ b/giraph-core/src/main/java/org/apache/giraph/reducers/impl/MinReduce.java @@ -23,10 +23,12 @@ import java.io.IOException; import org.apache.giraph.reducers.OnSameReduceOperation; import org.apache.giraph.types.ops.DoubleTypeOps; +import org.apache.giraph.types.ops.IntTypeOps; import org.apache.giraph.types.ops.LongTypeOps; import org.apache.giraph.types.ops.NumericTypeOps; import org.apache.giraph.types.ops.TypeOpsUtils; import org.apache.hadoop.io.DoubleWritable; +import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.WritableComparable; @@ -42,6 +44,9 @@ public class MinReduce<T extends WritableComparable> /** LongWritable specialization */ public static final MinReduce<LongWritable> LONG = new MinReduce<>(LongTypeOps.INSTANCE); + /** IntWritable specialization */ + public static final MinReduce<IntWritable> INT = + new MinReduce<>(IntTypeOps.INSTANCE); /** Value type operations */ private NumericTypeOps<T> typeOps; http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/reducers/impl/OrReduce.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/reducers/impl/OrReduce.java b/giraph-core/src/main/java/org/apache/giraph/reducers/impl/OrReduce.java new file mode 100644 index 0000000..86dd891 --- /dev/null +++ b/giraph-core/src/main/java/org/apache/giraph/reducers/impl/OrReduce.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.giraph.reducers.impl; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; + +import org.apache.giraph.reducers.OnSameReduceOperation; +import org.apache.hadoop.io.BooleanWritable; + +/** + * + * ReduceOperation for calculating the OR function over boolean values. + * The default value when nothing is aggregated is false. + * + */ +public class OrReduce extends OnSameReduceOperation<BooleanWritable> { + /** Instance */ + public static final OrReduce INSTANCE = new OrReduce(); + + @Override + public BooleanWritable createInitialValue() { + return new BooleanWritable(false); + } + + @Override + public BooleanWritable reduceSingle( + BooleanWritable curValue, BooleanWritable valueToReduce) { + curValue.set(curValue.get() || valueToReduce.get()); + return curValue; + } + + @Override + public void write(DataOutput out) throws IOException { + } + + @Override + public void readFields(DataInput in) throws IOException { + } +} http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/reducers/impl/SumReduce.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/reducers/impl/SumReduce.java b/giraph-core/src/main/java/org/apache/giraph/reducers/impl/SumReduce.java index 3138733..8722ab8 100644 --- a/giraph-core/src/main/java/org/apache/giraph/reducers/impl/SumReduce.java +++ b/giraph-core/src/main/java/org/apache/giraph/reducers/impl/SumReduce.java @@ -23,10 +23,12 @@ import java.io.IOException; import org.apache.giraph.reducers.OnSameReduceOperation; import org.apache.giraph.types.ops.DoubleTypeOps; +import org.apache.giraph.types.ops.IntTypeOps; import org.apache.giraph.types.ops.LongTypeOps; import org.apache.giraph.types.ops.NumericTypeOps; import org.apache.giraph.types.ops.TypeOpsUtils; import org.apache.hadoop.io.DoubleWritable; +import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Writable; @@ -42,6 +44,9 @@ public class SumReduce<T extends Writable> /** LongWritable specialization */ public static final SumReduce<LongWritable> LONG = new SumReduce<>(LongTypeOps.INSTANCE); + /** IntWritable specialization */ + public static final SumReduce<IntWritable> INT = + new SumReduce<>(IntTypeOps.INSTANCE); /** Value type operations */ private NumericTypeOps<T> typeOps; http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/types/ops/BooleanTypeOps.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/types/ops/BooleanTypeOps.java b/giraph-core/src/main/java/org/apache/giraph/types/ops/BooleanTypeOps.java index a65fa3b..980668f 100644 --- a/giraph-core/src/main/java/org/apache/giraph/types/ops/BooleanTypeOps.java +++ b/giraph-core/src/main/java/org/apache/giraph/types/ops/BooleanTypeOps.java @@ -47,6 +47,11 @@ public enum BooleanTypeOps implements PrimitiveTypeOps<BooleanWritable> { } @Override + public BasicBooleanArrayList createArrayList() { + return new BasicBooleanArrayList(); + } + + @Override public BasicBooleanArrayList createArrayList(int capacity) { return new BasicBooleanArrayList(capacity); } http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/types/ops/ByteTypeOps.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/types/ops/ByteTypeOps.java b/giraph-core/src/main/java/org/apache/giraph/types/ops/ByteTypeOps.java index 2b27ba5..08f2c0f 100644 --- a/giraph-core/src/main/java/org/apache/giraph/types/ops/ByteTypeOps.java +++ b/giraph-core/src/main/java/org/apache/giraph/types/ops/ByteTypeOps.java @@ -21,7 +21,9 @@ import org.apache.giraph.types.ops.collections.BasicArrayList.BasicByteArrayList import org.apache.hadoop.io.ByteWritable; /** TypeOps implementation for working with ByteWritable type */ -public enum ByteTypeOps implements PrimitiveTypeOps<ByteWritable> { +public enum ByteTypeOps + implements PrimitiveTypeOps<ByteWritable>, + NumericTypeOps<ByteWritable> { /** Singleton instance */ INSTANCE(); @@ -46,7 +48,47 @@ public enum ByteTypeOps implements PrimitiveTypeOps<ByteWritable> { } @Override + public BasicByteArrayList createArrayList() { + return new BasicByteArrayList(); + } + + @Override public BasicByteArrayList createArrayList(int capacity) { return new BasicByteArrayList(capacity); } + + @Override + public ByteWritable createZero() { + return new ByteWritable((byte) 0); + } + + @Override + public ByteWritable createOne() { + return new ByteWritable((byte) 1); + } + + @Override + public ByteWritable createMinNegativeValue() { + return new ByteWritable(Byte.MIN_VALUE); + } + + @Override + public ByteWritable createMaxPositiveValue() { + return new ByteWritable(Byte.MAX_VALUE); + } + + @Override + public void plusInto(ByteWritable value, ByteWritable increment) { + value.set((byte) (value.get() + increment.get())); + } + + @Override + public void multiplyInto(ByteWritable value, ByteWritable multiplier) { + value.set((byte) (value.get() * multiplier.get())); + } + + @Override + public void negate(ByteWritable value) { + value.set((byte) -value.get()); + } } http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/types/ops/DoubleTypeOps.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/types/ops/DoubleTypeOps.java b/giraph-core/src/main/java/org/apache/giraph/types/ops/DoubleTypeOps.java index 1ca7796..42e18c4 100644 --- a/giraph-core/src/main/java/org/apache/giraph/types/ops/DoubleTypeOps.java +++ b/giraph-core/src/main/java/org/apache/giraph/types/ops/DoubleTypeOps.java @@ -48,11 +48,26 @@ public enum DoubleTypeOps } @Override + public BasicDoubleArrayList createArrayList() { + return new BasicDoubleArrayList(); + } + + @Override public BasicDoubleArrayList createArrayList(int capacity) { return new BasicDoubleArrayList(capacity); } @Override + public DoubleWritable createZero() { + return new DoubleWritable(0); + } + + @Override + public DoubleWritable createOne() { + return new DoubleWritable(1); + } + + @Override public DoubleWritable createMinNegativeValue() { return new DoubleWritable(Double.NEGATIVE_INFINITY); } @@ -63,11 +78,6 @@ public enum DoubleTypeOps } @Override - public DoubleWritable createZero() { - return new DoubleWritable(0); - } - - @Override public void plusInto(DoubleWritable value, DoubleWritable increment) { value.set(value.get() + increment.get()); } http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/types/ops/FloatTypeOps.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/types/ops/FloatTypeOps.java b/giraph-core/src/main/java/org/apache/giraph/types/ops/FloatTypeOps.java index 3c69868..586e39a 100644 --- a/giraph-core/src/main/java/org/apache/giraph/types/ops/FloatTypeOps.java +++ b/giraph-core/src/main/java/org/apache/giraph/types/ops/FloatTypeOps.java @@ -47,11 +47,26 @@ public enum FloatTypeOps } @Override + public BasicFloatArrayList createArrayList() { + return new BasicFloatArrayList(); + } + + @Override public BasicFloatArrayList createArrayList(int capacity) { return new BasicFloatArrayList(capacity); } @Override + public FloatWritable createZero() { + return new FloatWritable(0); + } + + @Override + public FloatWritable createOne() { + return new FloatWritable(1); + } + + @Override public FloatWritable createMinNegativeValue() { return new FloatWritable(Float.NEGATIVE_INFINITY); } @@ -62,11 +77,6 @@ public enum FloatTypeOps } @Override - public FloatWritable createZero() { - return new FloatWritable(0); - } - - @Override public void plusInto(FloatWritable value, FloatWritable increment) { value.set(value.get() + increment.get()); } http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/types/ops/IntTypeOps.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/types/ops/IntTypeOps.java b/giraph-core/src/main/java/org/apache/giraph/types/ops/IntTypeOps.java index 57e1b53..9be0ebd 100644 --- a/giraph-core/src/main/java/org/apache/giraph/types/ops/IntTypeOps.java +++ b/giraph-core/src/main/java/org/apache/giraph/types/ops/IntTypeOps.java @@ -52,8 +52,8 @@ public enum IntTypeOps } @Override - public BasicSet<IntWritable> createOpenHashSet(int capacity) { - return new BasicIntOpenHashSet(capacity); + public BasicArrayList<IntWritable> createArrayList() { + return new BasicIntArrayList(); } @Override @@ -62,12 +62,27 @@ public enum IntTypeOps } @Override + public BasicSet<IntWritable> createOpenHashSet(int capacity) { + return new BasicIntOpenHashSet(capacity); + } + + @Override public <V> Basic2ObjectMap<IntWritable, V> create2ObjectOpenHashMap( int capacity) { return new BasicInt2ObjectOpenHashMap<>(capacity); } @Override + public IntWritable createZero() { + return new IntWritable(0); + } + + @Override + public IntWritable createOne() { + return new IntWritable(1); + } + + @Override public IntWritable createMinNegativeValue() { return new IntWritable(Integer.MIN_VALUE); } @@ -78,11 +93,6 @@ public enum IntTypeOps } @Override - public IntWritable createZero() { - return new IntWritable(0); - } - - @Override public void plusInto(IntWritable value, IntWritable increment) { value.set(value.get() + increment.get()); } http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/types/ops/LongTypeOps.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/types/ops/LongTypeOps.java b/giraph-core/src/main/java/org/apache/giraph/types/ops/LongTypeOps.java index d7fa198..90f81f8 100644 --- a/giraph-core/src/main/java/org/apache/giraph/types/ops/LongTypeOps.java +++ b/giraph-core/src/main/java/org/apache/giraph/types/ops/LongTypeOps.java @@ -52,8 +52,8 @@ public enum LongTypeOps } @Override - public BasicSet<LongWritable> createOpenHashSet(int capacity) { - return new BasicLongOpenHashSet(capacity); + public BasicArrayList<LongWritable> createArrayList() { + return new BasicLongArrayList(); } @Override @@ -62,12 +62,27 @@ public enum LongTypeOps } @Override + public BasicSet<LongWritable> createOpenHashSet(int capacity) { + return new BasicLongOpenHashSet(capacity); + } + + @Override public <V> Basic2ObjectMap<LongWritable, V> create2ObjectOpenHashMap( int capacity) { return new BasicLong2ObjectOpenHashMap<>(capacity); } @Override + public LongWritable createZero() { + return new LongWritable(0); + } + + @Override + public LongWritable createOne() { + return new LongWritable(1); + } + + @Override public LongWritable createMinNegativeValue() { return new LongWritable(Long.MIN_VALUE); } @@ -78,11 +93,6 @@ public enum LongTypeOps } @Override - public LongWritable createZero() { - return new LongWritable(0); - } - - @Override public void plusInto(LongWritable value, LongWritable increment) { value.set(value.get() + increment.get()); } http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/types/ops/NumericTypeOps.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/types/ops/NumericTypeOps.java b/giraph-core/src/main/java/org/apache/giraph/types/ops/NumericTypeOps.java index 396c914..6420a1b 100644 --- a/giraph-core/src/main/java/org/apache/giraph/types/ops/NumericTypeOps.java +++ b/giraph-core/src/main/java/org/apache/giraph/types/ops/NumericTypeOps.java @@ -27,6 +27,17 @@ package org.apache.giraph.types.ops; */ public interface NumericTypeOps<T> extends TypeOps<T> { /** + * Value of zero + * @return New object with value of zero + */ + T createZero(); + /** + * Value of one + * @return New object with value of one + */ + T createOne(); + + /** * Minimal negative value representable via current type. * Negative infinity for floating point numbers. * @return New object with min negative value @@ -38,11 +49,7 @@ public interface NumericTypeOps<T> extends TypeOps<T> { * @return New object with max positive value */ T createMaxPositiveValue(); - /** - * Value of zero - * @return New object with value of zero - */ - T createZero(); + /** * value+=adder http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/types/ops/PrimitiveTypeOps.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/types/ops/PrimitiveTypeOps.java b/giraph-core/src/main/java/org/apache/giraph/types/ops/PrimitiveTypeOps.java index 72b684f..0c7c852 100644 --- a/giraph-core/src/main/java/org/apache/giraph/types/ops/PrimitiveTypeOps.java +++ b/giraph-core/src/main/java/org/apache/giraph/types/ops/PrimitiveTypeOps.java @@ -34,6 +34,12 @@ import org.apache.giraph.types.ops.collections.BasicArrayList; public interface PrimitiveTypeOps<T> extends TypeOps<T> { // primitive collections /** + * Create BasicArrayList of type T. + * @return BasicArrayList + */ + BasicArrayList<T> createArrayList(); + + /** * Create BasicArrayList of type T, given capacity. * @param capacity Capacity * @return BasicArrayList http://git-wip-us.apache.org/repos/asf/giraph/blob/bc2babcc/giraph-core/src/main/java/org/apache/giraph/types/ops/collections/BasicArrayList.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/types/ops/collections/BasicArrayList.java b/giraph-core/src/main/java/org/apache/giraph/types/ops/collections/BasicArrayList.java index a96fb69..434cc23 100644 --- a/giraph-core/src/main/java/org/apache/giraph/types/ops/collections/BasicArrayList.java +++ b/giraph-core/src/main/java/org/apache/giraph/types/ops/collections/BasicArrayList.java @@ -27,6 +27,7 @@ import it.unimi.dsi.fastutil.longs.LongArrayList; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; +import java.util.Arrays; import java.util.NoSuchElementException; import org.apache.giraph.types.ops.BooleanTypeOps; @@ -102,6 +103,25 @@ public abstract class BasicArrayList<T> implements Writable { public abstract void set(int index, T value); /** + * Sets given range of elements to a specified value. + * + * @param from From index (inclusive) + * @param to To index (exclusive) + * @param value Value + */ + public abstract void fill(int from, int to, T value); + + /** + * Returns underlying primitive collection: + * it.unimi.dsi.fastutil.{T}s.{T}ArrayList. + * + * Allows for direct access where primitive type is fixed. + * + * @return underlying primitive collection + */ + public abstract Object unwrap(); + + /** * TypeOps for type of elements this object holds * @return TypeOps */ @@ -155,6 +175,11 @@ public abstract class BasicArrayList<T> implements Writable { /** List */ private final BooleanArrayList list; + /** Constructor */ + public BasicBooleanArrayList() { + list = new BooleanArrayList(); + } + /** * Constructor * @param capacity Capacity @@ -218,6 +243,21 @@ public abstract class BasicArrayList<T> implements Writable { } @Override + public void fill(int from, int to, BooleanWritable value) { + if (to > list.size()) { + throw new ArrayIndexOutOfBoundsException( + "End index (" + to + ") is greater than array length (" + + list.size() + ")"); + } + Arrays.fill(list.elements(), from, to, value.get()); + } + + @Override + public BooleanArrayList unwrap() { + return list; + } + + @Override public void write(DataOutput out) throws IOException { out.writeInt(list.size()); for (int i = 0; i < list.size(); i++) { @@ -229,7 +269,7 @@ public abstract class BasicArrayList<T> implements Writable { public void readFields(DataInput in) throws IOException { int size = in.readInt(); list.clear(); - setCapacity(size); + list.ensureCapacity(size); for (int i = 0; i < size; ++i) { list.add(in.readBoolean()); } @@ -242,6 +282,11 @@ public abstract class BasicArrayList<T> implements Writable { /** List */ private final ByteArrayList list; + /** Constructor */ + public BasicByteArrayList() { + list = new ByteArrayList(); + } + /** * Constructor * @param capacity Capacity @@ -305,6 +350,21 @@ public abstract class BasicArrayList<T> implements Writable { } @Override + public void fill(int from, int to, ByteWritable value) { + if (to > list.size()) { + throw new ArrayIndexOutOfBoundsException( + "End index (" + to + ") is greater than array length (" + + list.size() + ")"); + } + Arrays.fill(list.elements(), from, to, value.get()); + } + + @Override + public ByteArrayList unwrap() { + return list; + } + + @Override public void write(DataOutput out) throws IOException { out.writeInt(list.size()); for (int i = 0; i < list.size(); i++) { @@ -316,7 +376,7 @@ public abstract class BasicArrayList<T> implements Writable { public void readFields(DataInput in) throws IOException { int size = in.readInt(); list.clear(); - setCapacity(size); + list.ensureCapacity(size); for (int i = 0; i < size; ++i) { list.add(in.readByte()); } @@ -329,6 +389,11 @@ public abstract class BasicArrayList<T> implements Writable { /** List */ private final IntArrayList list; + /** Constructor */ + public BasicIntArrayList() { + list = new IntArrayList(); + } + /** * Constructor * @param capacity Capacity @@ -392,6 +457,21 @@ public abstract class BasicArrayList<T> implements Writable { } @Override + public void fill(int from, int to, IntWritable value) { + if (to > list.size()) { + throw new ArrayIndexOutOfBoundsException( + "End index (" + to + ") is greater than array length (" + + list.size() + ")"); + } + Arrays.fill(list.elements(), from, to, value.get()); + } + + @Override + public IntArrayList unwrap() { + return list; + } + + @Override public void write(DataOutput out) throws IOException { out.writeInt(list.size()); for (int i = 0; i < list.size(); i++) { @@ -403,7 +483,7 @@ public abstract class BasicArrayList<T> implements Writable { public void readFields(DataInput in) throws IOException { int size = in.readInt(); list.clear(); - setCapacity(size); + list.ensureCapacity(size); for (int i = 0; i < size; ++i) { list.add(in.readInt()); } @@ -416,6 +496,11 @@ public abstract class BasicArrayList<T> implements Writable { /** List */ private final LongArrayList list; + /** Constructor */ + public BasicLongArrayList() { + list = new LongArrayList(); + } + /** * Constructor * @param capacity Capacity @@ -479,6 +564,21 @@ public abstract class BasicArrayList<T> implements Writable { } @Override + public void fill(int from, int to, LongWritable value) { + if (to > list.size()) { + throw new ArrayIndexOutOfBoundsException( + "End index (" + to + ") is greater than array length (" + + list.size() + ")"); + } + Arrays.fill(list.elements(), from, to, value.get()); + } + + @Override + public LongArrayList unwrap() { + return list; + } + + @Override public void write(DataOutput out) throws IOException { out.writeInt(list.size()); for (int i = 0; i < list.size(); i++) { @@ -490,7 +590,7 @@ public abstract class BasicArrayList<T> implements Writable { public void readFields(DataInput in) throws IOException { int size = in.readInt(); list.clear(); - setCapacity(size); + list.ensureCapacity(size); for (int i = 0; i < size; ++i) { list.add(in.readLong()); } @@ -503,6 +603,11 @@ public abstract class BasicArrayList<T> implements Writable { /** List */ private final FloatArrayList list; + /** Constructor */ + public BasicFloatArrayList() { + list = new FloatArrayList(); + } + /** * Constructor * @param capacity Capacity @@ -566,6 +671,21 @@ public abstract class BasicArrayList<T> implements Writable { } @Override + public void fill(int from, int to, FloatWritable value) { + if (to > list.size()) { + throw new ArrayIndexOutOfBoundsException( + "End index (" + to + ") is greater than array length (" + + list.size() + ")"); + } + Arrays.fill(list.elements(), from, to, value.get()); + } + + @Override + public FloatArrayList unwrap() { + return list; + } + + @Override public void write(DataOutput out) throws IOException { out.writeInt(list.size()); for (int i = 0; i < list.size(); i++) { @@ -577,7 +697,7 @@ public abstract class BasicArrayList<T> implements Writable { public void readFields(DataInput in) throws IOException { int size = in.readInt(); list.clear(); - setCapacity(size); + list.ensureCapacity(size); for (int i = 0; i < size; ++i) { list.add(in.readFloat()); } @@ -590,6 +710,11 @@ public abstract class BasicArrayList<T> implements Writable { /** List */ private final DoubleArrayList list; + /** Constructor */ + public BasicDoubleArrayList() { + list = new DoubleArrayList(); + } + /** * Constructor * @param capacity Capacity @@ -653,6 +778,21 @@ public abstract class BasicArrayList<T> implements Writable { } @Override + public void fill(int from, int to, DoubleWritable value) { + if (to > list.size()) { + throw new ArrayIndexOutOfBoundsException( + "End index (" + to + ") is greater than array length (" + + list.size() + ")"); + } + Arrays.fill(list.elements(), from, to, value.get()); + } + + @Override + public DoubleArrayList unwrap() { + return list; + } + + @Override public void write(DataOutput out) throws IOException { out.writeInt(list.size()); for (int i = 0; i < list.size(); i++) { @@ -664,7 +804,7 @@ public abstract class BasicArrayList<T> implements Writable { public void readFields(DataInput in) throws IOException { int size = in.readInt(); list.clear(); - setCapacity(size); + list.ensureCapacity(size); for (int i = 0; i < size; ++i) { list.add(in.readDouble()); }
