1 #ifndef PandaTree_Framework_Collection_h 2 #define PandaTree_Framework_Collection_h 4 #include "CollectionBase.h" 19 class Collection :
public E::base_type::collection_type {
22 typedef typename E::base_type::collection_type base_type;
24 typedef typename value_type::datastore data_type;
25 typedef value_type& reference;
26 typedef value_type
const& const_reference;
27 typedef value_type* pointer;
28 typedef value_type
const* const_pointer;
32 Collection(
char const* name =
"", UInt_t initialMax = 64) : base_type(name,
sizeof(E), kFALSE) { allocate_(initialMax); }
33 Collection(self_type
const& src) : base_type(src.ContainerBase::name_,
sizeof(E), kFALSE) { allocate_(src.data.nmax());
copy(src); }
35 self_type& operator=(self_type
const& rhs) {
copy(rhs);
return *
this; }
38 reference
at(UInt_t idx);
40 const_reference
at(UInt_t idx)
const;
44 const_reference
operator[](UInt_t idx)
const {
return *const_addr_(idx); }
46 iterator
begin() {
return iterator(addr_(), ContainerBase::unitSize_); }
48 const_iterator
begin()
const {
return const_iterator(const_addr_(), ContainerBase::unitSize_); }
50 iterator
end() {
return iterator(addr_(CollectionBase::size()), ContainerBase::unitSize_); }
52 const_iterator
end()
const {
return const_iterator(const_addr_(CollectionBase::size()), ContainerBase::unitSize_); }
54 reference
front() {
return *addr_(); }
56 const_reference
front()
const {
return *const_addr_(); }
58 reference
back() {
return *addr_(CollectionBase::size() - 1); }
60 const_reference
back()
const {
return *const_addr_(CollectionBase::size() - 1); }
65 void copy(self_type
const&);
81 Element& elemAt(UInt_t idx)
override {
return at(idx); }
82 Element const& elemAt(UInt_t idx)
const override {
return at(idx); }
84 std::vector<UInt_t> sort(ContainerBase::Comparison
const&)
override;
86 void print(std::ostream& = std::cout, UInt_t level = 1)
const override;
87 void dump(std::ostream& = std::cout)
const override;
92 Collection(
char const* name, UInt_t unitSize, Bool_t dummy) : base_type(name, unitSize, kFALSE) {}
94 void reallocate_(UInt_t)
override;
97 value_type* addr_(
unsigned idx = 0);
98 value_type
const* const_addr_(
unsigned idx = 0)
const;
100 template<
class T = E>
101 typename std::enable_if<std::is_constructible<T>::value>::type allocate_(UInt_t);
102 template<
class T = E>
103 typename std::enable_if<!std::is_constructible<T>::value>::type allocate_(UInt_t);
105 template<
class T = E>
106 typename std::enable_if<std::is_constructible<T>::value>::type
107 deallocate_(value_type* addr, data_type&);
108 template<
class T = E>
109 typename std::enable_if<!std::is_constructible<T>::value>::type
110 deallocate_(value_type* addr, data_type&);
116 if (ContainerBase::array_) {
117 deallocate_(addr_(), data);
120 ContainerBase::array_ = 0;
125 typename Collection<E>::reference
128 if (_idx >= CollectionBase::size())
129 throw std::out_of_range((ContainerBase::name_ +
"::at").Data());
135 typename Collection<E>::const_reference
138 if (_idx >= CollectionBase::size())
139 throw std::out_of_range((ContainerBase::name_ +
"::at").Data());
141 return *const_addr_(_idx);
148 if (_src.ContainerBase::unitSize_ != ContainerBase::unitSize_)
149 throw std::runtime_error((ContainerBase::name_ +
"::copy incompatible array").Data());
153 for (UInt_t iP(0); iP != CollectionBase::size(); ++iP)
154 (*
this)[iP] = _src[iP];
168 typename panda::Collection<E>::reference
182 auto indexComp([
this, &_c](UInt_t i1, UInt_t i2)->Bool_t {
183 return _c((*
this)[i1], (*
this)[i2]);
186 std::vector<UInt_t> sortedIndices(CollectionBase::size());
187 for (UInt_t iP(0); iP != CollectionBase::size(); ++iP)
188 sortedIndices[iP] = iP;
190 std::sort(sortedIndices.begin(), sortedIndices.end(), indexComp);
193 for (UInt_t iP(0); iP != CollectionBase::size(); ++iP)
194 (*
this)[iP] = tmpCollection[sortedIndices[iP]];
196 return sortedIndices;
203 _out << E::typeName() <<
"Collection" << std::endl;
211 _out << E::typeName() <<
"Collection" << std::endl;
220 if (_nmax <= data.nmax())
225 data_type tmpStore(data);
226 value_type* tmpArray(reinterpret_cast<value_type*>(ContainerBase::array_));
233 for (UInt_t iP(0); iP != CollectionBase::size(); ++iP)
234 (*
this)[iP] = tmpArray[iP];
237 deallocate_(tmpArray, tmpStore);
242 typename Collection<E>::value_type*
248 return reinterpret_cast<value_type*
>(ContainerBase::array_ + _idx * ContainerBase::unitSize_);
253 typename Collection<E>::value_type
const*
259 return reinterpret_cast<value_type const*
>(ContainerBase::array_ + _idx * ContainerBase::unitSize_);
265 typename std::enable_if<std::is_constructible<T>::value>::type
268 data.allocate(_nmax);
270 ContainerBase::array_ =
reinterpret_cast<Char_t*
>(std::allocator<value_type>().allocate(data.nmax()));
273 for (UInt_t iP(0); iP != data.nmax(); ++iP, ++p)
280 typename std::enable_if<!std::is_constructible<T>::value>::type
283 throw std::runtime_error((
"Cannot create a Collection of pure-virtual elements (" + ContainerBase::name_ +
")").Data());
289 typename std::enable_if<std::is_constructible<T>::value>::type
294 for (UInt_t iP(0); iP != _data.nmax(); ++iP, ++p)
297 std::allocator<value_type>().deallocate(_addr, _data.nmax());
304 typename std::enable_if<!std::is_constructible<T>::value>::type
307 throw std::runtime_error((
"Cannot deallocate pure-virtual elements (" + ContainerBase::name_ +
")").Data());
reference create_back()
Create an element at the end of the collection and return a reference.
Definition: Collection.h:169
reference operator[](UInt_t idx)
Element accessor with no range check.
Definition: Collection.h:42
const_iterator begin() const
Return an iterator pointing to the first element.
Definition: Collection.h:48
reference at(UInt_t idx)
Element accessor with range check.
Definition: Collection.h:126
iterator begin()
Return an iterator pointing to the first element.
Definition: Collection.h:46
reference front()
Reference to the first element.
Definition: Collection.h:54
void push_back(const_reference)
Append an element to the back and resize by 1.
Definition: Collection.h:161
void setName(char const *name) final
Set object name.
Definition: ContainerBase.h:26
void dump(std::ostream &=std::cout) const override
Dump the object content.
Definition: CollectionBase.cc:171
const_reference back() const
Reference to the last element.
Definition: Collection.h:60
const_reference front() const
Reference to the first element.
Definition: Collection.h:56
void copy(self_type const &)
Copy the array contents.
Definition: Collection.h:146
Template class for dynamic-size container implementations. Inherits from base_type::collection_type o...
Definition: Collection.h:19
const_reference operator[](UInt_t idx) const
Element accessor with no range check.
Definition: Collection.h:44
reference back()
Reference to the last element.
Definition: Collection.h:58
iterator end()
Return an iterator pointing to the end of the array (invalid address)
Definition: Collection.h:50
void print(std::ostream &=std::cout, UInt_t level=1) const override
Print the object content.
Definition: CollectionBase.cc:164
Base class for elements of containers.
Definition: Element.h:32
Iterator class for containers.
Definition: Iterator.h:11
void resize(UInt_t size)
Resize the container.
Definition: CollectionBase.cc:192
const_iterator end() const
Return an iterator pointing to the end of the array (invalid address)
Definition: Collection.h:52