• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

hqueues.hh

00001 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
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   } // end of namespace mln::util
00156 
00157 } // end of namespace mln
00158 
00159 #endif // !MLN_UTIL_HQUEUES_HH

Generated on Tue Oct 4 2011 15:23:55 for Milena (Olena) by  doxygen 1.7.1