BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Unique_hash_map.h
Go to the documentation of this file.
00001 // Copyright (c) 1997-2000  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Hash_map/include/CGAL/Unique_hash_map.h $
00019 // $Id: Unique_hash_map.h 28567 2006-02-16 14:30:13Z lsaboret $
00020 // 
00021 //
00022 // Author(s)     : Michael Seel <seel@mpi-sb.mpg.de>
00023 //                 Lutz Kettner <kettner@inf.ethz.ch>
00024 
00025 #ifndef CGAL_UNIQUE_HASH_MAP_H
00026 #define CGAL_UNIQUE_HASH_MAP_H
00027 
00028 #include <CGAL/basic.h>
00029 #include <CGAL/Handle_hash_function.h>
00030 #include <CGAL/Tools/chained_map.h>
00031 #include <cstddef>
00032 
00033 CGAL_BEGIN_NAMESPACE
00034 
00035 template <class Key_, class Data_, 
00036           class UniqueHashFunction = Handle_hash_function>
00037 class Unique_hash_map {
00038 public:
00039     typedef Key_                                     Key;
00040     typedef Data_                                    Data;
00041     typedef UniqueHashFunction                       Hash_function;
00042 
00043     // STL compliance
00044     typedef Key_                                     key_type;
00045     typedef Data_                                    data_type;
00046     typedef UniqueHashFunction                       hasher;
00047 
00048     typedef Unique_hash_map<Key,Data,Hash_function>  Self;
00049 
00050 private:
00051     typedef CGALi::chained_map<Data>                 Map;
00052     typedef typename Map::item                       Item;
00053 
00054 private:
00055     Hash_function  m_hash_function;
00056     Map            m_map;
00057 
00058 public:
00059 
00060     Unique_hash_map() { m_map.xdef() = Data(); }
00061 
00062     Unique_hash_map( const Data& deflt, std::size_t table_size = 1)
00063         : m_map( table_size) { m_map.xdef() = deflt; }
00064     
00065     Unique_hash_map( const Data& deflt,
00066                      std::size_t table_size,
00067                      const Hash_function& fct)
00068         : m_hash_function(fct), m_map( table_size) { m_map.xdef() = deflt; }
00069     
00070     Unique_hash_map( Key first1, Key beyond1, Data first2) {
00071         m_map.xdef() = Data();
00072         insert( first1, beyond1, first2);
00073     }
00074     Unique_hash_map( Key first1, Key beyond1, Data first2,
00075                      const Data& deflt,
00076                      std::size_t table_size   = 1,
00077                      const Hash_function& fct = Hash_function())
00078     : m_hash_function(fct), m_map( table_size) { 
00079         m_map.xdef() = deflt;
00080         insert( first1, beyond1, first2);
00081     }
00082 
00083     Data default_value() const { return m_map.cxdef(); }
00084 
00085     Hash_function  hash_function() const { return m_hash_function; }
00086 
00087     void clear() { m_map.clear(); }
00088 
00089     void clear( const Data& deflt) {
00090         m_map.clear();
00091         m_map.xdef() = deflt; }
00092 
00093     bool is_defined( const Key& key) const { 
00094         return m_map.lookup( m_hash_function(key)) != 0; 
00095     }
00096 
00097     const Data& operator[]( const Key& key) const {
00098         Item p = m_map.lookup( m_hash_function(key));
00099         if ( p != 0 )
00100             return m_map.inf(p);
00101         return m_map.cxdef();
00102     }
00103 
00104     Data& operator[]( const Key& key) {
00105         return m_map.access( m_hash_function(key)); 
00106     }
00107 
00108     Data insert( Key first1, Key beyond1, Data first2) {
00109         for ( ; first1 != beyond1; (++first1, ++first2)) {
00110             operator[]( first1) = first2;
00111         }
00112         return first2;
00113     }
00114 
00115     void statistics() const { m_map.statistics(); }
00116 };
00117 
00118 
00119 
00120 CGAL_END_NAMESPACE
00121 
00122 namespace boost {
00123   template <typename UniquePairAssociativeContainer>
00124   class associative_property_map;
00125 
00126   struct lvalue_property_map_tag;
00127 
00128   template <typename KeyType, typename ValueType>
00129   class associative_property_map<CGAL::Unique_hash_map<KeyType,ValueType> >
00130   {
00131     typedef CGAL::Unique_hash_map<KeyType,ValueType> C;
00132   public:
00133     typedef KeyType key_type;
00134     typedef ValueType value_type;
00135     typedef value_type& reference;
00136     typedef lvalue_property_map_tag category;
00137     associative_property_map() : m_c(0) { }
00138     associative_property_map(C& c) : m_c(&c) { }
00139     reference operator[](const key_type& k) const {
00140       return (*m_c)[k];
00141     }
00142   private:
00143     C* m_c;
00144   };
00145 
00146 
00147   template <typename KeyType, typename ValueType>
00148   associative_property_map<CGAL::Unique_hash_map<KeyType,ValueType> >
00149   make_assoc_property_map(CGAL::Unique_hash_map<KeyType,ValueType> & c)
00150   {
00151     return associative_property_map<CGAL::Unique_hash_map<KeyType,ValueType> >(c);
00152   }
00153   
00154   
00155   template <typename KeyType, typename ValueType>
00156   ValueType&  get(const associative_property_map<CGAL::Unique_hash_map<KeyType,ValueType> >& uhm, const KeyType& key)
00157   {
00158     return uhm[key];
00159   }
00160   
00161   template <typename KeyType, typename ValueType>
00162   void put(associative_property_map<CGAL::Unique_hash_map<KeyType,ValueType> >& uhm, const KeyType& key, const ValueType& val)
00163   {
00164     uhm[key] = val;
00165   }
00166 
00167 
00168 }
00169 
00170 
00171 
00172 #endif // CGAL_UNIQUE_HASH_MAP_H
00173 // EOF
00174 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines