Mediator.h
Go to the documentation of this file.
1 // Sliding median filter
2 // Created 2012 by Colin Raffel
3 // Portions Copyright (c) 2011 ashelly.myopenid.com under <http://www.opensource.org/licenses/mit-license>
4 
5 
6 #ifndef MEDIATOR_H
7 #define MEDIATOR_H
8 
9 template <typename Item> class Mediator
10 {
11 public:
12 
13  Mediator(int nItems ):N(nItems)
14  {
15  data = new Item[nItems];
16  pos = new int[nItems];
17  allocatedHeap = new int[nItems];
18  heap = allocatedHeap + (nItems/2);
19  minCt = maxCt = idx = 0;
20  // Set up initial heap fill pattern: median, max, min, max, ...
21  while (nItems--)
22  {
23  pos[nItems] = ((nItems + 1)/2)*((nItems & 1) ? -1 : 1);
24  heap[pos[nItems]] = nItems;
25  }
26  }
27 
29  {
30  delete[] data;
31  delete[] pos;
32  delete[] allocatedHeap;
33  };
34 
35  // Inserts item, maintains median in O(lg nItems)
36  void insert(const Item& v )
37  {
38  const int p = pos[idx];
39  const Item old = data[idx];
40  data[idx] = v;
41  idx = (idx+1) % N;
42  // New item is in minheap
43  if (p>0)
44  {
45  if (minCt < (N-1)/2)
46  {
47  minCt++;
48  }
49  else if (v > old)
50  {
51  minSortDown( p );
52  return;
53  }
54  if (minSortUp( p ) && mmCmpExch( 0, -1 ))
55  {
56  maxSortDown( -1 );
57  }
58  }
59  // New item is in maxheap
60  else if (p<0)
61  {
62  if (maxCt < N/2)
63  {
64  maxCt++;
65  }
66  else if (v < old)
67  {
68  maxSortDown( p );
69  return;
70  }
71  if (maxSortUp( p ) && minCt && mmCmpExch( 1, 0 ))
72  {
73  minSortDown( 1 );
74  }
75  }
76  // New item is at median
77  else
78  {
79  if (maxCt && maxSortUp( -1 ))
80  {
81  maxSortDown( -1 );
82  }
83  if (minCt && minSortUp( 1 ))
84  {
85  minSortDown( 1 );
86  }
87  }
88  };
89 
90  // Returns median item (or average of 2 when item count is even)
91  Item getMedian()
92  {
93  Item v = data[heap[0]];
94  if (minCt<maxCt)
95  {
96  v = (v + data[heap[-1]])/2;
97  }
98  return v;
99  };
100 
101 private:
102 
103  // Swaps items i&j in heap, maintains indexes
104  int mmexchange(const int i,const int j )
105  {
106  int t = heap[i];
107  heap[i] = heap[j];
108  heap[j] = t;
109  pos[heap[i]] = i;
110  pos[heap[j]] = j;
111  return 1;
112  };
113 
114  // Maintains minheap property for all items below i.
115  void minSortDown( int i )
116  {
117  for (i*=2; i <= minCt; i*=2)
118  {
119  if (i < minCt && mmless( i+1, i ))
120  {
121  ++i;
122  }
123  if (!mmCmpExch( i, i/2 ))
124  {
125  break;
126  }
127  }
128  };
129 
130  // Maintains maxheap property for all items below i. (negative indexes)
131  void maxSortDown( int i )
132  {
133  for (i*=2; i >= -maxCt; i*=2)
134  {
135  if (i > -maxCt && mmless( i, i-1 ))
136  {
137  --i;
138  }
139  if (!mmCmpExch( i/2, i ))
140  {
141  break;
142  }
143  }
144  };
145 
146  // Returns 1 if heap[i] < heap[j]
147  inline int mmless(const int i,const int j )
148  {
149  return (data[heap[i]] < data[heap[j]]);
150  };
151 
152  // Swaps items i&j if i<j; returns true if swapped
153  inline int mmCmpExch(const int i,const int j )
154  {
155  return (mmless( i, j ) && mmexchange( i, j ));
156  };
157 
158  // Maintains minheap property for all items above i, including median
159  // Returns true if median changed
160  inline int minSortUp( int i )
161  {
162  while (i > 0 && mmCmpExch( i, i/2 ))
163  {
164  i /= 2;
165  }
166  return (i == 0);
167  };
168 
169  // Maintains maxheap property for all items above i, including median
170  // Returns true if median changed
171  inline int maxSortUp( int i )
172  {
173  while (i < 0 && mmCmpExch( i/2, i ))
174  {
175  i /= 2;
176  }
177  return ( i==0 );
178  };
179  // Allocated size
180  const int N;
181  // Circular queue of values
182  Item* data;
183  // Index into `heap` for each value
184  int* pos;
185  // Max/median/min heap holding indexes into `data`.
186  int* heap;
187  // heap holds a pointer to the middle of its data; this is where the data is allocated.
189  // Position in circular queue
190  int idx;
191  // Count of items in min heap
192  int minCt;
193  // Count of items in max heap
194  int maxCt;
195 };
196 
197 #endif
int * heap
Definition: Mediator.h:186
Mediator(int nItems)
Definition: Mediator.h:13
Item * data
Definition: Mediator.h:182
void insert(const Item &v)
Definition: Mediator.h:36
int idx
Definition: Mediator.h:190
int mmCmpExch(const int i, const int j)
Definition: Mediator.h:153
int * allocatedHeap
Definition: Mediator.h:188
Item getMedian()
Definition: Mediator.h:91
void maxSortDown(int i)
Definition: Mediator.h:131
int mmless(const int i, const int j)
Definition: Mediator.h:147
const int N
Definition: Mediator.h:178
~Mediator()
Definition: Mediator.h:28
int maxCt
Definition: Mediator.h:194
void minSortDown(int i)
Definition: Mediator.h:115
int minCt
Definition: Mediator.h:192
int * pos
Definition: Mediator.h:184
int mmexchange(const int i, const int j)
Definition: Mediator.h:104
int minSortUp(int i)
Definition: Mediator.h:160
int maxSortUp(int i)
Definition: Mediator.h:171


timesync_ros
Author(s): Juraj Oršulić
autogenerated on Mon Jun 10 2019 15:28:33