Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleArray2D.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 /**
27  @example ExampleArray2D.cpp
28  @relates Array2D
29  @brief Example for usage of class Array2D
30  @ingroup g_examples
31  @author MIP
32 */
33 
34 #include <Base/Common/Array2D.hh>
35 #include <iostream>
36 
37 using namespace BIAS;
38 using namespace std;
39 
40 /** \cond POORLY_NAMED_EXAMPLE_CLASSES **/
41 /* foo */
42 class foo
43 {
44 public:
45  void set(const double d) { data=d; }
46  double get() const { return data; }
47 protected:
48  double data;
49 };
50 
51 void print(Array2D<foo> &a)
52 {
53  const unsigned nr = a.nrows(), nc = a.ncols();
54  cout << nr << " x "<< nc <<" :\n";
55  for (unsigned r=0; r<nr; r++){
56  for (unsigned c=0; c<nc; c++){
57  cout << a(r,c).get() << "\t";
58  }
59  cout << "\n";
60  }
61 }
62 /** \endcond */
63 
64 int main()
65 {
66  Array2D<foo> array;
67  cout << "empty Array2D: ";
68  print(array);
69  cout << "empty() = "<<boolalpha<<array.empty()<<endl;
70 
71  array.resize(2,2);
72  cout << "empty Array2D of size 2x2 : ";
73  print(array);
74 
75  foo f;
76  f.set(2.0);
77  array.fill(f);
78  cout << "filled Array2D of size 2x2 : ";
79  print(array);
80 
81 
82  array.resize(3,3);
83  cout << "partially filled Array2D of size 3x3 : ";
84  print(array);
85 
87  int i=0;
88  for (it = array.begin(); it!=array.end(); it++, i++){
89  it->set((double)i);
90  }
91  cout << "filled Array2D of size 3x3 : ";
92  print(array);
93 
94  cout << "array[1][1] = "<<array[1][1].get()<<endl;
95 
96  // should throw
97  //cout << array(3,3).get()<<endl;
98 
99  return 0;
100 }
generic two dimensional rectangular array holding arbitrary data types
Definition: Array2D.hh:31
unsigned ncols() const
Definition: Array2D.hh:71
for iterator access todo: derive from std::iterator class(es)
Definition: Array2D.hh:90
void resize(const unsigned nrows, const unsigned ncols)
preserves the content
Definition: Array2D.hh:203
const_iterator end() const
Definition: Array2D.hh:137
unsigned nrows() const
Definition: Array2D.hh:73
bool empty() const
Definition: Array2D.hh:65
void fill(const T &val)
fills the array with val
Definition: Array2D.hh:227
const_iterator begin() const
Definition: Array2D.hh:136