vbl_sparse_array_2d.h
Go to the documentation of this file.
1 // This is core/vbl/vbl_sparse_array_2d.h
2 #ifndef vbl_sparse_array_2d_h_
3 #define vbl_sparse_array_2d_h_
4 //:
5 // \file
6 // \brief a space efficient 2d array
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 02 Oct 96
9 //
10 // vbl_sparse_array_2d is a sparse 2D array allowing space
11 // efficient access of the form s(3000,7000) = 2.
12 //
13 // \verbatim
14 // Modifications
15 // 26 March 01 cjb updated documentation
16 // 10 April 01 IMS (Manchester ISBE) modified to use vbl_sparse_array_base
17 // 11 April 01 Peter Vanroose - vbl_index_2d moved to separate file
18 // 11 April 01 Ian Scott - replaced used of vbl_index_2d with std::pair
19 // \endverbatim
20 //---------------------------------------------------------------------------
21 
22 #include <iostream>
23 #include <utility>
24 #ifdef _MSC_VER
25 # include <vcl_msvc_warnings.h>
26 #endif
28 
29 //: Sparse 2D array allowing space efficient access of the form s(300,700) =2
30 template <class T>
31 class vbl_sparse_array_2d : public vbl_sparse_array_base<T, std::pair<unsigned, unsigned> >
32 {
34  public:
36 
37  //: Put a value into location (i,j).
38  bool put(unsigned i, unsigned j, const T& t)
39  {
40  return vbl_sparse_array_base<T, Index_type>::put(std::make_pair(i, j), t);
41  }
42 
43  //: Return contents of location (i,j).
44  // Returns an undefined value (in fact
45  // a T()) if location (i,j) has not been filled with a value.
46  T& operator () (unsigned i, unsigned j)
47  {
48  return vbl_sparse_array_base<T, Index_type>::operator() (std::make_pair(i, j));
49  }
50 
51  //: Return contents of (i,j). Assertion failure if not yet filled.
52  const T& operator () (unsigned i, unsigned j) const
53  {
54  return vbl_sparse_array_base<T, Index_type>::operator() (std::make_pair(i, j));
55  }
56 
57  //: Erase element at location (i,j). Assertion failure if not yet filled.
58  void erase(unsigned i, unsigned j) {
60  }
61 
62  //: Return true if location (i,j) has been filled.
63  bool fullp(unsigned i, unsigned j) const
64  {
65  return vbl_sparse_array_base<T, Index_type>::fullp(std::make_pair(i, j));
66  }
67 
68  //: Return the address of location (i,j). 0 if not yet filled.
69  T* get_addr(unsigned i, unsigned j)
70  {
71  return vbl_sparse_array_base<T, Index_type>::get_addr(std::make_pair(i, j));
72  }
73 
74  //: Print the Array to a stream in "(i,j): value" format.
75  std::ostream& print(std::ostream& out) const
76  {
77  for (const_iterator p = this->begin(); p != this->end(); ++p)
78  out << '(' << (*p).first.first
79  << ',' << (*p).first.second
80  << "): " << (*p).second << std::endl;
81  return out;
82  }
83 };
84 
85 //: Stream operator - print the Array to a stream in "(i,j): value" format.
86 template <class T>
87 inline std::ostream& operator<< (std::ostream& s, const vbl_sparse_array_2d<T>& a)
88 {
89  return a.print(s);
90 }
91 
92 #ifndef VBL_SPARSE_ARRAY_BASE_INSTANTIATE
93 #define VBL_SPARSE_ARRAY_BASE_INSTANTIATE(T) \
94 extern "please include vbl/vbl_sparse_array_base.hxx instead"
95 #endif // VBL_SPARSE_ARRAY_BASE_INSTANTIATE
96 #define VBL_SPARSE_ARRAY_2D_INSTANTIATE(T) \
97 extern "please include vbl/vbl_sparse_array_2d.hxx instead"
98 
99 #endif // vbl_sparse_array_2d_h_
Sparse 2D array allowing space efficient access of the form s(300,700) =2.
Definition: vbl_fwd.h:9
vbl_sparse_array_base< T, std::pair< unsigned, unsigned > >::const_iterator const_iterator
bool put(Index, const T &)
Put a value into location (i).
T & operator()(unsigned i, unsigned j)
Return contents of location (i,j).
void erase(Index)
Erase element at location (i). Assertion failure if not yet filled.
const_iterator end() const
A bidirectional iterator pointing just beyond last non-empty element.
void erase(unsigned i, unsigned j)
Erase element at location (i,j). Assertion failure if not yet filled.
T * get_addr(Index)
Return the address of location (i). 0 if not yet filled.
const_iterator begin() const
A bidirectional iterator pointing at the first non-empty element.
std::ostream & operator<<(std::ostream &s, const vbl_sparse_array_2d< T > &a)
Stream operator - print the Array to a stream in "(i,j): value" format.
base class for sparse arrays.
T * get_addr(unsigned i, unsigned j)
Return the address of location (i,j). 0 if not yet filled.
bool fullp(Index) const
Return true if location (i) has been filled.
A fully featured sparse array which devolves indexing to its templated type.
T & operator()(Index i)
Return contents at (i).
bool put(unsigned i, unsigned j, const T &t)
Put a value into location (i,j).
std::ostream & print(std::ostream &out) const
Print the Array to a stream in "(i,j): value" format.
bool fullp(unsigned i, unsigned j) const
Return true if location (i,j) has been filled.
vbl_sparse_array_base< T, std::pair< unsigned, unsigned > >::Index_type Index_type