core/vbl/vbl_big_sparse_array_3d.h
Go to the documentation of this file.
00001 // This is core/vbl/vbl_big_sparse_array_3d.h
00002 #ifndef vbl_big_sparse_array_3d_h_
00003 #define vbl_big_sparse_array_3d_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Sparse 3D array
00010 // \author  Andrew W. Fitzgibbon, Oxford RRG, 02 Oct 96
00011 //
00012 //  vbl_big_sparse_array_3d is a sparse 3D array allowing space efficient
00013 //  access of the form    s(300,700,900) = 2;
00014 //  It uses the 64-bit integer type "long long" (whenever supported by the
00015 //  compiler) to store the 3D index: 21 bits per dimension.
00016 //  Hence the largest possible coordinate in each dimension is 2^21-1 = 2097151.
00017 //  On platforms that do not have 64-bit integers, the maximum is 2^10-1 = 1023.
00018 //  (Actually, for some dimensions, it could be a factor 2 higher.)
00019 //
00020 // Example usage:
00021 // \code
00022 //  vbl_big_sparse_array_3d<double> x;
00023 //
00024 //  x(1,2,3) = 1.23;
00025 //  x(100,200,3) = 100.2003;
00026 //  x.put(200,300,4, 200.3004);
00027 //
00028 //  vcl_cout << "123 = " << x(1,2,3) << vcl_endl
00029 //           << "222 = " << x(2,2,2) << vcl_endl
00030 //           << "333 is full? " << x.fullp(3,3,3) << vcl_endl
00031 //           << x;
00032 // \endcode
00033 //
00034 // \verbatim
00035 //  Modifications
00036 //   180497 AWF - Moved to Basics
00037 //   261001 Peter Vanroose - documentation added about implementation
00038 //   261001 Peter Vanroose - bug fixed in bigencode - had 11,22 instead of 21,42.
00039 //   271001 Peter Vanroose - ported to vxl from BigSparseArray3; removed n1,n2,n3
00040 // \endverbatim
00041 //-----------------------------------------------------------------------------
00042 
00043 #include <vxl_config.h>
00044 
00045 #if VXL_HAS_INT_64
00046 typedef vxl_uint_64 ulonglong;
00047 #elif VXL_HAS_INT_32
00048 typedef vxl_uint_32 ulonglong;
00049 #else
00050 # error "only implemented with 32 and 64-bit ints"
00051 #endif
00052 
00053 #include <vcl_functional.h>
00054 #include <vcl_map.h>
00055 #include <vcl_iosfwd.h>
00056 
00057 template <class T>
00058 class vbl_big_sparse_array_3d
00059 {
00060  protected:
00061   // Data Members--------------------------------------------------------------
00062   typedef vcl_map<ulonglong, T, vcl_less<ulonglong> > Map;
00063   Map storage_;
00064 
00065  public:
00066   // Constructors/Destructor---------------------------------------------------
00067 
00068   //: Construct a vbl_big_sparse_array_3d
00069   vbl_big_sparse_array_3d() {}
00070  ~vbl_big_sparse_array_3d() {}
00071 
00072   // Potentially clunky copy constructor
00073   vbl_big_sparse_array_3d(vbl_big_sparse_array_3d<T> const& b) : storage_(b.storage_) {}
00074   // Potentially clunky assignment operator
00075   vbl_big_sparse_array_3d<T>& operator=(vbl_big_sparse_array_3d<T> const& b)
00076   { storage_ = b.storage_; return *this; }
00077 
00078   // Operations----------------------------------------------------------------
00079   T      & operator() (unsigned, unsigned, unsigned);
00080   T const& operator() (unsigned, unsigned, unsigned) const;
00081 
00082   //: Has this cell been assigned a value?
00083   bool fullp(unsigned, unsigned, unsigned) const;
00084   //: Put a value in a certain cell
00085   bool put(unsigned, unsigned, unsigned, T const&);
00086 
00087   // Computations--------------------------------------------------------------
00088   unsigned int count_nonempty() const { return (unsigned int)(storage_.size()); }
00089 
00090   // Data Control--------------------------------------------------------------
00091   vcl_ostream& print(vcl_ostream&) const;
00092 };
00093 
00094 template <class T>
00095 inline vcl_ostream& operator<<(vcl_ostream&s,vbl_big_sparse_array_3d<T>const& a)
00096 {
00097   return a.print(s);
00098 }
00099 
00100 #define VBL_BIG_SPARSE_ARRAY_3D_INSTANTIATE(T) \
00101 extern "Please #include <vbl/vbl_big_sparse_array_3d.txx> instead"
00102 
00103 #endif // vbl_big_sparse_array_3d_h_