vbl_big_sparse_array_3d.h
Go to the documentation of this file.
1 // This is core/vbl/vbl_big_sparse_array_3d.h
2 #ifndef vbl_big_sparse_array_3d_h_
3 #define vbl_big_sparse_array_3d_h_
4 //:
5 // \file
6 // \brief Sparse 3D array
7 // \author Andrew W. Fitzgibbon, Oxford RRG, 02 Oct 96
8 //
9 // vbl_big_sparse_array_3d is a sparse 3D array allowing space efficient
10 // access of the form s(300,700,900) = 2;
11 // It uses the 64-bit integer type "long long" (whenever supported by the
12 // compiler) to store the 3D index: 21 bits per dimension.
13 // Hence the largest possible coordinate in each dimension is 2^21-1 = 2097151.
14 // On platforms that do not have 64-bit integers, the maximum is 2^10-1 = 1023.
15 // (Actually, for some dimensions, it could be a factor 2 higher.)
16 //
17 // Example usage:
18 // \code
19 // vbl_big_sparse_array_3d<double> x;
20 //
21 // x(1,2,3) = 1.23;
22 // x(100,200,3) = 100.2003;
23 // x.put(200,300,4, 200.3004);
24 //
25 // std::cout << "123 = " << x(1,2,3) << std::endl
26 // << "222 = " << x(2,2,2) << std::endl
27 // << "333 is full? " << x.fullp(3,3,3) << std::endl
28 // << x;
29 // \endcode
30 //
31 // \verbatim
32 // Modifications
33 // 180497 AWF - Moved to Basics
34 // 261001 Peter Vanroose - documentation added about implementation
35 // 261001 Peter Vanroose - bug fixed in bigencode - had 11,22 instead of 21,42.
36 // 271001 Peter Vanroose - ported to vxl from BigSparseArray3; removed n1,n2,n3
37 // \endverbatim
38 //-----------------------------------------------------------------------------
39 
40 #include <functional>
41 #include <map>
42 #include <iosfwd>
43 #include <vxl_config.h>
44 
45 #if VXL_HAS_INT_64
46 typedef vxl_uint_64 ulonglong;
47 #elif VXL_HAS_INT_32
48 typedef vxl_uint_32 ulonglong;
49 #else
50 # error "only implemented with 32 and 64-bit ints"
51 #endif
52 
53 #ifdef _MSC_VER
54 # include <vcl_msvc_warnings.h>
55 #endif
56 
57 template <class T>
59 {
60  protected:
61  // Data Members--------------------------------------------------------------
62  typedef std::map<ulonglong, T, std::less<ulonglong> > Map;
63  Map storage_;
64 
65  public:
66  // Constructors/Destructor---------------------------------------------------
67 
68  //: Construct a vbl_big_sparse_array_3d
69  vbl_big_sparse_array_3d() = default;
70  ~vbl_big_sparse_array_3d() = default;
71 
72  // Potentially clunky copy constructor
74  // Potentially clunky assignment operator
76  { storage_ = b.storage_; return *this; }
77 
78  // Operations----------------------------------------------------------------
79  T & operator() (unsigned, unsigned, unsigned);
80  T const& operator() (unsigned, unsigned, unsigned) const;
81 
82  //: Has this cell been assigned a value?
83  bool fullp(unsigned, unsigned, unsigned) const;
84  //: Put a value in a certain cell
85  bool put(unsigned, unsigned, unsigned, T const&);
86 
87  // Computations--------------------------------------------------------------
88  unsigned int count_nonempty() const { return (unsigned int)(storage_.size()); }
89 
90  // Data Control--------------------------------------------------------------
91  std::ostream& print(std::ostream&) const;
92 };
93 
94 template <class T>
95 inline std::ostream& operator<<(std::ostream&s,vbl_big_sparse_array_3d<T>const& a)
96 {
97  return a.print(s);
98 }
99 
100 #define VBL_BIG_SPARSE_ARRAY_3D_INSTANTIATE(T) \
101 extern "Please #include <vbl/vbl_big_sparse_array_3d.hxx> instead"
102 
103 #endif // vbl_big_sparse_array_3d_h_
vbl_big_sparse_array_3d()=default
Construct a vbl_big_sparse_array_3d.
std::ostream & operator<<(std::ostream &s, vbl_big_sparse_array_3d< T >const &a)
~vbl_big_sparse_array_3d()=default
T & operator()(unsigned, unsigned, unsigned)
unsigned int count_nonempty() const
vbl_big_sparse_array_3d< T > & operator=(vbl_big_sparse_array_3d< T > const &b)
std::ostream & print(std::ostream &) const
bool fullp(unsigned, unsigned, unsigned) const
Has this cell been assigned a value?.
bool put(unsigned, unsigned, unsigned, T const &)
Put a value in a certain cell.
std::map< ulonglong, T, std::less< ulonglong > > Map