MatrixSize.hpp 1.32 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

#include <functional>
#include <memory>

#include <amdis/Observer.hpp>
#include <amdis/common/Index.hpp>

namespace AMDiS
{
  class MatrixSize
      : private ObserverSequence<event::adapt,2>
  {
    struct BasisCallback
    {
      template <class Basis>
      explicit BasisCallback(Basis const* basis)
        : dimension([basis](){ return basis->dimension(); })
      {}

      std::function<std::size_t()> dimension;
    };

  public:
    template <class RowBasis, class ColBasis>
    MatrixSize(RowBasis const& rowBasis, ColBasis const& colBasis)
      : ObserverSequence<event::adapt,2>(rowBasis, colBasis)
      , rowBasis_(&rowBasis)
      , colBasis_(&colBasis)
    {
      updateImpl(event::adapt{true}, index_t<0>{});
      updateImpl(event::adapt{true}, index_t<1>{});
    }

    /// Number of rows in the matrix
    std::size_t rows() const
    {
      return rows_;
    }

    /// Number of columns in the matrix
    std::size_t cols() const
    {
      return cols_;
    }

  protected:

    void updateImpl(event::adapt e, index_t<0> i) final { rows_ = rowBasis_.dimension(); }
    void updateImpl(event::adapt e, index_t<1> i) final { cols_ = colBasis_.dimension(); }

  private:
    BasisCallback rowBasis_;
    BasisCallback colBasis_;

    std::size_t rows_;
    std::size_t cols_;
  };

} // end namespace AMDiS