Coverage Report - org.apache.giraph.types.ops.collections.array.WDoubleArrayList
 
Classes in this File Line Coverage Branch Coverage Complexity
WDoubleArrayList
0%
0/67
0%
0/18
1.433
WDoubleArrayList$WReusableDoubleArrayList
0%
0/13
0%
0/4
1.433
 
 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, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.giraph.types.ops.collections.array;
 19  
 
 20  
 import java.io.DataInput;
 21  
 import java.io.DataOutput;
 22  
 import java.io.IOException;
 23  
 import java.util.Arrays;
 24  
 
 25  
 import org.apache.giraph.function.Consumer;
 26  
 import org.apache.giraph.function.Predicate;
 27  
 import org.apache.giraph.function.primitive.DoubleConsumer;
 28  
 import org.apache.giraph.function.primitive.DoublePredicate;
 29  
 import org.apache.giraph.types.ops.DoubleTypeOps;
 30  
 import org.apache.giraph.types.ops.PrimitiveTypeOps;
 31  
 import org.apache.giraph.types.ops.collections.ResettableIterator;
 32  
 import org.apache.giraph.types.ops.collections.WDoubleCollection;
 33  
 import org.apache.hadoop.io.DoubleWritable;
 34  
 import org.apache.giraph.utils.Varint;
 35  
 
 36  
 import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
 37  
 import it.unimi.dsi.fastutil.doubles.DoubleArrays;
 38  
 import it.unimi.dsi.fastutil.doubles.DoubleCollection;
 39  
 import it.unimi.dsi.fastutil.doubles.DoubleList;
 40  
 
 41  
 // AUTO-GENERATED class via class:
 42  
 // org.apache.giraph.generate.GeneratePrimitiveClasses
 43  
 
 44  
 /**
 45  
  * Writable extension of DoubleArrayList, as well as
 46  
  * DoubleWritable implementation of WArrayList.
 47  
  */
 48  0
 public class WDoubleArrayList
 49  
     extends DoubleArrayList
 50  
     implements WArrayList<DoubleWritable>, WDoubleCollection {
 51  
   /**
 52  
    * Creates a new array list with {@link #DEFAULT_INITIAL_CAPACITY} capacity.
 53  
    */
 54  
   public WDoubleArrayList() {
 55  0
     super();
 56  0
   }
 57  
 
 58  
   /**
 59  
    * Creates a new array list with given capacity.
 60  
    *
 61  
    * @param capacity the initial capacity of the array list (may be 0).
 62  
    */
 63  
   public WDoubleArrayList(int capacity) {
 64  0
     super(capacity);
 65  0
   }
 66  
 
 67  
   /**
 68  
    * Creates a new array list and fills it with a given type-specific
 69  
    * collection.
 70  
    *
 71  
    * @param c a type-specific collection that will be used to fill the array
 72  
    *          list.
 73  
    */
 74  
   public WDoubleArrayList(DoubleCollection c) {
 75  0
     super(c);
 76  0
   }
 77  
 
 78  
   /**
 79  
    * Creates a new array list and fills it with a given type-specific list.
 80  
    *
 81  
    * @param l a type-specific list that will be used to fill the array list.
 82  
    */
 83  
   public WDoubleArrayList(DoubleList l) {
 84  0
     super(l);
 85  0
   }
 86  
 
 87  
   @Override
 88  
   public PrimitiveTypeOps<DoubleWritable> getElementTypeOps() {
 89  0
     return DoubleTypeOps.INSTANCE;
 90  
   }
 91  
 
 92  
   @Override
 93  
   public int capacity() {
 94  0
     return elements().length;
 95  
   }
 96  
 
 97  
   @Override
 98  
   public void setCapacity(int n) {
 99  0
     if (n >= capacity()) {
 100  0
       ensureCapacity(n);
 101  
     } else {
 102  0
       trim(n);
 103  
     }
 104  0
   }
 105  
 
 106  
   @Override
 107  
   public void addW(DoubleWritable value) {
 108  0
     add(value.get());
 109  0
   }
 110  
 
 111  
   @Override
 112  
   public void getIntoW(int index, DoubleWritable to) {
 113  0
     to.set(getDouble(index));
 114  0
   }
 115  
 
 116  
   @Override
 117  
   public void popIntoW(DoubleWritable to) {
 118  0
     to.set(popDouble());
 119  0
   }
 120  
 
 121  
   @Override
 122  
   public void setW(int index, DoubleWritable value) {
 123  0
     set(index, value.get());
 124  0
   }
 125  
 
 126  
   @Override
 127  
   public void fillW(int from, int to, DoubleWritable value) {
 128  0
     if (to > size()) {
 129  0
       throw new ArrayIndexOutOfBoundsException(
 130  
           "End index (" + to + ") is greater than array length (" +
 131  0
               size() + ")");
 132  
     }
 133  0
     Arrays.fill(elements(), from, to, value.get());
 134  0
   }
 135  
 
 136  
   @Override
 137  
   public ResettableIterator<DoubleWritable> fastIteratorW() {
 138  0
     return fastIteratorW(getElementTypeOps().create());
 139  
   }
 140  
 
 141  
   @Override
 142  
   public ResettableIterator<DoubleWritable> fastIteratorW(
 143  
       DoubleWritable iterationValue) {
 144  0
     return WArrayListPrivateUtils.fastIterator(this, iterationValue);
 145  
   }
 146  
 
 147  
   @Override
 148  
   public void fastForEachW(Consumer<DoubleWritable> f) {
 149  0
     WArrayListPrivateUtils.fastForEach(this, f, getElementTypeOps().create());
 150  0
   }
 151  
 
 152  
   @Override
 153  
   public boolean fastForEachWhileW(Predicate<DoubleWritable> f) {
 154  0
     return WArrayListPrivateUtils.fastForEachWhile(
 155  0
         this, f, getElementTypeOps().create());
 156  
   }
 157  
 
 158  
   /**
 159  
    * Traverse all elements of the array list, calling given function on each
 160  
    * element, or until predicate returns false.
 161  
    *
 162  
    * @param f Function to call on each element.
 163  
    */
 164  
   public void forEachDouble(DoubleConsumer f) {
 165  0
     for (int i = 0; i < size(); ++i) {
 166  0
       f.apply(getDouble(i));
 167  
     }
 168  0
   }
 169  
 
 170  
   /**
 171  
    * Traverse all elements of the array list, calling given function on each
 172  
    * element.
 173  
    *
 174  
    * @param f Function to call on each element.
 175  
    * @return true if the predicate returned true for all elements,
 176  
    *    false if it returned false for some element.
 177  
    */
 178  
   public boolean forEachWhileDouble(DoublePredicate f) {
 179  0
     for (int i = 0; i < size(); ++i) {
 180  0
       if (!f.apply(getDouble(i))) {
 181  0
         return false;
 182  
       }
 183  
     }
 184  0
     return true;
 185  
   }
 186  
 
 187  
   @Override
 188  
   public void sort() {
 189  0
     DoubleArrays.quickSort(elements(), 0, size());
 190  0
   }
 191  
 
 192  
   @Override
 193  
   public void writeElements(DataOutput out) throws IOException {
 194  0
     for (int i = 0; i < size; i++) {
 195  0
       out.writeDouble(a[i]);
 196  
     }
 197  0
   }
 198  
 
 199  
   @Override
 200  
   public void write(DataOutput out) throws IOException {
 201  0
     Varint.writeUnsignedVarInt(size, out);
 202  0
     writeElements(out);
 203  0
   }
 204  
 
 205  
   @Override
 206  
   public void readElements(DataInput in, int size) throws IOException {
 207  0
     this.size = size;
 208  0
     resizeArrayForRead(size);
 209  0
     for (int i = 0; i < size; i++) {
 210  0
       a[i] = in.readDouble();
 211  
     }
 212  0
   }
 213  
 
 214  
   /**
 215  
    * Resize array for reading given number of elements.
 216  
    * @param size Number of elements that will be read.
 217  
    */
 218  
   protected void resizeArrayForRead(int size) {
 219  0
     if (size != a.length) {
 220  0
       a = new double[size];
 221  
     }
 222  0
   }
 223  
 
 224  
   @Override
 225  
   public void readFields(DataInput in) throws IOException {
 226  0
     readElements(in, Varint.readUnsignedVarInt(in));
 227  0
   }
 228  
 
 229  
   /**
 230  
    * Write a potentially null list to a DataOutput. Null list is written
 231  
    * equivalently as an empty list. Use WritableUtils.writeIfNotNullAndObject
 232  
    * if distinction is needed.
 233  
    *
 234  
    * @param list Array list to be written
 235  
    * @param out Data output
 236  
    */
 237  
   public static void writeOrNull(WDoubleArrayList list, DataOutput out)
 238  
       throws IOException {
 239  0
     if (list == null) {
 240  0
       Varint.writeUnsignedVarInt(0, out);
 241  
     } else {
 242  0
       list.write(out);
 243  
     }
 244  0
   }
 245  
 
 246  
   /**
 247  
    * Read array list from the DataInput stream into a newly created object.
 248  
    *
 249  
    * @param in Data input
 250  
    * @return New read array list object.
 251  
    */
 252  
   public static WDoubleArrayList readNew(DataInput in) throws IOException {
 253  0
     int size = Varint.readSignedVarInt(in);
 254  0
     WDoubleArrayList list = new WDoubleArrayList(size);
 255  0
     list.readElements(in, size);
 256  0
     return list;
 257  
   }
 258  
 
 259  
   /**
 260  
    * Variant of WDoubleArrayList that doesn't reallocate smaller backing
 261  
    * array on consecutive readFields/readElements calls, and so is suitable for
 262  
    * reusable use.
 263  
    * (and backing array will only grow on readFields/readElements calls)
 264  
    */
 265  0
   public static class WReusableDoubleArrayList
 266  
       extends WDoubleArrayList {
 267  
     /** Constructor */
 268  
     public WReusableDoubleArrayList() {
 269  0
       super();
 270  0
     }
 271  
 
 272  
     /**
 273  
      * Constructor
 274  
      * @param capacity Capacity
 275  
      */
 276  
     public WReusableDoubleArrayList(int capacity) {
 277  0
       super(capacity);
 278  0
     }
 279  
 
 280  
     @Override
 281  
     protected void resizeArrayForRead(int size) {
 282  0
       if (size > a.length) {
 283  0
         a = new double[size];
 284  
       }
 285  0
     }
 286  
 
 287  
     /**
 288  
      * Read array list from DataInput stream, into a given list if not null,
 289  
      * or creating a new list if given list is null.
 290  
      *
 291  
      * @param list Array list to be written
 292  
      * @param in Data input
 293  
      * @return Passed array list, or a new one if null is passed
 294  
      */
 295  
     public static WReusableDoubleArrayList readIntoOrCreate(
 296  
         WReusableDoubleArrayList list, DataInput in) throws IOException {
 297  0
       int size = Varint.readUnsignedVarInt(in);
 298  0
       if (list == null) {
 299  0
         list = new WReusableDoubleArrayList(size);
 300  
       }
 301  0
       list.readElements(in, size);
 302  0
       return list;
 303  
     }
 304  
   }
 305  
 }