2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011-2011 - DIGITEO - Calixte DENIZET
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
16 package org.scilab.modules.types;
18 import java.io.IOException;
19 import java.io.ObjectInput;
20 import java.io.ObjectOutput;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
26 * This class provides a representation on the Scilab Boolean Sparse datatype<br>
28 * This class is {@link java.io.Serializable} and any modification could impact
29 * load and store of data (Xcos files, Javasci saved data, etc...).<br>
31 * //TODO Sly : ajouter de la doc
33 * @see org.scilab.modules.javasci.Scilab
35 public class ScilabBooleanSparse implements ScilabType {
37 private static final long serialVersionUID = 879625048944109684L;
38 private static final ScilabTypeEnum type = ScilabTypeEnum.sci_boolean_sparse;
40 private static final int VERSION = 0;
45 private int[] nbItemRow;
47 private String varName;
52 public ScilabBooleanSparse() {
56 * Constructor with a unique value.
61 public ScilabBooleanSparse(boolean data) {
65 nbItemRow = new int[] { 1 };
66 colPos = new int[] { 0 };
78 * the number of true items
80 * contains the number of true in each rows
82 * the column position of each true
84 * if true the parameters validity is checked
86 public ScilabBooleanSparse(int rows, int cols, int nbItem, int[] nbItemRow, int[] colPos, boolean check) throws ScilabSparseException {
87 this(rows, cols, nbItem, nbItemRow, colPos);
89 ScilabSparse.checkValidity(rows, cols, nbItem, nbItemRow, colPos);
101 * the number of true items
103 * contains the number of true in each rows
105 * the column position of each true
107 public ScilabBooleanSparse(int rows, int cols, int nbItem, int[] nbItemRow, int[] colPos) {
110 this.nbItem = nbItem;
111 this.nbItemRow = nbItemRow;
112 this.colPos = colPos;
116 * Constructor with a matrix of real data.
121 public ScilabBooleanSparse(boolean[][] data) {
122 if (data.length != 0 && data[0].length != 0) {
124 cols = data[0].length;
125 nbItemRow = new int[rows];
126 List<Integer> listCol = new ArrayList<Integer>();
128 for (int i = 0; i < rows; i++) {
129 for (int j = 0; j < cols; j++) {
138 colPos = new int[listCol.size()];
140 for (Integer c : listCol) {
156 * the number of true items
158 * contains the number of true in each rows
160 * the column position of each true
162 public ScilabBooleanSparse(String varName, int rows, int cols, int nbItem, int[] nbItemRow, int[] colPos) {
163 this(rows, cols, nbItem, nbItemRow, colPos);
164 this.varName = varName;
170 public boolean isReference() {
175 * Return the type of Scilab
177 * @return the type of Scilab
181 public ScilabTypeEnum getType() {
186 * Check the emptiness of the associated data.
188 * @return true, if the associated data array is empty.
191 public boolean isEmpty() {
192 return rows == 0 && cols == 0;
196 * Get the real part of the data.
198 * @return the real part.
200 public int getNbNonNullItems() {
205 * Set the real part of the data.
210 public void setNbNonNullItems(int nbItem) {
211 this.nbItem = nbItem;
215 * Get the real part of the data.
217 * @return the real part.
219 public int[] getNbItemRow() {
224 * Set the real part of the data.
229 public void setNbItemRow(int[] nbItemRow) {
230 this.nbItemRow = nbItemRow;
234 * Get the column positions of the non null items.
236 * @return an integer array.
238 public int[] getScilabColPos() {
239 int[] cp = new int[colPos.length];
240 for (int i = 0; i < colPos.length; i++) {
241 cp[i] = colPos[i] + 1;
247 * Get the real part of the data.
249 * @return the real part.
251 public int[] getColPos() {
256 * Set the real part of the data.
261 public void setColPos(int[] colPos) {
262 this.colPos = colPos;
268 public String getVarName() {
275 public boolean isSwaped() {
280 * Get the real part of the full sparse matrix
282 * @return the full real matrix
284 public boolean[][] getFullMatrix() {
287 boolean[][] b = new boolean[rows][cols];
288 for (int i = 0; i < nbItemRow.length; i++) {
289 for (; j < prev + nbItemRow[i]; j++) {
290 b[i][colPos[j]] = true;
292 prev += nbItemRow[i];
299 * Get complex matrix as a serialized form
301 * @return the serialized matrix with complex values
303 // TODO Sly : faire qque chose ici...
304 public boolean[] getSerializedBooleanSparseMatrix() {
305 return new boolean[0];
309 * @return the height of the data matrix
310 * @see org.scilab.modules.types.ScilabType#getHeight()
313 public int getHeight() {
318 * @return the width of the data matrix
319 * @see org.scilab.modules.types.ScilabType#getWidth()
322 public int getWidth() {
330 public int hashCode() {
331 final int prime = 31;
333 result = prime * result + Arrays.hashCode(colPos);
334 result = prime * result + cols;
335 result = prime * result + nbItem;
336 result = prime * result + Arrays.hashCode(nbItemRow);
337 result = prime * result + rows;
342 * @see org.scilab.modules.types.ScilabType#equals(Object)
345 public boolean equals(Object obj) {
346 if (obj instanceof ScilabBooleanSparse) {
347 ScilabBooleanSparse sciSparse = (ScilabBooleanSparse) obj;
348 return this.getNbNonNullItems() == sciSparse.getNbNonNullItems() && ScilabSparse.compareNbItemRow(this.getNbItemRow(), sciSparse.getNbItemRow())
349 && Arrays.equals(this.getColPos(), sciSparse.getColPos());
358 public Object getSerializedObject() {
359 return new Object[] { new int[] { rows, cols }, nbItemRow, getScilabColPos() };
363 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
364 int version = in.readInt();
369 nbItem = in.readInt();
370 nbItemRow = (int[]) in.readObject();
371 colPos = (int[]) in.readObject();
372 varName = (String) in.readObject();
375 throw new ClassNotFoundException("A class ScilabBooleanSparse with a version " + version + " does not exists");
380 public void writeExternal(ObjectOutput out) throws IOException {
381 out.writeInt(VERSION);
384 out.writeInt(nbItem);
385 out.writeObject(nbItemRow);
386 out.writeObject(colPos);
387 out.writeObject(varName);
391 * Display the representation in the Scilab language of the type<BR>
392 * Note that the representation can be copied/pasted straight into Scilab
394 * @return a Scilab-like String representation of the data.
395 * @see java.lang.Object#toString()
398 public String toString() {
399 StringBuilder result = new StringBuilder();
403 return result.toString();
406 result.append("sparse([");
409 for (int i = 0; i < nbItemRow.length; i++) {
410 for (; j < prev + nbItemRow[i]; j++) {
411 result.append(Integer.toString(i + 1));
413 result.append(Integer.toString(colPos[j] + 1));
414 if (j < nbItem - 1) {
415 result.append(" ; ");
418 prev += nbItemRow[i];
421 result.append("], [");
422 for (int i = 0; i < nbItem; i++) {
424 if (i < nbItem - 1) {
429 result.append("], [");
430 result.append(Integer.toString(rows));
432 result.append(Integer.toString(cols));
436 return result.toString();