Skip to content

Commit

Permalink
Merge pull request #523 from gsomix/Arrays
Browse files Browse the repository at this point in the history
CArray* to CDynamicArray conversion
  • Loading branch information
Soeren Sonnenburg committed May 15, 2012
2 parents cb50eab + ab548e1 commit a71d99e
Show file tree
Hide file tree
Showing 18 changed files with 666 additions and 1,287 deletions.
53 changes: 0 additions & 53 deletions src/interfaces/modular/Library.i
Expand Up @@ -20,11 +20,6 @@
%ignore TRIE_TERMINAL_CHARACTER;
%ignore NO_CHILD;

#pragma SWIG nowarn=312,362,389
%warnfilter(509) CArray;
%warnfilter(509) CArray2;
%warnfilter(509) CArray3;

/* Templated Datatype Classes */
%include <shogun/lib/DataType.h>
%include <shogun/lib/SGReferencedData.h>
Expand Down Expand Up @@ -443,54 +438,6 @@ namespace shogun
/* Hash */
%include <shogun/lib/Hash.h>

/* Template Class Array */
/*%include <shogun/lib/Array.h>
namespace shogun
{
%template(CharArray) CArray<char>;
%template(ByteArray) CArray<uint8_t>;
%template(ShortArray) CArray<int16_t>;
%template(WordArray) CArray<uint16_t>;
%template(IntArray) CArray<int32_t>;
%template(UIntArray) CArray<uint32_t>;
%template(LongArray) CArray<int64_t>;
%template(ULongArray) CArray<uint64_t>;
%template(ShortRealArray) CArray<float32_t>;
%template(RealArray) CArray<float64_t>;
}*/

/* Template Class Array2 */
/*%include <shogun/lib/Array2.h>
namespace shogun
{
%template(CharArray2) CArray2<char>;
%template(ByteArray2) CArray2<uint8_t>;
%template(ShortArray2) CArray2<int16_t>;
%template(WordArray2) CArray2<uint16_t>;
%template(IntArray2) CArray2<int32_t>;
%template(UIntArray2) CArray2<uint32_t>;
%template(LongArray2) CArray2<int64_t>;
%template(ULongArray2) CArray2<uint64_t>;
%template(ShortRealArray2) CArray2<float32_t>;
%template(RealArray2) CArray2<float64_t>;
}*/

/* Template Class Array3 */
/*%include <shogun/lib/Array3.h>
namespace shogun
{
%template(CharArray3) CArray3<char>;
%template(ByteArray3) CArray3<uint8_t>;
%template(ShortArray3) CArray3<int16_t>;
%template(WordArray3) CArray3<uint16_t>;
%template(IntArray3) CArray3<int32_t>;
%template(UIntArray3) CArray3<uint32_t>;
%template(LongArray3) CArray3<int64_t>;
%template(ULongArray3) CArray3<uint64_t>;
%template(ShortRealArray3) CArray3<float32_t>;
%template(RealArray3) CArray3<float64_t>;
}*/

%include <shogun/lib/Cache.h>
%include <shogun/lib/List.h>
%include <shogun/lib/Signal.h>
Expand Down
3 changes: 0 additions & 3 deletions src/interfaces/modular/Library_includes.i
Expand Up @@ -16,9 +16,6 @@
#include <shogun/lib/DynamicArray.h>
#include <shogun/structure/PlifBase.h>
#include <shogun/lib/Hash.h>
#include <shogun/lib/Array.h>
#include <shogun/lib/Array2.h>
#include <shogun/lib/Array3.h>
#include <shogun/lib/GCArray.h>
#include <shogun/lib/Compressor.h>
%}
119 changes: 95 additions & 24 deletions src/shogun/base/DynArray.h
Expand Up @@ -6,6 +6,7 @@
*
* Written (W) 1999-2009 Soeren Sonnenburg
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
* Copyright (C) 2012 Engeniy Andreev (gsomix)
*/

#ifndef _DYNARRAY_H_
Expand Down Expand Up @@ -41,7 +42,8 @@ template <class T> class DynArray
*/
DynArray(int32_t p_resize_granularity=128, bool tracable=true)
{
this->resize_granularity=p_resize_granularity;
resize_granularity=p_resize_granularity;
free_array=true;
use_sg_mallocs=tracable;

if (use_sg_mallocs)
Expand All @@ -53,13 +55,36 @@ template <class T> class DynArray
last_element_idx=-1;
}

DynArray(T* p_array, int32_t p_array_size, bool p_free_array=true, bool p_copy_array=false, bool tracable=true)
{
resize_granularity=p_array_size;
free_array=false;
use_sg_mallocs=tracable;

array=NULL;
set_array(p_array, p_array_size, p_array_size, p_free_array, p_copy_array);
}

DynArray(const T* p_array, int32_t p_array_size, bool tracable=true)
{
resize_granularity=p_array_size;
free_array=false;
use_sg_mallocs=tracable;

array=NULL;
set_array(p_array, p_array_size, p_array_size);
}

/** destructor */
virtual ~DynArray()
{
if (use_sg_mallocs)
SG_FREE(array);
else
free(array);
if (array!=NULL && free_array)
{
if (use_sg_mallocs)
SG_FREE(array);
else
free(array);
}
}

/** set the resize granularity
Expand Down Expand Up @@ -167,7 +192,7 @@ template <class T> class DynArray
}
else
{
if (resize_array(index))
if (free_array && resize_array(index))
return set_element(element, index);
else
return false;
Expand Down Expand Up @@ -293,27 +318,27 @@ template <class T> class DynArray
*/
bool resize_array(int32_t n)
{
int32_t new_num_elements= ((n/resize_granularity)+1)
int32_t new_num_elements=((n/resize_granularity)+1)
*resize_granularity;

T* p;

if (use_sg_mallocs)
p = SG_REALLOC(T, array, new_num_elements);
else
p = (T*) realloc(array, new_num_elements*sizeof(T));
if (use_sg_mallocs)
p = SG_REALLOC(T, array, new_num_elements);
else
p = (T*) realloc(array, new_num_elements*sizeof(T));
if (p)
{
array=p;
if (new_num_elements > num_elements)
{
memset(&array[num_elements], 0,
(new_num_elements-num_elements)*sizeof(T));
(new_num_elements-num_elements)*sizeof(T));
}
else if (n+1<new_num_elements)
{
memset(&array[n+1], 0,
(new_num_elements-n-1)*sizeof(T));
(new_num_elements-n-1)*sizeof(T));
}

//in case of shrinking we must adjust last element idx
Expand Down Expand Up @@ -346,12 +371,48 @@ template <class T> class DynArray
* @param array_size number of elements in array
*/
inline void set_array(T* p_array, int32_t p_num_elements,
int32_t array_size)
int32_t p_array_size, bool p_free_array=true, bool copy_array=false)
{
if (array!=NULL && free_array)
SG_FREE(array);

if (copy_array)
{
if (use_sg_mallocs)
array=SG_MALLOC(T, p_array_size);
else
array=(T*) malloc(p_array_size*sizeof(T));
memcpy(array, p_array, p_array_size*sizeof(T));
}
else
array=p_array;

num_elements=p_array_size;
last_element_idx=p_num_elements-1;
free_array=p_free_array;
}

/** set the array pointer and free previously allocated memory
*
* @param p_array new array
* @param p_num_elements last element index + 1
* @param array_size number of elements in array
*/
inline void set_array(const T* p_array, int32_t p_num_elements,
int32_t p_array_size)
{
SG_FREE(this->array);
this->array=p_array;
this->num_elements=array_size;
this->last_element_idx=p_num_elements-1;
if (array!=NULL && free_array)
SG_FREE(array);

if (use_sg_mallocs)
array=SG_MALLOC(T, p_array_size);
else
array=(T*) malloc(p_array_size*sizeof(T));
memcpy(array, p_array, p_array_size*sizeof(T));

num_elements=p_array_size;
last_element_idx=p_num_elements-1;
free_array=true;
}

/** clear the array (with zeros) */
Expand All @@ -375,6 +436,13 @@ template <class T> class DynArray
CMath::swap(array[i], array[CMath::random(i, last_element_idx)]);
}

/** */
void set_const(const T& const_element)
{
for (int32_t i=0; i<num_elements; i++)
array[i]=const_element;
}

/** operator overload for array read only access
* use set_element() for write access (will also make the array
* dynamically grow)
Expand Down Expand Up @@ -404,10 +472,10 @@ template <class T> class DynArray
{
SG_FREE(array);

if (use_sg_mallocs)
array=SG_MALLOC(T, orig.num_elements);
else
array=(T*) malloc(sizeof(T)*orig.num_elements);
if (use_sg_mallocs)
array=SG_MALLOC(T, orig.num_elements);
else
array=(T*) malloc(sizeof(T)*orig.num_elements);
}

memcpy(array, orig.array, sizeof(T)*orig.num_elements);
Expand All @@ -433,8 +501,11 @@ template <class T> class DynArray
/** the element in the array that has largest index */
int32_t last_element_idx;

/** whether SG_MALLOC or just malloc etc shall be used */
bool use_sg_mallocs;
/** whether SG_MALLOC or just malloc etc shall be used */
bool use_sg_mallocs;

/** */
bool free_array;
};
}
#endif /* _DYNARRAY_H_ */
4 changes: 2 additions & 2 deletions src/shogun/kernel/SpectrumMismatchRBFKernel.cpp
Expand Up @@ -279,9 +279,9 @@ void CSpectrumMismatchRBFKernel::compute_helper_all(const char *joint_seq,
}
else
{
CArray<float64_t> feats ;
CDynamicArray<float64_t> feats ;
feats.resize_array(kernel_matrix.get_dim1()) ;
feats.zero() ;
feats.set_const(0) ;

for (unsigned int j=0; j<joint_list_.size(); j++)
{
Expand Down
5 changes: 2 additions & 3 deletions src/shogun/kernel/SpectrumMismatchRBFKernel.h
Expand Up @@ -18,8 +18,7 @@
#include <shogun/features/StringFeatures.h>


#include <shogun/lib/Array.h>
#include <shogun/lib/Array2.h>
#include <shogun/lib/DynamicArray.h>
#include <string>
#include <vector>

Expand Down Expand Up @@ -211,7 +210,7 @@ class CSpectrumMismatchRBFKernel: public CStringKernel<char>
bool initialized;

/** kernel matrix */
CArray2<float64_t> kernel_matrix ;
CDynamicArray<float64_t> kernel_matrix ; // 2d
/** kernel matrix length */
int32_t kernel_matrix_length;
/** target letter 0 */
Expand Down
5 changes: 2 additions & 3 deletions src/shogun/kernel/SpectrumRBFKernel.h
Expand Up @@ -18,8 +18,7 @@
#include <shogun/features/StringFeatures.h>


#include <shogun/lib/Array.h>
#include <shogun/lib/Array2.h>
#include <shogun/lib/DynamicArray.h>

#include <vector> // profile
#include <string> // profile
Expand Down Expand Up @@ -171,7 +170,7 @@ class CSpectrumRBFKernel: public CStringKernel<char>
/** if kernel is initialized */
bool initialized;
/** kernel matrix */
CArray2<float64_t> kernel_matrix;
CDynamicArray<float64_t> kernel_matrix; // 2d
/** target letter 0 */
int32_t target_letter_0;

Expand Down

0 comments on commit a71d99e

Please sign in to comment.