Repository: giraph Updated Branches: refs/heads/trunk bac93fade -> 77ae12e00
http://git-wip-us.apache.org/repos/asf/giraph/blob/77ae12e0/giraph-core/src/test/java/org/apache/giraph/generate/GeneratePrimitiveClasses.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/generate/GeneratePrimitiveClasses.java b/giraph-core/src/test/java/org/apache/giraph/generate/GeneratePrimitiveClasses.java index 95896f6..be2417a 100644 --- a/giraph-core/src/test/java/org/apache/giraph/generate/GeneratePrimitiveClasses.java +++ b/giraph-core/src/test/java/org/apache/giraph/generate/GeneratePrimitiveClasses.java @@ -53,31 +53,32 @@ import freemarker.template.TemplateNotFoundException; * <a href="http://freemarker.org/docs/dgui_quickstart_template.html">tutorial</a> */ public class GeneratePrimitiveClasses { - // No Short since there is no ShortWritable for some reason public static enum PrimitiveType { - BOOLEAN("Boolean", false, false, false), - BYTE("Byte", true, false, false), - INT("Int", "Integer", true, true, false), - LONG("Long", true, true, false), - FLOAT("Float", true, false, true), - DOUBLE("Double", true, false, true); + BOOLEAN("Boolean"), + BYTE("Byte"), + SHORT("Short"), + INT("Int"), + LONG("Long"), + FLOAT("Float"), + DOUBLE("Double"); private final String name; + private final String nameLower; private final String boxed; private final boolean numeric; private final boolean id; private final boolean floating; + private final boolean hasWritable; - private PrimitiveType(String name, String boxed, boolean numeric, boolean id, boolean floating) { + private PrimitiveType(String name) { this.name = name; - this.boxed = boxed; - this.numeric = numeric; - this.id = id; - this.floating = floating; - } - - private PrimitiveType(String name, boolean numeric, boolean id, boolean floating) { - this(name, name, numeric, id, floating); + this.nameLower = name.toLowerCase(); + this.boxed = "Int".equals(name) ? "Integer" : name; + this.numeric = !"Boolean".equals(name); + this.id = "Int".equals(name) || "Long".equals(name); + this.floating = "Float".equals(name) || "Double".equals(name); + // For some reason there is no ShortWritable in current Hadoop version + this.hasWritable = !"Short".equals(name); } public String getName() { @@ -89,7 +90,7 @@ public class GeneratePrimitiveClasses { } public String getLower() { - return name.toLowerCase(); + return nameLower; } public String getBoxed() { @@ -107,6 +108,10 @@ public class GeneratePrimitiveClasses { public boolean isFloating() { return floating; } + + public boolean hasWritable() { + return hasWritable; + } } public static void main(String[] args) throws Exception { @@ -116,36 +121,46 @@ public class GeneratePrimitiveClasses { cfg.setDefaultEncoding("UTF-8"); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); + String[] primitiveFunctions = { "%sConsumer", "%sPredicate", "Obj2%sFunction" }; + + for (String function: primitiveFunctions) { + generateForAll( + cfg, + EnumSet.allOf(PrimitiveType.class), + String.format(function, "Type") + ".java", + "src/main/java/org/apache/giraph/function/primitive/" + function + ".java"); + } + + EnumSet<PrimitiveType> writableSet = EnumSet.noneOf(PrimitiveType.class); + EnumSet<PrimitiveType> ids = EnumSet.noneOf(PrimitiveType.class); + for (PrimitiveType type : EnumSet.allOf(PrimitiveType.class)) { + if (type.hasWritable()) { + writableSet.add(type); + if (type.isId()) { + ids.add(type); + } + } + } + generateForAll( cfg, - EnumSet.allOf(PrimitiveType.class), - "TypeConsumer.java", - "src/main/java/org/apache/giraph/function/primitive/%sConsumer.java"); + writableSet, + "TypeTypeOps.java", + "src/main/java/org/apache/giraph/types/ops/%sTypeOps.java"); generateForAll( cfg, - EnumSet.allOf(PrimitiveType.class), - "Obj2TypeFunction.java", - "src/main/java/org/apache/giraph/function/primitive/Obj2%sFunction.java"); - -// generateForAll( -// cfg, -// EnumSet.allOf(PrimitiveType.class), -// "TypeTypeOps.java", -// "src/main/java/org/apache/giraph/types/ops/%sTypeOps.java"); -// -// generateForAll( -// cfg, -// EnumSet.allOf(PrimitiveType.class), -// "WTypeArrayList.java", -// "src/main/java/org/apache/giraph/types/ops/collections/array/W%sArrayList.java"); + writableSet, + "WTypeCollection.java", + "src/main/java/org/apache/giraph/types/ops/collections/W%sCollection.java"); - EnumSet<PrimitiveType> ids = EnumSet.noneOf(PrimitiveType.class); - for (PrimitiveType type : PrimitiveType.values()) { - if (type.isId()) { - ids.add(type); - } - } + generateForAll( + cfg, + writableSet, + "WTypeArrayList.java", + "src/main/java/org/apache/giraph/types/ops/collections/array/W%sArrayList.java"); + + System.out.println("Successfully generated classes"); } /** http://git-wip-us.apache.org/repos/asf/giraph/blob/77ae12e0/giraph-core/src/test/java/org/apache/giraph/types/TestCollections.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/types/TestCollections.java b/giraph-core/src/test/java/org/apache/giraph/types/TestCollections.java index 2443147..97d6a41 100644 --- a/giraph-core/src/test/java/org/apache/giraph/types/TestCollections.java +++ b/giraph-core/src/test/java/org/apache/giraph/types/TestCollections.java @@ -18,8 +18,8 @@ package org.apache.giraph.types; import org.apache.giraph.types.ops.LongTypeOps; -import org.apache.giraph.types.ops.collections.BasicArrayList; import org.apache.giraph.types.ops.collections.BasicSet; +import org.apache.giraph.types.ops.collections.array.WArrayList; import org.apache.hadoop.io.LongWritable; import org.junit.Assert; import org.junit.Ignore; @@ -55,9 +55,9 @@ public class TestCollections { @Test public void testLargeBasicList() { int capacity = 123456789; - BasicArrayList<LongWritable> longSet = LongTypeOps.INSTANCE.createArrayList(capacity); - longSet.add(new LongWritable(capacity)); - longSet.add(new LongWritable(capacity)); + WArrayList<LongWritable> longSet = LongTypeOps.INSTANCE.createArrayList(capacity); + longSet.addW(new LongWritable(capacity)); + longSet.addW(new LongWritable(capacity)); Assert.assertEquals(2, longSet.size()); } } http://git-wip-us.apache.org/repos/asf/giraph/blob/77ae12e0/giraph-core/src/test/java/org/apache/giraph/writable/kryo/DirectWritableSerializerCopyTest.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/writable/kryo/DirectWritableSerializerCopyTest.java b/giraph-core/src/test/java/org/apache/giraph/writable/kryo/DirectWritableSerializerCopyTest.java index 233690c..b527915 100644 --- a/giraph-core/src/test/java/org/apache/giraph/writable/kryo/DirectWritableSerializerCopyTest.java +++ b/giraph-core/src/test/java/org/apache/giraph/writable/kryo/DirectWritableSerializerCopyTest.java @@ -17,13 +17,14 @@ */ package org.apache.giraph.writable.kryo; -import com.esotericsoftware.kryo.Kryo; -import org.apache.giraph.types.ops.collections.BasicArrayList.BasicDoubleArrayList; -import org.junit.Assert; +import org.apache.giraph.types.ops.collections.array.WDoubleArrayList; import org.apache.giraph.writable.kryo.serializers.DirectWritableSerializer; import org.apache.hadoop.io.DoubleWritable; +import org.junit.Assert; import org.junit.Test; +import com.esotericsoftware.kryo.Kryo; + public class DirectWritableSerializerCopyTest { @Test public void test1() { @@ -36,20 +37,20 @@ public class DirectWritableSerializerCopyTest { @Test public void test2() { - BasicDoubleArrayList list = new BasicDoubleArrayList(); - list.add(new DoubleWritable(0.11111111)); - list.add(new DoubleWritable(1000.9)); - list.add(new DoubleWritable(99999999.99999999)); - DirectWritableSerializer<BasicDoubleArrayList> serializer = + WDoubleArrayList list = new WDoubleArrayList(); + list.addW(new DoubleWritable(0.11111111)); + list.addW(new DoubleWritable(1000.9)); + list.addW(new DoubleWritable(99999999.99999999)); + DirectWritableSerializer<WDoubleArrayList> serializer = new DirectWritableSerializer<>(); Kryo kryo = new Kryo(); - BasicDoubleArrayList copy = serializer.copy(kryo, list); + WDoubleArrayList copy = serializer.copy(kryo, list); DoubleWritable reusable = new DoubleWritable(); - copy.getInto(0, reusable); + copy.getIntoW(0, reusable); Assert.assertEquals(0.11111111, reusable.get(), 0); - copy.getInto(1, reusable); + copy.getIntoW(1, reusable); Assert.assertEquals(1000.9, reusable.get(), 0); - copy.getInto(2, reusable); + copy.getIntoW(2, reusable); Assert.assertEquals(99999999.99999999, reusable.get(), 0); } } http://git-wip-us.apache.org/repos/asf/giraph/blob/77ae12e0/giraph-core/src/test/java/org/apache/giraph/writable/kryo/KryoWritableTest.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/writable/kryo/KryoWritableTest.java b/giraph-core/src/test/java/org/apache/giraph/writable/kryo/KryoWritableTest.java index 19c7ee4..c7d0db1 100644 --- a/giraph-core/src/test/java/org/apache/giraph/writable/kryo/KryoWritableTest.java +++ b/giraph-core/src/test/java/org/apache/giraph/writable/kryo/KryoWritableTest.java @@ -19,13 +19,10 @@ package org.apache.giraph.writable.kryo; import static org.junit.Assert.assertEquals; -import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; -import it.unimi.dsi.fastutil.longs.LongArrayList; - import java.util.Arrays; import java.util.List; -import org.apache.giraph.types.ops.collections.BasicArrayList.BasicLongArrayList; +import org.apache.giraph.types.ops.collections.array.WLongArrayList; import org.apache.giraph.utils.WritableUtils; import org.apache.hadoop.io.LongWritable; import org.junit.Assert; @@ -33,6 +30,9 @@ import org.junit.Test; import com.google.common.collect.ImmutableMap; +import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; +import it.unimi.dsi.fastutil.longs.LongArrayList; + /** * Tests some subtle cases of kryo serialization. @@ -90,7 +90,7 @@ public class KryoWritableTest { } - int multiplier = 5000; // use 5000 for profiling + int multiplier = 10; // use 5000 for profiling int longTestTimes = 1000 * multiplier; @Test @@ -146,18 +146,18 @@ public class KryoWritableTest { @Test public void testLongListWritable() throws Exception { - BasicLongArrayList from = new BasicLongArrayList(longListTestSize); + WLongArrayList from = new WLongArrayList(longListTestSize); LongWritable value = new LongWritable(); for (int i = 0; i < longListTestSize; i++) { value.set(i); - from.add(value); + from.addW(value); } - BasicLongArrayList to = new BasicLongArrayList(longListTestSize); + WLongArrayList to = new WLongArrayList(longListTestSize); value.set(0); for (int i = 0; i < longListTestTimes; i++) { - from.set((2 * i) % longListTestSize, value); + from.setW((2 * i) % longListTestSize, value); WritableUtils.copyInto(from, to, true); } } http://git-wip-us.apache.org/repos/asf/giraph/blob/77ae12e0/giraph-core/src/test/java/org/apache/giraph/writable/kryo/KryoWritableWrapperTest.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/writable/kryo/KryoWritableWrapperTest.java b/giraph-core/src/test/java/org/apache/giraph/writable/kryo/KryoWritableWrapperTest.java index 2291f16..6e0eb2f 100644 --- a/giraph-core/src/test/java/org/apache/giraph/writable/kryo/KryoWritableWrapperTest.java +++ b/giraph-core/src/test/java/org/apache/giraph/writable/kryo/KryoWritableWrapperTest.java @@ -19,13 +19,6 @@ package org.apache.giraph.writable.kryo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import it.unimi.dsi.fastutil.chars.Char2ObjectMap; -import it.unimi.dsi.fastutil.chars.Char2ObjectOpenHashMap; -import it.unimi.dsi.fastutil.floats.FloatArrayList; -import it.unimi.dsi.fastutil.ints.Int2BooleanMap; -import it.unimi.dsi.fastutil.ints.Int2BooleanOpenHashMap; -import it.unimi.dsi.fastutil.longs.LongArrayList; -import it.unimi.dsi.fastutil.longs.LongOpenHashSet; import java.io.IOException; import java.util.Arrays; @@ -37,6 +30,7 @@ import java.util.Random; import org.apache.giraph.conf.GiraphConfiguration; import org.apache.giraph.conf.GiraphConfigurationSettable; import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration; +import org.apache.giraph.types.ops.collections.array.WLongArrayList; import org.apache.giraph.utils.WritableUtils; import org.apache.giraph.writable.kryo.markers.NonKryoWritable; import org.apache.hadoop.conf.Configuration; @@ -49,6 +43,14 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Iterators; +import it.unimi.dsi.fastutil.chars.Char2ObjectMap; +import it.unimi.dsi.fastutil.chars.Char2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.floats.FloatArrayList; +import it.unimi.dsi.fastutil.ints.Int2BooleanMap; +import it.unimi.dsi.fastutil.ints.Int2BooleanOpenHashMap; +import it.unimi.dsi.fastutil.longs.LongArrayList; +import it.unimi.dsi.fastutil.longs.LongOpenHashSet; + /** @@ -189,6 +191,17 @@ public class KryoWritableWrapperTest { } @Test + public void testWFastutilLongList() throws ClassNotFoundException, IOException { + WLongArrayList list = new WLongArrayList(); + list.add(6); + WLongArrayList deser = kryoSerDeser(list); + deser.add(5); + list.add(5); + Assert.assertEquals(list, deser); + } + + + @Test public void testFastutilFloatList() throws ClassNotFoundException, IOException { FloatArrayList list = new FloatArrayList(); list.add(6L); http://git-wip-us.apache.org/repos/asf/giraph/blob/77ae12e0/giraph-core/templates/TypePredicate.java ---------------------------------------------------------------------- diff --git a/giraph-core/templates/TypePredicate.java b/giraph-core/templates/TypePredicate.java new file mode 100644 index 0000000..eb4203e --- /dev/null +++ b/giraph-core/templates/TypePredicate.java @@ -0,0 +1,36 @@ +/* + * 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.function.primitive; + +import java.io.Serializable; + +${generated_message} + +/** + * Primitive specialization of Function: + * (${type.lower}) -> boolean + */ +public interface ${type.camel}Predicate extends Serializable { + /** + * Returns the result of applying this predicate to {@code input}. + * + * @param input input + * @return result + */ + boolean apply(${type.lower} input); +} http://git-wip-us.apache.org/repos/asf/giraph/blob/77ae12e0/giraph-core/templates/TypeTypeOps.java ---------------------------------------------------------------------- diff --git a/giraph-core/templates/TypeTypeOps.java b/giraph-core/templates/TypeTypeOps.java new file mode 100644 index 0000000..8d92e8a --- /dev/null +++ b/giraph-core/templates/TypeTypeOps.java @@ -0,0 +1,147 @@ +/* + * 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.types.ops; + +<#if type.id> +import org.apache.giraph.types.ops.collections.Basic2ObjectMap.Basic${type.camel}2ObjectOpenHashMap; +import org.apache.giraph.types.ops.collections.BasicSet.Basic${type.camel}OpenHashSet; +</#if> +import org.apache.giraph.types.ops.collections.array.W${type.camel}ArrayList; +<#if type.id> +import org.apache.giraph.types.ops.collections.WritableWriter; +</#if> +import org.apache.hadoop.io.${type.camel}Writable; + +import java.io.DataInput; +import java.io.IOException; + +${generated_message} +<#if type.numeric && type.id> + <#assign parent_type = "PrimitiveIdTypeOps<${type.camel}Writable>, NumericTypeOps<${type.camel}Writable>"> +<#elseif type.numeric> + <#assign parent_type = "PrimitiveTypeOps<${type.camel}Writable>, NumericTypeOps<${type.camel}Writable>"> +<#elseif type.id> + <#assign parent_type = "PrimitiveIdTypeOps<${type.camel}Writable>"> +<#else> + <#assign parent_type = "PrimitiveTypeOps<${type.camel}Writable>"> +</#if> +<#macro cast_if_needed_v expr><#if type.lower == "byte">(${type.lower}) ${expr}<#else>${expr}</#if></#macro> +<#macro cast_if_needed_e expr><#if type.lower == "byte">(${type.lower}) (${expr})<#else>${expr}</#if></#macro> + +/** TypeOps implementation for working with ${type.camel}Writable type */ +public enum ${type.camel}TypeOps implements + ${parent_type} { + /** Singleton instance */ + INSTANCE; + + @Override + public Class<${type.camel}Writable> getTypeClass() { + return ${type.camel}Writable.class; + } + + @Override + public ${type.camel}Writable create() { + return new ${type.camel}Writable(); + } + + @Override + public ${type.camel}Writable createCopy(${type.camel}Writable from) { + return new ${type.camel}Writable(from.get()); + } + + @Override + public void set(${type.camel}Writable to, ${type.camel}Writable from) { + to.set(from.get()); + } + + @Override + public W${type.camel}ArrayList createArrayList() { + return new W${type.camel}ArrayList(); + } + + @Override + public W${type.camel}ArrayList createArrayList(int capacity) { + return new W${type.camel}ArrayList(capacity); + } + + @Override + public W${type.camel}ArrayList readNewArrayList(DataInput in) throws IOException { + return W${type.camel}ArrayList.readNew(in); + } +<#if type.id> + + @Override + public Basic${type.camel}OpenHashSet createOpenHashSet() { + return new Basic${type.camel}OpenHashSet(); + } + + @Override + public Basic${type.camel}OpenHashSet createOpenHashSet(long capacity) { + return new Basic${type.camel}OpenHashSet(capacity); + } + + @Override + public <V> Basic${type.camel}2ObjectOpenHashMap<V> create2ObjectOpenHashMap( + WritableWriter<V> valueWriter) { + return new Basic${type.camel}2ObjectOpenHashMap<>(valueWriter); + } + + @Override + public <V> Basic${type.camel}2ObjectOpenHashMap<V> create2ObjectOpenHashMap( + int capacity, WritableWriter<V> valueWriter) { + return new Basic${type.camel}2ObjectOpenHashMap<>(capacity, valueWriter); + } +</#if> +<#if type.numeric> + + @Override + public ${type.camel}Writable createZero() { + return new ${type.camel}Writable(<@cast_if_needed_v expr="0"/>); + } + + @Override + public ${type.camel}Writable createOne() { + return new ${type.camel}Writable(<@cast_if_needed_v expr="1"/>); + } + + @Override + public ${type.camel}Writable createMinNegativeValue() { + return new ${type.camel}Writable(${type.boxed}.${type.floating?string("NEGATIVE_INFINITY", "MIN_VALUE")}); + } + + @Override + public ${type.camel}Writable createMaxPositiveValue() { + return new ${type.camel}Writable(${type.boxed}.${type.floating?string("POSITIVE_INFINITY", "MAX_VALUE")}); + } + + @Override + public void plusInto(${type.camel}Writable value, ${type.camel}Writable increment) { + value.set(<@cast_if_needed_e expr="value.get() + increment.get()"/>); + } + + @Override + public void multiplyInto(${type.camel}Writable value, ${type.camel}Writable multiplier) { + value.set(<@cast_if_needed_e expr="value.get() * multiplier.get()"/>); + } + + @Override + public void negate(${type.camel}Writable value) { + value.set(<@cast_if_needed_e expr="-value.get()"/>); + } +</#if> +} http://git-wip-us.apache.org/repos/asf/giraph/blob/77ae12e0/giraph-core/templates/WTypeArrayList.java ---------------------------------------------------------------------- diff --git a/giraph-core/templates/WTypeArrayList.java b/giraph-core/templates/WTypeArrayList.java new file mode 100644 index 0000000..8803ebd --- /dev/null +++ b/giraph-core/templates/WTypeArrayList.java @@ -0,0 +1,304 @@ +/* + * 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.types.ops.collections.array; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.Arrays; + +import org.apache.giraph.function.Consumer; +import org.apache.giraph.function.Predicate; +import org.apache.giraph.function.primitive.${type.camel}Consumer; +import org.apache.giraph.function.primitive.${type.camel}Predicate; +import org.apache.giraph.types.ops.${type.camel}TypeOps; +import org.apache.giraph.types.ops.PrimitiveTypeOps; +import org.apache.giraph.types.ops.collections.ResettableIterator; +import org.apache.giraph.types.ops.collections.W${type.camel}Collection; +import org.apache.hadoop.io.${type.camel}Writable; +import org.apache.giraph.utils.Varint; + +import it.unimi.dsi.fastutil.${type.lower}s.${type.camel}ArrayList; +import it.unimi.dsi.fastutil.${type.lower}s.${type.camel}Arrays; +import it.unimi.dsi.fastutil.${type.lower}s.${type.camel}Collection; +import it.unimi.dsi.fastutil.${type.lower}s.${type.camel}List; + +${generated_message} + +/** + * Writable extension of ${type.camel}ArrayList, as well as + * ${type.camel}Writable implementation of WArrayList. + */ +public class W${type.camel}ArrayList + extends ${type.camel}ArrayList + implements WArrayList<${type.camel}Writable>, W${type.camel}Collection { + /** + * Creates a new array list with {@link #DEFAULT_INITIAL_CAPACITY} capacity. + */ + public W${type.camel}ArrayList() { + super(); + } + + /** + * Creates a new array list with given capacity. + * + * @param capacity the initial capacity of the array list (may be 0). + */ + public W${type.camel}ArrayList(int capacity) { + super(capacity); + } + + /** + * Creates a new array list and fills it with a given type-specific + * collection. + * + * @param c a type-specific collection that will be used to fill the array + * list. + */ + public W${type.camel}ArrayList(${type.camel}Collection c) { + super(c); + } + + /** + * Creates a new array list and fills it with a given type-specific list. + * + * @param l a type-specific list that will be used to fill the array list. + */ + public W${type.camel}ArrayList(${type.camel}List l) { + super(l); + } + + @Override + public PrimitiveTypeOps<${type.camel}Writable> getElementTypeOps() { + return ${type.camel}TypeOps.INSTANCE; + } + + @Override + public int capacity() { + return elements().length; + } + + @Override + public void setCapacity(int n) { + if (n >= capacity()) { + ensureCapacity(n); + } else { + trim(n); + } + } + + @Override + public void addW(${type.camel}Writable value) { + add(value.get()); + } + + @Override + public void getIntoW(int index, ${type.camel}Writable to) { + to.set(get${type.camel}(index)); + } + + @Override + public void popIntoW(${type.camel}Writable to) { + to.set(pop${type.camel}()); + } + + @Override + public void setW(int index, ${type.camel}Writable value) { + set(index, value.get()); + } + + @Override + public void fillW(int from, int to, ${type.camel}Writable value) { + if (to > size()) { + throw new ArrayIndexOutOfBoundsException( + "End index (" + to + ") is greater than array length (" + + size() + ")"); + } + Arrays.fill(elements(), from, to, value.get()); + } + + @Override + public ResettableIterator<${type.camel}Writable> fastIteratorW() { + return fastIteratorW(getElementTypeOps().create()); + } + + @Override + public ResettableIterator<${type.camel}Writable> fastIteratorW( + ${type.camel}Writable iterationValue) { + return WArrayListPrivateUtils.fastIterator(this, iterationValue); + } + + @Override + public void fastForEachW(Consumer<${type.camel}Writable> f) { + WArrayListPrivateUtils.fastForEach(this, f, getElementTypeOps().create()); + } + + @Override + public boolean fastForEachWhileW(Predicate<${type.camel}Writable> f) { + return WArrayListPrivateUtils.fastForEachWhile( + this, f, getElementTypeOps().create()); + } + + /** + * Traverse all elements of the array list, calling given function on each + * element, or until predicate returns false. + * + * @param f Function to call on each element. + */ + public void forEach${type.camel}(${type.camel}Consumer f) { + for (int i = 0; i < size(); ++i) { + f.apply(get${type.camel}(i)); + } + } + + /** + * Traverse all elements of the array list, calling given function on each + * element. + * + * @param f Function to call on each element. + * @return true if the predicate returned true for all elements, + * false if it returned false for some element. + */ + public boolean forEachWhile${type.camel}(${type.camel}Predicate f) { + for (int i = 0; i < size(); ++i) { + if (!f.apply(get${type.camel}(i))) { + return false; + } + } + return true; + } + + @Override + public void sort() { + ${type.camel}Arrays.quickSort(elements(), 0, size()); + } + + @Override + public void writeElements(DataOutput out) throws IOException { + for (int i = 0; i < size; i++) { + out.write${type.camel}(a[i]); + } + } + + @Override + public void write(DataOutput out) throws IOException { + Varint.writeUnsignedVarInt(size, out); + writeElements(out); + } + + @Override + public void readElements(DataInput in, int size) throws IOException { + this.size = size; + resizeArrayForRead(size); + for (int i = 0; i < size; i++) { + a[i] = in.read${type.camel}(); + } + } + + /** + * Resize array for reading given number of elements. + * @param size Number of elements that will be read. + */ + protected void resizeArrayForRead(int size) { + if (size != a.length) { + a = new ${type.lower}[size]; + } + } + + @Override + public void readFields(DataInput in) throws IOException { + readElements(in, Varint.readUnsignedVarInt(in)); + } + + /** + * Write a potentially null list to a DataOutput. Null list is written + * equivalently as an empty list. Use WritableUtils.writeIfNotNullAndObject + * if distinction is needed. + * + * @param list Array list to be written + * @param out Data output + */ + public static void writeOrNull(W${type.camel}ArrayList list, DataOutput out) + throws IOException { + if (list == null) { + Varint.writeUnsignedVarInt(0, out); + } else { + list.write(out); + } + } + + /** + * Read array list from the DataInput stream into a newly created object. + * + * @param in Data input + * @return New read array list object. + */ + public static W${type.camel}ArrayList readNew(DataInput in) throws IOException { + int size = Varint.readSignedVarInt(in); + W${type.camel}ArrayList list = new W${type.camel}ArrayList(size); + list.readElements(in, size); + return list; + } + + /** + * Variant of W${type.camel}ArrayList that doesn't reallocate smaller backing + * array on consecutive readFields/readElements calls, and so is suitable for + * reusable use. + * (and backing array will only grow on readFields/readElements calls) + */ + public static class WReusable${type.camel}ArrayList + extends W${type.camel}ArrayList { + /** Constructor */ + public WReusable${type.camel}ArrayList() { + super(); + } + + /** + * Constructor + * @param capacity Capacity + */ + public WReusable${type.camel}ArrayList(int capacity) { + super(capacity); + } + + @Override + protected void resizeArrayForRead(int size) { + if (size > a.length) { + a = new ${type.lower}[size]; + } + } + + /** + * Read array list from DataInput stream, into a given list if not null, + * or creating a new list if given list is null. + * + * @param list Array list to be written + * @param in Data input + * @return Passed array list, or a new one if null is passed + */ + public static WReusable${type.camel}ArrayList readIntoOrCreate( + WReusable${type.camel}ArrayList list, DataInput in) throws IOException { + int size = Varint.readUnsignedVarInt(in); + if (list == null) { + list = new WReusable${type.camel}ArrayList(size); + } + list.readElements(in, size); + return list; + } + } +} http://git-wip-us.apache.org/repos/asf/giraph/blob/77ae12e0/giraph-core/templates/WTypeCollection.java ---------------------------------------------------------------------- diff --git a/giraph-core/templates/WTypeCollection.java b/giraph-core/templates/WTypeCollection.java new file mode 100644 index 0000000..681d9f3 --- /dev/null +++ b/giraph-core/templates/WTypeCollection.java @@ -0,0 +1,50 @@ +/* + * 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.types.ops.collections; + +import org.apache.giraph.function.primitive.${type.camel}Consumer; +import org.apache.giraph.function.primitive.${type.camel}Predicate; +import org.apache.hadoop.io.${type.camel}Writable; + +import it.unimi.dsi.fastutil.${type.lower}s.${type.camel}Collection; + +${generated_message} + +/** + * Long specialization of WCollection + */ +public interface W${type.camel}Collection + extends WCollection<${type.camel}Writable>, ${type.camel}Collection { + /** + * Traverse all elements of the array list, calling given function on each + * element, or until predicate returns false. + * + * @param f Function to call on each element. + */ + void forEach${type.camel}(${type.camel}Consumer f); + + /** + * Traverse all elements of the array list, calling given function on each + * element. + * + * @param f Function to call on each element. + * @return true if the predicate returned true for all elements, + * false if it returned false for some element. + */ + boolean forEachWhile${type.camel}(${type.camel}Predicate f); +}
