1 #ifndef TIL_SPARSE_VECTOR_H_ 2 #define TIL_SPARSE_VECTOR_H_ 29 template <
typename TMap1,
typename TMap2,
typename TFunctor >
36 void operator()(TMap1 & map1,
const TMap2 & map2, TFunctor f)
38 typename TMap1::iterator i1 = map1.begin();
39 typename TMap2::const_iterator i2 = map2.begin();
41 if (i1 == map1.end())
goto map1over_checkmap2;
42 if (i2 == map2.end())
goto map2over;
48 if (i1->first < i2->first)
54 i1->second = f(i1->second,
typename TMap2::mapped_type());
55 if (++i1 == map1.end())
59 else if (i1->first > i2->first)
64 map1[i2->first] = f(
typename TMap1::mapped_type(), i2->second);
65 if (++i2 == map2.end())
73 i1->second = f(i1->second, i2->second);
74 if (++i1 == map1.end())
78 goto map1over_checkmap2;
80 if (++i2 == map2.end())
89 if (i2 == map2.end())
return;
94 map1[i2->first] = f(
typename TMap1::mapped_type(), i2->second);
96 while (++i2 != map2.end());
102 i1->second = f(i1->second,
typename TMap2::mapped_type());
104 while (++i1 != map1.end());
109 template <
typename TMap1,
typename TMap2,
typename TFunctor >
133 template <
typename T,
typename BaselinePolicy = policy::SVBaseline_Value<T> >
156 typedef std::map<std::size_t, T>
Map;
170 , m_proxyIndex(
std::numeric_limits<
std::size_t>::
max())
178 , m_proxyIndex(
std::numeric_limits<
std::size_t>::
max())
189 assert( i < m_size );
190 return ValueProxy(i, *
this);
224 T
get(std::size_t i)
const 227 typename Map::const_iterator pos ( m_data.find(i) );
228 if (pos != m_data.end())
234 return m_baselinePolicy(i);
263 void set(std::size_t i,
const T & value)
267 if (value != m_baselinePolicy(i))
277 sparse_iterator pos = m_data.find(i);
279 if (pos != m_data.end())
285 m_data.insert(std::make_pair(i,value));
292 sparse_iterator pos = m_data.find(i);
293 if (pos != m_data.end())
316 Map
const &
getMap()
const {
return m_data;}
329 std::size_t
size()
const {
return m_size; }
339 bool empty()
const {
return m_size == 0; }
342 void erase(
const sparse_iterator & i) { m_data.erase(i); }
347 for(
typename Map::const_iterator i = m_data.begin(); i != m_data.end(); ++i)
349 if ((i->second)!=0)
return false;
387 sparse_const_iterator
sparse_end ()
const {
return m_data.end (); }
389 const_iterator
begin()
const {
return const_iterator(0, *
this); }
390 const_iterator
end()
const {
return const_iterator(this->
size(), *
this); }
391 iterator
begin() {
return iterator(0, *
this); }
392 iterator
end() {
return iterator(this->
size(), *
this); }
414 {
for (
typename Map::iterator i = m_data.begin(); i != m_data.end(); ++i) i->second += value; }
416 {
for (
typename Map::iterator i = m_data.begin(); i != m_data.end(); ++i) i->second -= value; }
418 {
for (
typename Map::iterator i = m_data.begin(); i != m_data.end(); ++i) i->second *= value; }
427 BaselinePolicy m_baselinePolicy;
429 std::size_t m_proxyIndex;
482 template <
typename T,
typename BaselinePolicy >
486 typedef typename Map::iterator
Base;
493 template <
typename T,
typename BaselinePolicy >
497 typedef typename Map::const_iterator
Base;
506 template <
typename TSparseVector,
typename Proxy >
515 template <
typename XSparseVector,
typename XProxy >
517 : m_valueProxy(it.proxy()) {}
519 const Proxy &
proxy()
const {
return m_valueProxy; }
520 Proxy &
proxy() {
return m_valueProxy; }
543 template <
typename T1,
typename P1,
typename T2,
typename P2 >
544 inline bool operator ==
549 {
return i1.
proxy().index() == i2.proxy().index(); }
551 template <
typename T1,
typename P1,
typename T2,
typename P2 >
552 inline bool operator !=
557 {
return i1.
proxy().index() != i2.proxy().index(); }
561 template <
typename T,
typename BaselinePolicy >
573 pointer
operator->() {
return Base::m_valueProxy.operator->(); }
576 template <
typename T,
typename BaselinePolicy >
587 pointer
operator->() {
return Base::m_valueProxy.operator->(); }
620 template <
typename T,
typename BaselinePolicy >
T operator[](std::size_t i) const
Get value of i-th element.
const_reference reference
sparse_vector(std::size_t d)
Create a null vector of length d.
const Proxy & proxy() const
void clear()
Alike STL container's "clear".
const_iterator end() const
sparse_iterator sparse_begin()
sparse_const_iterator(const Base &b)
sparse_const_iterator(const sparse_iterator &i)
Adds some left-value operations to const_value_proxy.
OutOfRange(const std::string &msg)
void operator*=(const sparse_vector< T > &a)
BaselinePolicy baselinePolicy() const
Belongs to package Box Do not include directly, include til/Box.h instead.
std::map< std::size_t, T > Map
void operator()(TMap1 &map1, const TMap2 &map2, TFunctor f)
const_iterator(std::size_t i, const sparse_vector< T, BaselinePolicy > &p)
const_iterator begin() const
sparse_iterator sparse_end()
sparse_iterator(const Base &b)
void loop_mapEachAssign(TMap1 &map1, TMap2 &map2, TFunctor f)
numeric_array< T, D > size(const Box< T, D > &box)
Return the size of a box.
detail::sparse_vector_iterator_base< const sparse_vector< T, BaselinePolicy >, ConstValueProxy > Base
detail::sparse_vector_iterator_base< sparse_vector< T, BaselinePolicy >, ValueProxy > Base
sparse_vector()
Create a null vector of size 0.
void erase(const sparse_iterator &i)
Alike STL container's "erase".
A class that mimic the behavior of std::vector but with a storage policy focused on sparse data...
void operator/=(const sparse_vector< T > &a)
sparse_vector_iterator_base(std::size_t i, TSparseVector &sv)
iterator(std::size_t i, sparse_vector< T, BaselinePolicy > &p)
TImage::value_type max(const TImage &im)
Returns the maximum intensity of the input image.
sparse_const_iterator sparse_begin() const
ValueProxy operator[](std::size_t i)
Read-write access to i-th element.
sparse_const_iterator sparse_end() const
Map & getMap()
Returns the internal data.
void operator-=(const sparse_vector< T > &a)
const_iterator(const iterator &it)
const_reference operator*()
sparse_vector_iterator_base Self
TSparseVector::value_type value_type
sparse_vector_iterator_base(sparse_vector_iterator_base< XSparseVector, XProxy > it)
Map const & getMap() const
The value_proxy is designed to provide a simple access to container values that are in fact complicat...
std::size_t size() const
Alike STL container's "size".
Apply a functor to a pair of sparse vectors where either one has data.
void operator-=(const T &value)
BaselinePolicy & baselinePolicy()
bool empty() const
Alike STL container's "empty".
void fill(sparse_vector< T, BaselinePolicy > &v, typename boost::call_traits< T >::param_type value)
Specialized fill for sparse_vector.
void operator+=(const T &value)
Map::mapped_type value_type
void operator+=(const sparse_vector< T > &a)
void operator*=(const T &value)