1 #ifndef PandaTree_Framework_RefVector_h 2 #define PandaTree_Framework_RefVector_h 4 #include "ContainerBase.h" 21 typedef E element_type;
24 typedef std::vector<Short_t> Indices;
35 RefVector(self_type
const& orig) : container_(orig.container_), indices_(orig.indices_) {}
45 UShort_t
size()
const;
47 ref_type
at(UShort_t);
49 const_ref_type
at(UShort_t)
const;
51 element_type
const&
objAt(UShort_t)
const;
59 void addRef(element_type
const*);
66 void clear() {
if (indices_) indices_->clear(); }
73 Bool_t
isValid()
const {
return container_ && *container_ && indices_; }
88 void sort(ContainerBase::Comparison
const&);
91 template<Bool_t is_const>
94 typedef typename std::conditional<is_const, Short_t const, Short_t>::type index_type;
95 typedef typename std::conditional<is_const, const_ref_type, ref_type>::type value_type;
98 value_type
const& operator*()
const {
return ref_; }
99 value_type
const* operator->()
const {
return &ref_; }
105 template<Bool_t is_const>
111 typedef typename std::conditional<is_const, const_ref_type, ref_type>::type value_type;
113 typedef typename std::conditional<is_const, Indices::const_iterator, Indices::iterator>::type internal_type;
114 typedef int difference_type;
117 RefVectorIterator(self_type
const& it) : container_(it.container_), itr_(it.itr_) {}
119 bool operator==(self_type
const& rhs)
const {
return itr_ == rhs.itr_; }
120 bool operator!=(self_type
const& rhs)
const {
return itr_ != rhs.itr_; }
122 value_type operator*()
const {
return value_type(*container_, *itr_); }
123 ptr_type operator->()
const {
return ptr_type(*container_, *itr_); }
125 self_type& operator++() { ++itr_;
return *
this; }
126 self_type operator++(
int) {
auto copy(*
this); ++itr_;
return copy; }
127 self_type& operator--() { --itr_;
return *
this; }
128 self_type operator--(
int) {
auto copy(*
this); --itr_;
return copy; }
130 self_type& operator+=(
int n) { itr_ += n;
return *
this; }
131 self_type& operator-=(
int n) { itr_ -= n;
return *
this; }
133 self_type operator+(
int n)
const {
auto copy(*
this);
return (copy += n); }
134 self_type operator-(
int n)
const {
auto copy(*
this);
return (copy -= n); }
136 int operator-(self_type
const& rhs)
const {
137 return this->operator*().operator->() - (*rhs).operator->();
140 self_type& operator[](
int n) {
auto copy(*
this);
return (copy += n); }
141 bool operator<(self_type
const& rhs)
const {
return this->operator-(rhs) < 0; }
142 bool operator>(self_type
const& rhs)
const {
return this->operator-(rhs) > 0; }
143 bool operator<=(self_type
const& rhs)
const {
return !(this->operator>(rhs)); }
144 bool operator>=(self_type
const& rhs)
const {
return !(this->operator<(rhs)); }
150 internal_type itr_{};
156 const_iterator begin()
const {
return const_iterator(*container_, indices_->begin()); }
157 iterator begin() {
return iterator(*container_, indices_->begin()); }
158 const_iterator end()
const {
return const_iterator(*container_, indices_->end()); }
159 iterator end() {
return iterator(*container_, indices_->end()); }
163 Indices* indices_{0};
171 auto indexComp([
this, &_c](
short i1,
short i2)->Bool_t {
172 return _c(static_cast<Element const&>((*container_)->elemAt(i1)),
173 static_cast<Element const&>((*container_)->elemAt(i2)));
176 std::sort(indices_->begin(), indices_->end(), indexComp);
185 return indices_->size();
192 if (!container_ || !indices_)
193 throw std::runtime_error(
"at() called on an invalid RefVector");
202 if (!container_ || !indices_)
203 throw std::runtime_error(
"at() called on an invalid RefVector");
212 if (!container_ || !(*container_) || !indices_)
213 throw std::runtime_error(
"at() called on an invalid RefVector");
215 return static_cast<element_type const&
>((*container_)->elemAt(indices_->at(_i)));
222 container_ = _rhs.container_;
225 indices_->assign(_rhs.indices_->begin(), _rhs.indices_->end());
229 else if (_rhs.indices_)
230 throw std::runtime_error(
"Cannot copy a valid RefVector to an invalid RefVector");
242 if (!container_ || !(*container_) || !indices_)
243 throw std::runtime_error(
"Cannot push to an invalid RefVector");
245 for (UShort_t idx(0); idx != (*container_)->size(); ++idx) {
246 if (&(*container_)->elemAt(idx) == _obj) {
247 indices_->push_back(Short_t(idx));
252 throw std::runtime_error(
"Pushing an object not in referenced collection");
259 if (!container_ || !indices_)
260 throw std::runtime_error(
"Cannot push to an invalid RefVector");
263 throw std::runtime_error(
"Pushing a ref of a wrong object");
265 indices_->push_back(_ref.
idx());
269 std::vector<Short_t>*&
273 throw std::runtime_error(
"Invalid indices ref");
283 throw std::runtime_error(
"Invalid container ref");
291 std::ostream& operator<<(std::ostream& _out, panda::RefVector<E>
const& _ref)
293 _out <<
"RefVector<" << E::typeName() <<
">";
295 _out <<
" " << _ref.container()->getName();
void init()
Initializer.
Definition: RefVector.h:71
RefVector()
Default constructor.
Definition: RefVector.h:27
A reference to an element in a container.
Definition: Ref.h:20
void push_back(ref_type const &)
Setter function.
Definition: RefVector.h:257
void setContainer(ContainerBase const *&c)
Set the container (an Array or a Collection).
Definition: RefVector.h:43
ContainerBase const * container() const
Accessor to container.
Definition: RefVector.h:280
element_type const & objAt(UShort_t) const
Object accessor.
Definition: RefVector.h:210
void addRef(element_type const *)
Setter function.
Definition: RefVector.h:237
"pointer" wrapper for Ref
Definition: RefVector.h:92
self_type & operator=(self_type const &)
Assignment operator.
Definition: RefVector.h:220
Indices *& indices()
Accessor to indices.
Definition: RefVector.h:270
RefVector(self_type const &orig)
Copy constructor.
Definition: RefVector.h:35
const iterator
Definition: RefVector.h:106
ContainerBase const * container() const
Accessor to container.
Definition: Ref.h:142
void sort(ContainerBase::Comparison const &)
Sort the ref vector.
Definition: RefVector.h:168
index_type & idx()
Accessor to idx.
Definition: Ref.h:120
UShort_t size() const
Size of the vector.
Definition: RefVector.h:181
void clear()
Clear operation.
Definition: RefVector.h:66
void setIndices(Indices &indices)
Set the index.
Definition: RefVector.h:37
Base class for all containers.
Definition: ContainerBase.h:18
Bool_t isValid() const
Validity check. Both container and idx must be valid, and idx must not be 0xffffffff.
Definition: RefVector.h:73
A const version of Ref.
Definition: Ref.h:164
RefVector(ContainerBase const *&c, Indices &indices)
Standard constructor.
Definition: RefVector.h:33
Definition: MicroJet.h:12
A vector of references to elements in a container.
Definition: RefVector.h:18
ref_type at(UShort_t)
Element (ref) accessor.
Definition: RefVector.h:190