27 #ifndef MLN_LABELING_N_MAX_HH
28 # define MLN_LABELING_N_MAX_HH
30 # include <mln/core/concept/image.hh>
31 # include <mln/accu/math/count.hh>
32 # include <mln/labeling/compute.hh>
33 # include <mln/util/array.hh>
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
67 template <
typename L,
typename V>
69 n_max(
const util::array<V>& in_arr,
unsigned n)
71 mln_trace(
"mln::labeling::n_max");
73 mln_precondition(n < in_arr.nelements());
75 util::array<L> output(n + 1, 0);
76 for (
unsigned i = 0; i < n + 1; ++i)
79 for (
int j = n - 1; j > 0; --j)
81 if (in_arr[output[j]] < in_arr[output[j + 1]])
84 output[j] = output[j + 1];
89 for (
unsigned i = n; i < in_arr.nelements(); ++i)
91 if (in_arr[i] > in_arr[output[n]])
94 for (
unsigned j = n - 1; j > 0 && in_arr[output[j]] < in_arr[output[j + 1]]; --j)
97 output[j] = output[j + 1];
109 template <
typename L>
110 util::array<mln_value(L)>
111 n_max(
const Image<L>& lbl,
const mln_value(L)& nlabels,
unsigned n)
113 mln_precondition(
exact(lbl).is_valid());
115 typedef accu::math::count<mln_site(L)> accu_t;
118 util::array<mln_result(accu_t)>
121 return n_max<mln_value(L)>(counts, n);
125 # endif // !MLN_INCLUDE_ONLY
131 #endif // ! MLN_LABELING_N_MAX_HH