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 trace::entering(
"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));
107 trace::exiting("
data::impl::generic::sort_offsets_increasing");
114 template <typename I>
115 struct value_offset_greater_
118 inline value_offset_greater_(
const I& ima) : ima_(ima) {}
119 inline bool operator()(
unsigned lhs,
unsigned rhs)
const
122 || (ima_.element(lhs) == ima_.element(rhs)
128 template <
typename I>
130 util::array<unsigned>
131 sort_offsets_decreasing(
const Image<I>& input_)
133 trace::entering(
"data::impl::generic::sort_offsets_decreasing");
135 const I& input = exact(input_);
137 util::array<unsigned> v;
138 v.reserve(input.nelements());
139 mln_fwd_pixter(
const I) pxl(input);
141 v.append(pxl.offset());
142 std::sort(v.hook_std_vector_().begin(), v.hook_std_vector_().end(),
143 value_offset_greater_<I>(input));
145 trace::exiting("
data::impl::generic::sort_offsets_decreasing");
156 template <typename I>
158 util::array<
unsigned>
159 sort_offsets_increasing_radix(const Image<I>& input_)
161 trace::entering(
"data::impl::sort_offsets_increasing_radix");
163 const I& input = exact(input_);
165 typedef mln_vset(I) S;
166 const S& vset = input.values_eligible();
167 const
unsigned n = vset.nvalues();
170 histo::array<mln_value(I)> h = histo::
compute(input);
173 std::vector<
unsigned> loc(vset.nvalues());
175 for (
unsigned i = 1; i != n; ++i)
176 loc[i] = loc[i-1] + h[i-1];
179 util::array<
unsigned> vec(geom::nsites(input));
180 mln_fwd_pixter(const I) pxl(input);
182 vec[loc[vset.index_of(pxl.val())]++] = pxl.offset();
184 trace::exiting("
data::impl::sort_offsets_increasing_radix");
191 template <typename I>
193 util::array<
unsigned>
194 sort_offsets_decreasing_radix(const Image<I>& input_)
196 trace::entering(
"data::impl::sort_offsets_decreasing_radix");
198 const I& input = exact(input_);
200 typedef mln_vset(I) S;
201 const S& vset = input.values_eligible();
202 const
unsigned n = vset.nvalues();
205 histo::array<mln_value(I)> h = histo::
compute(input);
208 std::vector<
unsigned> loc(vset.nvalues());
210 for (
int i = n - 2; i >= 0; --i)
211 loc[i] = loc[i+1] + h[i+1];
214 util::array<
unsigned> vec(geom::nsites(input));
215 mln_fwd_pixter(const I) pxl(input);
217 vec[loc[vset.index_of(pxl.val())]++] = pxl.offset();
219 trace::exiting("
data::impl::sort_offsets_decreasing_radix");
233 template <
typename I>
235 util::array<unsigned>
236 sort_offsets_increasing_dispatch(trait::image::quant::any,
237 const Image<I>& input)
239 return impl::generic::sort_offsets_increasing(input);
242 template <
typename I>
244 util::array<unsigned>
245 sort_offsets_increasing_dispatch(trait::image::quant::low,
246 const Image<I>& input)
248 return impl::sort_offsets_increasing_radix(input);
251 template <
typename I>
253 util::array<unsigned>
254 sort_offsets_increasing_dispatch(
const Image<I>& input)
256 return sort_offsets_increasing_dispatch(mln_trait_image_quant(I)(),
262 template <
typename I>
264 util::array<unsigned>
265 sort_offsets_decreasing_dispatch(trait::image::quant::any,
266 const Image<I>& input)
268 return impl::generic::sort_offsets_decreasing(input);
271 template <
typename I>
273 util::array<unsigned>
274 sort_offsets_decreasing_dispatch(trait::image::quant::low,
275 const Image<I>& input)
277 return impl::sort_offsets_decreasing_radix(input);
280 template <
typename I>
282 util::array<unsigned>
283 sort_offsets_decreasing_dispatch(
const Image<I>& input)
285 return sort_offsets_decreasing_dispatch(mln_trait_image_quant(I)(),
295 template <
typename I>
297 util::array<unsigned>
300 trace::entering(
"data::sort_offsets_increasing");
301 mlc_is(mln_trait_image_speed(I),
302 trait::image::speed::fastest)::check();
304 mln_precondition(exact(input).is_valid());
307 trace::exiting(
"data::sort_offsets_increasing");
311 template <
typename I>
314 sort_offsets_decreasing(
const Image<I>& input)
316 trace::entering(
"data::sort_offsets_decreasing");
317 mlc_is(mln_trait_image_speed(I),
318 trait::image::speed::fastest)::check();
320 mln_precondition(exact(input).is_valid());
323 trace::exiting(
"data::sort_offsets_decreasing");
327 # endif // ! MLN_INCLUDE_ONLY
334 #endif // ! MLN_DATA_SORT_OFFSETS_HH