Chris@49
|
1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
|
Chris@49
|
2 // Copyright (C) 2008-2013 Conrad Sanderson
|
Chris@49
|
3 // Copyright (C) 2009-2010 Ian Cullinan
|
Chris@49
|
4 //
|
Chris@49
|
5 // This Source Code Form is subject to the terms of the Mozilla Public
|
Chris@49
|
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
|
Chris@49
|
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
Chris@49
|
8
|
Chris@49
|
9
|
Chris@49
|
10 //! \addtogroup field
|
Chris@49
|
11 //! @{
|
Chris@49
|
12
|
Chris@49
|
13
|
Chris@49
|
14
|
Chris@49
|
15 struct field_prealloc_n_elem
|
Chris@49
|
16 {
|
Chris@49
|
17 static const uword val = 16;
|
Chris@49
|
18 };
|
Chris@49
|
19
|
Chris@49
|
20
|
Chris@49
|
21
|
Chris@49
|
22 //! A lightweight 2D container for abitrary objects
|
Chris@49
|
23 //! (the objects must have a copy constructor)
|
Chris@49
|
24
|
Chris@49
|
25 template<typename oT>
|
Chris@49
|
26 class field
|
Chris@49
|
27 {
|
Chris@49
|
28 public:
|
Chris@49
|
29
|
Chris@49
|
30 typedef oT object_type;
|
Chris@49
|
31
|
Chris@49
|
32 const uword n_rows; //!< number of rows in the field (read-only)
|
Chris@49
|
33 const uword n_cols; //!< number of columns in the field (read-only)
|
Chris@49
|
34 const uword n_elem; //!< number of elements in the field (read-only)
|
Chris@49
|
35
|
Chris@49
|
36
|
Chris@49
|
37 private:
|
Chris@49
|
38
|
Chris@49
|
39 arma_aligned oT** mem; //!< pointer to memory used by the object
|
Chris@49
|
40 arma_aligned oT* mem_local[ field_prealloc_n_elem::val ]; //!< Internal memory, to avoid calling the 'new' operator for small amounts of memory
|
Chris@49
|
41
|
Chris@49
|
42
|
Chris@49
|
43 public:
|
Chris@49
|
44
|
Chris@49
|
45 inline ~field();
|
Chris@49
|
46 inline field();
|
Chris@49
|
47
|
Chris@49
|
48 inline field(const field& x);
|
Chris@49
|
49 inline const field& operator=(const field& x);
|
Chris@49
|
50
|
Chris@49
|
51 inline field(const subview_field<oT>& x);
|
Chris@49
|
52 inline const field& operator=(const subview_field<oT>& x);
|
Chris@49
|
53
|
Chris@49
|
54 inline explicit field(const uword n_elem_in);
|
Chris@49
|
55 inline field(const uword n_rows_in, const uword n_cols_in);
|
Chris@49
|
56
|
Chris@49
|
57 inline void set_size(const uword n_obj_in);
|
Chris@49
|
58 inline void set_size(const uword n_rows_in, const uword n_cols_in);
|
Chris@49
|
59
|
Chris@49
|
60 template<typename oT2>
|
Chris@49
|
61 inline void copy_size(const field<oT2>& x);
|
Chris@49
|
62
|
Chris@49
|
63 arma_inline oT& operator[](const uword i);
|
Chris@49
|
64 arma_inline const oT& operator[](const uword i) const;
|
Chris@49
|
65
|
Chris@49
|
66 arma_inline oT& at(const uword i);
|
Chris@49
|
67 arma_inline const oT& at(const uword i) const;
|
Chris@49
|
68
|
Chris@49
|
69 arma_inline oT& operator()(const uword i);
|
Chris@49
|
70 arma_inline const oT& operator()(const uword i) const;
|
Chris@49
|
71
|
Chris@49
|
72 arma_inline oT& at(const uword row, const uword col);
|
Chris@49
|
73 arma_inline const oT& at(const uword row, const uword col) const;
|
Chris@49
|
74
|
Chris@49
|
75 arma_inline oT& operator()(const uword row, const uword col);
|
Chris@49
|
76 arma_inline const oT& operator()(const uword row, const uword col) const;
|
Chris@49
|
77
|
Chris@49
|
78 inline field_injector<field> operator<<(const oT& val);
|
Chris@49
|
79 inline field_injector<field> operator<<(const injector_end_of_row<>& x);
|
Chris@49
|
80
|
Chris@49
|
81
|
Chris@49
|
82 inline subview_field<oT> row(const uword row_num);
|
Chris@49
|
83 inline const subview_field<oT> row(const uword row_num) const;
|
Chris@49
|
84
|
Chris@49
|
85 inline subview_field<oT> col(const uword col_num);
|
Chris@49
|
86 inline const subview_field<oT> col(const uword col_num) const;
|
Chris@49
|
87
|
Chris@49
|
88 inline subview_field<oT> rows(const uword in_row1, const uword in_row2);
|
Chris@49
|
89 inline const subview_field<oT> rows(const uword in_row1, const uword in_row2) const;
|
Chris@49
|
90
|
Chris@49
|
91 inline subview_field<oT> cols(const uword in_col1, const uword in_col2);
|
Chris@49
|
92 inline const subview_field<oT> cols(const uword in_col1, const uword in_col2) const;
|
Chris@49
|
93
|
Chris@49
|
94 inline subview_field<oT> subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2);
|
Chris@49
|
95 inline const subview_field<oT> subfield(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const;
|
Chris@49
|
96
|
Chris@49
|
97 inline subview_field<oT> subfield (const span& row_span, const span& col_span);
|
Chris@49
|
98 inline const subview_field<oT> subfield (const span& row_span, const span& col_span) const;
|
Chris@49
|
99
|
Chris@49
|
100 inline subview_field<oT> operator()(const span& row_span, const span& col_span);
|
Chris@49
|
101 inline const subview_field<oT> operator()(const span& row_span, const span& col_span) const;
|
Chris@49
|
102
|
Chris@49
|
103
|
Chris@49
|
104 inline void print(const std::string extra_text = "") const;
|
Chris@49
|
105 inline void print(std::ostream& user_stream, const std::string extra_text = "") const;
|
Chris@49
|
106
|
Chris@49
|
107 inline void fill(const oT& x);
|
Chris@49
|
108
|
Chris@49
|
109 inline void reset();
|
Chris@49
|
110 inline void reset_objects();
|
Chris@49
|
111
|
Chris@49
|
112 arma_inline bool is_empty() const;
|
Chris@49
|
113
|
Chris@49
|
114 arma_inline arma_warn_unused bool in_range(const uword i) const;
|
Chris@49
|
115 arma_inline arma_warn_unused bool in_range(const span& x) const;
|
Chris@49
|
116
|
Chris@49
|
117 arma_inline arma_warn_unused bool in_range(const uword in_row, const uword in_col) const;
|
Chris@49
|
118 arma_inline arma_warn_unused bool in_range(const span& row_span, const uword in_col) const;
|
Chris@49
|
119 arma_inline arma_warn_unused bool in_range(const uword in_row, const span& col_span) const;
|
Chris@49
|
120 arma_inline arma_warn_unused bool in_range(const span& row_span, const span& col_span) const;
|
Chris@49
|
121
|
Chris@49
|
122 inline bool save(const std::string name, const file_type type = arma_binary, const bool print_status = true) const;
|
Chris@49
|
123 inline bool save( std::ostream& os, const file_type type = arma_binary, const bool print_status = true) const;
|
Chris@49
|
124
|
Chris@49
|
125 inline bool load(const std::string name, const file_type type = auto_detect, const bool print_status = true);
|
Chris@49
|
126 inline bool load( std::istream& is, const file_type type = auto_detect, const bool print_status = true);
|
Chris@49
|
127
|
Chris@49
|
128
|
Chris@49
|
129 inline bool quiet_save(const std::string name, const file_type type = arma_binary) const;
|
Chris@49
|
130 inline bool quiet_save( std::ostream& os, const file_type type = arma_binary) const;
|
Chris@49
|
131
|
Chris@49
|
132 inline bool quiet_load(const std::string name, const file_type type = auto_detect);
|
Chris@49
|
133 inline bool quiet_load( std::istream& is, const file_type type = auto_detect);
|
Chris@49
|
134
|
Chris@49
|
135
|
Chris@49
|
136 // for container-like functionality
|
Chris@49
|
137
|
Chris@49
|
138 typedef oT value_type;
|
Chris@49
|
139 typedef uword size_type;
|
Chris@49
|
140
|
Chris@49
|
141
|
Chris@49
|
142 class iterator
|
Chris@49
|
143 {
|
Chris@49
|
144 public:
|
Chris@49
|
145
|
Chris@49
|
146 inline iterator(field<oT>& in_M, const bool at_end = false);
|
Chris@49
|
147
|
Chris@49
|
148 inline oT& operator* ();
|
Chris@49
|
149
|
Chris@49
|
150 inline iterator& operator++();
|
Chris@49
|
151 inline void operator++(int);
|
Chris@49
|
152
|
Chris@49
|
153 inline iterator& operator--();
|
Chris@49
|
154 inline void operator--(int);
|
Chris@49
|
155
|
Chris@49
|
156 inline bool operator!=(const iterator& X) const;
|
Chris@49
|
157 inline bool operator==(const iterator& X) const;
|
Chris@49
|
158
|
Chris@49
|
159 arma_aligned field<oT>& M;
|
Chris@49
|
160 arma_aligned uword i;
|
Chris@49
|
161 };
|
Chris@49
|
162
|
Chris@49
|
163
|
Chris@49
|
164 class const_iterator
|
Chris@49
|
165 {
|
Chris@49
|
166 public:
|
Chris@49
|
167
|
Chris@49
|
168 const_iterator(const field<oT>& in_M, const bool at_end = false);
|
Chris@49
|
169 const_iterator(const iterator& X);
|
Chris@49
|
170
|
Chris@49
|
171 inline const oT& operator*() const;
|
Chris@49
|
172
|
Chris@49
|
173 inline const_iterator& operator++();
|
Chris@49
|
174 inline void operator++(int);
|
Chris@49
|
175
|
Chris@49
|
176 inline const_iterator& operator--();
|
Chris@49
|
177 inline void operator--(int);
|
Chris@49
|
178
|
Chris@49
|
179 inline bool operator!=(const const_iterator& X) const;
|
Chris@49
|
180 inline bool operator==(const const_iterator& X) const;
|
Chris@49
|
181
|
Chris@49
|
182 arma_aligned const field<oT>& M;
|
Chris@49
|
183 arma_aligned uword i;
|
Chris@49
|
184 };
|
Chris@49
|
185
|
Chris@49
|
186 inline iterator begin();
|
Chris@49
|
187 inline const_iterator begin() const;
|
Chris@49
|
188 inline const_iterator cbegin() const;
|
Chris@49
|
189
|
Chris@49
|
190 inline iterator end();
|
Chris@49
|
191 inline const_iterator end() const;
|
Chris@49
|
192 inline const_iterator cend() const;
|
Chris@49
|
193
|
Chris@49
|
194 inline void clear();
|
Chris@49
|
195 inline bool empty() const;
|
Chris@49
|
196 inline uword size() const;
|
Chris@49
|
197
|
Chris@49
|
198
|
Chris@49
|
199 private:
|
Chris@49
|
200
|
Chris@49
|
201 inline void init(const field<oT>& x);
|
Chris@49
|
202 inline void init(const uword n_rows_in, const uword n_cols_in);
|
Chris@49
|
203
|
Chris@49
|
204 inline void delete_objects();
|
Chris@49
|
205 inline void create_objects();
|
Chris@49
|
206
|
Chris@49
|
207 friend class field_aux;
|
Chris@49
|
208 friend class subview_field<oT>;
|
Chris@49
|
209
|
Chris@49
|
210
|
Chris@49
|
211 public:
|
Chris@49
|
212
|
Chris@49
|
213 #ifdef ARMA_EXTRA_FIELD_PROTO
|
Chris@49
|
214 #include ARMA_INCFILE_WRAP(ARMA_EXTRA_FIELD_PROTO)
|
Chris@49
|
215 #endif
|
Chris@49
|
216 };
|
Chris@49
|
217
|
Chris@49
|
218
|
Chris@49
|
219
|
Chris@49
|
220 class field_aux
|
Chris@49
|
221 {
|
Chris@49
|
222 public:
|
Chris@49
|
223
|
Chris@49
|
224 template<typename oT> inline static void reset_objects(field< oT >& x);
|
Chris@49
|
225 template<typename eT> inline static void reset_objects(field< Mat<eT> >& x);
|
Chris@49
|
226 template<typename eT> inline static void reset_objects(field< Col<eT> >& x);
|
Chris@49
|
227 template<typename eT> inline static void reset_objects(field< Row<eT> >& x);
|
Chris@49
|
228 template<typename eT> inline static void reset_objects(field< Cube<eT> >& x);
|
Chris@49
|
229 inline static void reset_objects(field< std::string >& x);
|
Chris@49
|
230
|
Chris@49
|
231
|
Chris@49
|
232 template<typename oT> inline static bool save(const field< oT >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
233 template<typename oT> inline static bool save(const field< oT >& x, std::ostream& os, const file_type type, std::string& err_msg);
|
Chris@49
|
234 template<typename oT> inline static bool load( field< oT >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
235 template<typename oT> inline static bool load( field< oT >& x, std::istream& is, const file_type type, std::string& err_msg);
|
Chris@49
|
236
|
Chris@49
|
237 template<typename eT> inline static bool save(const field< Mat<eT> >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
238 template<typename eT> inline static bool save(const field< Mat<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg);
|
Chris@49
|
239 template<typename eT> inline static bool load( field< Mat<eT> >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
240 template<typename eT> inline static bool load( field< Mat<eT> >& x, std::istream& is, const file_type type, std::string& err_msg);
|
Chris@49
|
241
|
Chris@49
|
242 template<typename eT> inline static bool save(const field< Col<eT> >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
243 template<typename eT> inline static bool save(const field< Col<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg);
|
Chris@49
|
244 template<typename eT> inline static bool load( field< Col<eT> >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
245 template<typename eT> inline static bool load( field< Col<eT> >& x, std::istream& is, const file_type type, std::string& err_msg);
|
Chris@49
|
246
|
Chris@49
|
247 template<typename eT> inline static bool save(const field< Row<eT> >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
248 template<typename eT> inline static bool save(const field< Row<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg);
|
Chris@49
|
249 template<typename eT> inline static bool load( field< Row<eT> >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
250 template<typename eT> inline static bool load( field< Row<eT> >& x, std::istream& is, const file_type type, std::string& err_msg);
|
Chris@49
|
251
|
Chris@49
|
252 template<typename eT> inline static bool save(const field< Cube<eT> >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
253 template<typename eT> inline static bool save(const field< Cube<eT> >& x, std::ostream& os, const file_type type, std::string& err_msg);
|
Chris@49
|
254 template<typename eT> inline static bool load( field< Cube<eT> >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
255 template<typename eT> inline static bool load( field< Cube<eT> >& x, std::istream& is, const file_type type, std::string& err_msg);
|
Chris@49
|
256
|
Chris@49
|
257 inline static bool save(const field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
258 inline static bool save(const field< std::string >& x, std::ostream& os, const file_type type, std::string& err_msg);
|
Chris@49
|
259 inline static bool load( field< std::string >& x, const std::string& name, const file_type type, std::string& err_msg);
|
Chris@49
|
260 inline static bool load( field< std::string >& x, std::istream& is, const file_type type, std::string& err_msg);
|
Chris@49
|
261
|
Chris@49
|
262 };
|
Chris@49
|
263
|
Chris@49
|
264
|
Chris@49
|
265 //! @}
|