libmoost
/home/mhx/git/github/libmoost/include/moost/container/policies/vector_map.hpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00028 #ifndef MOOST_VECTOR_POLICY_HPP
00029 #define MOOST_VECTOR_POLICY_HPP
00030 
00031 #include "generic_map.hpp"
00032 #include <vector>
00033 
00034 namespace moost { namespace container { namespace policies {
00035 
00039 template <typename TVal>
00040 class vector_map : public generic_map< std::vector<TVal> >
00041 {
00042 public:
00043 
00044    typedef typename std::vector<TVal> map_type;
00045 
00046 public:
00047 
00048    vector_map(size_t vector_size = 0)
00049       : m_initial_vector_size(vector_size)
00050    {}
00051 
00052    virtual ~vector_map(){}
00053 
00055    virtual void init(map_type& map) const
00056    {
00057       if ( m_initial_vector_size > 0 )
00058          map.resize(m_initial_vector_size);
00059    }
00060 
00062    virtual void resize(map_type& map, size_t numKeys) const
00063    { map.resize(numKeys); }
00064 
00065    template <typename TKey>
00066    TVal operator()( const std::vector<TVal>& vec, const TKey& key ) const
00067    {
00068       if ( static_cast<size_t>(key) >= vec.size() )
00069          throw std::runtime_error("vector_map_policy::operator(): key not found");
00070       else
00071          return vec[key];
00072    }
00073 
00074    template <typename TKey>
00075    bool operator()( const std::vector<TVal>& vec, const TKey& key, TVal& val ) const
00076    {
00077       if ( static_cast<size_t>(key) >= vec.size() )
00078          return false;
00079       else
00080          val = vec[key];
00081       return true;
00082    }
00083 
00084 
00085    template <typename TKey, typename TMapIterator>
00086    TKey get_key( const std::vector<TVal>& vec, const TMapIterator& it ) const
00087    { return static_cast<int>(it - vec.begin()); }
00088 
00089    template <typename TIgnoreThis, typename TMapIterator>
00090    TVal get_value( const std::vector<TVal>& /*vec*/, const TMapIterator& it ) const
00091    { return *it; }
00092 
00093    //template <typename TKey>
00094    //bool find(const std::vector<TVal>& vec, const TKey& key) const
00095    //{
00096    //   if ( static_cast<size_t>(key) >= vec.size() )
00097    //      return vec.end();
00098    //   else
00099    //      return vec.begin() + key;
00100    //}
00101 
00102 private:
00103    const size_t m_initial_vector_size;
00104 };
00105 
00106 // -----------------------------------------------------------------------------
00107 
00108 template <typename TKey, typename TVal>
00109 struct map_policy_selector< TKey, TVal, std::vector<TVal> >
00110 {
00111    // will return generic_map
00112    typedef vector_map<TVal> policy_type;
00113 };
00114 
00115 }}}
00116 
00117 #endif // MOOST_VECTOR_POLICY_HPP