This project has retired. For details please refer to its Attic page.
LongDenseMatrix 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 java.util.ArrayList;
22  
23  /**
24   * A long matrix holds the values of the entries in long vectors. It keeps one
25   * long aggregator per matrix row.
26   */
27  public class LongDenseMatrix {
28    /** The number of rows in the matrix */
29    private int numRows;
30    /** The number of columns in the matrix */
31    private int numColumns;
32    /** The rows of the matrix */
33    private ArrayList<LongDenseVector> rows = null;
34  
35    /**
36     * Create a new matrix with the same number of rows and columns.
37     *
38     * @param size the number of rows and columns
39     */
40    public LongDenseMatrix(int size) {
41      this(size, size);
42    }
43  
44    /**
45     * Create a new matrix with the given number of rows and columns.
46     *
47     * @param numRows the number of rows
48     * @param numColumns the number of columns
49     */
50    public LongDenseMatrix(int numRows, int numColumns) {
51      this.numRows = numRows;
52      this.numColumns = numColumns;
53      rows = new ArrayList<LongDenseVector>();
54    }
55  
56    /**
57     * Create a empty matrix with all values set to 0.0
58     */
59    public void initialize() {
60      rows.clear();
61      for (int i = 0; i < numRows; ++i) {
62        rows.add(new LongDenseVector(numColumns));
63      }
64    }
65  
66    /**
67     * Get the number of rows in the matrix.
68     *
69     * @return the number of rows
70     */
71    public int getNumRows() {
72      return numRows;
73    }
74  
75    /**
76     * Get the number of the columns in the matrix.
77     *
78     * @return the number of rows
79     */
80    public int getNumColumns() {
81      return numColumns;
82    }
83  
84    /**
85     * Get a specific entry of the matrix.
86     *
87     * @param i the row
88     * @param j the column
89     * @return the value of the entry
90     */
91    public long get(int i, int j) {
92      return rows.get(i).get(j);
93    }
94  
95    /**
96     * Set a specific entry of the matrix.
97     *
98     * @param i the row
99     * @param j the column
100    * @param v the value of the entry
101    */
102   public void set(int i, int j, long v) {
103     rows.get(i).set(j, v);
104   }
105 
106   /**
107    * Get a specific row of the matrix.
108    *
109    * @param i the row number
110    * @return the row of the matrix
111    */
112   LongDenseVector getRow(int i) {
113     return rows.get(i);
114   }
115 
116   /**
117    * Add the long vector as a row in the matrix.
118    *
119    * @param vec the vector to add
120    */
121   void addRow(LongDenseVector vec) {
122     if (rows.size() >= numRows) {
123       throw new RuntimeException("Cannot add more rows!");
124     }
125     rows.add(vec);
126   }
127 }