Coverage Report - org.apache.giraph.aggregators.matrix.sparse.FloatSparseVector
 
Classes in this File Line Coverage Branch Coverage Complexity
FloatSparseVector
0%
0/38
0%
0/6
1.333
 
 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  
 
 19  
 package org.apache.giraph.aggregators.matrix.sparse;
 20  
 
 21  
 import it.unimi.dsi.fastutil.ints.Int2FloatMap;
 22  
 
 23  
 import java.io.DataInput;
 24  
 import java.io.DataOutput;
 25  
 import java.io.IOException;
 26  
 
 27  
 import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;
 28  
 import it.unimi.dsi.fastutil.objects.ObjectIterator;
 29  
 
 30  
 import org.apache.hadoop.io.Writable;
 31  
 
 32  
 /**
 33  
  * The float vector holds the values of a particular row.
 34  
  */
 35  
 public class FloatSparseVector implements Writable {
 36  
   /**
 37  
    * The entries of the vector are (key, value) pairs of the form (row, value)
 38  
    */
 39  0
   private Int2FloatOpenHashMap entries = null;
 40  
 
 41  
   /**
 42  
    * Create a new vector with default size.
 43  
    */
 44  0
   public FloatSparseVector() {
 45  0
     initialize(Int2FloatOpenHashMap.DEFAULT_INITIAL_SIZE);
 46  0
   }
 47  
 
 48  
   /**
 49  
    * Create a new vector with given size.
 50  
    *
 51  
    * @param size the size of the vector
 52  
    */
 53  0
   public FloatSparseVector(int size) {
 54  0
     initialize(size);
 55  0
   }
 56  
 
 57  
   /**
 58  
    * Initialize the values of the vector. The default value is 0.0
 59  
    *
 60  
    * @param size the size of the vector
 61  
    */
 62  
   private void initialize(int size) {
 63  0
     entries = new Int2FloatOpenHashMap(size);
 64  0
     entries.defaultReturnValue(0.0f);
 65  0
   }
 66  
 
 67  
   /**
 68  
    * Get a particular entry of the vector.
 69  
    *
 70  
    * @param i the entry
 71  
    * @return the value of the entry.
 72  
    */
 73  
   public float get(int i) {
 74  0
     return entries.get(i);
 75  
   }
 76  
 
 77  
   /**
 78  
    * Set the given value to the entry specified.
 79  
    *
 80  
    * @param i the entry
 81  
    * @param value the value to set to the entry
 82  
    */
 83  
   public void set(int i, float value) {
 84  0
     entries.put(i, value);
 85  0
   }
 86  
 
 87  
   /**
 88  
    * Clear the contents of the vector.
 89  
    */
 90  
   public void clear() {
 91  0
     entries.clear();
 92  0
   }
 93  
 
 94  
   /**
 95  
    * Add the vector specified. This is a vector addition that does an
 96  
    * element-by-element addition.
 97  
    *
 98  
    * @param other the vector to add.
 99  
    */
 100  
   public void add(FloatSparseVector other) {
 101  0
     ObjectIterator<Int2FloatMap.Entry> iter =
 102  0
         other.entries.int2FloatEntrySet().fastIterator();
 103  0
     while (iter.hasNext()) {
 104  0
       Int2FloatMap.Entry entry = iter.next();
 105  0
       entries.addTo(entry.getIntKey(), entry.getFloatValue());
 106  0
     }
 107  0
   }
 108  
 
 109  
   @Override
 110  
   public void write(DataOutput out) throws IOException {
 111  0
     out.writeInt(entries.size());
 112  0
     ObjectIterator<Int2FloatMap.Entry> iter =
 113  0
         entries.int2FloatEntrySet().fastIterator();
 114  0
     while (iter.hasNext()) {
 115  0
       Int2FloatMap.Entry entry = iter.next();
 116  0
       out.writeInt(entry.getIntKey());
 117  0
       out.writeFloat(entry.getFloatValue());
 118  0
     }
 119  0
   }
 120  
 
 121  
   @Override
 122  
   public void readFields(DataInput in) throws IOException {
 123  0
     int size = in.readInt();
 124  0
     initialize(size);
 125  0
     for (int i = 0; i < size; ++i) {
 126  0
       int row = in.readInt();
 127  0
       float value = in.readFloat();
 128  0
       entries.put(row, value);
 129  
     }
 130  0
   }
 131  
 }