This project has retired. For details please refer to its Attic page.
LongDenseVector xref
View Javadoc

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.longs.LongArrayList;
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 long dense vector holds the values of a particular row.
31   * See DoubleDenseVector for explanation on why the singleton is needed.
32   */
33  public class LongDenseVector implements Writable {
34    /** The entries of the vector. */
35    private final LongArrayList entries = new LongArrayList();
36    /** If true, this vector is singleton */
37    private boolean isSingleton = false;
38    /** The index of the singleton */
39    private int singletonIndex;
40    /** The value of the singleton */
41    private long singletonValue;
42  
43    /** Create a new vector with default size. */
44    public LongDenseVector() { }
45  
46    /**
47     * Create a new vector with given size.
48     *
49     * @param size the size of the vector
50     */
51    public LongDenseVector(int size) {
52      ensureCapacity(size);
53    }
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, long value) {
62      isSingleton = true;
63      this.singletonIndex = index;
64      this.singletonValue = value;
65    }
66  
67    /**
68     * Get the singleton index.
69     *
70     * @return the singleton index
71     */
72    public int getSingletonIndex() {
73      return singletonIndex;
74    }
75  
76    /**
77     * Get the singleton value.
78     *
79     * @return the singleton value
80     */
81    public long getSingletonValue() {
82      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 long get(int i) {
92      // The default value is 0.0
93      if (i >= entries.size()) {
94        return 0L;
95      }
96      return entries.getLong(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, long value) {
106     entries.set(i, value);
107   }
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(LongDenseVector other) {
116     if (isSingleton) {
117       throw new RuntimeException("Cannot add to singleton vector");
118     }
119     if (other.isSingleton) {
120       ensureCapacity(other.singletonIndex + 1);
121       entries.set(other.singletonIndex, entries.getLong(other.singletonIndex) +
122           other.singletonValue);
123     } else {
124       ensureCapacity(other.entries.size());
125       for (int i = 0; i < other.entries.size(); ++i) {
126         entries.set(i, entries.getLong(i) + other.entries.getLong(i));
127       }
128     }
129   }
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     if (entries.size() < size) {
138       entries.size(size);
139     }
140   }
141 
142   @Override
143   public void write(DataOutput out) throws IOException {
144     out.writeBoolean(isSingleton);
145     if (isSingleton) {
146       out.writeInt(singletonIndex);
147       out.writeLong(singletonValue);
148     } else {
149       out.writeInt(entries.size());
150       for (int i = 0; i < entries.size(); ++i) {
151         out.writeLong(entries.getLong(i));
152       }
153     }
154   }
155 
156   @Override
157   public void readFields(DataInput in) throws IOException {
158     isSingleton = in.readBoolean();
159     if (isSingleton) {
160       singletonIndex = in.readInt();
161       singletonValue = in.readLong();
162     } else {
163       int size = in.readInt();
164       for (int i = 0; i < size; ++i) {
165         entries.add(in.readLong());
166       }
167     }
168   }
169 }