Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
n_max.hh
1 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_LABELING_N_MAX_HH
27 # define MLN_LABELING_N_MAX_HH
28 
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>
33 
44 
45 
46 namespace mln
47 {
48 
49  namespace labeling
50  {
51 
52  template <typename L, typename V>
53  util::array<L>
54  n_max(const util::array<V>& in_arr, unsigned n);
55 
56 
57  template <typename L>
58  util::array<mln_value(L)>
59  n_max(const Image<L>& lbl, const mln_value(L)& nlabels, unsigned n);
60 
61 
62 # ifndef MLN_INCLUDE_ONLY
63 
64  template <typename L, typename V>
65  util::array<L>
66  n_max(const util::array<V>& in_arr, unsigned n)
67  {
68  trace::entering("mln::labeling::n_max");
69 
70  mln_precondition(n < in_arr.nelements());
71 
72  util::array<L> output(n + 1, 0);
73  for (unsigned i = 0; i < n + 1; ++i)
74  output[i] = i;
75  int swap = 0;
76  for (int j = n - 1; j > 0; --j)
77  {
78  if (in_arr[output[j]] < in_arr[output[j + 1]])
79  {
80  swap = output[j];
81  output[j] = output[j + 1];
82  output[j + 1] = swap;
83  }
84  }
85 
86  for (unsigned i = n; i < in_arr.nelements(); ++i)
87  {
88  if (in_arr[i] > in_arr[output[n]])
89  {
90  output[n] = i;
91  for (unsigned j = n - 1; j > 0 && in_arr[output[j]] < in_arr[output[j + 1]]; --j)
92  {
93  swap = output[j];
94  output[j] = output[j + 1];
95  output[j + 1] = swap;
96  }
97  }
98  }
99 
100  trace::exiting("mln::labeling::n_max");
101  return output;
102  }
103 
104 
105  template <typename L>
106  util::array<mln_value(L)>
107  n_max(const Image<L>& lbl, const mln_value(L)& nlabels, unsigned n)
108  {
109  mln_precondition(exact(lbl).is_valid());
110 
111  typedef accu::math::count<mln_site(L)> accu_t;
112  accu_t accu;
113 
114  util::array<mln_result(accu_t)>
115  counts = labeling::compute(accu, lbl, nlabels);
116 
117  return n_max<mln_value(L)>(counts, n);
118  }
119 
120 
121 # endif // !MLN_INCLUDE_ONLY
122 
123  } // end of namespace mln::labeling
124 
125 } // end of namespace mln
126 
127 #endif // ! MLN_LABELING_N_MAX_HH