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.Int2LongMap;
22import it.unimi.dsi.fastutil.ints.Int2LongOpenHashMap;
23import it.unimi.dsi.fastutil.objects.ObjectIterator;
2425import java.io.DataInput;
26import java.io.DataOutput;
27import java.io.IOException;
2829import org.apache.hadoop.io.Writable;
3031/**32 * The long vector holds the values of a particular row.33 */34publicclassLongSparseVectorimplements Writable {
35/**36 * The entries of the vector are (key, value) pairs of the form (row, value)37 */38private Int2LongOpenHashMap entries = null;
3940/**41 * Create a new vector with default size.42 */43publicLongSparseVector() {
44 initialize(Int2LongOpenHashMap.DEFAULT_INITIAL_SIZE);
45 }
4647/**48 * Create a new vector with given size.49 *50 * @param size the size of the vector51 */52publicLongSparseVector(int size) {
53 initialize(size);
54 }
5556/**57 * Initialize the values of the vector. The default value is 0.058 *59 * @param size the size of the vector60 */61privatevoid initialize(int size) {
62 entries = new Int2LongOpenHashMap(size);
63 entries.defaultReturnValue(0L);
64 }
6566/**67 * Get a particular entry of the vector.68 *69 * @param i the entry70 * @return the value of the entry.71 */72publiclong get(int i) {
73return entries.get(i);
74 }
7576/**77 * Set the given value to the entry specified.78 *79 * @param i the entry80 * @param value the value to set to the entry81 */82publicvoid set(int i, long value) {
83 entries.put(i, value);
84 }
8586/**87 * Increment value for a given key88 * @param key Key89 * @param value Increment90 */91publicvoid add(int key, long value) {
92 entries.addTo(key, value);
93 }
9495/**96 * Clear the contents of the vector.97 */98publicvoid clear() {
99 entries.clear();
100 }
101102/**103 * Add the vector specified. This is a vector addition that does an104 * element-by-element addition.105 *106 * @param other the vector to add.107 */108publicvoid add(LongSparseVector other) {
109 ObjectIterator<Int2LongMap.Entry> iter =
110 other.entries.int2LongEntrySet().fastIterator();
111while (iter.hasNext()) {
112 Int2LongMap.Entry entry = iter.next();
113 entries.addTo(entry.getIntKey(), entry.getLongValue());
114 }
115 }
116117 @Override
118publicvoid write(DataOutput out) throws IOException {
119 out.writeInt(entries.size());
120 ObjectIterator<Int2LongMap.Entry> iter =
121 entries.int2LongEntrySet().fastIterator();
122while (iter.hasNext()) {
123 Int2LongMap.Entry entry = iter.next();
124 out.writeInt(entry.getIntKey());
125 out.writeLong(entry.getLongValue());
126 }
127 }
128129 @Override
130publicvoid readFields(DataInput in) throws IOException {
131int size = in.readInt();
132 initialize(size);
133for (int i = 0; i < size; ++i) {
134int row = in.readInt();
135long value = in.readLong();
136 entries.put(row, value);
137 }
138 }
139 }