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.dense;
2021import it.unimi.dsi.fastutil.ints.IntArrayList;
2223import java.io.DataInput;
24import java.io.DataOutput;
25import java.io.IOException;
2627import org.apache.hadoop.io.Writable;
2829/**30 * The int dense vector holds the values of a particular row.31 * See DoubleDenseVector for explanation on why the singleton is needed.32 */33publicclassIntDenseVectorimplements Writable {
34/** The entries of the vector. */35privatefinal IntArrayList entries = new IntArrayList();
36/** If true, this vector is singleton */37privateboolean isSingleton = false;
38/** The index of the singleton */39privateint singletonIndex;
40/** The value of the singleton */41privateint singletonValue;
4243/** Create a new vector with default size. */44publicIntDenseVector() { }
4546/**47 * Create a new vector with given size.48 *49 * @param size the size of the vector50 */51publicIntDenseVector(int size) {
52 ensureCapacity(size);
53 }
5455/**56 * Set the singleton index and value.57 *58 * @param index the index59 * @param value the value60 */61publicvoid setSingleton(int index, int value) {
62 isSingleton = true;
63this.singletonIndex = index;
64this.singletonValue = value;
65 }
6667/**68 * Get the singleton index.69 *70 * @return the singleton index71 */72publicint getSingletonIndex() {
73return singletonIndex;
74 }
7576/**77 * Get the singleton value.78 *79 * @return the singleton value80 */81publicint getSingletonValue() {
82return singletonValue;
83 }
8485/**86 * Get a particular entry of the vector.87 *88 * @param i the entry89 * @return the value of the entry.90 */91publicint get(int i) {
92// The default value is 093if (i >= entries.size()) {
94return 0;
95 }
96return entries.getInt(i);
97 }
9899/**100 * Set the given value to the entry with the index specified.101 *102 * @param i the entry103 * @param value the value to set to the entry104 */105publicvoid set(int i, int value) {
106 entries.set(i, value);
107 }
108109/**110 * Add the vector specified. This is a vector addition that does an111 * element-by-element addition.112 *113 * @param other the vector to add.114 */115publicvoid add(IntDenseVector other) {
116if (isSingleton) {
117thrownew RuntimeException("Cannot add to singleton vector");
118 }
119if (other.isSingleton) {
120 ensureCapacity(other.singletonIndex + 1);
121 entries.set(other.singletonIndex, entries.getInt(other.singletonIndex) +
122 other.singletonValue);
123 } else {
124 ensureCapacity(other.entries.size());
125for (int i = 0; i < other.entries.size(); ++i) {
126 entries.set(i, entries.getInt(i) + other.entries.getInt(i));
127 }
128 }
129 }
130131/**132 * Resize the array to be at least the size specified.133 *134 * @param size the size of the array135 */136privatevoid ensureCapacity(int size) {
137if (entries.size() < size) {
138 entries.size(size);
139 }
140 }
141142 @Override
143publicvoid write(DataOutput out) throws IOException {
144 out.writeBoolean(isSingleton);
145if (isSingleton) {
146 out.writeInt(singletonIndex);
147 out.writeInt(singletonValue);
148 } else {
149 out.writeInt(entries.size());
150for (int i = 0; i < entries.size(); ++i) {
151 out.writeInt(entries.getInt(i));
152 }
153 }
154 }
155156 @Override
157publicvoid readFields(DataInput in) throws IOException {
158 isSingleton = in.readBoolean();
159if (isSingleton) {
160 singletonIndex = in.readInt();
161 singletonValue = in.readInt();
162 } else {
163int size = in.readInt();
164for (int i = 0; i < size; ++i) {
165 entries.add(in.readInt());
166 }
167 }
168 }
169 }