-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMultiMap.h
More file actions
237 lines (203 loc) · 6.87 KB
/
MultiMap.h
File metadata and controls
237 lines (203 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#pragma once
//
// FILE: MultiMap.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.0
// DATE: 2011-01-26
// PURPOSE: Arduino library for fast non-linear mapping or interpolation of values
// URL: https://github.com/RobTillaart/MultiMap
// URL: http://playground.arduino.cc/Main/MultiMap
#define MULTIMAP_LIB_VERSION (F("0.3.0"))
#include "Arduino.h"
////////////////////////////////////////////////////////////////////////
//
// SINGLE TYPE MULTIMAP - LINEAR SEARCH - the reference
//
// note: the in array must have increasing values
template<typename T>
T multiMap(T value, T* _in, T* _out, uint16_t size)
{
// output is constrained to out array
if (value <= _in[0]) return _out[0];
if (value >= _in[size-1]) return _out[size-1];
// search right interval
uint16_t pos = 1; // _in[0] already tested
while(value > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (value == _in[pos]) return _out[pos];
// interpolate in the right segment for the rest
return (value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
// if interpolation overflows use this line
// return T(float(value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1])) + _out[pos-1];
}
////////////////////////////////////////////////////////////////////////
//
// SINGLE TYPE MULTIMAP CACHE - LINEAR SEARCH
//
// note: the in array must have increasing values
// performance optimized version if inputs do not change often
// e.g. 2 2 2 2 2 3 3 3 3 5 5 5 5 5 5 8 8 8 8 5 5 5 5 5
// implements a minimal cache of the lastValue.
template<typename T>
T multiMapCache(T value, T* _in, T* _out, uint16_t size)
{
static T lastValue = -1; // possible bug for 1st call
static T cache = -1;
if (value == lastValue)
{
return cache;
}
lastValue = value;
// output is constrained to out array
if (value <= _in[0])
{
cache = _out[0];
}
else if (value >= _in[size-1])
{
cache = _out[size-1];
}
else
{
// search right interval; index 0 _in[0] already tested
uint16_t pos = 1;
while(value > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (value == _in[pos])
{
cache = _out[pos];
}
else
{
// interpolate in the right segment for the rest
cache = (value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
// if interpolation overflows use this line
// cache = T(float(value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1])) + _out[pos-1];
}
}
return cache;
}
////////////////////////////////////////////////////////////////////////
//
// SINGLE TYPE MULTIMAP - BINARY SEARCH
//
// should be faster for size >= 10
// (rule of thumb)
//
// note: the in array must have increasing values
template<typename T>
T multiMapBS(T value, T* _in, T* _out, uint16_t size)
{
// output is constrained to out array
if (value <= _in[0]) return _out[0];
if (value >= _in[size-1]) return _out[size-1];
// Binary Search, uint16_t needed to prevent overflow.
uint16_t lower = 0;
uint16_t upper = size - 1;
while (lower < upper - 1)
{
uint16_t mid = (lower + upper) / 2;
if (value >= _in[mid]) lower = mid;
else upper = mid;
}
// interpolate in the right segment for the rest
return (value - _in[lower]) * (_out[upper] - _out[lower]) / (_in[upper] - _in[lower]) + _out[lower];
// if interpolation overflows use this line
// return T(float(value - _in[lower]) * (_out[upper] - _out[lower]) / (_in[upper] - _in[lower])) + _out[lower];
}
////////////////////////////////////////////////////////////////////////
//
// MULTITYPE MULTIMAP - LINEAR SEARCH
//
// note: the in array must have increasing values
template<typename T1, typename T2>
T2 multiMap(T1 value, T1* _in, T2* _out, uint16_t size)
{
// output is constrained to out array
if (value <= _in[0]) return _out[0];
if (value >= _in[size-1]) return _out[size-1];
// search right interval
uint16_t pos = 1; // _in[0] already tested
while(value > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (value == _in[pos]) return _out[pos];
// interpolate in the right segment for the rest
return (value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
// if interpolation overflows use this line
// return T2(float(value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1])) + _out[pos-1];
}
////////////////////////////////////////////////////////////////////////
//
// MULTITYPE TYPE MULTIMAP CACHE - LINEAR SEARCH
//
// note: the in array must have increasing values
// performance optimized version if inputs do not change often
// e.g. 2 2 2 2 2 3 3 3 3 5 5 5 5 5 5 8 8 8 8 5 5 5 5 5
// implements a minimal cache of the lastValue.
template<typename T1, typename T2>
T2 multiMapCache(T1 value, T1* _in, T2* _out, uint16_t size)
{
static T1 lastValue = -1; // possible bug for 1st call
static T2 cache = -1;
if (value == lastValue)
{
return cache;
}
lastValue = value;
// output is constrained to out array
if (value <= _in[0])
{
cache = _out[0];
}
else if (value >= _in[size-1])
{
cache = _out[size-1];
}
else
{
// search right interval; index 0 _in[0] already tested
uint16_t pos = 1;
while(value > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (value == _in[pos])
{
cache = _out[pos];
}
else
{
// interpolate in the right segment for the rest
cache = (value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
// if interpolation overflows use this line
// return T2(float(value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1])) + _out[pos-1];
}
}
return cache;
}
////////////////////////////////////////////////////////////////////////
//
// MULTITYPE MULTIMAP - BINARY SEARCH
// should be faster for size >= 10
// (rule of thumb)
//
// note: the in array must have increasing values
template<typename T1, typename T2>
T2 multiMapBS(T1 value, T1* _in, T2* _out, uint16_t size)
{
// output is constrained to out array
if (value <= _in[0]) return _out[0];
if (value >= _in[size-1]) return _out[size-1];
// Binary Search, uint16_t needed to prevent overflow.
uint16_t lower = 0;
uint16_t upper = size - 1;
while (lower < upper - 1)
{
uint16_t mid = (lower + upper) / 2;
if (value >= _in[mid]) lower = mid;
else upper = mid;
}
// interpolate in the right segment for the rest
return (value - _in[lower]) * (_out[upper] - _out[lower]) / (_in[upper] - _in[lower]) + _out[lower];
// if interpolation overflows use this line
// return T2(float(value - _in[lower]) * (_out[upper] - _out[lower]) / (_in[upper] - _in[lower])) + _out[lower];
}
// -- END OF FILE --