SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SGNDArray.h
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2012 Fernando José Iglesias García
00008  * Written (W) 2010,2012 Soeren Sonnenburg
00009  * Copyright (C) 2010 Berlin Institute of Technology
00010  * Copyright (C) 2012 Soeren Sonnenburg
00011  */
00012 
00013 #ifndef __SGNDARRAY_H__
00014 #define __SGNDARRAY_H__
00015 
00016 #include <shogun/lib/config.h>
00017 #include <shogun/lib/DataType.h>
00018 
00019 namespace shogun
00020 {
00022 template<class T> class SGNDArray
00023 {
00024     public:
00026         SGNDArray() : array(NULL), dims(NULL), num_dims(0), do_free(false) { }
00027 
00029         SGNDArray(T* a, index_t* d, index_t nd, bool do_free_ndarray = false)
00030             : array(a), dims(d), num_dims(nd), do_free(do_free_ndarray) { }
00031 
00033         SGNDArray(index_t* d, index_t nd, bool do_free_ndarray = false)
00034             : dims(d), num_dims(nd), do_free(do_free_ndarray)
00035         {
00036             index_t tot = 1;
00037             for (int32_t i=0; i<nd; i++)
00038                 tot *= dims[i];
00039             array=SG_MALLOC(T, tot);
00040         }
00041 
00043         SGNDArray(const SGNDArray &orig)
00044             : array(orig.array), dims(orig.dims), num_dims(orig.num_dims),
00045             do_free(orig.do_free) { }
00046 
00048         virtual ~SGNDArray()
00049         {
00050         }
00051 
00053         virtual void free_ndarray()
00054         {
00055             if (do_free)
00056                 SG_FREE(array);
00057 
00058             SG_FREE(dims);
00059 
00060             array     = NULL;
00061             dims      = NULL;
00062             num_dims  = 0;
00063         }
00064 
00065 
00067         virtual void destroy_ndarray()
00068         {
00069             do_free = true;
00070             free_ndarray();
00071         }
00072 
00078         T* get_matrix(index_t matIdx) const
00079         {
00080             ASSERT(array && dims && num_dims > 2 && dims[2] > matIdx);
00081             return &array[matIdx*dims[0]*dims[1]];
00082         }
00083 
00088         inline const T& operator[](index_t index) const
00089         {
00090             return array[index];
00091         }
00092 
00097         inline T& operator[](index_t index)
00098         {
00099             return array[index];
00100         }
00101 
00106         void transpose_matrix(index_t matIdx) const
00107         {
00108             ASSERT(array && dims && num_dims > 2 && dims[2] > matIdx);
00109 
00110             T aux;
00111             // Index to acces directly the elements of the matrix of interest
00112             int32_t idx = matIdx*dims[0]*dims[1];
00113 
00114             for (int32_t i=0; i<dims[0]; i++)
00115                 for (int32_t j=0; j<i-1; j++)
00116                 {
00117                     aux = array[idx + i + j*dims[0]];
00118                     array[idx + i + j*dims[0]] = array[idx + j + i*dims[0]];
00119                     array[idx + j + i*dims[1]] = aux;
00120                 }
00121 
00122             // Swap the sizes of the two first dimensions
00123             index_t auxDim = dims[0];
00124             dims[0] = dims[1];
00125             dims[1] = auxDim;
00126         }
00127 
00128     public:
00130         T* array;
00132         index_t* dims;
00134         index_t num_dims;
00136         bool do_free;
00137 };
00138 }
00139 #endif // __SGNDARRAY_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation