vbl_big_sparse_array_3d.hxx
Go to the documentation of this file.
1 // This is core/vbl/vbl_big_sparse_array_3d.hxx
2 #ifndef vbl_big_sparse_array_3d_hxx_
3 #define vbl_big_sparse_array_3d_hxx_
4 
5 #include <utility>
6 #include <iostream>
8 #include <cassert>
9 #ifdef _MSC_VER
10 # include <vcl_msvc_warnings.h>
11 #endif
12 
13 // locals
14 inline ulonglong bigencode(unsigned i, unsigned j, unsigned k)
15 {
16  // Use a map of tuples if you need bigger sparse arrays
17 #if VXL_HAS_INT_64
18  assert( i <= 0x3fffff && j <= 0x1fffff && k <= 0x1fffff );
19  return (((ulonglong)i) << 42) |
20  (((ulonglong)j) << 21) |
21  ( (ulonglong)k);
22 #else
23  assert( i <= 0x3ff && j <= 0x7ff && k <= 0x7ff );
24  return (((ulonglong)i) << 22) |
25  (((ulonglong)j) << 11) |
26  ( (ulonglong)k);
27 #endif
28 }
29 
30 inline void bigdecode(ulonglong v, unsigned& i, unsigned& j, unsigned& k)
31 {
32 #if VXL_HAS_INT_64
33  k = (unsigned)(v & 0x1fffff); // 21 lowest bits
34  j = (unsigned)((v >> 21) & 0x1fffff); // "middle" 21 bits
35  i = (unsigned)((v >> 42) & 0x3fffff); // 22 highest bits
36 #else
37  k = (unsigned)(v & 0x7ff); // 11 lowest bits
38  j = (unsigned)((v >> 11) & 0x7ff); // "middle" 11 bits
39  i = (unsigned)((v >> 22) & 0x3ff); // 10 highest bits
40 #endif
41 }
42 
43 template <class T>
44 T& vbl_big_sparse_array_3d<T>::operator() (unsigned i, unsigned j, unsigned k)
45 {
46 #ifdef DEBUG
47  std::cout << "{vbl_big_sparse_array_3d(" << i << ',' << j << ',' << k
48  << ") - storage[" << bigencode(i,j,k) << "] - "
49  << storage_[bigencode(i,j,k)] << "}\n";
50 #endif
51  return storage_[bigencode(i,j,k)];
52 }
53 
54 template <class T>
55 T const& vbl_big_sparse_array_3d<T>::operator() (unsigned i, unsigned j, unsigned k) const
56 {
57  typename Map::const_iterator p = storage_.find(bigencode(i,j,k));
58 #ifdef DEBUG
59  std::cout << "{vbl_big_sparse_array_3d(" << i << ',' << j << ',' << k
60  << ") - storage[" << bigencode(i,j,k) << "] - "
61  << storage_[bigencode(i,j,k)] << "}\n";
62 #endif
63  assert(p != storage_.end());
64  return (*p).second;
65 }
66 
67 template <class T>
68 bool vbl_big_sparse_array_3d<T>::fullp(unsigned i, unsigned j, unsigned k) const
69 {
70 #ifdef DEBUG
71  std::cout << "{vbl_big_sparse_array_3d::fullp(" << i << ',' << j << ',' << k << ") - "
72  << (storage_.find(bigencode(i,j,k)) != storage_.end()) << "}\n";
73 #endif
74  return (storage_.find(bigencode(i,j,k)) != storage_.end());
75 }
76 
77 template <class T>
78 bool vbl_big_sparse_array_3d<T>::put(unsigned i, unsigned j, unsigned k, T const& t)
79 {
80  typedef typename Map::iterator iter;
81  typedef typename Map::value_type value_type;
82  ulonglong v = bigencode(i,j,k);
83  std::pair<iter,bool> res = storage_.insert(value_type(v,t));
84 #ifdef DEBUG
85  std::cout << "{vbl_big_sparse_array_3d::put(" << i << ',' << j << ',' << k << ") - "
86  << res.second << "}\n";
87 #endif
88  return res.second;
89 }
90 
91 template <class T>
92 std::ostream& vbl_big_sparse_array_3d<T>::print(std::ostream& out) const
93 {
94  for (typename Map::const_iterator p = storage_.begin(); p != storage_.end(); ++p) {
95  unsigned i,j,k;
96  bigdecode((*p).first, i, j, k);
97  out << '(' << i << ',' << j << ',' << k << "): " << (*p).second << std::endl;
98  }
99  return out;
100 }
101 
102 #define VBL_BIG_SPARSE_ARRAY_3D_INSTANTIATE_base(T) \
103 template class vbl_big_sparse_array_3d<T >
104 
105 #undef VBL_BIG_SPARSE_ARRAY_3D_INSTANTIATE
106 #define VBL_BIG_SPARSE_ARRAY_3D_INSTANTIATE(T) \
107 VBL_BIG_SPARSE_ARRAY_3D_INSTANTIATE_base(T); \
108 /*template std::ostream& operator << (std::ostream&, vbl_big_sparse_array_3d<T > const&) */
109 
110 #endif // vbl_big_sparse_array_3d_hxx_
T & operator()(unsigned, unsigned, unsigned)
std::ostream & print(std::ostream &) const
bool fullp(unsigned, unsigned, unsigned) const
Has this cell been assigned a value?.
ulonglong bigencode(unsigned i, unsigned j, unsigned k)
bool put(unsigned, unsigned, unsigned, T const &)
Put a value in a certain cell.
void bigdecode(ulonglong v, unsigned &i, unsigned &j, unsigned &k)