This project has retired. For details please refer to its Attic page.
FloatSparseVector xref
View Javadoc

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    private Int2FloatOpenHashMap entries = null;
40  
41    /**
42     * Create a new vector with default size.
43     */
44    public FloatSparseVector() {
45      initialize(Int2FloatOpenHashMap.DEFAULT_INITIAL_SIZE);
46    }
47  
48    /**
49     * Create a new vector with given size.
50     *
51     * @param size the size of the vector
52     */
53    public FloatSparseVector(int size) {
54      initialize(size);
55    }
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      entries = new Int2FloatOpenHashMap(size);
64      entries.defaultReturnValue(0.0f);
65    }
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      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      entries.put(i, value);
85    }
86  
87    /**
88     * Clear the contents of the vector.
89     */
90    public void clear() {
91      entries.clear();
92    }
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     ObjectIterator<Int2FloatMap.Entry> iter =
102         other.entries.int2FloatEntrySet().fastIterator();
103     while (iter.hasNext()) {
104       Int2FloatMap.Entry entry = iter.next();
105       entries.addTo(entry.getIntKey(), entry.getFloatValue());
106     }
107   }
108 
109   @Override
110   public void write(DataOutput out) throws IOException {
111     out.writeInt(entries.size());
112     ObjectIterator<Int2FloatMap.Entry> iter =
113         entries.int2FloatEntrySet().fastIterator();
114     while (iter.hasNext()) {
115       Int2FloatMap.Entry entry = iter.next();
116       out.writeInt(entry.getIntKey());
117       out.writeFloat(entry.getFloatValue());
118     }
119   }
120 
121   @Override
122   public void readFields(DataInput in) throws IOException {
123     int size = in.readInt();
124     initialize(size);
125     for (int i = 0; i < size; ++i) {
126       int row = in.readInt();
127       float value = in.readFloat();
128       entries.put(row, value);
129     }
130   }
131 }