dune-fem  2.4.1-rc
commindexmap.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_COMMINDEXMAP_HH
2 #define DUNE_FEM_COMMINDEXMAP_HH
3 
4 //- system includes
5 #include <set>
6 #include <vector>
7 
8 //- Dune includes
10 
11 namespace Dune
12 {
13 
14  namespace Fem
15  {
16 
18  {
19  typedef int IndexType ;
20  private:
22 
23  public:
26  : indices_( 0 )
27  {
28  indices_.setMemoryFactor( 1.1 );
29  }
30 
31  private:
32  // prohibit copying
34 
35  public:
37  const IndexType& operator [] ( const size_t i ) const
38  {
39  assert( i < size() );
40  return indices_[ i ];
41  }
42 
44  void clear()
45  {
46  resize( 0 );
47  }
48 
51  template <class GlobalKey>
52  void insert( const std :: vector< GlobalKey > &idx )
53  {
54  const size_t size = idx.size();
55  size_t count = indices_.size();
56 
57  // reserve memory
58  resize( count + size );
59  assert( indices_.size() == (count + size) );
60 
61  // copy indices to index vector
62  for( size_t i = 0; i < size; ++i, ++count )
63  {
64  assert( idx[ i ] >= 0 );
65  indices_[ count ] = idx[ i ];
66  }
67  }
68 
70  template <class GlobalKey>
71  void set( const std :: set< GlobalKey > &idxSet )
72  {
73  // resize to given new size
74  resize( idxSet.size() );
75 
76  // copy all elements from set to array
77  size_t count = 0;
78  typedef typename std :: set< GlobalKey > :: const_iterator iterator;
79  const iterator end = idxSet.end();
80  for(iterator it = idxSet.begin(); it != end; ++it, ++count)
81  {
82  indices_[count] = *it;
83  }
84  }
85 
87  size_t size () const
88  {
89  return indices_.size();
90  }
91 
93  void print( std :: ostream &s, int rank ) const
94  {
95  const size_t size = this->size();
96  s << "Start print: size = " << size << std :: endl;
97  for( size_t i = 0; i < size; ++i )
98  s << rank << " idx[ " << i << " ] = " << indices_[ i ] << std :: endl;
99  s << "End of Array" << std :: endl;
100  }
101 
103  template <class CommBuffer>
104  void writeToBuffer(CommBuffer& buffer) const
105  {
106  const size_t idxSize = indices_.size();
107  buffer.write( idxSize );
108  //std::cout << "P[" << MPIManager ::rank() << " write Buffer size " << idxSize << std::endl;
109  for(size_t i=0; i<idxSize; ++i)
110  {
111  //std::cout << "P[" << MPIManager ::rank() << " write idx " << indices_[i] << std::endl;
112  buffer.write( indices_[i] );
113  }
114  }
115 
117  template <class CommBuffer>
118  void readFromBuffer(CommBuffer& buffer)
119  {
120  size_t idxSize;
121  buffer.read( idxSize );
122  //std::cout << "P[" << MPIManager ::rank() << " read Buffer size " << idxSize << std::endl;
123  indices_.resize( idxSize );
124  for(size_t i=0; i<idxSize; ++i)
125  {
126  buffer.read( indices_[i] );
127  //std::cout << "P[" << MPIManager ::rank() << " read idx " << indices_[i] << std::endl;
128  }
129  }
130 
131  protected:
133  inline void resize ( size_t size )
134  {
135  indices_.resize( size );
136  }
137 
138  inline void reserve ( size_t size )
139  {
140  indices_.reserve( size );
141  }
142 
143  };
144 
145  } // namespace Fem
146 
147 } // namespace Dune
148 
149 #endif // #ifndef DUNE_FEM_COMMINDEXMAP_HH
size_t size() const
return number of enties of array
Definition: arrays.hh:213
void resize(size_t nsize)
Definition: arrays.hh:491
size_t size() const
return size of map
Definition: commindexmap.hh:87
void print(std::ostream &s, int rank) const
print map for debugging only
Definition: commindexmap.hh:93
void insert(const std::vector< GlobalKey > &idx)
Definition: commindexmap.hh:52
void clear()
clear index map
Definition: commindexmap.hh:44
Definition: coordinate.hh:4
const IndexType & operator[](const size_t i) const
return index map for entry i
Definition: commindexmap.hh:37
void reserve(size_t mSize)
Definition: arrays.hh:517
void setMemoryFactor(const double memFactor)
set memory factor
Definition: arrays.hh:465
Definition: commindexmap.hh:17
void resize(size_t size)
resize map with size size
Definition: commindexmap.hh:133
CommunicationIndexMap()
constructor creating empty map
Definition: commindexmap.hh:25
void reserve(size_t size)
Definition: commindexmap.hh:138
void writeToBuffer(CommBuffer &buffer) const
write all indices to buffer
Definition: commindexmap.hh:104
void readFromBuffer(CommBuffer &buffer)
read all indices from buffer
Definition: commindexmap.hh:118