$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
hqueues.hh
1 // Copyright (C) 2008, 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_UTIL_HQUEUES_HH
27 # define MLN_UTIL_HQUEUES_HH
28 
38 
39 
40 # include <mln/core/site_set/p_queue_fast.hh>
41 # include <mln/histo/array.hh>
42 # include <mln/value/set.hh>
43 
44 
45 namespace mln
46 {
47 
48  namespace util
49  {
50 
51  template <typename P, typename T>
52  struct hqueues
53  {
54  enum {
55  nvalues = mln_card(T)
56  };
57 
58  hqueues(const histo::array<T>& h);
59 
60  const p_queue_fast<P>& operator[](unsigned i) const;
61  p_queue_fast<P>& operator[](unsigned i);
62 
63  const p_queue_fast<P>& operator()(const T& v) const;
64  p_queue_fast<P>& operator()(const T& v);
65 
66  const mln::value::set<T>& vset() const;
67 
68  protected:
69  void pre_allocate_(unsigned i);
70 
73  std::vector<bool> allocated_;
74  std::vector< p_queue_fast<P> >queues_;
75  };
76 
77 
78 # ifndef MLN_INCLUDE_ONLY
79 
80  template <typename P, typename T>
81  inline
83  : h_ (h),
84  s_ (mln::value::set<T>::the()),
85  allocated_ (nvalues, false),
86  queues_ (nvalues)
87  {
88  }
89 
90  template <typename P, typename T>
91  inline
92  void
93  hqueues<P,T>::pre_allocate_(unsigned i)
94  {
95  mln_precondition(i < nvalues);
96  if (!allocated_[i])
97  {
98  queues_[i].reserve(h_[i]);
99  allocated_[i] = true;
100  }
101  }
102 
103 
104  template <typename P, typename T>
105  inline
106  const p_queue_fast<P>&
107  hqueues<P,T>::operator[](unsigned i) const
108  {
109  mln_precondition(i < nvalues);
110  pre_allocate_(i);
111  return queues_[i];
112  }
113 
114  template <typename P, typename T>
115  inline
116  p_queue_fast<P>&
117  hqueues<P,T>::operator[](unsigned i)
118  {
119  mln_precondition(i < nvalues);
120  pre_allocate_(i);
121  return queues_[i];
122  }
123 
124  template <typename P, typename T>
125  inline
126  const p_queue_fast<P>&
127  hqueues<P,T>::operator()(const T& v) const
128  {
129  unsigned i = s_.index_of(v);
130  pre_allocate_(i);
131  return queues_[i];
132  }
133 
134  template <typename P, typename T>
135  inline
136  p_queue_fast<P>&
137  hqueues<P,T>::operator()(const T& v)
138  {
139  unsigned i = s_.index_of(v);
140  pre_allocate_(i);
141  return queues_[i];
142  }
143 
144 
145  template <typename P, typename T>
146  inline
147  const mln::value::set<T>&
148  hqueues<P,T>::vset() const
149  {
150  return s_;
151  }
152 
153 # endif // ! MLN_INCLUDE_ONLY
154 
155  } // end of namespace mln::util
156 
157 } // end of namespace mln
158 
159 #endif // !MLN_UTIL_HQUEUES_HH