Added README.md
[clueless-kmeans.git] / arrays.h
1 /*
2  *  clueless-kmeans is a variant of k-means which enforces balanced
3  *  distribution of classes in every cluster
4  *
5  *  Copyright (c) 2013 Idiap Research Institute, http://www.idiap.ch/
6  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
7  *
8  *  This file is part of clueless-kmeans.
9  *
10  *  clueless-kmeans is free software: you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  version 3 as published by the Free Software Foundation.
13  *
14  *  clueless-kmeans is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with selector.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #ifndef ARRAYS_H
25 #define ARRAYS_H
26
27 template<class T>
28 T inline sqdist(int dim, T *x, T *y) {
29   T result = 0;
30   for(int k = 0; k < dim; k++) result += sq(x[k] - y[k]);
31   return result;
32 }
33
34 template<class T>
35 T inline sum(int dim, T *x) {
36   T result = 0;
37   for(int k = 0; k < dim; k++) result += x[k];
38   return result;
39 }
40
41 template<class T>
42 T inline dotprod(int dim, T *x, T *y) {
43   T result = 0;
44   for(int k = 0; k < dim; k++) result += x[k] * y[k];
45   return result;
46 }
47
48 template<class T>
49 void fill_vector(int dim, T *vector, T v) {
50   for(int k = 0; k < dim; k++) {
51     vector[k] = v;
52   }
53 }
54
55 template<class T>
56 void copy_vector(int dim, T *vector_dst, T *vector_src) {
57   for(int k = 0; k < dim; k++) {
58     vector_dst[k] = vector_src[k];
59   }
60 }
61
62 template<class T>
63 T **allocate_array(int a, int b) {
64   T *whole = new T[a * b];
65   T **array = new T *[a];
66   for(int k = 0; k < a; k++) {
67     array[k] = whole;
68     whole += b;
69   }
70   return array;
71 }
72
73 template<class T>
74 void deallocate_array(T **array) {
75   if(array) {
76     delete[] array[0];
77     delete[] array;
78   }
79 }
80
81 template<class T>
82 void fill_array(int a, int b, T **array, T v) {
83   for(int k = 0; k < a * b; k++) {
84     array[0][k] = v;
85   }
86 }
87
88 template<class T>
89 T ***allocate_volume(int a, int b, int c) {
90   T *whole = new T[a * b * c];
91   T **column = new T *[a * b];
92   T ***volume = new T **[a];
93
94   for(int k = 0; k < a; k++) {
95     volume[k] = column;
96     for(int l = 0; l < b; l++) {
97       column[l] = whole;
98       whole += c;
99     }
100     column += b;
101   }
102
103   return volume;
104 }
105
106 template<class T>
107 void deallocate_volume(T ***volume) {
108   if(volume) {
109     delete[] volume[0][0];
110     delete[] volume[0];
111     delete[] volume;
112   }
113 }
114
115 template<class T>
116 void fill_volume(int a, int b, int c, T ***volume, T v) {
117   for(int k = 0; k < a * b * c; k++) {
118     volume[0][0][k] = v;
119   }
120 }
121
122 #endif