BWAPI
|
00001 // ============================================================================= 00002 // 00003 // Copyright (c) 2001-2007 Max-Planck-Institute Saarbruecken (Germany). 00004 // All rights reserved. 00005 // 00006 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or 00007 // modify it under the terms of the GNU Lesser General Public License as 00008 // published by the Free Software Foundation; version 2.1 of the License. 00009 // See the file LICENSE.LGPL distributed with CGAL. 00010 // 00011 // Licensees holding a valid commercial license may use this file in 00012 // accordance with the commercial license agreement provided with the software. 00013 // 00014 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00015 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00016 // 00017 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/STL_Extension/include/CGAL/Cache.h $ 00018 // $Id: Cache.h 37883 2007-04-03 16:07:32Z ameyer $ 00019 // 00020 // Author(s) : Michael Hemmer <hemmer@mpi-inf.mpg.de> 00021 // 00022 // ============================================================================= 00023 00024 #ifndef CGAL_CACHE_H 00025 #define CGAL_CACHE_H 1 00026 00027 #include <CGAL/basic.h> 00028 #include <CGAL/function_objects.h> 00029 00030 #include <map> 00031 00032 CGAL_BEGIN_NAMESPACE 00033 00052 template < class Input_, 00053 class Output_, 00054 class Creator_ = Creator_1<Input_, Output_>, 00055 class Canonicalizer_ = Creator_1<Input_, Input_>, 00056 class Compare_ = std::less<Input_> > 00057 class Cache{ 00058 public: 00060 typedef Input_ Input; 00061 00063 typedef Output_ Output; 00064 00068 typedef Creator_ Creator; 00069 00073 typedef Canonicalizer_ Canonicalizer; 00074 00076 typedef Compare_ Compare; 00077 00078 typedef Cache<Input,Output,Creator,Canonicalizer,Compare> Self; 00079 private: 00080 typedef std::map<Input,Output,Compare> Map; 00081 Map map; 00082 public: 00083 typedef Map _Rep_type; 00085 typedef typename _Rep_type::iterator Iterator; 00087 typedef typename _Rep_type::const_iterator Const_iterator; 00089 typedef typename _Rep_type::reverse_iterator Reverse_iterator; 00091 typedef typename _Rep_type::const_reverse_iterator Const_reverse_iterator; 00093 typedef typename _Rep_type::size_type Size_type; 00094 public: 00096 Cache() : map() {}; 00097 00103 Output operator () (const Input& input) { 00104 Canonicalizer canonicalize; 00105 Input key = canonicalize(input); 00106 typename Map::iterator it = map.find(key); 00107 if (it == map.end()) { 00108 Creator create; 00109 Output out = create(key); 00110 map.insert(it,typename Map::value_type(key,out)); 00111 return out; 00112 } else { 00113 return (*it).second; 00114 } 00115 } 00116 00118 void clear() { map.clear(); } 00119 00121 bool is_empty() const { return map.empty(); } 00122 00124 Size_type size() { return map.size(); } 00125 00127 Size_type max_size() const { return map.max_size(); } 00128 00130 Iterator begin() { return map.begin(); } 00131 00133 Iterator end() { return map.end(); } 00134 00136 Const_iterator begin() const { return map.begin(); } 00137 00139 Const_iterator end() const { return map.end(); } 00140 00144 Reverse_iterator rbegin() { return map.rbegin(); } 00145 00149 Reverse_iterator rend() { return map.rend(); } 00150 00154 Const_reverse_iterator rbegin() const { return map.rbegin(); } 00155 00159 Const_reverse_iterator rend() const { return map.rend(); } 00160 00161 }; 00162 00163 CGAL_END_NAMESPACE 00164 00165 #endif