DUNE-FEM (unstable)

commindexmap.hh
1#ifndef DUNE_FEM_COMMINDEXMAP_HH
2#define DUNE_FEM_COMMINDEXMAP_HH
3
4#include <set>
5#include <vector>
6
7#include <dune/fem/storage/dynamicarray.hh>
8
9namespace Dune
10{
11
12 namespace Fem
13 {
14
15 class CommunicationIndexMap
16 {
17 public:
18 // this typedef will also change the type of index
19 // for AuxiliaryDofs and AdaptiveLeafIndexSet
20 // TODO: extract to a more common place
21 typedef int IndexType ;
22 private:
24
25 static const IndexType invalidIndex = IndexType(-1);
26
27 public:
29 CommunicationIndexMap()
30 : indices_( 0 )
31 {
32 indices_.setMemoryFactor( 1.1 );
33 }
34
35 CommunicationIndexMap( const CommunicationIndexMap& ) = delete;
36
38 const IndexType& operator [] ( const size_t i ) const
39 {
40 assert( i < size() );
41 return indices_[ i ];
42 }
43
45 void erase( const size_t i )
46 {
47 indices_[ i ] = invalidIndex;
48 }
49
51 void clear()
52 {
53 resize( 0 );
54 }
55
58 template <class GlobalKey>
59 void insert( const std :: vector< GlobalKey > &idx )
60 {
61 const size_t size = idx.size();
62 size_t count = indices_.size();
63
64 // reserve memory
65 resize( count + size );
66 assert( indices_.size() == (count + size) );
67
68 // copy indices to index vector
69 for( size_t i = 0; i < size; ++i, ++count )
70 {
71 assert( idx[ i ] >= 0 );
72 indices_[ count ] = idx[ i ];
73 }
74 }
75
77 template <class GlobalKey>
78 void set( const std :: set< GlobalKey > &idxSet )
79 {
80 // resize to given new size
81 resize( idxSet.size() );
82
83 // copy all elements from set to array
84 size_t count = 0;
85 typedef typename std :: set< GlobalKey > :: const_iterator iterator;
86 const iterator end = idxSet.end();
87 for(iterator it = idxSet.begin(); it != end; ++it, ++count)
88 {
89 indices_[count] = *it;
90 }
91 }
92
94 size_t size () const
95 {
96 return indices_.size();
97 }
98
100 void print( std :: ostream &s, int rank ) const
101 {
102 const size_t size = this->size();
103 s << "Start print: size = " << size << std :: endl;
104 for( size_t i = 0; i < size; ++i )
105 s << rank << " idx[ " << i << " ] = " << indices_[ i ] << std :: endl;
106 s << "End of Array" << std :: endl;
107 }
108
110 template <class CommBuffer>
111 void writeToBuffer(CommBuffer& buffer) const
112 {
113 const size_t idxSize = indices_.size();
114 buffer.write( idxSize );
115 //std::cout << "P[" << MPIManager ::rank() << " write Buffer size " << idxSize << std::endl;
116 for(size_t i=0; i<idxSize; ++i)
117 {
118 //std::cout << "P[" << MPIManager ::rank() << " write idx " << indices_[i] << std::endl;
119 buffer.write( indices_[i] );
120 }
121 }
122
124 template <class CommBuffer>
125 void readFromBuffer(CommBuffer& buffer)
126 {
127 size_t idxSize;
128 buffer.read( idxSize );
129 //std::cout << "P[" << MPIManager ::rank() << " read Buffer size " << idxSize << std::endl;
130 indices_.resize( idxSize );
131 for(size_t i=0; i<idxSize; ++i)
132 {
133 buffer.read( indices_[i] );
134 //std::cout << "P[" << MPIManager ::rank() << " read idx " << indices_[i] << std::endl;
135 }
136 }
137
139 void compress()
140 {
141 size_t writePos = 0;
142 const size_t idxSize = indices_.size();
143 for(size_t i=0; i<idxSize; ++i)
144 {
145 if( indices_[ i ] != invalidIndex )
146 {
147 if( i != writePos )
148 indices_[ writePos ] = indices_[ i ];
149 writePos++;
150 }
151 }
152 indices_.resize( writePos );
153 }
154
155 protected:
157 inline void resize ( size_t size )
158 {
159 indices_.resize( size );
160 }
161
162 inline void reserve ( size_t size )
163 {
164 indices_.reserve( size );
165 }
166
167 };
168
169 } // namespace Fem
170
171} // namespace Dune
172
173#endif // #ifndef DUNE_FEM_COMMINDEXMAP_HH
void setMemoryFactor(double memFactor)
set memory factor
Definition: dynamicarray.hh:296
void reserve(size_type mSize)
Definition: dynamicarray.hh:371
void resize(size_type nsize)
Definition: dynamicarray.hh:334
size_type size() const
return size of array
Definition: dynamicarray.hh:170
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 27, 22:29, 2024)