forked from andrewprock/ustl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathumatrix.h
73 lines (68 loc) · 3.5 KB
/
umatrix.h
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
61
62
63
64
65
66
67
68
69
70
71
72
73
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#pragma once
#include "utuple.h"
namespace ustl {
/// \class matrix umatrix.h ustl.h
/// \ingroup Sequences
///
/// \brief A two-dimensional array of NX*NY elements of type T.
///
template <size_t NX, size_t NY, typename T>
class matrix : public tuple<NX*NY,T> {
public:
typedef tuple<NX,T> row_type;
typedef tuple<NY,T> column_type;
typedef tuple<NX*NY,T> tuple_type;
typedef typename tuple_type::value_type value_type;
typedef typename tuple_type::size_type size_type;
typedef typename tuple_type::pointer pointer;
typedef typename tuple_type::const_pointer const_pointer;
typedef typename tuple_type::reference reference;
typedef typename tuple_type::const_reference const_reference;
typedef typename tuple_type::iterator iterator;
typedef typename tuple_type::const_iterator const_iterator;
typedef typename tuple_type::range_t range_t;
typedef typename tuple_type::const_range_t const_range_t;
typedef typename tuple_type::reverse_iterator reverse_iterator;
typedef typename tuple_type::const_reverse_iterator const_reverse_iterator;
public:
inline matrix (void) : tuple<NX*NY,T>() { }
inline size_type columns (void) const { return NX; }
inline size_type rows (void) const { return NY; }
inline const_iterator at (size_type i) const { return matrix::begin() + i * NX; }
inline iterator at (size_type i) { return matrix::begin() + i * NX; }
inline const_iterator operator[] (size_type i) const { return at (i); }
inline iterator operator[] (size_type i) { return at (i); }
inline row_type row (size_type r) const { return row_type (at (r)); }
inline column_type column (size_type c) const;
template <typename T2>
inline const matrix& operator= (const matrix<NX,NY,T2>& src) { tuple_type::operator= (src); return *this; }
inline const matrix& operator= (const matrix<NX,NY,T>& src) { tuple_type::operator= (src); return *this; }
inline const matrix& operator+= (const_reference v) { tuple_type::operator+= (v); return *this; }
inline const matrix& operator-= (const_reference v) { tuple_type::operator-= (v); return *this; }
inline const matrix& operator*= (const_reference v) { tuple_type::operator*= (v); return *this; }
inline const matrix& operator/= (const_reference v) { tuple_type::operator/= (v); return *this; }
inline const matrix operator+ (const_reference v) const
{ matrix result (*this); result += v; return result; }
inline const matrix operator- (const_reference v) const
{ matrix result (*this); result -= v; return result; }
inline const matrix operator* (const_reference v) const
{ matrix result (*this); result *= v; return result; }
inline const matrix operator/ (const_reference v) const
{ matrix result (*this); result /= v; return result; }
void text_write (ostringstream& os) const;
};
template <size_t NX, size_t NY, typename T>
inline typename matrix<NX,NY,T>::column_type matrix<NX,NY,T>::column (size_type c) const
{
column_type result;
const_iterator src (matrix::begin() + c);
iterator dest (result.begin());
for (uoff_t i = 0; i < NY; ++ i, ++ dest, src += NX)
*dest = *src;
return result;
}
} // namespace ustl