vbl_sparse_array_base.h
Go to the documentation of this file.
1 // This is core/vbl/vbl_sparse_array_base.h
2 #ifndef vbl_sparse_array_base_h_
3 #define vbl_sparse_array_base_h_
4 //:
5 // \file
6 // \brief base class for sparse arrays.
7 // \author Ian Scott, Manchester ISBE
8 // \date 10 April 2001
9 
10 #include <functional>
11 #include <map>
12 #include <cstddef>
13 #ifdef _MSC_VER
14 # include <vcl_msvc_warnings.h>
15 #endif
16 
17 //: A fully featured sparse array which devolves indexing to its templated type
18 // If you just want an ordinary sparse array use vbl_sparse_array_1d,
19 // vbl_sparse_array_2d, or vbl_sparse_array_3d.
20 //
21 // Design Decision: Advanced Users only.
22 //
23 // The sparse array design has as much of the code as possible in this
24 // templated base class. This allows us to code harden this class
25 // while leaving the three derived classes in vbl, simple, easy to
26 // understand and use.
27 // I rejected to use templating over the number of dimensions because
28 // this can lead into recursive templating which is in theory very nice,
29 // but in practice very horrible. It also makes the interface rather
30 // unintuitive.
31 // If you are worried about the speed aspects of using a pair of integers
32 // instead of a single encoded integer, then you can create
33 // an encoder class as the index type, and use it directly, or hide the
34 // details by writing a specialising derivative of vbl_sparse_array_base.
35 
36 template <class T, class Index>
38 {
39  protected:
40  //: The type of the storage
41  typedef std::map<Index, T, std::less<Index> > Map;
42  //: This stores a compact list of the values.
44 
45  public:
46 
47  typedef std::size_t size_type;
48 
49  //: Return contents at (i)
50  T & operator () (Index i) { return storage_[i]; }
51 
52  //: Return contents at (i). Asserts that (i) is non-empty.
53  T const& operator () (Index i) const;
54 
55  //: Erase element at location (i). Assertion failure if not yet filled.
56  void erase(Index );
57 
58  //: Return true if location (i) has been filled.
59  bool fullp(Index ) const;
60 
61  //: Put a value into location (i).
62  bool put(Index , const T& );
63 
64  //: Return the address of location (i). 0 if not yet filled.
65  T* get_addr(Index);
66 
67  //: Empty the sparse matrix.
68  void clear();
69 
70  //: The type of iterators into the efficient storage
71  typedef typename Map::const_iterator const_iterator;
72 
73  //: Return number of locations that have been assigned a value using "put".
74  size_type count_nonempty() const { return storage_.size(); }
75 
76  //: The type of objects used to index the sparse array
77  typedef Index Index_type;
78 
79  //: The type of values stored by the sparse array
80  typedef T T_type;
81 
82  //: The type of values of the controlled sequence
83  // The value_type is a std::pair<Index_type, typename T_type>
84  typedef typename Map::value_type sequence_value_type;
85 
86  //: A bidirectional iterator pointing at the first non-empty element
87  // If the array is empty it points just beyond the end.
88  const_iterator begin() const { return storage_.begin(); }
89 
90  //: A bidirectional iterator pointing just beyond last non-empty element.
91  const_iterator end() const { return storage_.end(); }
92 };
93 
94 #endif // vbl_sparse_array_base_h_
Map::const_iterator const_iterator
The type of iterators into the efficient storage.
size_type count_nonempty() const
Return number of locations that have been assigned a value using "put".
bool put(Index, const T &)
Put a value into location (i).
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.
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.
void clear()
Empty the sparse matrix.
T T_type
The type of values stored by the sparse array.
Index Index_type
The type of objects used to index the sparse array.
bool fullp(Index) const
Return true if location (i) has been filled.
Map storage_
This stores a compact list of the values.
A fully featured sparse array which devolves indexing to its templated type.
T & operator()(Index i)
Return contents at (i).
std::map< Index, T, std::less< Index > > Map
The type of the storage.
Map::value_type sequence_value_type
The type of values of the controlled sequence.