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.Int2ObjectOpenHashMap; 22 23 /** 24 * A double matrix holds the values of the entries in double vectors. It keeps 25 * one double aggregator per matrix row. 26 */ 27 public class DoubleSparseMatrix { 28 /** The number of rows in the matrix */ 29 private int numRows; 30 /** The rows of the matrix */ 31 private Int2ObjectOpenHashMap<DoubleSparseVector> rows; 32 33 /** 34 * Create a new matrix with the given number of rows. 35 * 36 * @param numRows the number of rows. 37 */ 38 public DoubleSparseMatrix(int numRows) { 39 this.numRows = numRows; 40 rows = new Int2ObjectOpenHashMap<DoubleSparseVector>(numRows); 41 rows.defaultReturnValue(null); 42 } 43 44 /** 45 * Create a empty matrix with all values set to 0.0 46 */ 47 public void initialize() { 48 rows.clear(); 49 for (int i = 0; i < numRows; ++i) { 50 setRow(i, new DoubleSparseVector()); 51 } 52 } 53 54 /** 55 * Get the number of rows in the matrix. 56 * 57 * @return the number of rows. 58 */ 59 public int getNumRows() { 60 return numRows; 61 } 62 63 /** 64 * Get a specific entry of the matrix. 65 * 66 * @param i the row 67 * @param j the column 68 * @return the value of the entry 69 */ 70 public double get(int i, int j) { 71 return rows.get(i).get(j); 72 } 73 74 /** 75 * Set a specific entry of the matrix. 76 * 77 * @param i the row 78 * @param j the column 79 * @param v the value of the entry 80 */ 81 public void set(int i, int j, double v) { 82 rows.get(i).set(j, v); 83 } 84 85 /** 86 * Get a specific row of the matrix. 87 * 88 * @param i the row number 89 * @return the row of the matrix 90 */ 91 DoubleSparseVector getRow(int i) { 92 return rows.get(i); 93 } 94 95 /** 96 * Set the double vector as the row specified. 97 * 98 * @param i the row 99 * @param vec the vector to set as the row 100 */ 101 void setRow(int i, DoubleSparseVector vec) { 102 rows.put(i, vec); 103 } 104 }