libMesh::vectormap< Key, Tp > Class Template Reference

#include <vectormap.h>

Inheritance diagram for libMesh::vectormap< Key, Tp >:

Classes

struct  FirstCompare
 
struct  FirstOrder
 

Public Types

typedef Key key_type
 
typedef Tp mapped_type
 
typedef std::pair< Key, Tp > value_type
 
typedef std::vector< value_typevector_type
 

Public Member Functions

 vectormap ()
 
 vectormap (const vectormap< Key, Tp > &other)
 
void insert (const value_type &x)
 
void sort ()
 
const Tp & operator[] (const key_type &key) const
 
unsigned int count (const key_type &key) const
 

Private Attributes

bool _sorted
 

Detailed Description

template<typename Key, typename Tp>
class libMesh::vectormap< Key, Tp >

This vectormap templated class is intended to provide the performance characteristics of a sorted std::vector with an interface more closely resembling that of a std::map, for use in particular when memory is tight.

This class is limited in its applicability. The typical use case is:

vectormap<KeyType,ValType> vmap;
for ( ; ;)
vmap.insert (std::make_pair(key,val));

val1 = vmap[key1];
val2 = vmap[key2];

Note in particular the two-phase usage. It is not advised to do intermediate insert/lookups, as each time an insertion is done the sort is invalidated, and must be reperformed. Additionally, during the insersion phase, there is no accounting for duplicate keys. Each insertion is accepted regardless of key value, and then duplicate keys are removed later.

Author
Benjamin S. Kirk

Definition at line 61 of file vectormap.h.

Member Typedef Documentation

template<typename Key, typename Tp>
typedef Key libMesh::vectormap< Key, Tp >::key_type

Definition at line 66 of file vectormap.h.

template<typename Key, typename Tp>
typedef Tp libMesh::vectormap< Key, Tp >::mapped_type

Definition at line 67 of file vectormap.h.

template<typename Key, typename Tp>
typedef std::pair<Key, Tp> libMesh::vectormap< Key, Tp >::value_type

Definition at line 68 of file vectormap.h.

template<typename Key, typename Tp>
typedef std::vector<value_type> libMesh::vectormap< Key, Tp >::vector_type

Definition at line 69 of file vectormap.h.

Constructor & Destructor Documentation

template<typename Key, typename Tp>
libMesh::vectormap< Key, Tp >::vectormap ( )
inline

Default constructor. Initializes sorted member to false.

Definition at line 98 of file vectormap.h.

98  :
99  _sorted(false)
100  {}
template<typename Key, typename Tp>
libMesh::vectormap< Key, Tp >::vectormap ( const vectormap< Key, Tp > &  other)
inline

Copy constructor.

Definition at line 105 of file vectormap.h.

105  :
106  std::vector<std::pair<Key, Tp> > (other),
107  _sorted(other._sorted)
108  {}

Member Function Documentation

template<typename Key, typename Tp>
unsigned int libMesh::vectormap< Key, Tp >::count ( const key_type key) const
inline

*returns the number of occurances of key. For a map-like object, this should be 1 or 0.

Definition at line 162 of file vectormap.h.

Referenced by libMesh::MetisPartitioner::_do_partition(), libMesh::ParmetisPartitioner::assign_partitioning(), libMesh::ParmetisPartitioner::build_graph(), and libMesh::ParmetisPartitioner::initialize().

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,
175  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  }
template<typename Key, typename Tp>
void libMesh::vectormap< Key, Tp >::insert ( const value_type x)
inline

Inserts x into the vectormap.

Definition at line 113 of file vectormap.h.

Referenced by libMesh::MetisPartitioner::_do_partition(), and libMesh::ParmetisPartitioner::initialize().

114  {
115  _sorted = false;
116  this->push_back(x);
117  }
template<typename Key, typename Tp>
const Tp& libMesh::vectormap< Key, Tp >::operator[] ( const key_type key) const
inline
Returns
the value corresponding to key

Definition at line 137 of file vectormap.h.

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 
149  typename vectormap<Key,Tp>::const_iterator
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  }
template<typename Key, typename Tp>
void libMesh::vectormap< Key, Tp >::sort ( )
inline

Sort & unique the vectormap, preparing for use.

Definition at line 122 of file vectormap.h.

Referenced by libMesh::vectormap< dof_id_type, dof_id_type >::count(), and libMesh::vectormap< dof_id_type, dof_id_type >::operator[]().

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  }

Member Data Documentation


The documentation for this class was generated from the following file:

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

Hosted By:
SourceForge.net Logo