Last change
on this file since 134 was
88,
checked in by rosiere, 16 years ago
|
Almost complete design
with Test and test platform
|
-
Property svn:keywords set to
Id
|
File size:
1.5 KB
|
Rev | Line | |
---|
[88] | 1 | #ifndef Morpheo_Max_h |
---|
| 2 | #define Morpheo_Max_h |
---|
[78] | 3 | |
---|
| 4 | /* |
---|
| 5 | * $Id: Max.h 88 2008-12-10 18:31:39Z rosiere $ |
---|
| 6 | * |
---|
| 7 | * [ Description ] |
---|
| 8 | * |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | #include <stdint.h> |
---|
| 12 | |
---|
| 13 | namespace morpheo { |
---|
[87] | 14 | |
---|
[88] | 15 | template<typename T> inline T max (T * x, uint32_t size) |
---|
[78] | 16 | { |
---|
[87] | 17 | T res = x[0]; |
---|
[78] | 18 | |
---|
[87] | 19 | for (uint32_t i=1; i<size; i++) |
---|
[88] | 20 | { |
---|
| 21 | T tmp = x[i]; |
---|
| 22 | if (res<tmp) |
---|
| 23 | res = tmp; |
---|
| 24 | } |
---|
[78] | 25 | |
---|
| 26 | return res; |
---|
| 27 | } |
---|
[87] | 28 | |
---|
[88] | 29 | template<typename T> inline T max (T ** x, uint32_t size1, uint32_t size2) |
---|
[87] | 30 | { |
---|
[88] | 31 | T res = max(x[0],size2); |
---|
| 32 | |
---|
| 33 | for (uint32_t i=1; i<size1; i++) |
---|
| 34 | { |
---|
| 35 | T tmp = max(x[i],size2); |
---|
| 36 | if (res<tmp) |
---|
| 37 | res = tmp; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | return res; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | template<typename T> inline T max (T ** x, uint32_t size1, uint32_t * size2) |
---|
| 44 | { |
---|
| 45 | T res = max(x[0],size2[0]); |
---|
| 46 | |
---|
| 47 | for (uint32_t i=1; i<size1; i++) |
---|
| 48 | { |
---|
| 49 | T tmp = max(x[i],size2[i]); |
---|
| 50 | if (res<tmp) |
---|
| 51 | res = tmp; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | return res; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | template<typename T> inline T max (T * x, uint32_t size, uint32_t n) |
---|
| 59 | { |
---|
[87] | 60 | if ((n==0) or (n>size)) |
---|
| 61 | return max<T>(x,size); |
---|
| 62 | |
---|
| 63 | // validity array |
---|
| 64 | bool val [size]; |
---|
| 65 | for (uint32_t i=0; i<size; i++) |
---|
| 66 | val [i] = true; |
---|
| 67 | |
---|
| 68 | // find min |
---|
| 69 | T min = x[0]; |
---|
| 70 | |
---|
| 71 | for (uint32_t i=1; i<size; i++) |
---|
| 72 | if (min>x[i]) |
---|
| 73 | min = x[i]; |
---|
| 74 | |
---|
| 75 | // find n max |
---|
| 76 | T res = min; |
---|
| 77 | for (uint32_t i=0; i<n; i++) |
---|
| 78 | { |
---|
| 79 | |
---|
| 80 | uint32_t index = 0; |
---|
| 81 | res = min; // always take the minor |
---|
| 82 | for (uint32_t j=0; j<size; j++) |
---|
| 83 | if ((res<x[j]) and val[j]) // find greater value, and slot is valid |
---|
| 84 | { |
---|
| 85 | res = x[j]; |
---|
| 86 | index = j; |
---|
| 87 | } |
---|
| 88 | val [index] = false; // invalid this value |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | return res; |
---|
| 92 | } |
---|
| 93 | |
---|
[78] | 94 | |
---|
| 95 | }; // end namespace morpheo |
---|
| 96 | |
---|
| 97 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.