View Javadoc
1   /*
2    * Copyright 2007, 2008, 2009 Ange Optimization ApS
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package eu.simuline.octave.type;
17  
18  import java.util.Arrays;
19  
20  /**
21   * Represents a Boolean matrix and is appropriate for sparse matrices. 
22   *
23   * @see OctaveBoolean
24   */
25  public final class OctaveSparseBoolean implements OctaveObject {
26  
27      private static final int PRIME = 31;
28  
29      private final int rows;
30  
31      private final int columns;
32  
33      // number of non-zero, i.e. of non-false, i.e. of true. 
34      // meant number of entries with value true. 
35      // seems to be rowIndices.length which equals columnIndices.length. 
36      private int nnz;
37  
38      private final int[] rowIndexes;
39  
40      private final int[] columnIndexes;
41  
42      private final boolean[] data;
43  
44      private OctaveSparseBoolean(final int rows,
45  				final int columns,
46  				final int nnz,
47  				final int[] rowIndexes,
48  				final int[] columnIndexes,
49  				final boolean[] data) {
50          this.rows = rows;
51          this.columns = columns;
52          this.nnz = nnz;
53          this.rowIndexes = rowIndexes;
54          this.columnIndexes = columnIndexes;
55          this.data = data;
56      }
57  
58      /**
59       * @param rows
60       * @param columns
61       * @param nnz
62       */
63      public OctaveSparseBoolean(final int rows,
64  			       final int columns,
65  			       final int nnz) {
66  	// ****         nnz=0
67          this(rows, columns, 0, new int[nnz], new int[nnz], new boolean[nnz]);
68      }
69  
70      @Override
71      public OctaveSparseBoolean shallowCopy() {
72          return new OctaveSparseBoolean(this.rows,
73  				       this.columns,
74  				       this.nnz,
75  				       this.rowIndexes,
76  				       this.columnIndexes,
77  				       this.data);
78      }
79  
80      /**
81       * @param value
82       * @param row
83       * @param column
84       */
85      @SuppressWarnings("checkstyle:nowhitespaceafter")
86      public void set(final boolean value, final int row, final int column) {
87          this.data         [nnz] = value;
88          this.rowIndexes   [nnz] = row;
89          this.columnIndexes[nnz] = column;
90          ++this.nnz;
91      }
92  
93      /**
94       * @return the rows
95       */
96      public int getRows() {
97          return this.rows;
98      }
99  
100     /**
101      * @return the columns
102      */
103     public int getColumns() {
104         return this.columns;
105     }
106 
107     /**
108      * @return the nnz
109      */
110     public int getNnz() {
111         return this.nnz;
112     }
113 
114     /**
115      * @return the rowIndexes
116      */
117     @edu.umd.cs.findbugs.annotations.SuppressWarnings
118     (value = "EI_EXPOSE_REP", 
119      justification = "Not woth fixing: class has to be rewritten anyway. ")
120     public int[] getRowIndexes() {
121         return this.rowIndexes;
122     }
123 
124     /**
125      * @return the columnIndexes
126      */
127     @edu.umd.cs.findbugs.annotations.SuppressWarnings
128     (value = "EI_EXPOSE_REP", 
129      justification = "Not woth fixing: class has to be rewritten anyway. ")
130     public int[] getColumnIndexes() {
131         return this.columnIndexes;
132     }
133 
134     /**
135      * @return the data
136      */
137     @edu.umd.cs.findbugs.annotations.SuppressWarnings
138     (value = "EI_EXPOSE_REP", 
139      justification = "Not woth fixing: class has to be rewritten anyway. ")
140     public boolean[] getData() {
141         return this.data;
142     }
143 
144     @Override
145     public int hashCode() {
146         int result = 1;
147         result = PRIME * result + Arrays.hashCode(columnIndexes);
148         result = PRIME * result + columns;
149         result = PRIME * result + Arrays.hashCode(data);
150         result = PRIME * result + nnz;
151         result = PRIME * result + Arrays.hashCode(rowIndexes);
152         result = PRIME * result + rows;
153         return result;
154     }
155 
156     @Override
157     @SuppressWarnings("PMD.NPathComplexity")
158     public boolean equals(final Object obj) {
159         if (this == obj) {
160             return true;
161         }
162         if (obj == null) {
163             return false;
164         }
165         if (getClass() != obj.getClass()) {
166             return false;
167         }
168         final OctaveSparseBooleaneu/simuline/octave/type/OctaveSparseBoolean.html#OctaveSparseBoolean">OctaveSparseBoolean other = (OctaveSparseBoolean) obj;
169         if (!Arrays.equals(this.columnIndexes, other.columnIndexes)) {
170             return false;
171         }
172         if (this.columns != other.columns) {
173             return false;
174         }
175         if (!Arrays.equals(this.data, other.data)) {
176             return false;
177         }
178         if (this.nnz != other.nnz) {
179             return false;
180         }
181         if (!Arrays.equals(this.rowIndexes, other.rowIndexes)) {
182             return false;
183         }
184 	return this.rows == other.rows;
185         // if (rows != other.rows) {
186         //     return false;
187         // }
188         // return true;
189     }
190 
191 }