Dune Core Modules (2.9.0)

cachedpartitionlist.hh
1#ifndef DUNE_SPGRID_CACHEDPARTITIONLIST_HH
2#define DUNE_SPGRID_CACHEDPARTITIONLIST_HH
3
4#include <limits>
5
6#include <dune/grid/common/exceptions.hh>
7
8#include <dune/grid/spgrid/partitionlist.hh>
9
10namespace Dune
11{
12
13 // SPCachedPartitionList
14 // ---------------------
15
16 template< int dim >
17 class SPCachedPartitionList
18 : public SPPartitionList< dim >
19 {
20 typedef SPCachedPartitionList< dim > This;
21 typedef SPPartitionList< dim > Base;
22
23 protected:
24 typedef typename Base::Node Node;
25
26 public:
27 typedef typename Base::MultiIndex MultiIndex;
28 typedef typename Base::Partition Partition;
29
30 SPCachedPartitionList ()
31 : first_( std::numeric_limits< unsigned int >::max() ),
32 last_( std::numeric_limits< unsigned int >::min() ),
33 cache_( nullptr )
34 {}
35
36 SPCachedPartitionList ( const This &other )
37 : Base( other ),
38 cache_( nullptr )
39 {
40 updateCache();
41 }
42
43 ~SPCachedPartitionList () { delete[] cache_; }
44
45 const This &operator= ( const This &other )
46 {
47 *(Base *)this = other;
48 updateCache();
49 return *this;
50 }
51
52 bool contains ( const unsigned int number ) const;
53 bool contains ( const MultiIndex &id, const unsigned int number ) const;
54 const Partition &partition ( const unsigned int number ) const;
55
56 unsigned int minNumber () const;
57 unsigned int maxNumber () const;
58
59 void updateCache ();
60
61 private:
62 unsigned int first_, last_;
63 const Node **cache_;
64 };
65
66
67
68 // Implementation of SPCachedPartitionList
69 // ---------------------------------------
70
71 template< int dim >
72 inline bool
73 SPCachedPartitionList< dim >::contains ( const unsigned int number ) const
74 {
75 const bool inRange = (number >= minNumber()) && (number <= maxNumber());
76 return inRange && (cache_[ number - minNumber() ] != 0);
77 }
78
79
80 template< int dim >
81 inline bool
82 SPCachedPartitionList< dim >
83 ::contains ( const MultiIndex &id, const unsigned int number ) const
84 {
85 if( contains( number ) )
86 return cache_[ number - minNumber() ]->partition().contains( id );
87 else
88 return false;
89 }
90
91
92 template< int dim >
93 inline const typename SPCachedPartitionList< dim >::Partition &
94 SPCachedPartitionList< dim >::partition ( const unsigned int number ) const
95 {
96 assert( contains( number ) );
97 return cache_[ number - minNumber() ]->partition();
98 }
99
100
101 template< int dim >
102 inline unsigned int SPCachedPartitionList< dim >::minNumber () const
103 {
104 return first_;
105 }
106
107
108 template< int dim >
109 inline unsigned int SPCachedPartitionList< dim >::maxNumber () const
110 {
111 return last_;
112 }
113
114
115 template< int dim >
116 inline void SPCachedPartitionList< dim >::updateCache ()
117 {
118 // find new cache size
121 for( const Node *it = Base::head_; it; it = it->next() )
122 {
123 first_ = std::min( first_, it->partition().number() );
124 last_ = std::max( last_, it->partition().number() );
125 }
126
127 // create new empty cache
128 delete[] cache_;
129 const unsigned int cacheSize = last_ - first_ + 1;
130 cache_ = new const Node *[ cacheSize ];
131 for( unsigned int i = 0; i < cacheSize; ++i )
132 cache_[ i ] = 0;
133
134 // fill cache
135 for( const Node *it = Base::head_; it; it = it->next() )
136 {
137 const unsigned int number = it->partition().number();
138 if( cache_[ number - first_ ] != 0 )
139 DUNE_THROW( GridError, "Partition number " << number << " is not unique." );
140 cache_[ number - first_ ] = it;
141 }
142 }
143
144} // namespace Dune
145
146#endif // #ifndef DUNE_SPGRID_CACHEDPARTITIONLIST_HH
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
auto min(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::min()
Definition: defaults.hh:89
auto max(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::max()
Definition: defaults.hh:81
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)