1 /* Matrix class implementation: inline functions.
2    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #ifndef PPL_Matrix_inlines_hh
25 #define PPL_Matrix_inlines_hh 1
26 
27 namespace Parma_Polyhedra_Library {
28 
29 template <typename Row>
30 inline dimension_type
max_num_rows()31 Matrix<Row>::max_num_rows() {
32   return std::vector<Row>().max_size();
33 }
34 
35 template <typename Row>
36 inline dimension_type
max_num_columns()37 Matrix<Row>::max_num_columns() {
38   return Row::max_size();
39 }
40 
41 template <typename Row>
42 inline void
m_swap(Matrix & x)43 Matrix<Row>::m_swap(Matrix& x) {
44   using std::swap;
45   swap(rows, x.rows);
46   swap(num_columns_, x.num_columns_);
47 }
48 
49 template <typename Row>
50 inline dimension_type
num_rows() const51 Matrix<Row>::num_rows() const {
52   return rows.size();
53 }
54 
55 template <typename Row>
56 inline dimension_type
num_columns() const57 Matrix<Row>::num_columns() const {
58   return num_columns_;
59 }
60 
61 template <typename Row>
62 inline dimension_type
capacity() const63 Matrix<Row>::capacity() const {
64   return rows.capacity();
65 }
66 
67 template <typename Row>
68 inline bool
has_no_rows() const69 Matrix<Row>::has_no_rows() const {
70   return num_rows() == 0;
71 }
72 
73 template <typename Row>
74 inline void
resize(dimension_type n)75 Matrix<Row>::resize(dimension_type n) {
76   resize(n, n);
77 }
78 
79 template <typename Row>
80 inline void
reserve_rows(dimension_type requested_capacity)81 Matrix<Row>::reserve_rows(dimension_type requested_capacity) {
82 
83   rows.reserve(requested_capacity);
84 }
85 
86 template <typename Row>
87 inline void
add_zero_rows_and_columns(dimension_type n,dimension_type m)88 Matrix<Row>::add_zero_rows_and_columns(dimension_type n, dimension_type m) {
89   resize(num_rows() + n, num_columns() + m);
90 }
91 
92 template <typename Row>
93 inline void
add_zero_rows(dimension_type n)94 Matrix<Row>::add_zero_rows(dimension_type n) {
95   resize(num_rows() + n, num_columns());
96 }
97 
98 template <typename Row>
99 inline void
add_row(const Row & x)100 Matrix<Row>::add_row(const Row& x) {
101   // TODO: Optimize this.
102   Row row(x);
103   add_zero_rows(1);
104   // Now x may have been invalidated, if it was a row of this matrix.
105   swap(rows.back(), row);
106   PPL_ASSERT(OK());
107 }
108 
109 template <typename Row>
110 inline void
add_recycled_row(Row & x)111 Matrix<Row>::add_recycled_row(Row& x) {
112   add_zero_rows(1);
113   swap(rows.back(), x);
114   PPL_ASSERT(OK());
115 }
116 
117 template <typename Row>
118 inline void
remove_trailing_rows(dimension_type n)119 Matrix<Row>::remove_trailing_rows(dimension_type n) {
120   resize(num_rows() - n, num_columns());
121 }
122 
123 template <typename Row>
124 inline void
remove_rows(iterator first,iterator last)125 Matrix<Row>::remove_rows(iterator first, iterator last) {
126   rows.erase(first, last);
127 }
128 
129 template <typename Row>
130 inline void
add_zero_columns(dimension_type n)131 Matrix<Row>::add_zero_columns(dimension_type n) {
132   resize(num_rows(), num_columns() + n);
133 }
134 
135 template <typename Row>
136 inline void
remove_trailing_columns(dimension_type n)137 Matrix<Row>::remove_trailing_columns(dimension_type n) {
138   PPL_ASSERT(n <= num_columns());
139   resize(num_rows(), num_columns() - n);
140 }
141 
142 template <typename Row>
143 inline void
clear()144 Matrix<Row>::clear() {
145   resize(0, 0);
146 }
147 
148 template <typename Row>
149 inline typename Matrix<Row>::iterator
begin()150 Matrix<Row>::begin() {
151   return rows.begin();
152 }
153 
154 template <typename Row>
155 inline typename Matrix<Row>::iterator
end()156 Matrix<Row>::end() {
157   return rows.end();
158 }
159 
160 template <typename Row>
161 inline typename Matrix<Row>::const_iterator
begin() const162 Matrix<Row>::begin() const {
163   return rows.begin();
164 }
165 
166 template <typename Row>
167 inline typename Matrix<Row>::const_iterator
end() const168 Matrix<Row>::end() const {
169   return rows.end();
170 }
171 
172 template <typename Row>
173 inline Row&
operator [](dimension_type i)174 Matrix<Row>::operator[](dimension_type i) {
175   PPL_ASSERT(i < rows.size());
176   return rows[i];
177 }
178 
179 template <typename Row>
180 inline const Row&
operator [](dimension_type i) const181 Matrix<Row>::operator[](dimension_type i) const {
182   PPL_ASSERT(i < rows.size());
183   return rows[i];
184 }
185 
186 template <typename Row>
187 inline memory_size_type
total_memory_in_bytes() const188 Matrix<Row>::total_memory_in_bytes() const {
189   return sizeof(*this) + external_memory_in_bytes();
190 }
191 
192 template <typename Row>
193 inline void
swap(Matrix<Row> & x,Matrix<Row> & y)194 swap(Matrix<Row>& x, Matrix<Row>& y) {
195   x.m_swap(y);
196 }
197 
198 } // namespace Parma_Polyhedra_Library
199 
200 #endif // !defined(PPL_Matrix_inlines_hh)
201