53 typedef const T *const_pointer;
65 inline
reference(drwnSparseVec<T>& v, size_t indx);
70 inline operator T() const;
72 inline
reference& operator=(const reference& x);
80 typedef const T& const_reference;
81 typedef size_t size_type;
82 typedef ptrdiff_t difference_type;
93 typedef random_access_iterator_tag iterator_category;
95 typedef size_t size_type;
96 typedef ptrdiff_t difference_type;
98 typedef const T *const_pointer;
100 typedef const T& const_reference;
108 inline bool operator!=(
const iterator& it)
const;
110 inline bool operator>(
const iterator& it)
const;
111 inline bool operator<=(
const iterator& it)
const;
112 inline bool operator>=(
const iterator& it)
const;
113 inline const_reference operator*()
const;
120 inline iterator operator+(difference_type n);
121 inline iterator& operator+=(difference_type n);
122 inline iterator operator-(difference_type n);
123 inline iterator& operator-=(difference_type n);
124 inline const T& operator[](difference_type n)
const;
125 inline reference operator[](difference_type n);
127 inline difference_type operator-(
const iterator& it)
const {
128 return (_indx - it._indx);
130 inline difference_type operator+(
const iterator& it)
const {
131 return (_indx + it._indx);
150 bool empty()
const {
return (_size == 0); }
151 void clear() { _size = 0; _data.clear(); }
152 void resize(
size_t n);
153 size_t size()
const {
return _size; }
154 size_t capacity()
const {
return _size; }
155 size_t max_size()
const {
return size_type(-1) /
sizeof(value_type); }
156 void reserve(
size_t size) { }
159 size_t nnz()
const {
return _data.size(); }
161 iterator begin() {
return iterator(*
this, 0); }
162 iterator end() {
return iterator(*
this, _size); }
165 void push_back(
const T& x);
167 void insert(
size_t position,
const T& x);
168 void insert(
size_t position,
typename vector<T>::const_iterator first,
169 typename vector<T>::const_iterator last);
170 void insert(iterator position,
const T& x) {
171 assert(position._p ==
this);
172 return insert(position._indx, x);
174 void insert(iterator position,
typename vector<T>::const_iterator first,
175 typename vector<T>::const_iterator last) {
176 assert(position._p ==
this);
177 return insert(position._indx, first, last);
183 const T& operator[](
size_t indx)
const;
184 reference operator[](
size_t indx);
187 operator vector<T>()
const {
return decode(); }
198 vector<T> decode()
const;
201 void encode(
const vector<T>& v);
223 _size(v._size), _data(v._data)
245 for (
typename map<size_t, T>::iterator it = _data.begin(); it != _data.end(); it++) {
246 if (it->first >= _size) {
247 it = _data.erase(it);
255 std::swap(_size, v._size);
271 DRWN_ASSERT(_size > 0);
272 typename map<size_t, T>::iterator it = _data.find(_size - 1);
273 if (it != _data.end()) {
283 DRWN_ASSERT((position >= this->begin()._indx) && (position <= this->end()._indx));
285 if (position != this->end()._indx) {
286 map<size_t, T> oldVec(_data);
289 for (
typename map<size_t, T>::const_iterator it = oldVec.begin(); it != oldVec.end(); it++) {
290 if (it->first >= position) {
291 _data[it->first + 1] = it->second;
293 _data[it->first] = it->second;
307 typename vector<T>::const_iterator last)
310 DRWN_ASSERT((position >= this->begin()._indx) && (position <= this->end()._indx));
312 map<size_t, T> oldVec;
313 if (position != this->end()._indx) {
318 for (
typename vector<T>::const_iterator it = first; it != last; it++) {
320 _data[position + delta] = *it;
325 for (
typename map<size_t, T>::const_iterator it = oldVec.begin(); it != oldVec.end(); it++) {
326 if (it->first >= position) {
327 _data[it->first + delta] = it->second;
329 _data[it->first] = it->second;
357 typename map<size_t, T>::const_iterator it = _data.find(indx);
358 if (it != _data.end()) {
379 template <
typename T>
383 for (
typename map<size_t, T>::const_iterator it = y._data.begin(); it != y._data.end(); ++it) {
384 d += x[it->first] * it->second;
390 template <
typename T>
394 for (
typename map<size_t, T>::const_iterator it = x._data.begin(); it != x._data.end(); ++it) {
395 d += y[it->first] * it->second;
401 template <
typename T>
406 if (x.nnz() < y.nnz()) {
407 for (
typename map<size_t, T>::const_iterator it = x._data.begin(); it != x._data.end(); ++it) {
408 d += y[it->first] * it->second;
411 for (
typename map<size_t, T>::const_iterator it = y._data.begin(); it != y._data.end(); ++it) {
412 d += x[it->first] * it->second;
423 vector<T> v(_size, T(0));
424 for (
typename map<size_t, T>::const_iterator it = _data.begin(); it != _data.end(); it++) {
425 v[it->first] = it->second;
435 for (
size_t i = 0; i < v.size(); i++) {
460 typename map<size_t, T>::iterator it = _p->_data.find(_indx);
461 if (it == _p->_data.end()) {
471 typename map<size_t, T>::iterator it = _p->_data.find(_indx);
472 if (it == _p->_data.end()) {
474 _p->_data.insert(make_pair(_indx, x));
490 this->operator=((T)x);
497 typename map<size_t, T>::iterator it = _p->_data.find(_indx);
498 if (it != _p->_data.end()) {
499 return (*
this = (it->second + x));
508 typename map<size_t, T>::iterator it = _p->_data.find(_indx);
509 if (it != _p->_data.end()) {
510 return (*
this = (it->second - x));
513 return (*
this = (T(0) - x));
519 typename map<size_t, T>::iterator it = _p->_data.find(_indx);
520 if (it != _p->_data.end()) {
521 return (*
this = (it->second * x));
524 return (*
this = (T(0) * x));
530 typename map<size_t, T>::iterator it = _p->_data.find(_indx);
531 if (it != _p->_data.end()) {
532 return (*
this = (it->second / x));
535 return (*
this = (T(0) / x));
571 return ((_p == it._p) && (_indx == it._indx));
577 return ((_p != it._p) || (_indx != it._indx));
583 return (_indx < it._indx);
589 return (_indx > it._indx);
595 return (_indx <= it._indx);
601 return (_indx >= it._indx);
607 DRWN_ASSERT(_p != NULL);
620 _indx = std::min(_indx + 1, _p->_size);
627 if (_indx > 0) _indx -= 1;
634 iterator copy(*
this);
642 iterator copy(*
this);
680 return (*_p)[_indx + n];
Definition: drwnSparseVector.h:57
Definition: drwnSparseVector.h:84
Quick-and-dirty sparse vector class as a plugin replacement for std::vector.
Definition: drwnSparseVector.h:44
bool operator<(const CvSize &r, const CvSize &s)
inequality operator for CvSize objects (allows partial sorting)
Definition: drwnOpenCVUtils.cpp:88
static T dot(const vector< T > &x, const drwnSparseVec< T > &y)
efficient dot product between a vector and a sparse vector
Definition: drwnSparseVector.h:380
bool operator==(const CvRect &r, const CvRect &s)
equality operator for CvRect objects
Definition: drwnOpenCVUtils.cpp:83