/** * Encode the double data * * @param var the data to encode * @param vec the resulting buffer */ private void encode(ScilabDouble var, VectorOfDouble vec) { // Header encodeHeader(var, vec, ScilabTypeEnum.sci_matrix); // specific flag for managing the complex case if (var.isReal()) { vec.add(0f); } else { vec.add(1f); } // push the data for (int i = 0; i < var.getHeight(); i++) { for (int j = 0; j < var.getWidth(); j++) { vec.add(var.getRealElement(i, j)); } } // push the complex data if (!var.isReal()) { for (int i = 0; i < var.getHeight(); i++) { for (int j = 0; j < var.getWidth(); j++) { vec.add(var.getImaginaryElement(i, j)); } } } }
/** * Helper method to add an header of the detected type * * @param var the scilab matrix type to encode * @param vec the raw encoded data container * @param as the type to encode * @param detected the detected type */ @SuppressWarnings({"unchecked", "fallthrough"}) private void encodeHeader(Object var, VectorOfDouble vec, final ScilabTypeEnum as) { ScilabType matrix = null; ArrayList<ScilabType> list = null; // defensive programming switch (as) { case sci_boolean: case sci_ints: case sci_matrix: case sci_strings: matrix = (ScilabType) var; break; case sci_list: case sci_mlist: case sci_tlist: list = (ArrayList<ScilabType>) var; break; default: throw new IllegalArgumentException(); } vec.add(as.swigValue()); if (matrix instanceof ScilabInteger) { vec.add(((ScilabInteger) matrix).getPrec().swigValue()); } if (matrix != null) { vec.add(2); vec.add(matrix.getHeight()); vec.add(matrix.getWidth()); } else if (list != null) { vec.add(list.size()); } else { throw new IllegalArgumentException(); } }