Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleSparseArray2D.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  @example ExampleSparseArray2D.cpp
27  @relates SparseArray2D
28  @brief Example for a sparse array 2D
29  @ingroup g_examples
30  @author MIP
31 */
32 
33 #include <iostream>
34 #include <Base/Common/SparseArray2D.hh>
35 
36 using namespace BIAS;
37 using namespace std;
38 
39 /** \cond POORLY_NAMED_EXAMPLE_CLASSES **/
40 /* foo */
41 class foo
42 {
43 public:
44  void set(const double d) { data=d; }
45  double get() const { return data; }
46 protected:
47  double data;
48 };
49 
50 ostream &operator<<(ostream& os, const foo& f)
51 {
52  os << f.get();
53  return os;
54 }
55 
56 void print(const SparseArray2D<foo> &a)
57 {
58  cout << a << endl;
59 }
60 /** \endcond */
61 
62 int main()
63 {
64  SparseArray2D<foo> array;
65  cout << "empty SparseArray2D: ";
66  print(array);
67  cout << "empty() = "<<boolalpha<<array.empty()<<endl;
68 
69  array.resize(2,2);
70  cout << "empty Array2D of size 2x2 : ";
71  print(array);
72 
73  foo f;
74  f.set(2.0);
75  array(0,1) = f;
76  cout << "filled Array2D of size 2x2 : ";
77  print(array);
78 
79 
80  array.resize(3,3);
81  array(2,2) = f;
82  cout << "partially filled Array2D of size 3x3 : ";
83  print(array);
84 
85  cout << "indices (4,0) are valid: "<<boolalpha<<array.is_valid(4,0)<<endl;
86  cout << "indices (2,2) are valid: "<<boolalpha<<array.is_valid(2,2)<<endl;
87  cout << "indices (2,1) are valid: "<<boolalpha<<array.is_valid(2,1)<<endl;
88 
90  int i=0;
91  for (it = array.begin(); it!=array.end(); it++, i++){
92  it->set((double)i);
93  }
94  cout << "partially filled Array2D of size 3x3 : ";
95  print(array);
96 
97  unsigned c, r;
98  for (r=0; r<3; r++){
99  for (c=0; c<3; c++){
100  array(r,c).set(r*3+c);
101  }
102  }
103  cout << "filled Array2D of size 3x3 : ";
104  print(array);
105 
106  cout << "array[1][1] = "<<array(1,1).get()<<endl;
107 
108  // should throw
109  //cout << array(3,3).get()<<endl;
110 
111  return 0;
112 }
const_iterator begin() const
for iterator access todo: derive from std::iterator class(es)
void resize(const unsigned nrows, const unsigned ncols)
preserves the content
bool is_valid(const unsigned row, const unsigned col) const
bool empty() const
generic two dimensional psarsly populated rectangular array holding arbitrary data types ...
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
const_iterator end() const