26 #ifndef MLN_DATA_SORT_OFFSETS_HH
27 # define MLN_DATA_SORT_OFFSETS_HH
36 # include <mln/core/concept/image.hh>
37 # include <mln/histo/compute.hh>
38 # include <mln/util/array.hh>
39 # include <mln/util/ord.hh>
40 # include <mln/geom/nsites.hh>
66 # ifndef MLN_INCLUDE_ONLY
77 struct value_offset_less_
80 inline value_offset_less_(
const I& ima) : ima_(ima) {}
81 inline bool operator()(
unsigned lhs,
unsigned rhs)
const
84 || (ima_.element(lhs) == ima_.element(rhs)
95 mln_trace(
"data::impl::generic::sort_offsets_increasing");
97 const I& input =
exact(input_);
99 util::array<unsigned> v;
100 v.reserve(input.nelements());
101 mln_fwd_pixter(
const I) pxl(input);
103 v.append(pxl.offset());
104 std::sort(v.hook_std_vector_().begin(), v.hook_std_vector_().end(),
105 value_offset_less_<I>(input));
113 template <typename I>
114 struct value_offset_greater_
117 inline value_offset_greater_(
const I& ima) : ima_(ima) {}
118 inline bool operator()(
unsigned lhs,
unsigned rhs)
const
121 || (ima_.element(lhs) == ima_.element(rhs)
127 template <
typename I>
129 util::array<unsigned>
130 sort_offsets_decreasing(
const Image<I>& input_)
132 mln_trace(
"data::impl::generic::sort_offsets_decreasing");
134 const I& input =
exact(input_);
136 util::array<unsigned> v;
137 v.reserve(input.nelements());
138 mln_fwd_pixter(
const I) pxl(input);
140 v.append(pxl.offset());
141 std::sort(v.hook_std_vector_().begin(), v.hook_std_vector_().end(),
142 value_offset_greater_<I>(input));
154 template <typename I>
156 util::array<
unsigned>
157 sort_offsets_increasing_radix(const Image<I>& input_)
159 mln_trace(
"data::impl::sort_offsets_increasing_radix");
161 const I& input =
exact(input_);
163 typedef mln_vset(I) S;
164 const S& vset = input.values_eligible();
165 const
unsigned n = vset.nvalues();
171 std::vector<
unsigned> loc(vset.nvalues());
173 for (
unsigned i = 1; i != n; ++i)
174 loc[i] = loc[i-1] + h[i-1];
177 util::array<
unsigned> vec(geom::
nsites(input));
178 mln_fwd_pixter(const I) pxl(input);
180 vec[loc[vset.index_of(pxl.val())]++] = pxl.offset();
188 template <typename I>
190 util::array<
unsigned>
191 sort_offsets_decreasing_radix(const Image<I>& input_)
193 mln_trace(
"data::impl::sort_offsets_decreasing_radix");
195 const I& input =
exact(input_);
197 typedef mln_vset(I) S;
198 const S& vset = input.values_eligible();
199 const
unsigned n = vset.nvalues();
205 std::vector<
unsigned> loc(vset.nvalues());
207 for (
int i = n - 2; i >= 0; --i)
208 loc[i] = loc[i+1] + h[i+1];
211 util::array<
unsigned> vec(geom::
nsites(input));
212 mln_fwd_pixter(const I) pxl(input);
214 vec[loc[vset.index_of(pxl.val())]++] = pxl.offset();
229 template <
typename I>
231 util::array<unsigned>
232 sort_offsets_increasing_dispatch(trait::image::quant::any,
233 const Image<I>& input)
238 template <
typename I>
240 util::array<unsigned>
241 sort_offsets_increasing_dispatch(trait::image::quant::low,
242 const Image<I>& input)
244 return impl::sort_offsets_increasing_radix(input);
247 template <
typename I>
249 util::array<unsigned>
250 sort_offsets_increasing_dispatch(
const Image<I>& input)
252 return sort_offsets_increasing_dispatch(mln_trait_image_quant(I)(),
258 template <
typename I>
260 util::array<unsigned>
261 sort_offsets_decreasing_dispatch(trait::image::quant::any,
262 const Image<I>& input)
264 return impl::generic::sort_offsets_decreasing(input);
267 template <
typename I>
269 util::array<unsigned>
270 sort_offsets_decreasing_dispatch(trait::image::quant::low,
271 const Image<I>& input)
273 return impl::sort_offsets_decreasing_radix(input);
276 template <
typename I>
278 util::array<unsigned>
279 sort_offsets_decreasing_dispatch(
const Image<I>& input)
281 return sort_offsets_decreasing_dispatch(mln_trait_image_quant(I)(),
291 template <
typename I>
293 util::array<unsigned>
296 mln_trace(
"data::sort_offsets_increasing");
297 mlc_is(mln_trait_image_speed(I),
298 trait::image::speed::fastest)::check();
300 mln_precondition(
exact(input).is_valid());
301 util::array<unsigned> output = internal::sort_offsets_increasing_dispatch(input);
306 template <
typename I>
308 util::array<unsigned>
309 sort_offsets_decreasing(
const Image<I>& input)
311 mln_trace(
"data::sort_offsets_decreasing");
312 mlc_is(mln_trait_image_speed(I),
313 trait::image::speed::fastest)::check();
315 mln_precondition(
exact(input).is_valid());
316 util::array<unsigned> output = internal::sort_offsets_decreasing_dispatch(input);
321 # endif // ! MLN_INCLUDE_ONLY
328 #endif // ! MLN_DATA_SORT_OFFSETS_HH