SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGNDArray.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2012 Fernando José Iglesias García
8  * Written (W) 2010,2012 Soeren Sonnenburg
9  * Copyright (C) 2010 Berlin Institute of Technology
10  * Copyright (C) 2012 Soeren Sonnenburg
11  */
12 
13 #include <shogun/lib/common.h>
14 #include <shogun/lib/SGNDArray.h>
16 
17 namespace shogun
18 {
19 
20 template<class T> SGNDArray<T>::SGNDArray() :
22 {
23  init_data();
24 }
25 
26 template<class T> SGNDArray<T>::SGNDArray(T* a, index_t* d, index_t nd, bool ref_counting) :
27  SGReferencedData(ref_counting)
28 {
29  array = a;
30  dims = d;
31  num_dims = nd;
32 }
33 
34 template<class T> SGNDArray<T>::SGNDArray(index_t* d, index_t nd, bool ref_counting) :
35  SGReferencedData(ref_counting), dims(d), num_dims(nd)
36 {
37  int64_t total = 1;
38  for (int32_t i=0; i<num_dims; i++)
39  total *= dims[i];
40  ASSERT(total>0)
41  array = SG_MALLOC(T, total);
42 }
43 
44 template<class T> SGNDArray<T>::SGNDArray(const SGNDArray &orig) :
45  SGReferencedData(orig)
46 {
47  copy_data(orig);
48 }
49 
50 template<class T> SGNDArray<T>::~SGNDArray()
51 {
52  unref();
53 }
54 
55 template<class T> void SGNDArray<T>::copy_data(const SGReferencedData &orig)
56 {
57  array = ((SGNDArray*)(&orig))->array;
58  dims = ((SGNDArray*)(&orig))->dims;
59  num_dims = ((SGNDArray*)(&orig))->num_dims;
60 }
61 
62 template<class T> void SGNDArray<T>::init_data()
63 {
64  array = NULL;
65  dims = NULL;
66  num_dims = 0;
67 }
68 
69 template<class T> void SGNDArray<T>::free_data()
70 {
71  SG_FREE(array);
72  SG_FREE(dims);
73 
74  array = NULL;
75  dims = NULL;
76  num_dims = 0;
77 }
78 
79 template<class T> void SGNDArray<T>::transpose_matrix(index_t matIdx) const
80 {
81  ASSERT(array && dims && num_dims > 2 && dims[2] > matIdx)
82 
83  T aux;
84  // Index to acces directly the elements of the matrix of interest
85  int64_t idx = int64_t(matIdx)*int64_t(dims[0])*dims[1];
86 
87  for (int64_t i=0; i<dims[0]; i++)
88  for (int64_t j=0; j<i-1; j++)
89  {
90  aux = array[idx + i + j*dims[0]];
91  array[idx + i + j*dims[0]] = array[idx + j + i*dims[0]];
92  array[idx + j + i*dims[1]] = aux;
93  }
94 
95  // Swap the sizes of the two first dimensions
96  index_t auxDim = dims[0];
97  dims[0] = dims[1];
98  dims[1] = auxDim;
99 }
100 
101 template class SGNDArray<bool>;
102 template class SGNDArray<char>;
103 template class SGNDArray<int8_t>;
104 template class SGNDArray<uint8_t>;
105 template class SGNDArray<int16_t>;
106 template class SGNDArray<uint16_t>;
107 template class SGNDArray<int32_t>;
108 template class SGNDArray<uint32_t>;
109 template class SGNDArray<int64_t>;
110 template class SGNDArray<uint64_t>;
111 template class SGNDArray<float32_t>;
112 template class SGNDArray<float64_t>;
113 template class SGNDArray<floatmax_t>;
114 }

SHOGUN Machine Learning Toolbox - Documentation