dimension.hpp 2.95 KB
Newer Older
1
// Software License for MTL
2
//
3
4
5
6
7
// Copyright (c) 2007 The Trustees of Indiana University.
//               2008 Dresden University of Technology and the Trustees of Indiana University.
//               2010 SimuNova UG (haftungsbeschränkt), www.simunova.com.
// 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
// See also license.mtl.txt in the distribution.

#ifndef MTL_DIMENSIONS_INCLUDE
#define MTL_DIMENSIONS_INCLUDE

#include <iostream>
#include <cassert>
#include <boost/mpl/if.hpp>
#include <boost/utility/enable_if.hpp>

namespace mtl {

23
// dimension is a type for declaring matrix dimensions
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// num_rows() and num_cols() return the number or rows and columns
// is_static says whether it is declared at compile time or not

// Compile time version
namespace fixed
{

    /// Compile-time dimensions
    template <std::size_t Rows, std::size_t Cols>
    struct dimensions
    {
	typedef std::size_t  size_type;

        static size_type const Num_Rows= Rows;
        static size_type const Num_Cols= Cols;

	// To have the same interface as fixed
#ifndef NDEBUG
	/// Constructor does not need arguments but if given they are compared against the template arguments in debug mode
43
	explicit dimensions(size_type r= Rows, size_type c= Cols)
44
	{
45
	    assert(r == Rows); assert(c == Cols);
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
	}
#else
	explicit dimensions(size_type, size_type) {}
	explicit dimensions(size_type) {}
	explicit dimensions() {}
#endif

	size_type num_rows() const { return Rows; } ///< Number of rows
	size_type num_cols() const { return Cols; } ///< Number of columns

	/// To check whether dimensions are static
	static bool const is_static= true;

	/// Transposed dimension (type)
	typedef dimensions<Cols, Rows> transposed_type;
61
62
63
	transposed_type transpose() const
	{
	    return transposed_type();
64
65
66
67
68
	}
    };

    /// Output of dimensions
    template <std::size_t R, std::size_t C>
69
    inline std::ostream& operator<< (std::ostream& stream, dimensions<R, C>)
70
    {
71
	return stream << R << 'x' << C;
72
73
74
75
76
77
78
79
80
81
82
    }

} // namespace fixed

namespace non_fixed
{
    /// Run-time dimensions
    struct dimensions
    {
	typedef std::size_t  size_type;

83
	/// Constructor
84
	dimensions(size_type r= 0, size_type c= 0) : r(r), c(c) {}
85

86
	/// Assignment
87
	dimensions& operator=(const dimensions& x)
88
	{
89
	    r= x.r; c= x.c; return *this;
90
91
92
93
94
95
	}
	size_type num_rows() const { return r; } ///< Number of rows
	size_type num_cols() const { return c; } ///< Number of columns

	/// Transposed dimension
	typedef dimensions transposed_type;
96
97
98
	transposed_type transpose()
	{
	    return transposed_type(c, r);
99
100
101
102
103
104
105
106
107
	}

	/// To check whether dimensions are static
	static bool const is_static= false;
    protected:
	size_type r, c;
    };

    /// Output of dimensions
108
    inline std::ostream& operator<< (std::ostream& stream, dimensions d)
109
    {
110
	return stream << d.num_rows() << 'x' << d.num_cols();
111
112
113
114
115
116
117
    }

} // namespace non_fixed

} // namespace mtl

#endif // MTL_DIMENSIONS_INCLUDE