00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef MLN_UTIL_HQUEUES_HH
00027 # define MLN_UTIL_HQUEUES_HH
00028
00038
00039
00040 # include <mln/core/site_set/p_queue_fast.hh>
00041 # include <mln/histo/array.hh>
00042 # include <mln/value/set.hh>
00043
00044
00045 namespace mln
00046 {
00047
00048 namespace util
00049 {
00050
00051 template <typename P, typename T>
00052 struct hqueues
00053 {
00054 enum {
00055 nvalues = mln_card(T)
00056 };
00057
00058 hqueues(const histo::array<T>& h);
00059
00060 const p_queue_fast<P>& operator[](unsigned i) const;
00061 p_queue_fast<P>& operator[](unsigned i);
00062
00063 const p_queue_fast<P>& operator()(const T& v) const;
00064 p_queue_fast<P>& operator()(const T& v);
00065
00066 const mln::value::set<T>& vset() const;
00067
00068 protected:
00069 void pre_allocate_(unsigned i);
00070
00071 const histo::array<T>& h_;
00072 const mln::value::set<T>& s_;
00073 std::vector<bool> allocated_;
00074 std::vector< p_queue_fast<P> >queues_;
00075 };
00076
00077
00078 # ifndef MLN_INCLUDE_ONLY
00079
00080 template <typename P, typename T>
00081 inline
00082 hqueues<P,T>::hqueues(const histo::array<T>& h)
00083 : h_ (h),
00084 s_ (mln::value::set<T>::the()),
00085 allocated_ (nvalues, false),
00086 queues_ (nvalues)
00087 {
00088 }
00089
00090 template <typename P, typename T>
00091 inline
00092 void
00093 hqueues<P,T>::pre_allocate_(unsigned i)
00094 {
00095 mln_precondition(i < nvalues);
00096 if (!allocated_[i])
00097 {
00098 queues_[i].reserve(h_[i]);
00099 allocated_[i] = true;
00100 }
00101 }
00102
00103
00104 template <typename P, typename T>
00105 inline
00106 const p_queue_fast<P>&
00107 hqueues<P,T>::operator[](unsigned i) const
00108 {
00109 mln_precondition(i < nvalues);
00110 pre_allocate_(i);
00111 return queues_[i];
00112 }
00113
00114 template <typename P, typename T>
00115 inline
00116 p_queue_fast<P>&
00117 hqueues<P,T>::operator[](unsigned i)
00118 {
00119 mln_precondition(i < nvalues);
00120 pre_allocate_(i);
00121 return queues_[i];
00122 }
00123
00124 template <typename P, typename T>
00125 inline
00126 const p_queue_fast<P>&
00127 hqueues<P,T>::operator()(const T& v) const
00128 {
00129 unsigned i = s_.index_of(v);
00130 pre_allocate_(i);
00131 return queues_[i];
00132 }
00133
00134 template <typename P, typename T>
00135 inline
00136 p_queue_fast<P>&
00137 hqueues<P,T>::operator()(const T& v)
00138 {
00139 unsigned i = s_.index_of(v);
00140 pre_allocate_(i);
00141 return queues_[i];
00142 }
00143
00144
00145 template <typename P, typename T>
00146 inline
00147 const mln::value::set<T>&
00148 hqueues<P,T>::vset() const
00149 {
00150 return s_;
00151 }
00152
00153 # endif // ! MLN_INCLUDE_ONLY
00154
00155 }
00156
00157 }
00158
00159 #endif // !MLN_UTIL_HQUEUES_HH