dune-spgrid  2.7
cachedpartitionlist.hh
Go to the documentation of this file.
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 
9 
10 namespace Dune
11 {
12 
13  // SPCachedPartitionList
14  // ---------------------
15 
16  template< int dim >
18  : public SPPartitionList< dim >
19  {
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 
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
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 >
117  {
118  // find new cache size
119  first_ = std::numeric_limits< unsigned int >::max();
120  last_ = std::numeric_limits< unsigned int >::min();
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
Definition: iostream.hh:7
Definition: cachedpartitionlist.hh:19
void updateCache()
Definition: cachedpartitionlist.hh:116
const This & operator=(const This &other)
Definition: cachedpartitionlist.hh:45
SPCachedPartitionList()
Definition: cachedpartitionlist.hh:30
SPCachedPartitionList(const This &other)
Definition: cachedpartitionlist.hh:36
Base::MultiIndex MultiIndex
Definition: cachedpartitionlist.hh:27
const Partition & partition(const unsigned int number) const
Definition: cachedpartitionlist.hh:94
Base::Node Node
Definition: cachedpartitionlist.hh:24
bool contains(const MultiIndex &id, const unsigned int number) const
Definition: cachedpartitionlist.hh:83
bool contains(const unsigned int number) const
Definition: cachedpartitionlist.hh:73
unsigned int maxNumber() const
Definition: cachedpartitionlist.hh:109
Base::Partition Partition
Definition: cachedpartitionlist.hh:28
~SPCachedPartitionList()
Definition: cachedpartitionlist.hh:43
unsigned int minNumber() const
Definition: cachedpartitionlist.hh:102
Definition: partition.hh:79
unsigned int number() const
Definition: partition.hh:242
Definition: partitionlist.hh:16
Definition: partitionlist.hh:76
const Node * next() const
Definition: partitionlist.hh:109
const Partition & partition() const
Definition: partitionlist.hh:107