core/vbl/vbl_sparse_array_base.txx
Go to the documentation of this file.
00001 // This is core/vbl/vbl_sparse_array_base.txx
00002 #ifndef vbl_sparse_array_base_txx_
00003 #define vbl_sparse_array_base_txx_
00004 //:
00005 // \file
00006 // \brief Contains a base class for sparse arrays.
00007 // \author Ian Scott
00008 
00009 #include "vbl_sparse_array_base.h"
00010 #include <vcl_cassert.h>
00011 #include <vcl_utility.h>
00012 
00013 //: Empty the sparse matrix.
00014 template <class T, class Index>
00015 void vbl_sparse_array_base<T, Index>::clear()
00016 {
00017   storage_.clear();
00018 }
00019 
00020 //: Return contents of (i).  Assertion failure if not yet filled.
00021 template <class T, class Index>
00022 T const & vbl_sparse_array_base<T, Index>::operator () (Index i) const
00023 {
00024   typename Map::const_iterator p = storage_.find(i);
00025 
00026   assert(p != storage_.end());
00027 
00028   return (*p).second;
00029 }
00030 
00031 //: Erase element at location (i). Assertion failure if not yet filled.
00032 template <class T, class Index>
00033 void vbl_sparse_array_base<T, Index>::erase (Index i)
00034 {
00035   typename Map::iterator p = storage_.find(i);
00036 
00037   assert(p != storage_.end());
00038 
00039   storage_.erase(p);
00040 }
00041 
00042 //: Return the memory address of location (i).  0 if not yet filled.
00043 template <class T, class Index>
00044 T* vbl_sparse_array_base<T, Index>::get_addr(Index i)
00045 {
00046   typename Map::iterator p = storage_.find(i);
00047 
00048   if (p == storage_.end())
00049     return 0;
00050 
00051   return &(*p).second;
00052 }
00053 
00054 //: Return true if location (i) has been filled.
00055 template <class T, class Index>
00056 bool vbl_sparse_array_base<T, Index>::fullp(Index i) const
00057 {
00058   return storage_.find(i) != storage_.end();
00059 }
00060 
00061 //: Put a value into location (i).
00062 template <class T, class Index>
00063 bool vbl_sparse_array_base<T, Index>::put(Index i, const T& t)
00064 {
00065   typedef typename Map::iterator iter;
00066   typedef typename Map::value_type value_type;
00067   vcl_pair<iter,bool> res = storage_.insert(value_type(i,t));
00068 
00069   return res.second;
00070 }
00071 
00072 #undef VBL_SPARSE_ARRAY_BASE_INSTANTIATE
00073 #define VBL_SPARSE_ARRAY_BASE_INSTANTIATE(T, I) \
00074 template class vbl_sparse_array_base<T , I >
00075 
00076 #endif // vbl_sparse_array_base_txx_