Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DoubleSparseVector |
|
| 1.3;1.3 |
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.Int2DoubleMap; | |
22 | import it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap; | |
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 double vector holds the values of a particular row. | |
33 | */ | |
34 | public class DoubleSparseVector implements Writable { | |
35 | /** | |
36 | * The entries of the vector are (key, value) pairs of the form (row, value) | |
37 | */ | |
38 | 0 | private Int2DoubleOpenHashMap entries = null; |
39 | ||
40 | /** | |
41 | * Create a new vector with default size. | |
42 | */ | |
43 | 0 | public DoubleSparseVector() { |
44 | 0 | initialize(Int2DoubleOpenHashMap.DEFAULT_INITIAL_SIZE); |
45 | 0 | } |
46 | ||
47 | /** | |
48 | * Create a new vector with given size. | |
49 | * | |
50 | * @param size the size of the vector | |
51 | */ | |
52 | 0 | public DoubleSparseVector(int size) { |
53 | 0 | initialize(size); |
54 | 0 | } |
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 | 0 | entries = new Int2DoubleOpenHashMap(size); |
63 | 0 | entries.defaultReturnValue(0.0f); |
64 | 0 | } |
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 double get(int i) { | |
73 | 0 | 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, double value) { | |
83 | 0 | entries.put(i, value); |
84 | 0 | } |
85 | ||
86 | /** | |
87 | * Increment value for a given key | |
88 | * @param key Key | |
89 | * @param value Increment | |
90 | */ | |
91 | public void add(int key, double value) { | |
92 | 0 | entries.addTo(key, value); |
93 | 0 | } |
94 | ||
95 | /** | |
96 | * Clear the contents of the vector. | |
97 | */ | |
98 | public void clear() { | |
99 | 0 | entries.clear(); |
100 | 0 | } |
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(DoubleSparseVector other) { | |
109 | 0 | ObjectIterator<Int2DoubleMap.Entry> iter = |
110 | 0 | other.entries.int2DoubleEntrySet().fastIterator(); |
111 | 0 | while (iter.hasNext()) { |
112 | 0 | Int2DoubleMap.Entry entry = iter.next(); |
113 | 0 | entries.addTo(entry.getIntKey(), entry.getDoubleValue()); |
114 | 0 | } |
115 | 0 | } |
116 | ||
117 | @Override | |
118 | public void write(DataOutput out) throws IOException { | |
119 | 0 | out.writeInt(entries.size()); |
120 | 0 | ObjectIterator<Int2DoubleMap.Entry> iter = |
121 | 0 | entries.int2DoubleEntrySet().fastIterator(); |
122 | 0 | while (iter.hasNext()) { |
123 | 0 | Int2DoubleMap.Entry entry = iter.next(); |
124 | 0 | out.writeInt(entry.getIntKey()); |
125 | 0 | out.writeDouble(entry.getDoubleValue()); |
126 | 0 | } |
127 | 0 | } |
128 | ||
129 | @Override | |
130 | public void readFields(DataInput in) throws IOException { | |
131 | 0 | int size = in.readInt(); |
132 | 0 | initialize(size); |
133 | 0 | for (int i = 0; i < size; ++i) { |
134 | 0 | int row = in.readInt(); |
135 | 0 | double value = in.readDouble(); |
136 | 0 | entries.put(row, value); |
137 | } | |
138 | 0 | } |
139 | } |