vectormap.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_VECTORMAP_H
21 #define LIBMESH_VECTORMAP_H
22 
23 // C++ Includes -----------------------------------
24 #include <vector>
25 #include <algorithm>
26 
27 // libMesh includes
28 #include "libmesh/libmesh_common.h"
29 
30 
31 namespace libMesh
32 {
33 
60  template <typename Key, typename Tp>
61  class vectormap : public std::vector<std::pair<Key, Tp> >
62  {
63 
64  public:
65 
66  typedef Key key_type;
67  typedef Tp mapped_type;
68  typedef std::pair<Key, Tp> value_type;
69  typedef std::vector<value_type> vector_type;
70 
71  private:
72 
76  struct FirstOrder
77  {
78  bool operator()(const value_type &lhs,
79  const value_type &rhs) const
80  { return lhs.first < rhs.first; }
81  };
82 
86  struct FirstCompare
87  {
88  bool operator()(const value_type &lhs,
89  const value_type &rhs) const
90  { return lhs.first == rhs.first; }
91  };
92 
93  public:
94 
99  _sorted(false)
100  {}
101 
105  vectormap(const vectormap<Key,Tp> &other) :
106  std::vector<std::pair<Key, Tp> > (other),
107  _sorted(other._sorted)
108  {}
109 
113  void insert (const value_type &x)
114  {
115  _sorted = false;
116  this->push_back(x);
117  }
118 
122  void sort()
123  {
124  FirstOrder order;
125  FirstCompare comp;
126 
127  std::sort (this->begin(), this->end(), order);
128 
129  this->erase (std::unique (this->begin(), this->end(), comp), this->end());
130 
131  _sorted = true;
132  }
133 
137  const Tp & operator[](const key_type &key) const
138  {
139  if (!_sorted)
140  const_cast<vectormap<Key, Tp>*>(this)->sort();
141 
143 
144  value_type to_find;
145  to_find.first = key;
146 
147  FirstOrder order;
148 
150  lower_bound = std::lower_bound (this->begin(), this->end(), to_find, order);
151 
152  libmesh_assert (lower_bound != this->end());
153  libmesh_assert_equal_to (lower_bound->first, key);
154 
155  return lower_bound->second;
156  }
157 
162  unsigned int count (const key_type &key) const
163  {
164  if (!_sorted)
165  const_cast<vectormap<Key, Tp>*>(this)->sort();
166 
168 
169  value_type to_find;
170  to_find.first = key;
171 
172  FirstOrder order;
173 
174  std::pair<typename vectormap<Key,Tp>::const_iterator,
176  bounds = std::equal_range (this->begin(), this->end(), to_find, order);
177 
178  return std::distance (bounds.first, bounds.second);
179  }
180 
181 
182  private:
183 
184  bool _sorted;
185  };
186 
187 } // namespace libMesh
188 
189 #endif // LIBMESH_VECTORMAP_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:07 UTC

Hosted By:
SourceForge.net Logo