Image Component Library (ICL)
FastMedianList.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/FastMedianList.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <string.h>
35 #include <cmath>
36 
37 namespace icl{
38  namespace utils{
40 
86  public:
88 
89  inline FastMedianList(int size=0,int t2=-1, int t3=-1, int t4=-1):
90  m_iSize(size),m_iT2(t2),m_iT3(t3),m_iT4(t4), m_piTable(size ? new int[size]: 0){
91  clear();
92  }
93 
95  inline FastMedianList(const FastMedianList &l):
97  m_piTable = m_iSize ? new int[m_iSize] : 0;
98  clear();
99  }
100 
103  m_iSize = l.m_iSize;
104  m_iT2 = l.m_iT2;
105  m_iT3 = l.m_iT3;
106  m_iT4 = l.m_iT4;
107  m_piTable = m_iSize ? new int[m_iSize] : 0;
108  clear();
109  return *this;
110  }
111 
113  inline ~FastMedianList(){
114  if(m_piTable) delete [] m_piTable;
115  }
116 
118  inline void init(int size, int t2=-1, int t3=-1, int t4=-1){
119  m_iSize = size;
120  m_iT2 = t2;
121  m_iT3 = t3;
122  m_iT4 = t4;
123  if(m_piTable)delete [] m_piTable;
124  m_piTable = new int[size];
125  }
126 
128  inline int add(int index){
129  m_piTable[index]++;
130  m_iCount++;
131  return m_iCount;
132  }
133 
135  inline void add(int index, int val){
136  m_piTable[index]++;
137  m_iCount++;
138  if(val > m_iT2)return;
139  m_piTable[index]++;
140  m_iCount++;
141  if(val > m_iT3)return;
142  m_piTable[index]++;
143  m_iCount++;
144  if(val > m_iT4)return;
145  m_piTable[index]++;
146  m_iCount++;
147  }
148 
150  inline int median(){
151  if(m_iCount == 0)return -1;
152  int mass=0;
153  int count2 = m_iCount/2;
154  for(int i=0;i<m_iSize;i++){
155  mass+=m_piTable[i];
156  if(mass > count2){
157  return i;
158  }
159  }
160  return 0;
161  }
162 
164  inline void clear(){
165  memset(m_piTable,0,m_iSize*sizeof(int));
166  m_iCount=0;
167  }
168 
170  inline int mean(){
171  int m=0;
172 
173  if (!m_iCount) return 0;
174 
175  for(int i=0;i<m_iSize;i++){
176  m+=m_piTable[i]*i;
177  }
178  return m/m_iCount;
179 
180  }
181 
183  inline int variance(){
184  int var=0;
185  int m = mean();
186  for(int i=0;i<m_iSize;i++){
187  var+=( m_piTable[i]*(int)pow(float(i-m),2) );
188  }
189  return var/m_iCount;
190 
191  }
193  inline int getSize(){
194  return m_iCount;
195  }
196 
197  protected:
199  int m_iCount;
200 
202  int m_iSize;
203 
205  int m_iT2;
206 
208  int m_iT3;
209 
211  int m_iT4;
212 
214  int *m_piTable;
215  };
216 
217  } // namespace utils
218 }
219 
int getSize()
returns the current count of elements
Definition: FastMedianList.h:193
~FastMedianList()
destructor
Definition: FastMedianList.h:113
undocument this line if you encounter any issues!
Definition: Any.h:37
void add(int index, int val)
add a new datum (deprecated)
Definition: FastMedianList.h:135
void init(int size, int t2=-1, int t3=-1, int t4=-1)
initialization function
Definition: FastMedianList.h:118
FastMedianList & operator=(const FastMedianList &l)
Assign operator.
Definition: FastMedianList.h:102
int median()
calculates the median value
Definition: FastMedianList.h:150
int m_iT4
deprecated variable
Definition: FastMedianList.h:211
FastMedianList(int size=0, int t2=-1, int t3=-1, int t4=-1)
Create a new fast median list with given max size.
Definition: FastMedianList.h:89
int m_iT2
deprecated variable
Definition: FastMedianList.h:205
int mean()
calculates the mean value (not accelerated)
Definition: FastMedianList.h:170
int m_iCount
internal element count
Definition: FastMedianList.h:199
int add(int index)
add a new datum
Definition: FastMedianList.h:128
int variance()
calculates the variance of contained elements
Definition: FastMedianList.h:183
FastMedianList(const FastMedianList &l)
Copy cosntructor.
Definition: FastMedianList.h:95
void clear()
resets the internal lookup table
Definition: FastMedianList.h:164
int * m_piTable
internal counter array
Definition: FastMedianList.h:214
int m_iT3
deprecated variable
Definition: FastMedianList.h:208
int m_iSize
internal storage of max size
Definition: FastMedianList.h:202
Utility class for fast calculation of a median (calculating a median in O(N))
Definition: FastMedianList.h:85