cppx-core
Matrix_.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
2 
3 #include <cppx-core/collections/Range_.hpp> // cppx::(is_in, up_to_)
4 #include <cppx-core/failure-handling/macro-fail.hpp> // CPPX_FAIL_
5 #include <cppx-core/language/syntax/macro-use.hpp> // CPPX_USE_STD
6 #include <cppx-core/language/types/signed-size-types.hpp> // cppx::(Size, Index)
7 #include <cppx-core/text/basic-string-building.hpp> // cppx::operator<<
8 
9 #include <vector> // std::vector
10 #include <stdexcept> // std::out_of_range
11 
12 namespace cppx
13 {
14  CPPX_USE_STD( out_of_range, vector );
15 
16  template< class Item >
17  class Matrix_
18  {
19  vector<Item> m_items;
20  Size m_width;
21  Size m_height;
22 
23  template< class Result, class Self >
24  static auto checked_at( Self& self, const Index col, const Index row )
25  -> Result&
26  {
27  hopefully(
28  is_in( up_to( self.m_width ), col ) and
29  is_in( up_to( self.m_height ), row )
30  )
31  or CPPX_FAIL_( out_of_range,
32  "Item position ("s << col << "," << row << ") is out of range"
33  );
34  return self.m_items[self.index_of( col, row )];
35  }
36 
37  public:
38  auto n_items() const -> Size { return m_items.size(); }
39  auto width() const -> Size { return m_width; }
40  auto height() const -> Size { return m_height; }
41 
42  auto begin() const { return m_items.begin(); }
43  auto end() const { return m_items.end(); }
44  auto data() const { return m_items.data(); }
45  auto begin() { return m_items.begin(); }
46  auto end() { return m_items.end(); }
47  auto data() { return m_items.data(); }
48 
49  auto index_of( const Index col, const Index row ) const
50  -> Index
51  { return row*m_width + col; }
52 
53  auto col_of( const Index i ) const
54  -> Index
55  { return i % m_width; }
56 
57  auto row_of( const Index i ) const
58  -> Index
59  { return i / m_width; }
60 
61  auto operator()( const Index col, const Index row ) const
62  -> const Item&
63  { return m_items[index_of( col, row )]; }
64 
65  auto operator()( const Index col, const Index row )
66  -> Item&
67  { return m_items[index_of( col, row )]; }
68 
69  auto at( const Index col, const Index row ) const
70  -> const Item&
71  { return checked_at<const Item>( *this, col, row ); }
72 
73  auto at( const Index col, const Index row )
74  -> Item&
75  { return checked_at<Item>( *this, col, row ); }
76 
78  m_items(),
79  m_width( 0 ),
80  m_height( 0 )
81  {}
82 
83  Matrix_( const Size width, const Size height ):
84  m_items( width*height ),
85  m_width( width ),
86  m_height( height )
87  {}
88  };
89 } // namespace cppx
auto data() const
Definition: Matrix_.hpp:44
auto col_of(const Index i) const -> Index
Definition: Matrix_.hpp:53
auto data()
Definition: Matrix_.hpp:47
auto at(const Index col, const Index row) const -> const Item &
Definition: Matrix_.hpp:69
auto height() const -> Size
Definition: Matrix_.hpp:40
auto hopefully(const Truth condition) -> Truth
auto begin() const
Definition: Matrix_.hpp:42
auto is_in(const basic_string_view< Char > &sv, const Char ch) noexcept -> Truth
Definition: is_in.hpp:21
auto operator()(const Index col, const Index row) -> Item &
Definition: Matrix_.hpp:65
auto begin()
Definition: Matrix_.hpp:45
auto row_of(const Index i) const -> Index
Definition: Matrix_.hpp:57
CPPX_USE_STD(basic_string, basic_string_view, bitset, char_traits, size)
auto at(const Index col, const Index row) -> Item &
Definition: Matrix_.hpp:73
Size Index
Same as Size but with name signifying use as index.
constexpr auto up_to(const Integer n) noexcept -> Range_< Integer >
Definition: Range_.hpp:61
auto index_of(const Index col, const Index row) const -> Index
Definition: Matrix_.hpp:49
Signed_< size_t > Size
A Signed_ equivalent of size_t.
auto width() const -> Size
Definition: Matrix_.hpp:39
auto end() const
Definition: Matrix_.hpp:43
auto n_items() const -> Size
Definition: Matrix_.hpp:38
Macros for generating more concise and clear using statements, primarily $use_cppx and $use_std,...
Matrix_(const Size width, const Size height)
Definition: Matrix_.hpp:83
Signed Size and Index, plus unsigned equivalents Unsigned_size and Unsigned_index.
auto operator()(const Index col, const Index row) const -> const Item &
Definition: Matrix_.hpp:61
auto end()
Definition: Matrix_.hpp:46
#define CPPX_FAIL_(X,...)
Definition: macro-fail.hpp:13