Skip to content

Commit

Permalink
for the current way of serialization we need to split Serializable cl…
Browse files Browse the repository at this point in the history
…asses for container types
  • Loading branch information
vigsterkr committed Feb 22, 2018
1 parent ea17588 commit 1b47dc2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 28 deletions.
29 changes: 18 additions & 11 deletions examples/meta/generator/translate.py
Expand Up @@ -81,7 +81,7 @@ def getSGTypesToStore():
def getSGTypeToStoreMethodName(sgType):
""" Translates given SG* type into meta language type """
assert sgType in getSGTypesToStore()

if sgType=="RealVector":
return "real_vector"
elif sgType=="FloatVector":
Expand All @@ -90,10 +90,10 @@ def getSGTypeToStoreMethodName(sgType):
return "real_matrix"
elif sgType=="FloatMatrix":
return "float_matrix"

else:
raise RuntimeError("Given Shogun type \"%s\" cannot be translated to meta type", sgType)


def getVarsToStore(program):
""" Extracts all variables in program that should be stored """
Expand Down Expand Up @@ -293,18 +293,25 @@ def injectVarsStoring(self, statementList, programName, varsToStore):
sgType = vartypeAST[list(vartypeAST.keys())[0]]
assert sgType in getBasicTypesToStore() or sgType in getSGTypesToStore()

appendElementIdentifier = "append_element"
if "cpp" in self.targetDict["FileExtension"]:
methodNameSuffix = "<%s>" % self.targetDict["Type"][sgType]
if sgType.endswith("Vector"):
suffix = self.targetDict["Type"][sgType].replace("SGVector", "")
elif sgType.endswith("Matrix"):
suffix = self.targetDict["Type"][sgType].replace("SGMatrix", "")
else:
suffix = "<%s>" % self.targetDict["Type"][sgType]
else:
suffix = "_"
if sgType in getBasicTypesToStore():
methodNameSuffix = sgType
suffix += sgType
elif sgType in getSGTypesToStore():
methodNameSuffix = getSGTypeToStoreMethodName(sgType)
methodNameSuffix = "_%s" % methodNameSuffix
suffix += getSGTypeToStoreMethodName(sgType)
appendElementIdentifier += suffix

methodCall = {
"MethodCall": [{"Identifier": storage},
{"Identifier": "append_element%s" % methodNameSuffix},
{"Identifier": appendElementIdentifier},
{"ArgumentList": [varnameIdentifierExpr,
varnameExpr]}]
}
Expand Down Expand Up @@ -489,7 +496,7 @@ def translateInit(self, init):
)

normalArgs = [
arg for arg in initialisation["ArgumentList"]
arg for arg in initialisation["ArgumentList"]
if not "KeywordArgument" in arg
]
kwargsString = self.translateKwargs(
Expand Down Expand Up @@ -560,7 +567,7 @@ def translateExpr(self, expr, returnKwargs=False):
elif key == "StringLiteral":
template = Template(self.targetDict["Expr"]["StringLiteral"])
return template.substitute(literal=expr[key])

elif key == "CharLiteral":
template = Template(self.targetDict["Expr"]["CharLiteral"])
return template.substitute(literal=expr[key])
Expand Down Expand Up @@ -651,7 +658,7 @@ def translateGlobalCall(self, globalCall, returnKwargs):
)

normalArgs = [
arg for arg in argsList["ArgumentList"]
arg for arg in argsList["ArgumentList"]
if not "KeywordArgument" in arg
]
kwargsString = self.translateKwargs(
Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/swig/Library.i
Expand Up @@ -402,9 +402,9 @@ namespace shogun
{
/* Specialize DynamicObjectArray::append_element function */
#ifdef USE_FLOAT64
%template(append_element_real_vector) CDynamicObjectArray::append_element<float64_t>;
%template(append_element_real_matrix) CDynamicObjectArray::append_element<float64_t>;
%template(append_element_real) CDynamicObjectArray::append_element<float64_t, float64_t>;
%template(append_element_real_vector) CDynamicObjectArray::append_element<SGVector<float64_t>, SGVector<float64_t>>;
%template(append_element_real_matrix) CDynamicObjectArray::append_element<SGMatrix<float64_t>, SGMatrix<float64_t>>;
#ifdef SWIGOCTAVE
/* (Octave converts single element arrays to scalars and our typemaps take that for real) */
%extend CDynamicObjectArray {
Expand All @@ -418,9 +418,9 @@ namespace shogun
#endif
#endif
#ifdef USE_FLOAT32
%template(append_element_float_vector) CDynamicObjectArray::append_element<float32_t>;
%template(append_element_float_matrix) CDynamicObjectArray::append_element<float32_t>;
%template(append_element_float) CDynamicObjectArray::append_element<float32_t, float32_t>;
%template(append_element_float_vector) CDynamicObjectArray::append_element<SGVector<float32_t>, SGVector<float32_t>>;
%template(append_element_float_matrix) CDynamicObjectArray::append_element<SGMatrix<float32_t>, SGMatrix<float32_t>>;
#endif
#ifdef USE_INT32
%template(append_element_int) CDynamicObjectArray::append_element<int32_t, int32_t>;
Expand Down
37 changes: 31 additions & 6 deletions src/shogun/io/Serializable.h
Expand Up @@ -71,13 +71,20 @@ template<class T> class CSerializable: public CSGObject
* @param value Value to serialize as CSGObject.
* @param value_name Name under which value is registered.
*/
CSerializable(T value, const char* value_name="")
CSerializable(T value, const char* value_name=""): CSGObject()
{
init();
m_value = value;
m_value_name = value_name;
//FIXME: add support for std::string serialization
}

/**
* Get stored value
*
* @return stored value
*/
virtual T get_value() const { return m_value; }

/** @return name of the CSGObject, without C prefix */
virtual const char* get_name() const { return "Serializable"; }

Expand All @@ -86,18 +93,36 @@ template<class T> class CSerializable: public CSGObject
{
set_generic<typename extract_value_type<T>::value_type>();
m_value = 0;
m_value_name = "Unnamed";

SG_ADD(&m_value, "value", "Serialized value", MS_NOT_AVAILABLE);
}

protected:
/** Serialized value. */
T m_value;
};

// FIXME: once the class factory is refactored this should be dropped and
// CSerializable should be use directly
template<class T> class CVectorSerializable: public CSerializable<SGVector<T>>
{
public:
CVectorSerializable() : CSerializable<SGVector<T>>() {}
CVectorSerializable(SGVector<T> value, const char* value_name=""): CSerializable<SGVector<T>>(value, value_name) {}
virtual ~CVectorSerializable() {}

/** Name of serialized value */
const char* m_value_name;
/** @return name of the CSGObject, without C prefix */
virtual const char* get_name() const { return "VectorSerializable"; }
};

template<class T> class CMatrixSerializable: public CSerializable<SGMatrix<T>>
{
public:
CMatrixSerializable() : CSerializable<SGMatrix<T>>() {}
CMatrixSerializable(SGMatrix<T> value, const char* value_name=""): CSerializable<SGMatrix<T>>(value, value_name) {}
virtual ~CMatrixSerializable() {}

/** @return name of the CSGObject, without C prefix */
virtual const char* get_name() const { return "MatrixSerializable"; }
};
};
#endif // SHOGUN_SERIALIZABLE_H_
24 changes: 17 additions & 7 deletions src/shogun/lib/DynamicObjectArray.h
@@ -1,8 +1,8 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Heiko Strathmann, Evgeniy Andreev,
* Sergey Lisitsyn, Leon Kuchenbecker, Yuyu Zhang, Thoralf Klein,
* Authors: Soeren Sonnenburg, Heiko Strathmann, Evgeniy Andreev,
* Sergey Lisitsyn, Leon Kuchenbecker, Yuyu Zhang, Thoralf Klein,
* Fernando Iglesias, Björn Esser
*/

Expand Down Expand Up @@ -277,15 +277,25 @@ class CDynamicObjectArray : public CSGObject
return success;
}

template <typename T, typename T2 = typename std::enable_if<!std::is_base_of<CSGObject, typename std::remove_pointer<T>::type>::value, T>::type>
template <typename T, typename T2 = typename std::enable_if_t<std::is_arithmetic<T>::value>>
inline bool append_element(T e, const char* name="")
{
auto serialized_element = new CSerializable<T>(e, name);
bool success = m_array.append_element(serialized_element);
if (success)
SG_REF(serialized_element);
return append_element(serialized_element);
}

return success;
template <typename T>
inline bool append_element(SGVector<T> e, const char* name="")
{
auto serialized_element = new CVectorSerializable<T>(e, name);
return append_element(serialized_element);
}

template <typename T>
inline bool append_element(SGMatrix<T> e, const char* name="")
{
auto serialized_element = new CMatrixSerializable<T>(e, name);
return append_element(serialized_element);
}

/** append array element to the end of array
Expand Down

0 comments on commit 1b47dc2

Please sign in to comment.