SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SGNDArray.cpp
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 #include <shogun/lib/common.h>
00014 #include <shogun/lib/SGNDArray.h>
00015 #include <shogun/lib/SGReferencedData.h>
00016 
00017 namespace shogun
00018 {
00019 
00020 template<class T> SGNDArray<T>::SGNDArray() :
00021     SGReferencedData()
00022 {
00023     init_data();
00024 }
00025 
00026 template<class T> SGNDArray<T>::SGNDArray(T* a, index_t* d, index_t nd, bool ref_counting) :
00027     SGReferencedData(ref_counting)
00028 {
00029     array = a;
00030     dims = d;
00031     num_dims = nd;
00032 }
00033 
00034 template<class T> SGNDArray<T>::SGNDArray(index_t* d, index_t nd, bool ref_counting) :
00035     SGReferencedData(ref_counting), dims(d), num_dims(nd)
00036 {
00037     int64_t total = 1;
00038     for (int32_t i=0; i<num_dims; i++)
00039         total *= dims[i];
00040     ASSERT(total>0)
00041     array = SG_MALLOC(T, total);
00042 }
00043 
00044 template<class T> SGNDArray<T>::SGNDArray(const SGNDArray &orig) :
00045     SGReferencedData(orig)
00046 {
00047     copy_data(orig);
00048 }
00049 
00050 template<class T> SGNDArray<T>::~SGNDArray()
00051 {
00052     unref();
00053 }
00054 
00055 template<class T> void SGNDArray<T>::copy_data(const SGReferencedData &orig)
00056 {
00057     array = ((SGNDArray*)(&orig))->array;
00058     dims = ((SGNDArray*)(&orig))->dims;
00059     num_dims = ((SGNDArray*)(&orig))->num_dims;
00060 }
00061 
00062 template<class T> void SGNDArray<T>::init_data()
00063 {
00064     array = NULL;
00065     dims = NULL;
00066     num_dims = 0;
00067 }
00068 
00069 template<class T> void SGNDArray<T>::free_data()
00070 {
00071     SG_FREE(array);
00072     SG_FREE(dims);
00073 
00074     array     = NULL;
00075     dims      = NULL;
00076     num_dims  = 0;
00077 }
00078 
00079 template<class T> void SGNDArray<T>::transpose_matrix(index_t matIdx) const
00080 {
00081     ASSERT(array && dims && num_dims > 2 && dims[2] > matIdx)
00082 
00083     T aux;
00084     // Index to acces directly the elements of the matrix of interest
00085     int64_t idx = int64_t(matIdx)*int64_t(dims[0])*dims[1];
00086 
00087     for (int64_t i=0; i<dims[0]; i++)
00088         for (int64_t j=0; j<i-1; j++)
00089         {
00090             aux = array[idx + i + j*dims[0]];
00091             array[idx + i + j*dims[0]] = array[idx + j + i*dims[0]];
00092             array[idx + j + i*dims[1]] = aux;
00093         }
00094 
00095     // Swap the sizes of the two first dimensions
00096     index_t auxDim = dims[0];
00097     dims[0] = dims[1];
00098     dims[1] = auxDim;
00099 }
00100 
00101 template class SGNDArray<bool>;
00102 template class SGNDArray<char>;
00103 template class SGNDArray<int8_t>;
00104 template class SGNDArray<uint8_t>;
00105 template class SGNDArray<int16_t>;
00106 template class SGNDArray<uint16_t>;
00107 template class SGNDArray<int32_t>;
00108 template class SGNDArray<uint32_t>;
00109 template class SGNDArray<int64_t>;
00110 template class SGNDArray<uint64_t>;
00111 template class SGNDArray<float32_t>;
00112 template class SGNDArray<float64_t>;
00113 template class SGNDArray<floatmax_t>;
00114 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation