26 #ifndef MLN_LABELING_N_MAX_HH
27 # define MLN_LABELING_N_MAX_HH
29 # include <mln/core/concept/image.hh>
30 # include <mln/accu/math/count.hh>
31 # include <mln/labeling/compute.hh>
32 # include <mln/util/array.hh>
52 template <
typename L,
typename V>
54 n_max(
const util::array<V>& in_arr,
unsigned n);
58 util::array<mln_value(L)>
59 n_max(
const Image<L>& lbl,
const mln_value(L)& nlabels,
unsigned n);
62 # ifndef MLN_INCLUDE_ONLY
64 template <
typename L,
typename V>
66 n_max(
const util::array<V>& in_arr,
unsigned n)
68 trace::entering(
"mln::labeling::n_max");
70 mln_precondition(n < in_arr.nelements());
72 util::array<L> output(n + 1, 0);
73 for (
unsigned i = 0; i < n + 1; ++i)
76 for (
int j = n - 1; j > 0; --j)
78 if (in_arr[output[j]] < in_arr[output[j + 1]])
81 output[j] = output[j + 1];
86 for (
unsigned i = n; i < in_arr.nelements(); ++i)
88 if (in_arr[i] > in_arr[output[n]])
91 for (
unsigned j = n - 1; j > 0 && in_arr[output[j]] < in_arr[output[j + 1]]; --j)
94 output[j] = output[j + 1];
100 trace::exiting(
"mln::labeling::n_max");
105 template <
typename L>
106 util::array<mln_value(L)>
107 n_max(
const Image<L>& lbl,
const mln_value(L)& nlabels,
unsigned n)
109 mln_precondition(exact(lbl).is_valid());
111 typedef accu::math::count<mln_site(L)> accu_t;
114 util::array<mln_result(accu_t)>
117 return n_max<mln_value(L)>(counts, n);
121 # endif // !MLN_INCLUDE_ONLY
127 #endif // ! MLN_LABELING_N_MAX_HH