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
132 public static final void sendDataAsBuffer(String varName, int[] indexes, DoubleBuffer data, int rows, int cols, int handlerId) {
133 if (indexes.length != 0) {
134 addElement(indexes, new ScilabDoubleReference(null, data, null, rows, cols));
136 handlers.get(handlerId).handle(new ScilabDoubleReference(varName, data, null, rows, cols));
141 * Send complex matrix
146 * an integer array with the indexes of the (sub)*-list which
147 * will contain the data
153 * true if the matrix is stored row by row
157 public static final void sendData(String varName, int[] indexes, double[][] real, double[][] img, boolean swaped, int handlerId) {
158 if (indexes.length != 0) {
159 addElement(indexes, new ScilabDouble(null, real, img, swaped));
161 handlers.get(handlerId).handle(new ScilabDouble(varName, real, img, swaped));
166 * Send complex matrix as DoubleBuffer
171 * an integer array with the indexes of the (sub)*-list which
172 * will contain the data
176 public static final void sendDataAsBuffer(String varName, int[] indexes, DoubleBuffer real, DoubleBuffer imag, int rows, int cols, int handlerId) {
177 if (indexes.length != 0) {
178 addElement(indexes, new ScilabDoubleReference(null, real, imag, rows, cols));
180 handlers.get(handlerId).handle(new ScilabDoubleReference(varName, real, imag, rows, cols));
190 * an integer array with the indexes of the (sub)*-list which
191 * will contain the data
195 * true if the matrix is stored row by row
199 public static final void sendData(String varName, int[] indexes, int[][] data, boolean swaped, int handlerId) {
200 if (indexes.length != 0) {
201 addElement(indexes, new ScilabInteger(null, data, false, swaped));
203 handlers.get(handlerId).handle(new ScilabInteger(varName, data, false, swaped));
208 * Send int32 matrix as IntBuffer
213 * an integer array with the indexes of the (sub)*-list which
214 * will contain the data
220 public static final void sendDataAsBuffer(String varName, int[] indexes, IntBuffer data, int rows, int cols, int handlerId) {
221 if (indexes.length != 0) {
222 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, false));
224 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, false));
234 * an integer array with the indexes of the (sub)*-list which
235 * will contain the data
239 * true if the matrix is stored row by row
243 public static final void sendUnsignedData(String varName, int[] indexes, short[][] data, boolean swaped, int handlerId) {
244 if (indexes.length != 0) {
245 addElement(indexes, new ScilabInteger(null, data, true, swaped));
247 handlers.get(handlerId).handle(new ScilabInteger(varName, data, true, swaped));
252 * Send uint16 matrix as ShortBuffer
257 * an integer array with the indexes of the (sub)*-list which
258 * will contain the data
264 public static final void sendUnsignedDataAsBuffer(String varName, int[] indexes, ShortBuffer data, int rows, int cols, int handlerId) {
265 if (indexes.length != 0) {
266 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, true));
268 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, true));
278 * an integer array with the indexes of the (sub)*-list which
279 * will contain the data
283 * true if the matrix is stored row by row
287 public static final void sendData(String varName, int[] indexes, short[][] data, boolean swaped, int handlerId) {
288 if (indexes.length != 0) {
289 addElement(indexes, new ScilabInteger(null, data, false, swaped));
291 handlers.get(handlerId).handle(new ScilabInteger(varName, data, false, swaped));
296 * Send int16 matrix as ShortBuffer
301 * an integer array with the indexes of the (sub)*-list which
302 * will contain the data
308 public static final void sendDataAsBuffer(String varName, int[] indexes, ShortBuffer data, int rows, int cols, int handlerId) {
309 if (indexes.length != 0) {
310 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, false));
312 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, false));
322 * an integer array with the indexes of the (sub)*-list which
323 * will contain the data
327 * true if the matrix is stored row by row
331 public static final void sendUnsignedData(String varName, int[] indexes, byte[][] data, boolean swaped, int handlerId) {
332 if (indexes.length != 0) {
333 addElement(indexes, new ScilabInteger(null, data, true, swaped));
335 handlers.get(handlerId).handle(new ScilabInteger(varName, data, true, swaped));
340 * Send uint8 matrix as ByteBuffer
345 * an integer array with the indexes of the (sub)*-list which
346 * will contain the data
352 public static final void sendUnsignedDataAsBuffer(String varName, int[] indexes, ByteBuffer data, int rows, int cols, int handlerId) {
353 if (indexes.length != 0) {
354 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, true));
356 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, true));
361 * Send boolean matrix
366 * an integer array with the indexes of the (sub)*-list which
367 * will contain the data
371 * true if the matrix is stored row by row
375 public static final void sendData(String varName, int[] indexes, boolean[][] data, boolean swaped, int handlerId) {
376 if (indexes.length != 0) {
377 addElement(indexes, new ScilabBoolean(null, data, swaped));
379 handlers.get(handlerId).handle(new ScilabBoolean(varName, data, swaped));
384 * Send boolean matrix as reference
389 * an integer array with the indexes of the (sub)*-list which
390 * will contain the data
396 public static final void sendBooleanDataAsBuffer(String varName, int[] indexes, IntBuffer data, int rows, int cols, int handlerId) {
397 if (indexes.length != 0) {
398 addElement(indexes, new ScilabBooleanReference(null, data, rows, cols));
400 handlers.get(handlerId).handle(new ScilabBooleanReference(varName, data, rows, cols));
410 * an integer array with the indexes of the (sub)*-list which
411 * will contain the data
415 * true if the matrix is stored row by row
419 public static final void sendData(String varName, int[] indexes, byte[][] data, boolean swaped, int handlerId) {
420 if (indexes.length != 0) {
421 addElement(indexes, new ScilabInteger(null, data, false, swaped));
423 handlers.get(handlerId).handle(new ScilabInteger(varName, data, false, swaped));
428 * Send int8 matrix as ByteBuffer
433 * an integer array with the indexes of the (sub)*-list which
434 * will contain the data
440 public static final void sendDataAsBuffer(String varName, int[] indexes, ByteBuffer data, int rows, int cols, int handlerId) {
441 if (indexes.length != 0) {
442 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, false));
444 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, false));
449 * Send int64 or uint64 matrix
454 * an integer array with the indexes of the (sub)*-list which
455 * will contain the data
459 * true if the matrix is stored row by row
463 public static final void sendData(String varName, int[] indexes, long[][] data, boolean swaped, int handlerId) {
464 if (indexes.length != 0) {
465 addElement(indexes, new ScilabInteger(null, data, false, swaped));
467 handlers.get(handlerId).handle(new ScilabInteger(varName, data, false, swaped));
472 * Send int64 or uint64 matrix as LongBuffer
477 * an integer array with the indexes of the (sub)*-list which
478 * will contain the data
484 public static final void sendDataAsBuffer(String varName, int[] indexes, LongBuffer data, int rows, int cols, int handlerId) {
485 if (indexes.length != 0) {
486 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, false));
488 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, false));
498 * an integer array with the indexes of the (sub)*-list which
499 * will contain the data
503 * true if the matrix is stored row by row
507 public static final void sendUnsignedData(String varName, int[] indexes, int[][] data, boolean swaped, int handlerId) {
508 if (indexes.length != 0) {
509 addElement(indexes, new ScilabInteger(null, data, true, swaped));
511 handlers.get(handlerId).handle(new ScilabInteger(varName, data, true, swaped));
516 * Send uint32 matrix as IntBuffer
521 * an integer array with the indexes of the (sub)*-list which
522 * will contain the data
528 public static final void sendUnsignedDataAsBuffer(String varName, int[] indexes, IntBuffer data, int rows, int cols, int handlerId) {
529 if (indexes.length != 0) {
530 addElement(indexes, new ScilabIntegerReference(null, data, rows, cols, true));
532 handlers.get(handlerId).handle(new ScilabIntegerReference(varName, data, rows, cols, true));
542 * an integer array with the indexes of the (sub)*-list which
543 * will contain the data
547 * true if the matrix is stored row by row
551 public static final void sendData(String varName, int[] indexes, String[][] data, boolean swaped, int handlerId) {
552 if (indexes.length != 0) {
553 addElement(indexes, new ScilabString(null, data, swaped));
555 handlers.get(handlerId).handle(new ScilabString(varName, data, swaped));
560 * Send double sparse matrix
565 * an integer array with the indexes of the (sub)*-list which
566 * will contain the data
572 * the number of non null elements
574 * the number by row of non null elements
576 * the column position of the non null elements
582 public static final void sendData(String varName, int[] indexes, int row, int col, int nbItem, int[] nbItemRow, int[] colPos, double[] data, int handlerId) {
583 if (indexes.length != 0) {
584 addElement(indexes, new ScilabSparse(null, row, col, nbItem, nbItemRow, colPos, data, null));
586 handlers.get(handlerId).handle(new ScilabSparse(varName, row, col, nbItem, nbItemRow, colPos, data, null));
591 * Send double sparse matrix as buffer
592 * Useless for now due to Scilab limitations (A=sparse(...);A(1,1)=123; a new A is created so references are lost)
597 * an integer array with the indexes of the (sub)*-list which
598 * will contain the data
604 * the number of non null elements
606 * the number by row of non null elements
608 * the column position of the non null elements
614 /*public static final void sendDataAsBuffer(String varName, int[] indexes, int row, int col, int nbItem, IntBuffer nbItemRow, IntBuffer colPos, DoubleBuffer data, int handlerId) {
615 if (indexes.length != 0) {
616 addElement(indexes, new ScilabSparseReference(null, row, col, nbItem, nbItemRow, colPos, data, null));
618 handlers.get(handlerId).handle(new ScilabSparseReference(varName, row, col, nbItem, nbItemRow, colPos, data, null));
623 * Send complex sparse matrix
628 * an integer array with the indexes of the (sub)*-list which
629 * will contain the data
635 * the number of non null elements
637 * the number by row of non null elements
639 * the column position of the non null elements
647 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) {
648 if (indexes.length != 0) {
649 addElement(indexes, new ScilabSparse(null, row, col, nbItem, nbItemRow, colPos, real, imag));
651 handlers.get(handlerId).handle(new ScilabSparse(varName, row, col, nbItem, nbItemRow, colPos, real, imag));
656 * Send complex sparse matrix as buffer
657 * Useless for now due to Scilab limitations (A=sparse(...);A(1,1)=123; a new A is created so references are lost)
662 * an integer array with the indexes of the (sub)*-list which
663 * will contain the data
669 * the number of non null elements
671 * the number by row of non null elements
673 * the column position of the non null elements
681 /*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) {
682 if (indexes.length != 0) {
683 addElement(indexes, new ScilabSparse(null, row, col, nbItem, nbItemRow, colPos, real, imag));
685 handlers.get(handlerId).handle(new ScilabSparse(varName, row, col, nbItem, nbItemRow, colPos, real, imag));
690 * Send boolean sparse matrix
691 * Useless for now due to Scilab limitations (A=sparse(...);A(1,1)=123; a new A is created so references are lost)
696 * an integer array with the indexes of the (sub)*-list which
697 * will contain the data
703 * the number of true elements
705 * the number by row of true elements
707 * the column position of the true elements
711 private static final void sendData(String varName, int[] indexes, int row, int col, int nbItem, int[] nbItemRow, int[] colPos, int handlerId) {
712 if (indexes.length != 0) {
713 addElement(indexes, new ScilabBooleanSparse(null, row, col, nbItem, nbItemRow, colPos));
715 handlers.get(handlerId).handle(new ScilabBooleanSparse(varName, row, col, nbItem, nbItemRow, colPos));
720 * Send boolean sparse matrix as Buffer
725 * an integer array with the indexes of the (sub)*-list which
726 * will contain the data
732 * the number of true elements
734 * the number by row of true elements
736 * the column position of the true elements
740 /*public static final void sendDataAsBuffer(String varName, int[] indexes, int row, int col, int nbItem, IntBuffer nbItemRow, IntBuffer colPos, int handlerId) {
741 if (indexes.length != 0) {
742 addElement(indexes, new ScilabBooleanSparse(null, row, col, nbItem, nbItemRow, colPos));
744 handlers.get(handlerId).handle(new ScilabBooleanSparse(varName, row, col, nbItem, nbItemRow, colPos));
749 * Send double polynomial matrix
754 * an integer array with the indexes of the (sub)*-list which
755 * will contain the data
757 * the polynomial variable name
761 * true if the matrix is stored row by row
765 public static final void sendPolynomial(String varName, int[] indexes, String polyVarName, double[][][] data, boolean swaped, int handlerId) {
766 if (indexes.length != 0) {
767 addElement(indexes, new ScilabPolynomial(null, polyVarName, data, null, swaped));
769 handlers.get(handlerId).handle(new ScilabPolynomial(varName, polyVarName, data, null, swaped));
774 * Send complex polynomial matrix
779 * an integer array with the indexes of the (sub)*-list which
780 * will contain the data
782 * the polynomial variable name
788 * true if the matrix is stored row by row
792 public static final void sendPolynomial(String varName, int[] indexes, String polyVarName, double[][][] real, double[][][] img, boolean swaped,
794 if (indexes.length != 0) {
795 addElement(indexes, new ScilabPolynomial(null, polyVarName, real, img, swaped));
797 handlers.get(handlerId).handle(new ScilabPolynomial(varName, polyVarName, real, img, swaped));
802 * Send list, tlist and mlist
807 * an integer array with the indexes of the (sub)*-list which
808 * will contain the data
810 * a char which must take the values 'l' for list or 'm' for
811 * mlist or 't' for tlist
815 public static final void sendData(String varName, int nbItems, int[] indexes, char type, int handlerId) {
817 if (indexes.length == 0) {
821 ScilabType var = null;
824 var = new ScilabList(name, nbItems);
827 var = new ScilabMList(name, nbItems);
830 var = new ScilabTList(name, nbItems);
834 if (indexes.length == 0) {
835 lists.put(Thread.currentThread(), (ArrayList<ScilabType>) var);
837 addElement(indexes, var);
842 * Call when the list filling is finished a * @param indexes an integer
843 * array with the indexes of the (sub)*-list which will contain the data
848 public static final void closeList(int[] indexes, int handlerId) {
849 Thread t = Thread.currentThread();
850 ArrayList<ScilabType> var = lists.get(t);
851 if (var != null && indexes.length == 0) {
852 handlers.get(handlerId).handle((ScilabType) var);
858 * Add an element to the list
861 * the indexes where to put the variable
863 * the variable to put
865 private static final void addElement(int[] indexes, ScilabType data) {
866 ArrayList<ScilabType> list = lists.get(Thread.currentThread());
868 for (int i = 0; i < indexes.length - 1; i++) {
869 list = (ArrayList<ScilabType>) list.get(indexes[i] - 1);
872 int n = indexes[indexes.length - 1] - 1;
873 if (n < list.size()) {
874 list.set(indexes[indexes.length - 1] - 1, data);