core/vbl/vbl_big_sparse_array_3d.txx
Go to the documentation of this file.
00001 // This is core/vbl/vbl_big_sparse_array_3d.txx
00002 #ifndef vbl_big_sparse_array_3d_txx_
00003 #define vbl_big_sparse_array_3d_txx_
00004 
00005 #include "vbl_big_sparse_array_3d.h"
00006 #include <vcl_cassert.h>
00007 #include <vcl_utility.h> // for vcl_pair<T,bool>
00008 #include <vcl_iostream.h>
00009 
00010 // locals
00011 inline ulonglong bigencode(unsigned i, unsigned j, unsigned k)
00012 {
00013   // Use a map of tuples if you need bigger sparse arrays
00014 #if VXL_HAS_INT_64
00015   assert( i <= 0x3fffff && j <= 0x1fffff && k <= 0x1fffff );
00016   return (((ulonglong)i) << 42) |
00017          (((ulonglong)j) << 21) |
00018          ( (ulonglong)k);
00019 #else
00020   assert( i <= 0x3ff && j <= 0x7ff && k <= 0x7ff );
00021   return (((ulonglong)i) << 22) |
00022          (((ulonglong)j) << 11) |
00023          ( (ulonglong)k);
00024 #endif
00025 }
00026 
00027 inline void bigdecode(ulonglong v, unsigned& i, unsigned& j, unsigned& k)
00028 {
00029 #if VXL_HAS_INT_64
00030   k = (unsigned)(v & 0x1fffff); // 21 lowest bits
00031   j = (unsigned)((v >> 21) & 0x1fffff); // "middle" 21 bits
00032   i = (unsigned)((v >> 42) & 0x3fffff); // 22 highest bits
00033 #else
00034   k = (unsigned)(v & 0x7ff); // 11 lowest bits
00035   j = (unsigned)((v >> 11) & 0x7ff); // "middle" 11 bits
00036   i = (unsigned)((v >> 22) & 0x3ff); // 10 highest bits
00037 #endif
00038 }
00039 
00040 template <class T>
00041 T& vbl_big_sparse_array_3d<T>::operator() (unsigned i, unsigned j, unsigned k)
00042 {
00043 #ifdef DEBUG
00044   vcl_cout << "{vbl_big_sparse_array_3d(" << i << ',' << j << ',' << k
00045            << ") - storage[" << bigencode(i,j,k) << "] - "
00046            << storage_[bigencode(i,j,k)] << "}\n";
00047 #endif
00048   return storage_[bigencode(i,j,k)];
00049 }
00050 
00051 template <class T>
00052 T const& vbl_big_sparse_array_3d<T>::operator() (unsigned i, unsigned j, unsigned k) const
00053 {
00054   typename Map::const_iterator p = storage_.find(bigencode(i,j,k));
00055 #ifdef DEBUG
00056   vcl_cout << "{vbl_big_sparse_array_3d(" << i << ',' << j << ',' << k
00057            << ") - storage[" << bigencode(i,j,k) << "] - "
00058            << storage_[bigencode(i,j,k)] << "}\n";
00059 #endif
00060   assert(p != storage_.end());
00061   return (*p).second;
00062 }
00063 
00064 template <class T>
00065 bool vbl_big_sparse_array_3d<T>::fullp(unsigned i, unsigned j, unsigned k) const
00066 {
00067 #ifdef DEBUG
00068   vcl_cout << "{vbl_big_sparse_array_3d::fullp(" << i << ',' << j << ',' << k << ") - "
00069            << (storage_.find(bigencode(i,j,k)) != storage_.end()) << "}\n";
00070 #endif
00071   return (storage_.find(bigencode(i,j,k)) != storage_.end());
00072 }
00073 
00074 template <class T>
00075 bool vbl_big_sparse_array_3d<T>::put(unsigned i, unsigned j, unsigned k, T const& t)
00076 {
00077   typedef typename Map::iterator iter;
00078   typedef typename Map::value_type value_type;
00079   ulonglong v = bigencode(i,j,k);
00080   vcl_pair<iter,bool> res = storage_.insert(value_type(v,t));
00081 #ifdef DEBUG
00082   vcl_cout << "{vbl_big_sparse_array_3d::put(" << i << ',' << j << ',' << k << ") - "
00083            << res.second << "}\n";
00084 #endif
00085   return res.second;
00086 }
00087 
00088 template <class T>
00089 vcl_ostream& vbl_big_sparse_array_3d<T>::print(vcl_ostream& out) const
00090 {
00091   for (typename Map::const_iterator p = storage_.begin(); p != storage_.end(); ++p) {
00092     unsigned i,j,k;
00093     bigdecode((*p).first, i, j, k);
00094     out << '(' << i << ',' << j << ',' << k << "): " << (*p).second << vcl_endl;
00095   }
00096   return out;
00097 }
00098 
00099 #define VBL_BIG_SPARSE_ARRAY_3D_INSTANTIATE_base(T) \
00100 template class vbl_big_sparse_array_3d<T >
00101 
00102 #undef VBL_BIG_SPARSE_ARRAY_3D_INSTANTIATE
00103 #define VBL_BIG_SPARSE_ARRAY_3D_INSTANTIATE(T) \
00104 VBL_BIG_SPARSE_ARRAY_3D_INSTANTIATE_base(T); \
00105 VCL_INSTANTIATE_INLINE(vcl_ostream& operator << (vcl_ostream&, vbl_big_sparse_array_3d<T > const&))
00106 
00107 #endif // vbl_big_sparse_array_3d_txx_