$extrastylesheet
00001 // The libMesh Finite Element Library. 00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 00003 00004 // This library is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public 00006 // License as published by the Free Software Foundation; either 00007 // version 2.1 of the License, or (at your option) any later version. 00008 00009 // This library 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 // Lesser General Public License for more details. 00013 00014 // You should have received a copy of the GNU Lesser General Public 00015 // License along with this library; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 00019 00020 #ifndef LIBMESH_VECTORMAP_H 00021 #define LIBMESH_VECTORMAP_H 00022 00023 // C++ Includes ----------------------------------- 00024 #include <vector> 00025 #include <algorithm> 00026 00027 // libMesh includes 00028 #include "libmesh/libmesh_common.h" 00029 00030 00031 namespace libMesh 00032 { 00033 00060 template <typename Key, typename Tp> 00061 class vectormap : public std::vector<std::pair<Key, Tp> > 00062 { 00063 00064 public: 00065 00066 typedef Key key_type; 00067 typedef Tp mapped_type; 00068 typedef std::pair<Key, Tp> value_type; 00069 typedef std::vector<value_type> vector_type; 00070 typedef typename vector_type::difference_type difference_type; 00071 00072 private: 00073 00077 struct FirstOrder 00078 { 00079 bool operator()(const value_type &lhs, 00080 const value_type &rhs) const 00081 { return lhs.first < rhs.first; } 00082 }; 00083 00087 struct FirstCompare 00088 { 00089 bool operator()(const value_type &lhs, 00090 const value_type &rhs) const 00091 { return lhs.first == rhs.first; } 00092 }; 00093 00094 public: 00095 00099 vectormap() : 00100 _sorted(false) 00101 {} 00102 00106 vectormap(const vectormap<Key,Tp> &other) : 00107 std::vector<std::pair<Key, Tp> > (other), 00108 _sorted(other._sorted) 00109 {} 00110 00114 void insert (const value_type &x) 00115 { 00116 _sorted = false; 00117 this->push_back(x); 00118 } 00119 00123 void sort() 00124 { 00125 FirstOrder order; 00126 FirstCompare comp; 00127 00128 std::sort (this->begin(), this->end(), order); 00129 00130 this->erase (std::unique (this->begin(), this->end(), comp), this->end()); 00131 00132 _sorted = true; 00133 } 00134 00138 const Tp & operator[](const key_type &key) const 00139 { 00140 if (!_sorted) 00141 const_cast<vectormap<Key, Tp>*>(this)->sort(); 00142 00143 libmesh_assert (_sorted); 00144 00145 value_type to_find; 00146 to_find.first = key; 00147 00148 FirstOrder order; 00149 00150 typename vectormap<Key,Tp>::const_iterator 00151 lower_bound = std::lower_bound (this->begin(), this->end(), to_find, order); 00152 00153 libmesh_assert (lower_bound != this->end()); 00154 libmesh_assert_equal_to (lower_bound->first, key); 00155 00156 return lower_bound->second; 00157 } 00158 00163 difference_type 00164 count (const key_type &key) const 00165 { 00166 if (!_sorted) 00167 const_cast<vectormap<Key, Tp>*>(this)->sort(); 00168 00169 libmesh_assert (_sorted); 00170 00171 value_type to_find; 00172 to_find.first = key; 00173 00174 FirstOrder order; 00175 00176 std::pair<typename vectormap<Key,Tp>::const_iterator, 00177 typename vectormap<Key,Tp>::const_iterator> 00178 bounds = std::equal_range (this->begin(), this->end(), to_find, order); 00179 00180 return std::distance (bounds.first, bounds.second); 00181 } 00182 00183 00184 private: 00185 00186 bool _sorted; 00187 }; 00188 00189 } // namespace libMesh 00190 00191 #endif // LIBMESH_VECTORMAP_H