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