stlab.adobe.com Adobe Systems Incorporated

static_table.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2007 Adobe Systems Incorporated
00003     Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
00004     or a copy at http://stlab.adobe.com/licenses.html)
00005 */
00006 
00007 /*************************************************************************************************/
00008 
00009 #ifndef ADOBE_STATIC_TABLE_HPP
00010 #define ADOBE_STATIC_TABLE_HPP
00011 
00012 /*************************************************************************************************/
00013 
00014 #include <adobe/config.hpp>
00015 
00016 #include <utility>
00017 #include <stdexcept>
00018 
00019 #include <adobe/algorithm/lower_bound.hpp>
00020 #include <adobe/algorithm/sort.hpp>
00021 
00022 /*************************************************************************************************/
00023 
00024 namespace adobe {
00025 
00026 /*************************************************************************************************/
00027 
00028 //***************************************************************************//
00029 //***************************************************************************//
00030 //***************************************************************************//
00031 
00119 //***************************************************************************//
00120 //***************************************************************************//
00121 //***************************************************************************//
00122 
00187 template <typename KeyType, typename ValueType>
00188 struct static_table_traits
00189 {
00190     typedef bool                            result_type;
00191     typedef KeyType                         key_type;
00192     typedef ValueType                       value_type;
00193     typedef std::pair<key_type, value_type> entry_type;
00194 
00195     result_type operator()(const entry_type& x, const entry_type& y) const
00196     {
00197         return (*this)(x, y.first);
00198     }
00199 
00200     // revisit: MM. For debugging purposes, VC 8 requires the definition of
00201     // this (unnecessary overload) in debug versions.
00202     result_type operator()(const key_type& x, const entry_type& y) const
00203     {
00204         return x < y.first;
00205     }
00206 
00207     result_type operator()(const entry_type& x, const key_type& y) const
00208     {
00209         return x.first < y;
00210     }
00211 
00212     result_type equal(const key_type& x, const key_type& y) const
00213     {
00214         return x == y;
00215     }
00216 };
00217 
00218 /*************************************************************************************************/
00219 
00220 template <typename KeyType, typename ValueType, std::size_t Size, typename Traits = static_table_traits<KeyType, ValueType> >
00221 struct static_table
00222 {
00223     typedef Traits                              traits_type;
00224     typedef typename traits_type::key_type      key_type;
00225     typedef typename traits_type::value_type    value_type;
00226     typedef typename traits_type::entry_type    entry_type;
00227 
00228     const value_type& operator()(const key_type& key) const
00229     {
00230         const entry_type* iter(adobe::lower_bound(table_m, key, traits_type()));
00231 
00232         if (iter == boost::end(table_m) || !traits_type().equal(iter->first, key))
00233             throw std::logic_error("static_table key not found");
00234 
00235         return iter->second;
00236     }
00237 
00238     bool operator()(const key_type& key, value_type& result) const
00239     {
00240         const entry_type* iter(adobe::lower_bound(table_m, key, traits_type()));
00241 
00242         if (iter == boost::end(table_m) || !traits_type().equal(iter->first, key))
00243             return false;
00244 
00245         result = iter->second;
00246 
00247         return true;
00248     }
00249 
00250     void sort()
00251     {
00252         adobe::sort(table_m, traits_type());
00253     }
00254 
00255 public:
00256     entry_type table_m[Size];
00257 };
00258 
00259 /*************************************************************************************************/
00260 
00261 } // namespace adobe
00262 
00263 /*************************************************************************************************/
00264 
00265 #endif // ADOBE_STATIC_TABLE_HPP
00266 
00267 /*************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google