Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

vbl_sparse_array_base.h

Go to the documentation of this file.
00001 // This is core/vbl/vbl_sparse_array_base.h
00002 #ifndef vbl_sparse_array_base_h_
00003 #define vbl_sparse_array_base_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief base class for sparse arrays.
00010 // \author Ian Scott, Manchester ISBE
00011 // \date   10 April 2001
00012 
00013 #include <vcl_functional.h>
00014 #include <vcl_map.h>
00015 
00016 
00017 //: A fully featured sparse array which devolves indexing to its templated type
00018 // If you just want an ordinary sparse array use vbl_sparse_array_1d,
00019 // vbl_sparse_array_2d, or vbl_sparse_array_3d.
00020 //
00021 // Design Decision: Advanced Users only.
00022 //
00023 // The sparse array design has as much of the code as possible in this
00024 // templated base class. This allows us to code harden this class
00025 // while leaving the three derived classes in vbl, simple, easy to
00026 // understand and use.
00027 // I rejected to use templating over the number of dimensions because
00028 // this can lead into recursive templating which is in theory very nice,
00029 // but in practice very horrible. It also makes the interface rather
00030 // unintuitive.
00031 // If you are worried about the speed aspects of using a pair of integers
00032 // instead of a single encoded integer, then you can create
00033 // an encoder class as the index type, and use it directly, or hide the
00034 // details by writing a specialising derivative of vbl_sparse_array_base.
00035 
00036 template <class T, class Index>
00037 class vbl_sparse_array_base
00038 {
00039  protected:
00040   //: The type of the storage
00041   typedef vcl_map<Index, T, vcl_less<Index> > Map;
00042   //: This stores a compact list of the values.
00043   Map storage_;
00044 
00045  public:
00046 
00047   //: Return contents at (i)
00048   T      & operator () (Index i) { return storage_[i]; }
00049 
00050   //: Return contents at (i). Asserts if (i) empty.
00051   T const& operator () (Index i) const;
00052 
00053   //: Erase element at location (i). Assertion failure if not yet filled.
00054   void erase(Index );
00055 
00056   //: Return true if location (i) has been filled.
00057   bool fullp(Index ) const;
00058 
00059   //: Put a value into location (i).
00060   bool put(Index , const T& );
00061 
00062   //: Return the address of location (i).  0 if not yet filled.
00063   T* get_addr(Index);
00064 
00065   //: Empty the sparse matrix.
00066   void clear();
00067 
00068   //: The type of iterators into the efficient storage
00069   typedef typename Map::const_iterator const_iterator;
00070 
00071   //: Return number of locations that have been assigned a value using "put".
00072   unsigned count_nonempty() const { return storage_.size(); }
00073 
00074   //: The type of objects used to index the sparse array
00075   typedef Index Index_type;
00076 
00077   //: The type of values stored by the sparse array
00078   typedef T T_type;
00079 
00080   //: The type of values of the controlled sequence
00081   // The value_type is a vcl_pair<Index_type, typename T_type>
00082   typedef typename Map::value_type sequence_value_type;
00083 
00084   //: A bidirectional iterator pointing at the first non-empty element
00085   // If the array is empty it points just beyond the end.
00086   const_iterator begin() const { return storage_.begin(); }
00087 
00088   //: A bidirectional iterator pointing just beyond last non-empty element.
00089   const_iterator end() const { return storage_.end(); }
00090 
00091 #if 0
00092   //: DEPRECATED Return contents at (i)
00093   // use operator () instead
00094   //
00095   // Deprecated 10 April 2001,
00096   // because the interface does not generalise to more than one
00097   // dimension. e.g. array[i,j] will not compile.
00098   T      & operator [] (Index i) { return storage_[i]; }
00099 
00100   //: DEPRECATED Return contents at (i).
00101   // use operator () instead;
00102   //
00103   // Deprecated 10 April 2001,
00104   // because the interface does not generalise to more than one
00105   // dimension. e.g. array[i,j] will not compile.
00106   T const& operator [] (Index i) const;
00107 #endif
00108 };
00109 
00110 #endif // vbl_sparse_array_base_h_

Generated on Thu Jan 10 14:39:08 2008 for core/vbl by  doxygen 1.4.4