2 * Scilab (http://www.scilab.org/) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Calixte DENIZET
4 * Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
17 package org.scilab.modules.types;
19 import java.nio.ByteBuffer;
20 import java.nio.DoubleBuffer;
21 import java.nio.IntBuffer;
22 import java.nio.LongBuffer;
23 import java.nio.ShortBuffer;
24 import java.util.ArrayList;
25 import java.util.HashMap;
27 import java.util.Vector;
30 * Class to handle the Scilab data retrievment and conversion to a ScilabType
31 * object. All the functions sendFoo are used from C++ and should not be used on
34 * An handler which implements ScilabVariablesHandler must be used to achieve
35 * the retrievment. The id returned by addScilabVariablesHandler must be passed
36 * from C/C++ to functions in the class org_modules_types::ScilabToJava to
37 * guarantee that the correct handler would receive its datas.
39 public final class ScilabVariables {
41 private static final Map<Thread, ArrayList<ScilabType>> lists = new HashMap<Thread, ArrayList<ScilabType>>();
42 private static final Vector<ScilabVariablesHandler> handlers = new Vector<ScilabVariablesHandler>();
45 * Register a new handler
49 * @return the id to use from C/C++
51 public static final int addScilabVariablesHandler(ScilabVariablesHandler handler) {
53 for (; i < handlers.size(); i++) {
54 ScilabVariablesHandler h = handlers.get(i);
57 } else if (h == null) {
58 handlers.set(i, handler);
62 handlers.add(handler);
68 * Unregister an handler (the id must be considered as lost !!)
71 * the handler to remove
73 public static final void removeScilabVariablesHandler(ScilabVariablesHandler handler) {
74 if (handler != null) {
75 for (int i = 0; i < handlers.size(); i++) {
76 if (handlers.get(i) == handler) {
77 handlers.set(i, null);
84 * Unregister an handler with a given id (the id must be considered as lost
88 * the handler id to remove
90 public static final void removeScilabVariablesHandler(int id) {
91 if (id >= 0 && id < handlers.size()) {
92 handlers.set(id, null);
102 * an integer array with the indexes of the (sub)*-list which
103 * will contain the data
107 * true if the matrix is stored row by row
111 public static final void sendData(String varName, int[] indexes, double[][] data, boolean swaped, int handlerId) {
112 if (indexes.length != 0) {
113 addElement(indexes, new ScilabDouble(null, data, null, swaped));
115 handlers.get(handlerId).handle(new ScilabDouble(varName, data, null, swaped));
120 * Send double matrix as DoubleBuffer
125 * an integer array with the indexes of the (sub)*-list which
126 * will contain the data
129 * @param rows number of rows
130 * @param cols number of columns
134 public static final void sendDataAsBuffer(String varName, int[] indexes, DoubleBuffer data, int rows, int cols, int handlerId) {
135 if (indexes.length != 0) {
136 addElement(indexes, new ScilabDoubleReference(null, data, null, rows, cols));
138 handlers.get(handlerId).handle(new ScilabDoubleReference(varName, data, null, rows, cols));
143 * Send complex matrix
148 * an integer array with the indexes of the (sub)*-list which
149 * will contain the data
155 * true if the matrix is stored row by row
159 public static final void sendData(String varName, int[] indexes, double[][] real, double[][] img, boolean swaped, int handlerId) {
160 if (indexes.length != 0) {
161 addElement(indexes, new ScilabDouble(null, real, img, swaped));
163 handlers.get(handlerId).handle(new ScilabDouble(varName, real, img, swaped));
168 * Send complex matrix as DoubleBuffer
173 * an integer array with the indexes of the (sub)*-list which
174 * will contain the data
175 * @param real complex real part data buffer
176 * @param imag complex imaginary part data buffer
177 * @param rows number of rows
178 * @param cols number of columns
182 public static final void sendDataAsBuffer(String varName, int[] indexes, DoubleBuffer real, DoubleBuffer imag, int rows, int cols, int handlerId) {
183 if (indexes.length != 0) {
184 addElement(indexes, new ScilabDoubleReference(null, real, imag, rows, cols));
186 handlers.get(handlerId).handle(new ScilabDoubleReference(varName, real, imag, rows, cols));
196 * an integer array with the indexes of the (sub)*-list which
197 * will contain the data
201 * true if the matrix is stored row by row
205 public static final void sendData(String varName, int[] indexes, int[][] data, boolean swaped, int handlerId) {
206 if (indexes.length != 0) {
207 addElement(indexes, new ScilabInteger(null, data, false, swaped));
209 handlers.get(handlerId).handle(new ScilabInteger(varName, data, false, swaped));
214 * Send int32 matrix as IntBuffer
219 * an integer array with the indexes of the (sub)*-list which
220 * will contain the data
223 * @param rows number of rows
224 * @param cols number of columns
228 public static final void sendDataAsBuffer(String varName, int[] indexes, IntBuffer data, int rows, int cols, int handlerId) {
229 if (indexes.length != 0) {
230 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, false));
232 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, false));
242 * an integer array with the indexes of the (sub)*-list which
243 * will contain the data
247 * true if the matrix is stored row by row
251 public static final void sendUnsignedData(String varName, int[] indexes, short[][] data, boolean swaped, int handlerId) {
252 if (indexes.length != 0) {
253 addElement(indexes, new ScilabInteger(null, data, true, swaped));
255 handlers.get(handlerId).handle(new ScilabInteger(varName, data, true, swaped));
260 * Send uint16 matrix as ShortBuffer
265 * an integer array with the indexes of the (sub)*-list which
266 * will contain the data
269 * @param rows number of rows
270 * @param cols number of columns
274 public static final void sendUnsignedDataAsBuffer(String varName, int[] indexes, ShortBuffer data, int rows, int cols, int handlerId) {
275 if (indexes.length != 0) {
276 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, true));
278 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, true));
288 * an integer array with the indexes of the (sub)*-list which
289 * will contain the data
293 * true if the matrix is stored row by row
297 public static final void sendData(String varName, int[] indexes, short[][] data, boolean swaped, int handlerId) {
298 if (indexes.length != 0) {
299 addElement(indexes, new ScilabInteger(null, data, false, swaped));
301 handlers.get(handlerId).handle(new ScilabInteger(varName, data, false, swaped));
306 * Send int16 matrix as ShortBuffer
311 * an integer array with the indexes of the (sub)*-list which
312 * will contain the data
315 * @param rows number of rows
316 * @param cols number of columns
320 public static final void sendDataAsBuffer(String varName, int[] indexes, ShortBuffer data, int rows, int cols, int handlerId) {
321 if (indexes.length != 0) {
322 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, false));
324 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, false));
334 * an integer array with the indexes of the (sub)*-list which
335 * will contain the data
339 * true if the matrix is stored row by row
343 public static final void sendUnsignedData(String varName, int[] indexes, byte[][] data, boolean swaped, int handlerId) {
344 if (indexes.length != 0) {
345 addElement(indexes, new ScilabInteger(null, data, true, swaped));
347 handlers.get(handlerId).handle(new ScilabInteger(varName, data, true, swaped));
352 * Send uint8 matrix as ByteBuffer
357 * an integer array with the indexes of the (sub)*-list which
358 * will contain the data
361 * @param rows number of rows
362 * @param cols number of columns
366 public static final void sendUnsignedDataAsBuffer(String varName, int[] indexes, ByteBuffer data, int rows, int cols, int handlerId) {
367 if (indexes.length != 0) {
368 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, true));
370 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, true));
375 * Send boolean matrix
380 * an integer array with the indexes of the (sub)*-list which
381 * will contain the data
385 * true if the matrix is stored row by row
389 public static final void sendData(String varName, int[] indexes, boolean[][] data, boolean swaped, int handlerId) {
390 if (indexes.length != 0) {
391 addElement(indexes, new ScilabBoolean(null, data, swaped));
393 handlers.get(handlerId).handle(new ScilabBoolean(varName, data, swaped));
398 * Send boolean matrix as reference
403 * an integer array with the indexes of the (sub)*-list which
404 * will contain the data
407 * @param rows number of rows
408 * @param cols number of columns
412 public static final void sendBooleanDataAsBuffer(String varName, int[] indexes, IntBuffer data, int rows, int cols, int handlerId) {
413 if (indexes.length != 0) {
414 addElement(indexes, new ScilabBooleanReference(null, data, rows, cols));
416 handlers.get(handlerId).handle(new ScilabBooleanReference(varName, data, rows, cols));
426 * an integer array with the indexes of the (sub)*-list which
427 * will contain the data
431 * true if the matrix is stored row by row
435 public static final void sendData(String varName, int[] indexes, byte[][] data, boolean swaped, int handlerId) {
436 if (indexes.length != 0) {
437 addElement(indexes, new ScilabInteger(null, data, false, swaped));
439 handlers.get(handlerId).handle(new ScilabInteger(varName, data, false, swaped));
444 * Send int8 matrix as ByteBuffer
449 * an integer array with the indexes of the (sub)*-list which
450 * will contain the data
453 * @param rows number of rows
454 * @param cols number of columns
458 public static final void sendDataAsBuffer(String varName, int[] indexes, ByteBuffer data, int rows, int cols, int handlerId) {
459 if (indexes.length != 0) {
460 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, false));
462 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, false));
467 * Send int64 or uint64 matrix
472 * an integer array with the indexes of the (sub)*-list which
473 * will contain the data
477 * true if the matrix is stored row by row
481 public static final void sendData(String varName, int[] indexes, long[][] data, boolean swaped, int handlerId) {
482 if (indexes.length != 0) {
483 addElement(indexes, new ScilabInteger(null, data, false, swaped));
485 handlers.get(handlerId).handle(new ScilabInteger(varName, data, false, swaped));
490 * Send int64 or uint64 matrix as LongBuffer
495 * an integer array with the indexes of the (sub)*-list which
496 * will contain the data
499 * @param rows number of rows
500 * @param cols number of columns
504 public static final void sendDataAsBuffer(String varName, int[] indexes, LongBuffer data, int rows, int cols, int handlerId) {
505 if (indexes.length != 0) {
506 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, false));
508 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, false));
518 * an integer array with the indexes of the (sub)*-list which
519 * will contain the data
523 * true if the matrix is stored row by row
527 public static final void sendUnsignedData(String varName, int[] indexes, int[][] data, boolean swaped, int handlerId) {
528 if (indexes.length != 0) {
529 addElement(indexes, new ScilabInteger(null, data, true, swaped));
531 handlers.get(handlerId).handle(new ScilabInteger(varName, data, true, swaped));
536 * Send uint32 matrix as IntBuffer
541 * an integer array with the indexes of the (sub)*-list which
542 * will contain the data
545 * @param rows number of rows
546 * @param cols number of columns
550 public static final void sendUnsignedDataAsBuffer(String varName, int[] indexes, IntBuffer data, int rows, int cols, int handlerId) {
551 if (indexes.length != 0) {
552 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, true));
554 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, true));
564 * an integer array with the indexes of the (sub)*-list which
565 * will contain the data
569 * true if the matrix is stored row by row
573 public static final void sendData(String varName, int[] indexes, String[][] data, boolean swaped, int handlerId) {
574 if (indexes.length != 0) {
575 addElement(indexes, new ScilabString(null, data, swaped));
577 handlers.get(handlerId).handle(new ScilabString(varName, data, swaped));
582 * Send double sparse matrix
587 * an integer array with the indexes of the (sub)*-list which
588 * will contain the data
594 * the number of non null elements
596 * the number by row of non null elements
598 * the column position of the non null elements
604 public static final void sendData(String varName, int[] indexes, int row, int col, int nbItem, int[] nbItemRow, int[] colPos, double[] data, int handlerId) {
605 if (indexes.length != 0) {
606 addElement(indexes, new ScilabSparse(null, row, col, nbItem, nbItemRow, colPos, data, null));
608 handlers.get(handlerId).handle(new ScilabSparse(varName, row, col, nbItem, nbItemRow, colPos, data, null));
613 * Send double sparse matrix as buffer
614 * Useless for now due to Scilab limitations (A=sparse(...);A(1,1)=123; a new A is created so references are lost)
619 * an integer array with the indexes of the (sub)*-list which
620 * will contain the data
626 * the number of non null elements
628 * the number by row of non null elements
630 * the column position of the non null elements
636 /*public static final void sendDataAsBuffer(String varName, int[] indexes, int row, int col, int nbItem, IntBuffer nbItemRow, IntBuffer colPos, DoubleBuffer data, int handlerId) {
637 if (indexes.length != 0) {
638 addElement(indexes, new ScilabSparseReference(null, row, col, nbItem, nbItemRow, colPos, data, null));
640 handlers.get(handlerId).handle(new ScilabSparseReference(varName, row, col, nbItem, nbItemRow, colPos, data, null));
645 * Send complex sparse matrix
650 * an integer array with the indexes of the (sub)*-list which
651 * will contain the data
657 * the number of non null elements
659 * the number by row of non null elements
661 * the column position of the non null elements
669 public static final void sendData(String varName, int[] indexes, int row, int col, int nbItem, int[] nbItemRow, int[] colPos, double[] real, double[] imag, int handlerId) {
670 if (indexes.length != 0) {
671 addElement(indexes, new ScilabSparse(null, row, col, nbItem, nbItemRow, colPos, real, imag));
673 handlers.get(handlerId).handle(new ScilabSparse(varName, row, col, nbItem, nbItemRow, colPos, real, imag));
678 * Send complex sparse matrix as buffer
679 * Useless for now due to Scilab limitations (A=sparse(...);A(1,1)=123; a new A is created so references are lost)
684 * an integer array with the indexes of the (sub)*-list which
685 * will contain the data
691 * the number of non null elements
693 * the number by row of non null elements
695 * the column position of the non null elements
703 /*private static final void sendDataAsBuffer(String varName, int[] indexes, int row, int col, int nbItem, IntBuffer nbItemRow, IntBuffer colPos, DoubleBuffer real, DoubleBuffer imag, int handlerId) {
704 if (indexes.length != 0) {
705 addElement(indexes, new ScilabSparse(null, row, col, nbItem, nbItemRow, colPos, real, imag));
707 handlers.get(handlerId).handle(new ScilabSparse(varName, row, col, nbItem, nbItemRow, colPos, real, imag));
712 * Send boolean sparse matrix
713 * Useless for now due to Scilab limitations (A=sparse(...);A(1,1)=123; a new A is created so references are lost)
718 * an integer array with the indexes of the (sub)*-list which
719 * will contain the data
725 * the number of true elements
727 * the number by row of true elements
729 * the column position of the true elements
733 private static final void sendData(String varName, int[] indexes, int row, int col, int nbItem, int[] nbItemRow, int[] colPos, int handlerId) {
734 if (indexes.length != 0) {
735 addElement(indexes, new ScilabBooleanSparse(null, row, col, nbItem, nbItemRow, colPos));
737 handlers.get(handlerId).handle(new ScilabBooleanSparse(varName, row, col, nbItem, nbItemRow, colPos));
742 * Send boolean sparse matrix as Buffer
747 * an integer array with the indexes of the (sub)*-list which
748 * will contain the data
754 * the number of true elements
756 * the number by row of true elements
758 * the column position of the true elements
762 /*public static final void sendDataAsBuffer(String varName, int[] indexes, int row, int col, int nbItem, IntBuffer nbItemRow, IntBuffer colPos, int handlerId) {
763 if (indexes.length != 0) {
764 addElement(indexes, new ScilabBooleanSparse(null, row, col, nbItem, nbItemRow, colPos));
766 handlers.get(handlerId).handle(new ScilabBooleanSparse(varName, row, col, nbItem, nbItemRow, colPos));
771 * Send double polynomial matrix
776 * an integer array with the indexes of the (sub)*-list which
777 * will contain the data
779 * the polynomial variable name
783 * true if the matrix is stored row by row
787 public static final void sendPolynomial(String varName, int[] indexes, String polyVarName, double[][][] data, boolean swaped, int handlerId) {
788 if (indexes.length != 0) {
789 addElement(indexes, new ScilabPolynomial(null, polyVarName, data, null, swaped));
791 handlers.get(handlerId).handle(new ScilabPolynomial(varName, polyVarName, data, null, swaped));
796 * Send complex polynomial matrix
801 * an integer array with the indexes of the (sub)*-list which
802 * will contain the data
804 * the polynomial variable name
810 * true if the matrix is stored row by row
814 public static final void sendPolynomial(String varName, int[] indexes, String polyVarName, double[][][] real, double[][][] img, boolean swaped,
816 if (indexes.length != 0) {
817 addElement(indexes, new ScilabPolynomial(null, polyVarName, real, img, swaped));
819 handlers.get(handlerId).handle(new ScilabPolynomial(varName, polyVarName, real, img, swaped));
824 * Send list, tlist and mlist
828 * @param nbItems number of element in the list
830 * an integer array with the indexes of the (sub)*-list which
831 * will contain the data
833 * a char which must take the values 'l' for list or 'm' for
834 * mlist or 't' for tlist
838 public static final void sendData(String varName, int nbItems, int[] indexes, char type, int handlerId) {
840 if (indexes.length == 0) {
844 ScilabType var = null;
847 var = new ScilabList(name, nbItems);
850 var = new ScilabMList(name, nbItems);
853 var = new ScilabTList(name, nbItems);
857 if (indexes.length == 0) {
858 lists.put(Thread.currentThread(), (ArrayList<ScilabType>) var);
860 addElement(indexes, var);
865 * Call when the list filling is finished a * @param indexes an integer
866 * array with the indexes of the (sub)*-list which will contain the data
868 * @param indexes to close
872 public static final void closeList(int[] indexes, int handlerId) {
873 Thread t = Thread.currentThread();
874 ArrayList<ScilabType> var = lists.get(t);
875 if (var != null && indexes.length == 0) {
876 handlers.get(handlerId).handle((ScilabType) var);
882 * Add an element to the list
885 * the indexes where to put the variable
887 * the variable to put
889 private static final void addElement(int[] indexes, ScilabType data) {
890 ArrayList<ScilabType> list = lists.get(Thread.currentThread());
892 for (int i = 0; i < indexes.length - 1; i++) {
893 list = (ArrayList<ScilabType>) list.get(indexes[i] - 1);
896 int n = indexes[indexes.length - 1] - 1;
897 if (n < list.size()) {
898 list.set(indexes[indexes.length - 1] - 1, data);