Skip to content

Commit

Permalink
Merge pull request #3550 from karlnapf/feature/meta_bool_arrays
Browse files Browse the repository at this point in the history
fix bool arrays in meta examples
  • Loading branch information
karlnapf committed Nov 18, 2016
2 parents 9d8c1e7 + 98baa52 commit 996a7e7
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 56 deletions.
8 changes: 4 additions & 4 deletions examples/meta/generator/targets/csharp.json
Expand Up @@ -15,7 +15,6 @@
"RealVector": "var $name = new double[$arguments]",
"LongRealVector": "var $name = new double[$arguments]",
"ComplexVector": "var $name = new Complex[$arguments]",
"BoolMatrix": "var $name = new bool[$arguments]",
"CharMatrix": "var $name = new char[$arguments]",
"ByteMatrix": "var $name = new byte[$arguments]",
"WordMatrix": "var $name = new ushort[$arguments]",
Expand Down Expand Up @@ -47,7 +46,6 @@
"RealVector": "double[]",
"LongRealVector": "double[]",
"ComplexVector": "Complex[]",
"BoolMatrix": "bool[,]",
"CharMatrix": "char[,]",
"ByteMatrix": "byte[,]",
"WordMatrix": "ushort[,]",
Expand Down Expand Up @@ -79,12 +77,14 @@
"Access": {
"Vector": "$identifier[$indices]",
"Matrix": "$identifier[$indices]",
"BoolVector": "$identifier.get_element($indices)"
"BoolVector": "$identifier.get_element($indices)",
"BoolMatrix": "$identifier.get_element($indices)"
},
"Assign": {
"Vector": "$identifier[$indices] = $expr",
"Matrix": "$identifier[$indices] = $expr",
"BoolVector": "$identifier.set_element($expr, $indices)"
"BoolVector": "$identifier.set_element($expr, $indices)",
"BoolMatrix": "$identifier.set_element($expr, $indices)"
},
"ZeroIndexed": true
},
Expand Down
12 changes: 6 additions & 6 deletions examples/meta/generator/targets/java.json
Expand Up @@ -11,7 +11,6 @@
"Init": {
"Construct": "$typeName $name = new $typeName($arguments)",
"Copy": "$typeName $name = $expr",
"BoolVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)",
"CharVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)",
"ByteVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)",
"WordVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)",
Expand All @@ -21,7 +20,6 @@
"ShortRealVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)",
"RealVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)",
"LongRealVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)",
"BoolMatrix": "DoubleMatrix $name = new DoubleMatrix($arguments)",
"CharMatrix": "DoubleMatrix $name = new DoubleMatrix($arguments)",
"ByteMatrix": "DoubleMatrix $name = new DoubleMatrix($arguments)",
"WordMatrix": "DoubleMatrix $name = new DoubleMatrix($arguments)",
Expand All @@ -42,7 +40,6 @@
"real": "double",
"string": "String",
"char": "char",
"BoolVector": "DoubleMatrix",
"CharVector": "DoubleMatrix",
"ByteVector": "DoubleMatrix",
"WordVector": "DoubleMatrix",
Expand All @@ -54,7 +51,6 @@
"RealVector": "DoubleMatrix",
"LongRealVector": "DoubleMatrix",
"ComplexVector": "DoubleMatrix",
"BoolMatrix": "DoubleMatrix",
"CharMatrix": "DoubleMatrix",
"ByteMatrix": "DoubleMatrix",
"WordMatrix": "DoubleMatrix",
Expand Down Expand Up @@ -85,11 +81,15 @@
"Element": {
"Access": {
"Vector": "$identifier.get($indices)",
"Matrix": "$identifier.get($indices)"
"Matrix": "$identifier.get($indices)",
"BoolVector": "$identifier.get_element($indices)",
"BoolMatrix": "$identifier.get_element($indices)"
},
"Assign": {
"Vector": "$identifier.put($indices, $expr)",
"Matrix": "$identifier.put($indices, $expr)"
"Matrix": "$identifier.put($indices, $expr)",
"BoolVector": "$identifier.set_element($expr, $indices)",
"BoolMatrix": "$identifier.set_element($expr, $indices)"
},
"ZeroIndexed": true
},
Expand Down
10 changes: 6 additions & 4 deletions examples/meta/generator/targets/octave.json
Expand Up @@ -5,7 +5,6 @@
"Init": {
"Construct": "$name = $typeName($arguments)",
"Copy": "$name = $expr",
"BoolVector": "$name = zeros(1, $arguments, 'int8')",
"CharVector": "$name = zeros(1, $arguments, 'char')",
"ByteVector": "$name = zeros(1, $arguments, 'int8')",
"WordVector": "$name = zeros(1, $arguments, 'int16')",
Expand All @@ -15,7 +14,6 @@
"ShortRealVector": "$name = zeros(1, $arguments, 'float32')",
"RealVector": "$name = zeros(1, $arguments, 'float64')",
"LongRealVector": "$name = zeros(1, $arguments, 'float64')",
"BoolMatrix": "$name = zeros($arguments, 'int8')",
"CharMatrix": "$name = zeros($arguments, 'char')",
"ByteMatrix": "$name = zeros($arguments, 'int8')",
"WordMatrix": "$name = zeros($arguments, 'int16')",
Expand Down Expand Up @@ -48,11 +46,15 @@
"Element": {
"Access": {
"Vector": "$identifier($indices)",
"Matrix": "$identifier($indices)"
"Matrix": "$identifier($indices)",
"BoolVector": "$identifier.get_element($indices)",
"BoolMatrix": "$identifier.get_element($indices)"
},
"Assign": {
"Vector": "$identifier($indices) = $expr",
"Matrix": "$identifier($indices) = $expr"
"Matrix": "$identifier($indices) = $expr",
"BoolVector": "$identifier.set_element($expr, $indices)",
"BoolMatrix": "$identifier.set_element($expr, $indices)"
},
"ZeroIndexed": false
},
Expand Down
14 changes: 8 additions & 6 deletions examples/meta/generator/targets/ruby.json
Expand Up @@ -5,7 +5,6 @@
"Init": {
"Construct": "$name = Modshogun::$typeName.new $arguments",
"Copy": "$name = $expr",
"BoolVector": "$name = NArray.byte($arguments)",
"CharVector": "$name = NArray.byte($arguments)",
"ByteVector": "$name = NArray.byte($arguments)",
"WordVector": "$name = NArray.byte($arguments)",
Expand All @@ -16,7 +15,6 @@
"RealVector": "$name = NArray.float($arguments)",
"LongRealVector": "$name = NArray.float($arguments)",
"ComplexVector": "$name = NArray.scomplex($arguments)",
"BoolMatrix": "$name = NMatrix.byte($arguments)",
"CharMatrix": "$name = NMatrix.float($arguments)",
"ByteMatrix": "$name = NMatrix.byte($arguments)",
"WordMatrix": "$name = NMatrix.byte($arguments)",
Expand All @@ -36,8 +34,8 @@
"StringLiteral": "\"$literal\"",
"CharLiteral": "'$literal'",
"BoolLiteral": {
"True": "1",
"False": "0"
"True": "true",
"False": "false"
},
"IntLiteral": "$number",
"RealLiteral": "$number",
Expand All @@ -50,11 +48,15 @@
"Element": {
"Access": {
"Vector": "$identifier[$indices]",
"Matrix": "$identifier[$indices]"
"Matrix": "$identifier[$indices]",
"BoolVector": "$identifier.get_element $indices",
"BoolMatrix": "$identifier.get_element $indices"
},
"Assign": {
"Vector": "$identifier[$indices] = $expr",
"Matrix": "$identifier[$indices] = $expr"
"Matrix": "$identifier[$indices] = $expr",
"BoolVector": "$identifier.set_element $expr, $indices",
"BoolMatrix": "$identifier.set_element $expr, $indices"
},
"ZeroIndexed": true
},
Expand Down
15 changes: 8 additions & 7 deletions examples/meta/src/meta_api/matrix_types.sg
@@ -1,11 +1,12 @@
BoolMatrix bool_matrix(2,3)
#bool_matrix[0,0]=False
#bool_matrix[1,0]=True
#bool_matrix[0,1]=False
#bool_matrix[1,1]=True
#bool_matrix[0,2]=False
#bool_matrix[1,2]=True
bool_matrix[0,0]=bool_matrix[1,0]
bool_matrix[0,0]=False
bool_matrix[1,0]=True
bool_matrix[0,1]=False
bool_matrix[1,1]=True
bool_matrix[0,2]=False
bool_matrix[1,2]=True
bool bool_element=bool_matrix[1,0]
bool_matrix[0,0]=bool_element

ByteMatrix byte_matrix(2,3)
byte_matrix[0,0]=0
Expand Down
7 changes: 4 additions & 3 deletions examples/meta/src/meta_api/vector_types.sg
@@ -1,7 +1,8 @@
BoolVector bool_vector(2)
#bool_vector[0]=False
#bool_vector[1]=True
bool_vector[0]=bool_vector[1]
bool_vector[0]=False
bool_vector[1]=True
bool bool_element=bool_vector[1]
bool_vector[0]=bool_element

ByteVector byte_vector(2)
byte_vector[0]=0
Expand Down
18 changes: 13 additions & 5 deletions src/interfaces/java_modular/swig_typemaps.i
Expand Up @@ -7,6 +7,14 @@
* Written (W) 2011 Baozeng Ding
*
*/

/*
* NOTE: Disabled boolean vector and matrix typemaps due to jblas is not able
* to represent them.
* Once we use a proper matrix library, can re-enable them again.
* See github issue 3554
* Heiko Strathmann
*/

%include <java/enums.swg>

Expand Down Expand Up @@ -148,7 +156,7 @@ import org.ujmp.core.booleanmatrix.impl.DefaultDenseBooleanMatrix2D;
%enddef

/*Define concrete examples of the TYPEMAP_SGVector macros */
TYPEMAP_SGVECTOR(bool, boolean, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
//TYPEMAP_SGVECTOR(bool, boolean, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR(char, byte, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR(uint8_t, byte, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGVECTOR(int16_t, short, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
Expand Down Expand Up @@ -282,7 +290,7 @@ TYPEMAP_SGVECTOR(float64_t, double, Double, jdouble, "()[D", "org/jblas/DoubleMa
%enddef

/*Define concrete examples of the TYPEMAP_SGVECTOR macros */
TYPEMAP_SGVECTOR(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V")
//TYPEMAP_SGVECTOR(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V")
TYPEMAP_SGVECTOR(char, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V")
TYPEMAP_SGVECTOR(uint8_t, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V")
TYPEMAP_SGVECTOR(int16_t, short, Short, jshort, "toShortArray", "()[[S", "org/ujmp/core/shortmatrix/impl/DefaultDenseShortMatrix2D", "([SII)V")
Expand Down Expand Up @@ -377,7 +385,7 @@ TYPEMAP_SGVECTOR(float64_t, double, Double, jdouble, "toDoubleArray", "()[[D", "
%enddef

/*Define concrete examples of the TYPEMAP_SGVECTOR_REF macros */
TYPEMAP_SGVECTOR_REF(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V")
//TYPEMAP_SGVECTOR_REF(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V")
TYPEMAP_SGVECTOR_REF(char, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V")
TYPEMAP_SGVECTOR_REF(uint8_t, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V")
TYPEMAP_SGVECTOR_REF(int16_t, short, Short, jshort, "toShortArray", "()[[S", "org/ujmp/core/shortmatrix/impl/DefaultDenseShortMatrix2D", "([SII)V")
Expand Down Expand Up @@ -491,7 +499,7 @@ TYPEMAP_SGVECTOR_REF(float64_t, double, Double, jdouble, "toDoubleArray", "()[[D
%enddef

/*Define concrete examples of the TYPEMAP_SGMATRIX macros */
TYPEMAP_SGMATRIX(bool, boolean, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
//TYPEMAP_SGMATRIX(bool, boolean, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGMATRIX(char, byte, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGMATRIX(uint8_t, byte, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
TYPEMAP_SGMATRIX(int16_t, short, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V")
Expand Down Expand Up @@ -608,7 +616,7 @@ TYPEMAP_SGMATRIX(float64_t, double, Double, jdouble, "()[D", "org/jblas/DoubleMa
%enddef

/*Define concrete examples of the TYPEMAP_SGMATRIX macros */
TYPEMAP_SGMATRIX(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V")
//TYPEMAP_SGMATRIX(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V")
TYPEMAP_SGMATRIX(char, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V")
TYPEMAP_SGMATRIX(uint8_t, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V")
TYPEMAP_SGMATRIX(int16_t, short, Short, jshort, "toShortArray", "()[[S", "org/ujmp/core/shortmatrix/impl/DefaultDenseShortMatrix2D", "([SII)V")
Expand Down
26 changes: 26 additions & 0 deletions src/shogun/lib/SGMatrix.h
Expand Up @@ -165,6 +165,32 @@ template<class T> class SGMatrix : public SGReferencedData
return matrix[index];
}

#endif // SWIG should skip this part

/** Get element at index
*
* @param row row index
* @param col column index
* @return element at index
*/
const T& get_element(index_t row, index_t col)
{
return (*this)(row, col);
}

/** Set element at index
*
* @param el element to set
* @param row row index
* @param col column index
*/
void set_element(const T& el, index_t row, index_t col)
{
(*this)(row, col)=el;
}

#ifndef SWIG // SWIG should skip this part

/**
* Get the matrix (no copying is done here)
*
Expand Down
14 changes: 0 additions & 14 deletions src/shogun/lib/SGVector.cpp
Expand Up @@ -239,20 +239,6 @@ void SGVector<complex128_t>::range_fill_vector(complex128_t* vec,
Not supported for complex128_t\n");
}

template<class T>
const T& SGVector<T>::get_element(index_t index)
{
REQUIRE(vector && (index>=0) && (index<vlen), "Provided index (%d) must be between 0 and %d.\n", index, vlen);
return vector[index];
}

template<class T>
void SGVector<T>::set_element(const T& p_element, index_t index)
{
REQUIRE(vector && (index>=0) && (index<vlen), "Provided index (%d) must be between 0 and %d.\n", index, vlen);
vector[index]=p_element;
}

template<class T>
void SGVector<T>::resize_vector(int32_t n)
{
Expand Down
19 changes: 12 additions & 7 deletions src/shogun/lib/SGVector.h
Expand Up @@ -161,20 +161,25 @@ template<class T> class SGVector : public SGReferencedData
static void random_vector(T* vec, int32_t len, T min_value, T max_value);
#endif // SWIG // SWIG should skip this part

/** Get vector element at index
/** Get element at index
*
* @param index index
* @return vector element at index
* @return element at index
*/
const T& get_element(index_t index);
const T& get_element(index_t index)
{
return (*this)[index];
}

/** Set vector element at index 'index' return false in case of trouble
/** Set element at index
*
* @param p_element vector element to set
* @param el element to set
* @param index index
* @return if setting was successful
*/
void set_element(const T& p_element, index_t index);
void set_element(const T& el, index_t index)
{
(*this)[index]=el;
}

#ifndef SWIG // SWIG should skip this part
/** Resize vector
Expand Down

0 comments on commit 996a7e7

Please sign in to comment.