shifted_inserter.hpp 2.58 KB
Newer Older
1
// Software License for MTL
2
3
//
// Copyright (c) 2007 The Trustees of Indiana University.
4
//               2008 Dresden University of Technology and the Trustees of Indiana University.
5
//               2010 SimuNova UG (haftungsbeschränkt), www.simunova.com.
6
7
// All rights reserved.
// Authors: Peter Gottschling and Andrew Lumsdaine
8
//
9
// This file is part of the Matrix Template Library
10
//
11
12
13
14
15
16
17
18
19
20
21
22
23
// See also license.mtl.txt in the distribution.

#ifndef MTL_MATRIX_SHIFTED_INSERTER_INCLUDE
#define MTL_MATRIX_SHIFTED_INSERTER_INCLUDE

#include <boost/numeric/mtl/concept/collection.hpp>
#include <boost/numeric/mtl/operation/update.hpp>

namespace mtl { namespace matrix {

/// Inserter with shifted row and column indices
/** The main work is performed by the underlying base inserter whose type is given as template
    argument. **/
24
template <typename BaseInserter>
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class shifted_inserter
{
  public:
    typedef shifted_inserter                                self;
    typedef typename BaseInserter::matrix_type              matrix_type;
    typedef typename Collection<matrix_type>::size_type     size_type;
    typedef operations::update_proxy<BaseInserter, size_type>   proxy_type;

    /// Constructor with matrix \p A, the slot size and the offsets
    shifted_inserter(matrix_type& A, size_type slot_size= 0,
		     size_type row_offset= 0, size_type col_offset= 0)
      : ins(A, slot_size), row_offset(row_offset), col_offset(col_offset) {}

    void set_row_offset(size_type ro) { row_offset= ro; } ///< Change row offset
    void set_col_offset(size_type co) { col_offset= co; } ///< Change column offset

    size_type get_row_offset() const { return row_offset; } ///< Get row offset
    size_type get_col_offset() const { return col_offset; } ///< Get column offset

  private:
    struct bracket_proxy
    {
	bracket_proxy(BaseInserter& ref, size_type row, size_type col_offset) : ref(ref), row(row), col_offset(col_offset) {}
48

49
50
51
52
53
54
	proxy_type operator[](size_type col)
	{   return proxy_type(ref, row, col+col_offset); }

	BaseInserter&   ref;
	size_type       row, col_offset;
    };
55

56
57
  public:
    /// To be used in ins[r][c] << value;
58
59
    bracket_proxy operator[] (size_type row)
    {	return bracket_proxy(ins, row+row_offset, col_offset);    }
60
61

    /// To be used in ins(r, c) << value;
62
    proxy_type operator() (size_type row, size_type col)
63
64
65
66
67
68
69
70
71
72
73
74
    {	return proxy_type(ins, row+row_offset, col+col_offset);    }

    // update, modify and operator<< are used from BaseInserter

  private:
    BaseInserter ins;
    size_type    row_offset, col_offset;
};

}} // namespace mtl::matrix

#endif // MTL_MATRIX_SHIFTED_INSERTER_INCLUDE