vbl_sparse_array_base.hxx
Go to the documentation of this file.
1 // This is core/vbl/vbl_sparse_array_base.hxx
2 #ifndef vbl_sparse_array_base_hxx_
3 #define vbl_sparse_array_base_hxx_
4 //:
5 // \file
6 // \brief Contains a base class for sparse arrays.
7 // \author Ian Scott
8 
9 #include <utility>
10 #include "vbl_sparse_array_base.h"
11 #include <cassert>
12 #ifdef _MSC_VER
13 # include <vcl_msvc_warnings.h>
14 #endif
15 
16 //: Empty the sparse matrix.
17 template <class T, class Index>
19 {
20  storage_.clear();
21 }
22 
23 //: Return contents of (i). Assertion failure if not yet filled.
24 template <class T, class Index>
26 {
27  typename Map::const_iterator p = storage_.find(i);
28 
29  assert(p != storage_.end());
30 
31  return (*p).second;
32 }
33 
34 //: Erase element at location (i). Assertion failure if not yet filled.
35 template <class T, class Index>
37 {
38  typename Map::iterator p = storage_.find(i);
39 
40  assert(p != storage_.end());
41 
42  storage_.erase(p);
43 }
44 
45 //: Return the memory address of location (i). 0 if not yet filled.
46 template <class T, class Index>
48 {
49  typename Map::iterator p = storage_.find(i);
50 
51  if (p == storage_.end())
52  return nullptr;
53 
54  return &(*p).second;
55 }
56 
57 //: Return true if location (i) has been filled.
58 template <class T, class Index>
60 {
61  return storage_.find(i) != storage_.end();
62 }
63 
64 //: Put a value into location (i).
65 template <class T, class Index>
66 bool vbl_sparse_array_base<T, Index>::put(Index i, const T& t)
67 {
68  typedef typename Map::iterator iter;
69  typedef typename Map::value_type value_type;
70  std::pair<iter,bool> res = storage_.insert(value_type(i,t));
71 
72  return res.second;
73 }
74 
75 #undef VBL_SPARSE_ARRAY_BASE_INSTANTIATE
76 #define VBL_SPARSE_ARRAY_BASE_INSTANTIATE(T, I) \
77 template class vbl_sparse_array_base<T , I >
78 
79 #endif // vbl_sparse_array_base_hxx_
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.
T * get_addr(Index)
Return the address of location (i). 0 if not yet filled.
void clear()
Empty the sparse matrix.
base class for sparse arrays.
bool fullp(Index) const
Return true if location (i) has been filled.
T & operator()(Index i)
Return contents at (i).