This project has retired. For details please refer to its Attic page.
LongSparseVector 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.Int2LongMap;
22  import it.unimi.dsi.fastutil.ints.Int2LongOpenHashMap;
23  import it.unimi.dsi.fastutil.objects.ObjectIterator;
24  
25  import java.io.DataInput;
26  import java.io.DataOutput;
27  import java.io.IOException;
28  
29  import org.apache.hadoop.io.Writable;
30  
31  /**
32   * The long vector holds the values of a particular row.
33   */
34  public class LongSparseVector implements Writable {
35    /**
36     * The entries of the vector are (key, value) pairs of the form (row, value)
37     */
38    private Int2LongOpenHashMap entries = null;
39  
40    /**
41     * Create a new vector with default size.
42     */
43    public LongSparseVector() {
44      initialize(Int2LongOpenHashMap.DEFAULT_INITIAL_SIZE);
45    }
46  
47    /**
48     * Create a new vector with given size.
49     *
50     * @param size the size of the vector
51     */
52    public LongSparseVector(int size) {
53      initialize(size);
54    }
55  
56    /**
57     * Initialize the values of the vector. The default value is 0.0
58     *
59     * @param size the size of the vector
60     */
61    private void initialize(int size) {
62      entries = new Int2LongOpenHashMap(size);
63      entries.defaultReturnValue(0L);
64    }
65  
66    /**
67     * Get a particular entry of the vector.
68     *
69     * @param i the entry
70     * @return the value of the entry.
71     */
72    public long get(int i) {
73      return entries.get(i);
74    }
75  
76    /**
77     * Set the given value to the entry specified.
78     *
79     * @param i the entry
80     * @param value the value to set to the entry
81     */
82    public void set(int i, long value) {
83      entries.put(i, value);
84    }
85  
86    /**
87     * Increment value for a given key
88     * @param key Key
89     * @param value Increment
90     */
91    public void add(int key, long value) {
92      entries.addTo(key, value);
93    }
94  
95    /**
96     * Clear the contents of the vector.
97     */
98    public void clear() {
99      entries.clear();
100   }
101 
102   /**
103    * Add the vector specified. This is a vector addition that does an
104    * element-by-element addition.
105    *
106    * @param other the vector to add.
107    */
108   public void add(LongSparseVector other) {
109     ObjectIterator<Int2LongMap.Entry> iter =
110         other.entries.int2LongEntrySet().fastIterator();
111     while (iter.hasNext()) {
112       Int2LongMap.Entry entry = iter.next();
113       entries.addTo(entry.getIntKey(), entry.getLongValue());
114     }
115   }
116 
117   @Override
118   public void write(DataOutput out) throws IOException {
119     out.writeInt(entries.size());
120     ObjectIterator<Int2LongMap.Entry> iter =
121         entries.int2LongEntrySet().fastIterator();
122     while (iter.hasNext()) {
123       Int2LongMap.Entry entry = iter.next();
124       out.writeInt(entry.getIntKey());
125       out.writeLong(entry.getLongValue());
126     }
127   }
128 
129   @Override
130   public void readFields(DataInput in) throws IOException {
131     int size = in.readInt();
132     initialize(size);
133     for (int i = 0; i < size; ++i) {
134       int row = in.readInt();
135       long value = in.readLong();
136       entries.put(row, value);
137     }
138   }
139 }