OpenMS
ConstRefVector.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/CONCEPT/Types.h>
12 
13 #include <algorithm>
14 #include <typeinfo>
15 #include <vector>
16 
17 namespace OpenMS
18 {
19 
42  template <typename ContainerT>
44  {
45 public:
46 
48  template <class ValueT>
50  {
51  friend class ConstRefVector;
52 
53 public:
54  typedef ValueT ValueType;
56  typedef typename std::vector<ValueType*>::difference_type difference_type;
57  typedef const value_type& reference;
58  typedef const value_type* pointer;
59  typedef std::random_access_iterator_tag iterator_category;
60 
62 
64 
66 
68 
69  ConstRefVectorConstIterator(const typename std::vector<ValueType*>* vec, unsigned int position)
70  {
71  vector_ = (typename std::vector<ValueType*>*)vec;
72  position_ = position;
73  }
74 
75  ConstRefVectorConstIterator(typename std::vector<ValueType*>* vec, unsigned int position)
76  {
77  vector_ = vec;
78  position_ = position;
79  }
80 
81  bool operator<(const ConstRefVectorConstIterator& it) const
82  {
83  return position_ < it.position_;
84  }
85 
86  bool operator>(const ConstRefVectorConstIterator& it) const
87  {
88  return position_ > it.position_;
89  }
90 
92  {
93  return position_ < it.position_ || position_ == it.position_;
94  }
95 
97  {
98  return position_ > it.position_ || position_ == it.position_;
99  }
100 
102  {
103  return position_ == it.position_ && vector_ == it.vector_;
104  }
105 
107  {
108  return position_ != it.position_ || vector_ != it.vector_;
109  }
110 
112  {
113  position_ += 1;
114  return *this;
115  }
116 
118  {
119  ConstRefVectorConstIterator tmp(*this);
120  ++(*this);
121  return tmp;
122  }
123 
125  {
126  position_ -= 1;
127  return *this;
128  }
129 
131  {
132  ConstRefVectorConstIterator tmp(*this);
133  --(*this);
134  return tmp;
135  }
136 
138  {
139  ConstRefVectorConstIterator tmp(*this);
140  tmp.position_ -= n;
141  return tmp;
142  }
143 
145  {
146  ConstRefVectorConstIterator tmp(*this);
147  tmp.position_ += n;
148  return tmp;
149  }
150 
152  {
153  position_ += n;
154  return *this;
155  }
156 
158  {
159  position_ -= n;
160  return *this;
161  }
162 
164  {
165  return i1.position_ - i2.position_;
166  }
167 
169  {
171  tmp.position_ += n;
172  return tmp;
173  }
174 
176  {
177  return *((*vector_)[position_]);
178  }
179 
181  {
182  return (*vector_)[position_];
183  }
184 
185 protected:
186 
187  typename std::vector<ValueType*>* vector_;
188  unsigned int position_;
189  };
190 
191 
193  template <class ValueT>
195  public ConstRefVectorConstIterator<ValueT>
196  {
197  friend class ConstRefVector;
198 
199 public:
200 
201  typedef ValueT ValueType;
204 
207 
209 
211 
212  ConstRefVectorIterator(typename std::vector<ValueType*>* vec, unsigned int position) :
213  ConstRefVectorConstIterator<ValueType>(vec, position)
214  {
215  }
216 
218 
220 
222  {
223  return *((*vector_)[position_]);
224  }
225 
227  {
228  return (*vector_)[position_];
229  }
230 
232  {
234  return *this;
235  }
236 
238  {
239  ConstRefVectorIterator tmp(*this);
240  ++(*this);
241  return tmp;
242  }
243 
245  {
247  return *this;
248  }
249 
251  {
252  ConstRefVectorIterator tmp(*this);
253  --(*this);
254  return tmp;
255  }
256 
258  {
259  ConstRefVectorIterator tmp(*this);
260  tmp.position_ -= n;
261  return tmp;
262  }
263 
265  {
266  ConstRefVectorIterator tmp(*this);
267  tmp.position_ += n;
268  return tmp;
269  }
270 
272  {
273  ConstRefVectorIterator tmp(i);
274  tmp.position_ += n;
275  return tmp;
276  }
277 
279  {
281  return *this;
282  }
283 
285  {
287  return *this;
288  }
289 
291  {
292  unsigned int tmp = i1.position_;
293  i1.position_ = i2.position_;
294  i2.position_ = tmp;
295  }
296 
297  };
298 
299 
301 
302  typedef ContainerT ContainerType;
305  typedef typename ContainerType::value_type ValueType;
308  typedef std::reverse_iterator<Iterator> ReverseIterator;
309  typedef std::reverse_iterator<ConstIterator> ConstReverseIterator;
311 
313 
315  typedef typename ContainerType::size_type size_type;
316  typedef typename ContainerType::difference_type difference_type;
317  typedef typename ContainerType::reference reference;
318  typedef typename ContainerType::const_reference const_reference;
319  typedef typename ContainerType::pointer pointer;
325 
326 
328  void push_back(const ValueType& x)
329  {
330  const ValueType* element = &x;
331  vector_.push_back(element);
332  }
333 
335  void pop_back()
336  {
337  vector_.pop_back();
338  }
339 
341  size_type size() const
342  {
343  return vector_.size();
344  }
345 
348  {
349  return std::max(vector_.size(), capacity_);
350  }
351 
354  {
355  size_type cap = capacity();
356 
357  if (n > cap)
358  {
359  vector_.reserve(n);
360  capacity_ = n;
361  }
362  }
363 
366  {
367  return vector_.max_size();
368  }
369 
372  {
373  return Iterator((std::vector<const ValueType*>*) & vector_, (unsigned int)0);
374  }
375 
378  {
379  return Iterator((std::vector<const ValueType*>*) & vector_, (unsigned int)(vector_.size()));
380  }
381 
384  {
385  return ConstIterator((const std::vector<const ValueType*>*) & vector_, (unsigned int)0);
386  }
387 
390  {
391  return ConstIterator((const std::vector<const ValueType*>*) & vector_, (unsigned int)(vector_.size()));
392  }
393 
396  {
397  return ReverseIterator(end());
398  }
399 
402  {
403  return ReverseIterator(begin());
404  }
405 
408  {
409  return ConstReverseIterator(end());
410  }
411 
414  {
415  return ConstReverseIterator(begin());
416  }
417 
419  void resize(size_type new_size)
420  {
421  vector_.resize(new_size);
422  capacity_ = vector_.capacity();
423  }
424 
426  void resize(size_type new_size, const ValueType& t)
427  {
428  vector_.resize(new_size, &t);
429  capacity_ = vector_.capacity();
430  }
431 
434  {
435  return *(begin());
436  }
437 
440  {
441  return *(end() - 1);
442  }
443 
445  void clear()
446  {
447  vector_.clear();
448  }
449 
451  bool empty() const
452  {
453  return vector_.empty();
454  }
455 
458  {
459  return *(vector_[n]);
460  }
461 
463  bool operator==(const ConstRefVector& array) const
464  {
466  {
467  return false;
468  }
469  if (size() != array.size())
470  {
471  return false;
472  }
473  for (Size i = 0; i < size(); i++)
474  {
475  if (typeid(*(vector_[i])) != typeid(*(array.vector_[i])))
476  {
477  return false;
478  }
479  if (vector_[i]->operator!=(* array.vector_[i]))
480  {
481  return false;
482  }
483  }
484  return true;
485  }
486 
488  bool operator!=(const ConstRefVector& array) const
489  {
490  return !(operator==(array));
491  }
492 
494  bool operator<(const ConstRefVector& array) const
495  {
496  return size() < array.size();
497  }
498 
500  bool operator>(const ConstRefVector& array) const
501  {
502  return size() > array.size();
503  }
504 
506  bool operator<=(const ConstRefVector& array) const
507  {
508  return operator<(array) || operator==(array);
509  }
510 
512  bool operator>=(const ConstRefVector& array) const
513  {
514  return operator>(array) || operator==(array);
515  }
516 
518  void swap(ConstRefVector& array)
519  {
520  vector_.swap(array.vector_);
521  }
522 
524  friend void swap(ConstRefVector& a1, ConstRefVector& a2)
525  {
526  a1.vector_.swap(a2.vector_);
527  }
528 
530  Iterator insert(Iterator pos, const ValueType& element)
531  {
532  const ValueType* pointer = &element;
533  vector_.insert(vector_.begin() + pos.position_, pointer);
534  return pos;
535  }
536 
538  void insert(Iterator pos, size_type n, const ValueType& element)
539  {
540  const ValueType* pointer;
541  std::vector<const ValueType*> tmp;
542  for (size_type i = 0; i < n; i++)
543  {
544  pointer = &element;
545  tmp.push_back(pointer);
546  }
547  vector_.insert(vector_.begin() + pos.position_, tmp.begin(), tmp.end());
548  }
549 
551  template <class InputIterator>
552  void insert(Iterator pos, InputIterator f, InputIterator l)
553  {
554  const ValueType* pointer;
555  std::vector<const ValueType*> tmp;
556  for (InputIterator it = f; it != l; ++it)
557  {
558  pointer = &(*it);
559  tmp.push_back(pointer);
560  }
561  vector_.insert(vector_.begin() + pos.position_, tmp.begin(), tmp.end());
562  }
563 
566  {
567  vector_.erase(vector_.begin() + pos.position_);
568  return pos;
569  }
570 
573  {
574  vector_.erase(vector_.begin() + first.position_, vector_.begin() + last.position_);
575  return first;
576  }
577 
579 
580  ConstRefVector() :
582  capacity_(0),
583  base_container_ptr_(nullptr)
584  {
585  }
586 
589  capacity_(0),
591  {
592  vector_ = std::vector<const ValueType*>(n);
593  }
594 
596  ConstRefVector(size_type n, const ValueType& element) :
597  capacity_(0),
599  {
600  vector_ = std::vector<const ValueType*>(n, &element);
601  }
602 
605  capacity_(0),
607  {
608  const ValueType* element;
609  for (ConstIterator it = p.begin(); it != p.end(); ++it)
610  {
611  element = &(*it);
612  vector_.push_back(element);
613  }
614  }
615 
617  template <class InputIterator>
618  ConstRefVector(InputIterator f, InputIterator l) :
619  capacity_(0),
620  base_container_ptr_(nullptr)
621  {
622  const ValueType* pointer;
623  for (InputIterator it = f; it != l; ++it)
624  {
625  pointer = &(*it);
626  vector_.push_back(pointer);
627  }
628  }
629 
632  capacity_(0),
634  {
635  const ValueType* element;
636  for (typename ContainerType::iterator it = p.begin(); it != p.end(); ++it)
637  {
638  element = &(*it);
639  vector_.push_back(element);
640  }
641  }
642 
645  {
646  }
647 
649 
652  {
653  if (this == &rhs) return *this;
654 
656  clear();
657  reserve(rhs.size());
658  const ValueType* element;
659  for (ConstIterator it = rhs.begin(); it != rhs.end(); ++it)
660  {
661  element = &(*it);
662  vector_.push_back(element);
663  }
664 
665  return *this;
666  }
667 
669  template <class InputIterator>
670  void assign(InputIterator f, InputIterator l)
671  {
672  clear();
673  insert(end(), f, l);
674  }
675 
677  void assign(size_type n, const ValueType& x)
678  {
679  clear();
680  insert(end(), n, x);
681  }
682 
691 
693  void sortByIntensity(bool reverse = false)
694  {
695  if (reverse)
696  {
697  auto pointerCmp = [](auto& left, auto& right){typename ValueType::IntensityLess cmp; return cmp(*left, *right);};
698  std::sort(vector_.begin(), vector_.end(), [&](auto &left, auto &right) {return pointerCmp(right, left);});
699  }
700  else
701  {
702  std::sort(vector_.begin(), vector_.end(), [](auto& left, auto& right){typename ValueType::IntensityLess cmp; return cmp(*left, *right);});
703  }
704  }
705 
708  {
709  std::sort(vector_.begin(), vector_.end(), [](auto& left, auto& right){typename ValueType::PositionLess cmp; return cmp(*left, *right);});
710  }
711 
713 
727  template <typename ComparatorType>
728  void sortByComparator(ComparatorType const& comparator = ComparatorType())
729  {
730  std::sort(vector_.begin(), vector_.end(), [&](auto& left, auto& right){return comparator(*left, *right);});
731  }
732 
734 
735  //----------------------------------------------------------------------
736 
737 protected:
738 
740  std::vector<const ValueType*> vector_;
745  };
746 
747 } // namespace OpenMS
748 
ConstIterator for the ConstRefVector.
Definition: ConstRefVector.h:50
ValueT ValueType
Definition: ConstRefVector.h:54
ConstRefVectorConstIterator & operator++()
Definition: ConstRefVector.h:111
ConstRefVectorConstIterator operator--(int)
Definition: ConstRefVector.h:130
ConstRefVectorConstIterator & operator+=(difference_type n)
Definition: ConstRefVector.h:151
ConstRefVectorConstIterator operator++(int)
Definition: ConstRefVector.h:117
bool operator<=(const ConstRefVectorConstIterator &it) const
Definition: ConstRefVector.h:91
std::random_access_iterator_tag iterator_category
Definition: ConstRefVector.h:59
ConstRefVectorConstIterator operator+(difference_type n) const
Definition: ConstRefVector.h:144
bool operator>(const ConstRefVectorConstIterator &it) const
Definition: ConstRefVector.h:86
ValueType value_type
Definition: ConstRefVector.h:55
std::vector< ValueType * > * vector_
Definition: ConstRefVector.h:187
unsigned int position_
Definition: ConstRefVector.h:188
ConstRefVectorConstIterator & operator-=(difference_type n)
Definition: ConstRefVector.h:157
ConstRefVectorConstIterator(typename std::vector< ValueType * > *vec, unsigned int position)
Definition: ConstRefVector.h:75
ConstRefVectorConstIterator & operator=(const ConstRefVectorConstIterator &)=default
const value_type & reference
Definition: ConstRefVector.h:57
reference operator*() const
Definition: ConstRefVector.h:175
ConstRefVectorConstIterator(const typename std::vector< ValueType * > *vec, unsigned int position)
Definition: ConstRefVector.h:69
bool operator<(const ConstRefVectorConstIterator &it) const
Definition: ConstRefVector.h:81
ConstRefVectorConstIterator & operator--()
Definition: ConstRefVector.h:124
friend ConstRefVectorConstIterator operator+(difference_type n, const ConstRefVectorConstIterator &i)
Definition: ConstRefVector.h:168
const value_type * pointer
Definition: ConstRefVector.h:58
ConstRefVectorConstIterator operator-(difference_type n) const
Definition: ConstRefVector.h:137
bool operator>=(const ConstRefVectorConstIterator &it) const
Definition: ConstRefVector.h:96
ConstRefVectorConstIterator(const ConstRefVectorConstIterator &)=default
std::vector< ValueType * >::difference_type difference_type
Definition: ConstRefVector.h:56
bool operator!=(const ConstRefVectorConstIterator &it) const
Definition: ConstRefVector.h:106
pointer operator->() const
Definition: ConstRefVector.h:180
bool operator==(const ConstRefVectorConstIterator &it) const
Definition: ConstRefVector.h:101
friend difference_type operator-(const ConstRefVectorConstIterator &i1, const ConstRefVectorConstIterator &i2)
Definition: ConstRefVector.h:163
Mutable iterator for the ConstRefVector.
Definition: ConstRefVector.h:196
ConstRefVectorIterator(typename std::vector< ValueType * > *vec, unsigned int position)
Definition: ConstRefVector.h:212
ConstRefVectorIterator operator--(int)
Definition: ConstRefVector.h:250
ValueT ValueType
Definition: ConstRefVector.h:201
ConstRefVectorIterator operator++(int)
Definition: ConstRefVector.h:237
ConstRefVectorIterator(const ConstRefVectorIterator &)=default
ConstRefVectorIterator & operator++()
Definition: ConstRefVector.h:231
ConstRefVectorIterator & operator+=(typename ConstRefVectorIterator::difference_type n)
Definition: ConstRefVector.h:278
friend void swap(ConstRefVectorIterator &i1, ConstRefVectorIterator &i2)
Definition: ConstRefVector.h:290
ConstRefVectorIterator operator+(typename ConstRefVectorIterator::difference_type n) const
Definition: ConstRefVector.h:264
ConstRefVectorIterator & operator--()
Definition: ConstRefVector.h:244
ConstRefVectorIterator & operator-=(typename ConstRefVectorIterator::difference_type n)
Definition: ConstRefVector.h:284
ConstRefVectorIterator & operator=(const ConstRefVectorIterator &rhs)=default
ConstRefVectorIterator operator-(typename ConstRefVectorIterator::difference_type n) const
Definition: ConstRefVector.h:257
reference operator*() const
Definition: ConstRefVector.h:221
ConstRefVectorConstIterator< ValueType >::value_type * pointer
Definition: ConstRefVector.h:203
friend ConstRefVectorIterator operator+(typename ConstRefVectorIterator::difference_type n, const ConstRefVectorIterator &i)
Definition: ConstRefVector.h:271
pointer operator->() const
Definition: ConstRefVector.h:226
ConstRefVectorConstIterator< ValueType >::value_type & reference
Definition: ConstRefVector.h:202
This vector holds pointer to the elements of another container.
Definition: ConstRefVector.h:44
friend void swap(ConstRefVector &a1, ConstRefVector &a2)
See std::vector documentation.
Definition: ConstRefVector.h:524
ConstRefVector(InputIterator f, InputIterator l)
See std::vector documentation.
Definition: ConstRefVector.h:618
void pop_back()
See std::vector documentation.
Definition: ConstRefVector.h:335
ConstReverseIterator const_reverse_iterator
Definition: ConstRefVector.h:323
void insert(Iterator pos, InputIterator f, InputIterator l)
See std::vector documentation.
Definition: ConstRefVector.h:552
std::vector< const ValueType * > vector_
the internal vector of ValueType pointers
Definition: ConstRefVector.h:740
const_reference front() const
See std::vector documentation.
Definition: ConstRefVector.h:433
ConstRefVector(ContainerType &p)
See std::vector documentation.
Definition: ConstRefVector.h:631
ConstRefVector(const ConstRefVector &p)
See std::vector documentation.
Definition: ConstRefVector.h:604
ConstRefVector()
See std::vector documentation.
Definition: ConstRefVector.h:581
Iterator begin()
See std::vector documentation.
Definition: ConstRefVector.h:371
Iterator iterator
Definition: ConstRefVector.h:320
ConstRefVector(size_type n, const ValueType &element)
See std::vector documentation.
Definition: ConstRefVector.h:596
ContainerType::reference reference
Definition: ConstRefVector.h:317
ContainerType::pointer pointer
Definition: ConstRefVector.h:319
std::reverse_iterator< Iterator > ReverseIterator
Definition: ConstRefVector.h:308
bool operator!=(const ConstRefVector &array) const
See std::vector documentation.
Definition: ConstRefVector.h:488
Iterator erase(Iterator pos)
See std::vector documentation.
Definition: ConstRefVector.h:565
ConstRefVector & operator=(const ConstRefVector &rhs)
See std::vector documentation.
Definition: ConstRefVector.h:651
ConstReverseIterator rend() const
See std::vector documentation.
Definition: ConstRefVector.h:413
void resize(size_type new_size)
See std::vector documentation.
Definition: ConstRefVector.h:419
void reserve(size_type n)
See std::vector documentation.
Definition: ConstRefVector.h:353
ConstIterator const_iterator
Definition: ConstRefVector.h:321
void assign(InputIterator f, InputIterator l)
See std::vector documentation.
Definition: ConstRefVector.h:670
size_type size() const
See std::vector documentation.
Definition: ConstRefVector.h:341
const_reference back() const
See std::vector documentation.
Definition: ConstRefVector.h:439
bool empty() const
See std::vector documentation.
Definition: ConstRefVector.h:451
Iterator erase(Iterator first, Iterator last)
See std::vector documentation.
Definition: ConstRefVector.h:572
ConstRefVector(size_type n)
See std::vector documentation.
Definition: ConstRefVector.h:588
bool operator<(const ConstRefVector &array) const
Comparison of container sizes.
Definition: ConstRefVector.h:494
Iterator insert(Iterator pos, const ValueType &element)
See std::vector documentation.
Definition: ConstRefVector.h:530
ConstIterator end() const
See std::vector documentation.
Definition: ConstRefVector.h:389
bool operator>(const ConstRefVector &array) const
Comparison of container sizes.
Definition: ConstRefVector.h:500
size_type capacity_
the current capacity
Definition: ConstRefVector.h:742
ConstRefVectorIterator< const ValueType > Iterator
Definition: ConstRefVector.h:306
ContainerType::value_type ValueType
Definition: ConstRefVector.h:305
ValueType value_type
STL-compliance type definitions.
Definition: ConstRefVector.h:314
void sortByPosition()
Lexicographically sorts the elements by their position.
Definition: ConstRefVector.h:707
ConstReverseIterator rbegin() const
See std::vector documentation.
Definition: ConstRefVector.h:407
void sortByComparator(ComparatorType const &comparator=ComparatorType())
Definition: ConstRefVector.h:728
ContainerType::const_reference const_reference
Definition: ConstRefVector.h:318
void sortByIntensity(bool reverse=false)
Sorting.
Definition: ConstRefVector.h:693
ReverseIterator reverse_iterator
Definition: ConstRefVector.h:322
size_type max_size() const
See std::vector documentation.
Definition: ConstRefVector.h:365
const ContainerType * base_container_ptr_
Pointer to the base container.
Definition: ConstRefVector.h:744
ReverseIterator rend()
See std::vector documentation.
Definition: ConstRefVector.h:401
bool operator>=(const ConstRefVector &array) const
Comparison of container sizes.
Definition: ConstRefVector.h:512
Iterator end()
See std::vector documentation.
Definition: ConstRefVector.h:377
void assign(size_type n, const ValueType &x)
See std::vector documentation.
Definition: ConstRefVector.h:677
ContainerType::difference_type difference_type
Definition: ConstRefVector.h:316
const_reference operator[](size_type n) const
See std::vector documentation.
Definition: ConstRefVector.h:457
void clear()
See std::vector documentation.
Definition: ConstRefVector.h:445
size_type capacity() const
See std::vector documentation.
Definition: ConstRefVector.h:347
ConstIterator begin() const
See std::vector documentation.
Definition: ConstRefVector.h:383
~ConstRefVector()
See std::vector documentation.
Definition: ConstRefVector.h:644
ContainerType::size_type size_type
Definition: ConstRefVector.h:315
bool operator<=(const ConstRefVector &array) const
Comparison of container sizes.
Definition: ConstRefVector.h:506
void push_back(const ValueType &x)
See std::vector documentation.
Definition: ConstRefVector.h:328
ReverseIterator rbegin()
See std::vector documentation.
Definition: ConstRefVector.h:395
void insert(Iterator pos, size_type n, const ValueType &element)
See std::vector documentation.
Definition: ConstRefVector.h:538
ContainerT ContainerType
Type definitions.
Definition: ConstRefVector.h:303
std::reverse_iterator< ConstIterator > ConstReverseIterator
Definition: ConstRefVector.h:309
void swap(ConstRefVector &array)
See std::vector documentation.
Definition: ConstRefVector.h:518
ConstRefVectorConstIterator< const ValueType > ConstIterator
Definition: ConstRefVector.h:307
void resize(size_type new_size, const ValueType &t)
See std::vector documentation.
Definition: ConstRefVector.h:426
bool operator==(const ConstRefVector &array) const
See std::vector documentation.
Definition: ConstRefVector.h:463
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
static String & reverse(String &this_s)
Definition: StringUtilsSimple.h:330
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19