Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IntDenseVector |
|
| 2.0;2 |
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.dense; | |
20 | ||
21 | import it.unimi.dsi.fastutil.ints.IntArrayList; | |
22 | ||
23 | import java.io.DataInput; | |
24 | import java.io.DataOutput; | |
25 | import java.io.IOException; | |
26 | ||
27 | import org.apache.hadoop.io.Writable; | |
28 | ||
29 | /** | |
30 | * The int dense vector holds the values of a particular row. | |
31 | * See DoubleDenseVector for explanation on why the singleton is needed. | |
32 | */ | |
33 | public class IntDenseVector implements Writable { | |
34 | /** The entries of the vector. */ | |
35 | 0 | private final IntArrayList entries = new IntArrayList(); |
36 | /** If true, this vector is singleton */ | |
37 | 0 | private boolean isSingleton = false; |
38 | /** The index of the singleton */ | |
39 | private int singletonIndex; | |
40 | /** The value of the singleton */ | |
41 | private int singletonValue; | |
42 | ||
43 | /** Create a new vector with default size. */ | |
44 | 0 | public IntDenseVector() { } |
45 | ||
46 | /** | |
47 | * Create a new vector with given size. | |
48 | * | |
49 | * @param size the size of the vector | |
50 | */ | |
51 | 0 | public IntDenseVector(int size) { |
52 | 0 | ensureCapacity(size); |
53 | 0 | } |
54 | ||
55 | /** | |
56 | * Set the singleton index and value. | |
57 | * | |
58 | * @param index the index | |
59 | * @param value the value | |
60 | */ | |
61 | public void setSingleton(int index, int value) { | |
62 | 0 | isSingleton = true; |
63 | 0 | this.singletonIndex = index; |
64 | 0 | this.singletonValue = value; |
65 | 0 | } |
66 | ||
67 | /** | |
68 | * Get the singleton index. | |
69 | * | |
70 | * @return the singleton index | |
71 | */ | |
72 | public int getSingletonIndex() { | |
73 | 0 | return singletonIndex; |
74 | } | |
75 | ||
76 | /** | |
77 | * Get the singleton value. | |
78 | * | |
79 | * @return the singleton value | |
80 | */ | |
81 | public int getSingletonValue() { | |
82 | 0 | return singletonValue; |
83 | } | |
84 | ||
85 | /** | |
86 | * Get a particular entry of the vector. | |
87 | * | |
88 | * @param i the entry | |
89 | * @return the value of the entry. | |
90 | */ | |
91 | public int get(int i) { | |
92 | // The default value is 0 | |
93 | 0 | if (i >= entries.size()) { |
94 | 0 | return 0; |
95 | } | |
96 | 0 | return entries.getInt(i); |
97 | } | |
98 | ||
99 | /** | |
100 | * Set the given value to the entry with the index specified. | |
101 | * | |
102 | * @param i the entry | |
103 | * @param value the value to set to the entry | |
104 | */ | |
105 | public void set(int i, int value) { | |
106 | 0 | entries.set(i, value); |
107 | 0 | } |
108 | ||
109 | /** | |
110 | * Add the vector specified. This is a vector addition that does an | |
111 | * element-by-element addition. | |
112 | * | |
113 | * @param other the vector to add. | |
114 | */ | |
115 | public void add(IntDenseVector other) { | |
116 | 0 | if (isSingleton) { |
117 | 0 | throw new RuntimeException("Cannot add to singleton vector"); |
118 | } | |
119 | 0 | if (other.isSingleton) { |
120 | 0 | ensureCapacity(other.singletonIndex + 1); |
121 | 0 | entries.set(other.singletonIndex, entries.getInt(other.singletonIndex) + |
122 | other.singletonValue); | |
123 | } else { | |
124 | 0 | ensureCapacity(other.entries.size()); |
125 | 0 | for (int i = 0; i < other.entries.size(); ++i) { |
126 | 0 | entries.set(i, entries.getInt(i) + other.entries.getInt(i)); |
127 | } | |
128 | } | |
129 | 0 | } |
130 | ||
131 | /** | |
132 | * Resize the array to be at least the size specified. | |
133 | * | |
134 | * @param size the size of the array | |
135 | */ | |
136 | private void ensureCapacity(int size) { | |
137 | 0 | if (entries.size() < size) { |
138 | 0 | entries.size(size); |
139 | } | |
140 | 0 | } |
141 | ||
142 | @Override | |
143 | public void write(DataOutput out) throws IOException { | |
144 | 0 | out.writeBoolean(isSingleton); |
145 | 0 | if (isSingleton) { |
146 | 0 | out.writeInt(singletonIndex); |
147 | 0 | out.writeInt(singletonValue); |
148 | } else { | |
149 | 0 | out.writeInt(entries.size()); |
150 | 0 | for (int i = 0; i < entries.size(); ++i) { |
151 | 0 | out.writeInt(entries.getInt(i)); |
152 | } | |
153 | } | |
154 | 0 | } |
155 | ||
156 | @Override | |
157 | public void readFields(DataInput in) throws IOException { | |
158 | 0 | isSingleton = in.readBoolean(); |
159 | 0 | if (isSingleton) { |
160 | 0 | singletonIndex = in.readInt(); |
161 | 0 | singletonValue = in.readInt(); |
162 | } else { | |
163 | 0 | int size = in.readInt(); |
164 | 0 | for (int i = 0; i < size; ++i) { |
165 | 0 | entries.add(in.readInt()); |
166 | } | |
167 | } | |
168 | 0 | } |
169 | } |