Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleSparseArray2D.cpp

Example for a sparse array 2D

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExampleSparseArray2D.cpp
@relates SparseArray2D
@brief Example for a sparse array 2D
@ingroup g_examples
@author MIP
*/
#include <iostream>
#include <Base/Common/SparseArray2D.hh>
using namespace BIAS;
using namespace std;
/** \cond POORLY_NAMED_EXAMPLE_CLASSES **/
/* foo */
class foo
{
public:
void set(const double d) { data=d; }
double get() const { return data; }
protected:
double data;
};
ostream &operator<<(ostream& os, const foo& f)
{
os << f.get();
return os;
}
void print(const SparseArray2D<foo> &a)
{
cout << a << endl;
}
/** \endcond */
int main()
{
cout << "empty SparseArray2D: ";
print(array);
cout << "empty() = "<<boolalpha<<array.empty()<<endl;
array.resize(2,2);
cout << "empty Array2D of size 2x2 : ";
print(array);
foo f;
f.set(2.0);
array(0,1) = f;
cout << "filled Array2D of size 2x2 : ";
print(array);
array.resize(3,3);
array(2,2) = f;
cout << "partially filled Array2D of size 3x3 : ";
print(array);
cout << "indices (4,0) are valid: "<<boolalpha<<array.is_valid(4,0)<<endl;
cout << "indices (2,2) are valid: "<<boolalpha<<array.is_valid(2,2)<<endl;
cout << "indices (2,1) are valid: "<<boolalpha<<array.is_valid(2,1)<<endl;
int i=0;
for (it = array.begin(); it!=array.end(); it++, i++){
it->set((double)i);
}
cout << "partially filled Array2D of size 3x3 : ";
print(array);
unsigned c, r;
for (r=0; r<3; r++){
for (c=0; c<3; c++){
array(r,c).set(r*3+c);
}
}
cout << "filled Array2D of size 3x3 : ";
print(array);
cout << "array[1][1] = "<<array(1,1).get()<<endl;
// should throw
//cout << array(3,3).get()<<endl;
return 0;
}