/usr/include/OpenMS/DATASTRUCTURES/ConstRefVector.h is in libopenms-dev 1.11.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 | // --------------------------------------------------------------------------
// OpenMS -- Open-Source Mass Spectrometry
// --------------------------------------------------------------------------
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
// ETH Zurich, and Freie Universitaet Berlin 2002-2013.
//
// This software is released under a three-clause BSD license:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of any author or any participating institution
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
// For a full list of authors, refer to the file AUTHORS.
// --------------------------------------------------------------------------
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// --------------------------------------------------------------------------
// $Maintainer: Erhan Kenar $
// $Authors: $
// --------------------------------------------------------------------------
#ifndef OPENMS_DATASTRUCTURES_CONSTREFVECTOR_H
#define OPENMS_DATASTRUCTURES_CONSTREFVECTOR_H
#include <OpenMS/config.h>
#include <OpenMS/CONCEPT/Types.h>
#include <OpenMS/KERNEL/ComparatorUtils.h>
#include <vector>
#include <typeinfo>
#include <algorithm>
namespace OpenMS
{
/**
@brief This vector holds pointer to the elements of another container.
If you for example want to sort the elements of a constant container, you would have to copy the whole container.
@n To avoid copy actions this class only holds pointer to the constant elements of a container.
@n You can insert new elements, but it is not possible to change existing ones.
The following code demonstrates the use of this class:
@code
FeatureMap<> map;
map.resize(10); //...fill map with data
//Create pointer vector to the map
ConstRefVector<FeatureMap<> > ref_vector(map);
//Sort the pointer vector without changing the original data
ref_vector.sortByIntensity();
@endcode
@improvement Check if we can omit the iterator template arguments (Clemens)
@ingroup Datastructures
*/
template <typename ContainerT>
class ConstRefVector
{
public:
///ConstIterator for the ConstRefVector
template <class ValueT>
class ConstRefVectorConstIterator
{
friend class ConstRefVector;
public:
typedef ValueT ValueType;
typedef ValueType value_type;
typedef typename std::vector<ValueType *>::difference_type difference_type;
typedef const value_type & reference;
typedef const value_type * pointer;
typedef std::random_access_iterator_tag iterator_category;
ConstRefVectorConstIterator()
{
}
ConstRefVectorConstIterator(const typename std::vector<ValueType *> * vec, unsigned int position)
{
vector_ = (typename std::vector<ValueType *> *)vec;
position_ = position;
}
ConstRefVectorConstIterator(typename std::vector<ValueType *> * vec, unsigned int position)
{
vector_ = vec;
position_ = position;
}
ConstRefVectorConstIterator(const ConstRefVectorConstIterator & it)
{
vector_ = it.vector_;
position_ = it.position_;
}
~ConstRefVectorConstIterator()
{}
ConstRefVectorConstIterator & operator=(const ConstRefVectorConstIterator & rhs)
{
if (this == &rhs) return *this;
vector_ = rhs.vector_;
position_ = rhs.position_;
return *this;
}
bool operator<(const ConstRefVectorConstIterator & it) const
{
return position_ < it.position_;
}
bool operator>(const ConstRefVectorConstIterator & it) const
{
return position_ > it.position_;
}
bool operator<=(const ConstRefVectorConstIterator & it) const
{
return position_ < it.position_ || position_ == it.position_;
}
bool operator>=(const ConstRefVectorConstIterator & it) const
{
return position_ > it.position_ || position_ == it.position_;
}
bool operator==(const ConstRefVectorConstIterator & it) const
{
return position_ == it.position_ && vector_ == it.vector_;
}
bool operator!=(const ConstRefVectorConstIterator & it) const
{
return position_ != it.position_ || vector_ != it.vector_;
}
ConstRefVectorConstIterator & operator++()
{
position_ += 1;
return *this;
}
ConstRefVectorConstIterator operator++(int)
{
ConstRefVectorConstIterator tmp(*this);
++(*this);
return tmp;
}
ConstRefVectorConstIterator & operator--()
{
position_ -= 1;
return *this;
}
ConstRefVectorConstIterator operator--(int)
{
ConstRefVectorConstIterator tmp(*this);
--(*this);
return tmp;
}
ConstRefVectorConstIterator operator-(difference_type n) const
{
ConstRefVectorConstIterator tmp(*this);
tmp.position_ -= n;
return tmp;
}
ConstRefVectorConstIterator operator+(difference_type n) const
{
ConstRefVectorConstIterator tmp(*this);
tmp.position_ += n;
return tmp;
}
ConstRefVectorConstIterator & operator+=(difference_type n)
{
position_ += n;
return *this;
}
ConstRefVectorConstIterator & operator-=(difference_type n)
{
position_ -= n;
return *this;
}
friend difference_type operator-(const ConstRefVectorConstIterator & i1, const ConstRefVectorConstIterator & i2)
{
return i1.position_ - i2.position_;
}
friend ConstRefVectorConstIterator operator+(difference_type n, const ConstRefVectorConstIterator & i)
{
ConstRefVectorConstIterator tmp(i);
tmp.position_ += n;
return tmp;
}
reference operator*()
{
return *((*vector_)[position_]);
}
pointer operator->()
{
return (*vector_)[position_];
}
pointer operator->() const
{
return (*vector_)[position_];
}
protected:
typename std::vector<ValueType *> * vector_;
unsigned int position_;
};
/// Mutable iterator for the ConstRefVector
template <class ValueT>
class ConstRefVectorIterator :
public ConstRefVectorConstIterator<ValueT>
{
friend class ConstRefVector;
public:
typedef ValueT ValueType;
typedef typename ConstRefVectorConstIterator<ValueType>::value_type & reference;
typedef typename ConstRefVectorConstIterator<ValueType>::value_type * pointer;
using ConstRefVectorConstIterator<ValueType>::vector_;
using ConstRefVectorConstIterator<ValueType>::position_;
ConstRefVectorIterator()
{
}
ConstRefVectorIterator(typename std::vector<ValueType *> * vec, unsigned int position) :
ConstRefVectorConstIterator<ValueType>(vec, position)
{
}
ConstRefVectorIterator(const ConstRefVectorIterator<ValueType> & it) :
ConstRefVectorConstIterator<ValueType>(it)
{
}
~ConstRefVectorIterator()
{
}
reference operator*()
{
return *((*vector_)[position_]);
}
pointer operator->()
{
return (*vector_)[position_];
}
pointer operator->() const
{
return (*vector_)[position_];
}
ConstRefVectorIterator & operator++()
{
ConstRefVectorConstIterator<ValueType>::position_ += 1;
return *this;
}
ConstRefVectorIterator operator++(int)
{
ConstRefVectorIterator tmp(*this);
++(*this);
return tmp;
}
ConstRefVectorIterator & operator--()
{
ConstRefVectorConstIterator<ValueType>::position_ -= 1;
return *this;
}
ConstRefVectorIterator operator--(int)
{
ConstRefVectorIterator tmp(*this);
--(*this);
return tmp;
}
ConstRefVectorIterator operator-(typename ConstRefVectorIterator::difference_type n) const
{
ConstRefVectorIterator tmp(*this);
tmp.position_ -= n;
return tmp;
}
ConstRefVectorIterator operator+(typename ConstRefVectorIterator::difference_type n) const
{
ConstRefVectorIterator tmp(*this);
tmp.position_ += n;
return tmp;
}
friend ConstRefVectorIterator operator+(typename ConstRefVectorIterator::difference_type n, const ConstRefVectorIterator & i)
{
ConstRefVectorIterator tmp(i);
tmp.position_ += n;
return tmp;
}
ConstRefVectorIterator & operator+=(typename ConstRefVectorIterator::difference_type n)
{
ConstRefVectorConstIterator<ValueType>::position_ += n;
return *this;
}
ConstRefVectorIterator & operator-=(typename ConstRefVectorIterator::difference_type n)
{
ConstRefVectorConstIterator<ValueType>::position_ -= n;
return *this;
}
friend void swap(ConstRefVectorIterator & i1, ConstRefVectorIterator & i2)
{
unsigned int tmp = i1.position_;
i1.position_ = i2.position_;
i2.position_ = tmp;
}
};
///Type definitions
//@{
/// Wrapped container type
typedef ContainerT ContainerType;
///
typedef typename ContainerType::value_type ValueType;
typedef ConstRefVectorIterator<const ValueType> Iterator;
typedef ConstRefVectorConstIterator<const ValueType> ConstIterator;
typedef std::reverse_iterator<Iterator> ReverseIterator;
typedef std::reverse_iterator<ConstIterator> ConstReverseIterator;
//@}
///STL-compliance type definitions
//@{
typedef ValueType value_type;
typedef typename ContainerType::size_type size_type;
typedef typename ContainerType::difference_type difference_type;
typedef typename ContainerType::reference reference;
typedef typename ContainerType::const_reference const_reference;
typedef typename ContainerType::pointer pointer;
typedef Iterator iterator;
typedef ConstIterator const_iterator;
typedef ReverseIterator reverse_iterator;
typedef ConstReverseIterator const_reverse_iterator;
//@}
/// See std::vector documentation.
void push_back(const ValueType & x)
{
const ValueType * element = &x;
vector_.push_back(element);
}
/// See std::vector documentation.
void pop_back()
{
vector_.pop_back();
}
/// See std::vector documentation.
size_type size() const
{
return vector_.size();
}
/// See std::vector documentation.
size_type capacity() const
{
return std::max(vector_.size(), capacity_);
}
/// See std::vector documentation.
void reserve(size_type n)
{
size_type cap = capacity();
if (n > cap)
{
vector_.reserve(n);
capacity_ = n;
}
}
/// See std::vector documentation.
size_type max_size() const
{
return vector_.max_size();
}
/// See std::vector documentation.
Iterator begin()
{
return Iterator((std::vector<const ValueType *> *) & vector_, (unsigned int)0);
}
/// See std::vector documentation.
Iterator end()
{
return Iterator((std::vector<const ValueType *> *) & vector_, (unsigned int)(vector_.size()));
}
/// See std::vector documentation.
ConstIterator begin() const
{
return ConstIterator((const std::vector<const ValueType *> *) & vector_, (unsigned int)0);
}
/// See std::vector documentation.
ConstIterator end() const
{
return ConstIterator((const std::vector<const ValueType *> *) & vector_, (unsigned int)(vector_.size()));
}
/// See std::vector documentation.
ReverseIterator rbegin()
{
return ReverseIterator(end());
}
/// See std::vector documentation.
ReverseIterator rend()
{
return ReverseIterator(begin());
}
/// See std::vector documentation.
ConstReverseIterator rbegin() const
{
return ConstReverseIterator(end());
}
/// See std::vector documentation.
ConstReverseIterator rend() const
{
return ConstReverseIterator(begin());
}
/// See std::vector documentation.
void resize(size_type new_size)
{
vector_.resize(new_size);
capacity_ = vector_.capacity();
}
/// See std::vector documentation.
void resize(size_type new_size, const ValueType & t)
{
vector_.resize(new_size, &t);
capacity_ = vector_.capacity();
}
/// See std::vector documentation.
const_reference front() const
{
return *(begin());
}
/// See std::vector documentation.
const_reference back() const
{
return *(end() - 1);
}
/// See std::vector documentation.
void clear()
{
vector_.clear();
}
/// See std::vector documentation.
bool empty() const
{
return vector_.empty();
}
/// See std::vector documentation.
const_reference operator[](size_type n) const
{
return *(vector_[n]);
}
/// See std::vector documentation.
bool operator==(const ConstRefVector & array) const
{
if (base_container_ptr_ != array.base_container_ptr_)
{
return false;
}
if (size() != array.size())
{
return false;
}
for (Size i = 0; i < size(); i++)
{
if (typeid(*(vector_[i])) != typeid(*(array.vector_[i])))
{
return false;
}
if (vector_[i]->operator!=(* array.vector_[i]))
{
return false;
}
}
return true;
}
/// See std::vector documentation.
bool operator!=(const ConstRefVector & array) const
{
return !(operator==(array));
}
/// Comparison of container sizes
bool operator<(const ConstRefVector & array) const
{
return size() < array.size();
}
/// Comparison of container sizes
bool operator>(const ConstRefVector & array) const
{
return size() > array.size();
}
/// Comparison of container sizes
bool operator<=(const ConstRefVector & array) const
{
return operator<(array) || operator==(array);
}
/// Comparison of container sizes
bool operator>=(const ConstRefVector & array) const
{
return operator>(array) || operator==(array);
}
/// See std::vector documentation.
void swap(ConstRefVector & array)
{
vector_.swap(array.vector_);
}
/// See std::vector documentation.
friend void swap(ConstRefVector & a1, ConstRefVector & a2)
{
a1.vector_.swap(a2.vector_);
}
/// See std::vector documentation.
Iterator insert(Iterator pos, const ValueType & element)
{
const ValueType * pointer = &element;
vector_.insert(vector_.begin() + pos.position_, pointer);
return pos;
}
/// See std::vector documentation.
void insert(Iterator pos, size_type n, const ValueType & element)
{
const ValueType * pointer;
std::vector<const ValueType *> tmp;
for (size_type i = 0; i < n; i++)
{
pointer = &element;
tmp.push_back(pointer);
}
vector_.insert(vector_.begin() + pos.position_, tmp.begin(), tmp.end());
}
/// See std::vector documentation.
template <class InputIterator>
void insert(Iterator pos, InputIterator f, InputIterator l)
{
const ValueType * pointer;
std::vector<const ValueType *> tmp;
for (InputIterator it = f; it != l; ++it)
{
pointer = &(*it);
tmp.push_back(pointer);
}
vector_.insert(vector_.begin() + pos.position_, tmp.begin(), tmp.end());
}
/// See std::vector documentation.
Iterator erase(Iterator pos)
{
vector_.erase(vector_.begin() + pos.position_);
return pos;
}
/// See std::vector documentation.
Iterator erase(Iterator first, Iterator last)
{
vector_.erase(vector_.begin() + first.position_, vector_.begin() + last.position_);
return first;
}
///@name Constructors and Destructor
//@{
/// See std::vector documentation.
ConstRefVector() :
capacity_(0),
base_container_ptr_(0)
{
}
/// See std::vector documentation.
ConstRefVector(size_type n) :
capacity_(0),
base_container_ptr_(0)
{
vector_ = std::vector<const ValueType *>(n);
}
/// See std::vector documentation.
ConstRefVector(size_type n, const ValueType & element) :
capacity_(0),
base_container_ptr_(0)
{
vector_ = std::vector<const ValueType *>(n, &element);
}
/// See std::vector documentation.
ConstRefVector(const ConstRefVector & p) :
capacity_(0),
base_container_ptr_(p.base_container_ptr_)
{
const ValueType * element;
for (ConstIterator it = p.begin(); it != p.end(); ++it)
{
element = &(*it);
vector_.push_back(element);
}
}
/// See std::vector documentation.
template <class InputIterator>
ConstRefVector(InputIterator f, InputIterator l) :
capacity_(0),
base_container_ptr_(0)
{
const ValueType * pointer;
for (InputIterator it = f; it != l; ++it)
{
pointer = &(*it);
vector_.push_back(pointer);
}
}
/// See std::vector documentation.
ConstRefVector(ContainerType & p) :
capacity_(0),
base_container_ptr_(&p)
{
const ValueType * element;
for (typename ContainerType::iterator it = p.begin(); it != p.end(); ++it)
{
element = &(*it);
vector_.push_back(element);
}
}
/// See std::vector documentation.
~ConstRefVector()
{
}
//@}
/// See std::vector documentation.
ConstRefVector & operator=(const ConstRefVector & rhs)
{
if (this == &rhs) return *this;
base_container_ptr_ = rhs.base_container_ptr_;
clear();
reserve(rhs.size());
const ValueType * element;
for (ConstIterator it = rhs.begin(); it != rhs.end(); ++it)
{
element = &(*it);
vector_.push_back(element);
}
return *this;
}
/// See std::vector documentation.
template <class InputIterator>
void assign(InputIterator f, InputIterator l)
{
clear();
insert(end(), f, l);
}
/// See std::vector documentation.
void assign(size_type n, const ValueType & x)
{
clear();
insert(end(), n, x);
}
/**
@brief Sorting.
These simplified sorting methods are supported for convenience.
@note To use these methods, the ValueType must provide an interface similar to Peak1D or Peak2D.
*/
//@{
/// Sorts the elements by intensity
void sortByIntensity(bool reverse = false)
{
if (reverse)
{
std::sort(vector_.begin(), vector_.end(), reverseComparator(pointerComparator(typename ValueType::IntensityLess())));
}
else
{
std::sort(vector_.begin(), vector_.end(), pointerComparator(typename ValueType::IntensityLess()));
}
}
/// Lexicographically sorts the elements by their position.
void sortByPosition()
{
std::sort(vector_.begin(), vector_.end(), pointerComparator(typename ValueType::PositionLess()));
}
//@}
/**
@name Generic sorting function templates.
Any element comparator can be
given as template argument. You can also give the comparator as an
argument to the function template (this is useful if the comparator is
not default constructed, but keep in mind that STL copies comparators
a lot).
<p> Thus your can e.g. write <code>elements.sortByComparator <
DFeature<1>::IntensityLess > ()</code>, if elements have type
<code>ConstRefVector < 1, DFeature <1> ></code>.
*/
//@{
template <typename ComparatorType>
void sortByComparator(ComparatorType const & comparator = ComparatorType())
{
std::sort(vector_.begin(), vector_.end(), pointerComparator(comparator));
}
//@}
//----------------------------------------------------------------------
protected:
///the internal vector of ValueType pointers
std::vector<const ValueType *> vector_;
///the current capacity
size_type capacity_;
/// Pointer to the base container
const ContainerType * base_container_ptr_;
};
} // namespace OpenMS
#endif // OPENMS_DATASTRUCTURES_CONSTREFVECTOR_H
|