8 #ifndef META_OCEAN_BASE_MEDIAN_H
9 #define META_OCEAN_BASE_MEDIAN_H
34 static inline T
median2(
const T& v0,
const T& v1);
46 static inline T
median3(
const T& v0,
const T& v1,
const T& v2);
59 static inline T
median4(
const T& v0,
const T& v1,
const T& v2,
const T& v3);
73 static inline T
median5(
const T& v0,
const T& v1,
const T& v2,
const T& v3,
const T& v4);
85 static T
median(T* values,
const size_t number);
97 static T
constMedian(
const T* values,
const size_t number);
100 template <
typename T>
103 return v0 < v1 ? v0 : v1;
106 template <
typename T>
150 template <
typename T>
326 template <
typename T>
327 inline T
Median::median5(
const T& v0,
const T& v1,
const T& v2,
const T& v3,
const T& v4)
1143 template <
typename T>
1146 ocean_assert(values && number > 0);
1149 size_t high = number - 1;
1150 size_t median = (low + high) >> 1;
1152 size_t ll, hh, middle;
1161 if (high == low + 1)
1163 if (values[low] > values[high])
1164 std::swap(values[low], values[high]);
1169 middle = (low + high) / 2;
1171 if (values[middle] > values[high])
1172 std::swap(values[middle], values[high]);
1174 if (values[low] > values[high])
1175 std::swap(values[low], values[high]);
1177 if (values[middle] > values[low])
1178 std::swap(values[middle], values[low]);
1180 std::swap(values[middle], values[low + 1]);
1187 while (ll < number && values[low] > values[++ll]);
1189 while (hh > 0 && values[--hh] > values[low]);
1194 std::swap(values[ll], values[hh]);
1197 std::swap(values[low], values[hh]);
1207 template <
typename T>
1210 ocean_assert(values !=
nullptr && number > 0);
1215 Memory memory = Memory::create<T>(number);
1217 if (memory.
data() ==
nullptr)
1219 ocean_assert(
false &&
"Out of memory");
1223 memcpy(memory.
data<T>(), values,
sizeof(T) * number);
1224 const T result =
median(memory.
data<T>(), number);
This class implements an object able to allocate memory.
Definition: base/Memory.h:22
void * data()
Returns the pointer to the writable memory which is allocated by this object.
Definition: base/Memory.h:303
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15