1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.aggregators.matrix.sparse;
2021import it.unimi.dsi.fastutil.ints.Int2FloatMap;
2223import java.io.DataInput;
24import java.io.DataOutput;
25import java.io.IOException;
2627import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;
28import it.unimi.dsi.fastutil.objects.ObjectIterator;
2930import org.apache.hadoop.io.Writable;
3132/**33 * The float vector holds the values of a particular row.34 */35publicclassFloatSparseVectorimplements Writable {
36/**37 * The entries of the vector are (key, value) pairs of the form (row, value)38 */39private Int2FloatOpenHashMap entries = null;
4041/**42 * Create a new vector with default size.43 */44publicFloatSparseVector() {
45 initialize(Int2FloatOpenHashMap.DEFAULT_INITIAL_SIZE);
46 }
4748/**49 * Create a new vector with given size.50 *51 * @param size the size of the vector52 */53publicFloatSparseVector(int size) {
54 initialize(size);
55 }
5657/**58 * Initialize the values of the vector. The default value is 0.059 *60 * @param size the size of the vector61 */62privatevoid initialize(int size) {
63 entries = new Int2FloatOpenHashMap(size);
64 entries.defaultReturnValue(0.0f);
65 }
6667/**68 * Get a particular entry of the vector.69 *70 * @param i the entry71 * @return the value of the entry.72 */73publicfloat get(int i) {
74return entries.get(i);
75 }
7677/**78 * Set the given value to the entry specified.79 *80 * @param i the entry81 * @param value the value to set to the entry82 */83publicvoid set(int i, float value) {
84 entries.put(i, value);
85 }
8687/**88 * Clear the contents of the vector.89 */90publicvoid clear() {
91 entries.clear();
92 }
9394/**95 * Add the vector specified. This is a vector addition that does an96 * element-by-element addition.97 *98 * @param other the vector to add.99 */100publicvoid add(FloatSparseVector other) {
101 ObjectIterator<Int2FloatMap.Entry> iter =
102 other.entries.int2FloatEntrySet().fastIterator();
103while (iter.hasNext()) {
104 Int2FloatMap.Entry entry = iter.next();
105 entries.addTo(entry.getIntKey(), entry.getFloatValue());
106 }
107 }
108109 @Override
110publicvoid write(DataOutput out) throws IOException {
111 out.writeInt(entries.size());
112 ObjectIterator<Int2FloatMap.Entry> iter =
113 entries.int2FloatEntrySet().fastIterator();
114while (iter.hasNext()) {
115 Int2FloatMap.Entry entry = iter.next();
116 out.writeInt(entry.getIntKey());
117 out.writeFloat(entry.getFloatValue());
118 }
119 }
120121 @Override
122publicvoid readFields(DataInput in) throws IOException {
123int size = in.readInt();
124 initialize(size);
125for (int i = 0; i < size; ++i) {
126int row = in.readInt();
127float value = in.readFloat();
128 entries.put(row, value);
129 }
130 }
131 }