1 #ifndef DataFormats_Common_DetSetVectorNew_h 2 #define DataFormats_Common_DetSetVectorNew_h 8 #include <boost/iterator/transform_iterator.hpp> 9 #include <boost/any.hpp> 13 #if !defined(__ROOTCLING__) 14 #define DSVN_USE_ATOMIC 54 namespace dstvdetails {
74 #ifdef DSVN_USE_ATOMIC 85 assert(rh.m_filling ==
false);
87 #ifdef DSVN_USE_ATOMIC 88 m_dataSize.store(rh.m_dataSize.exchange(m_dataSize.load()));
95 assert(m_filling ==
false);
96 assert(rh.m_filling ==
false);
98 #ifdef DSVN_USE_ATOMIC 99 m_dataSize.store(rh.m_dataSize.exchange(m_dataSize.load()));
107 #ifdef DSVN_USE_ATOMIC 110 mutable size_type m_dataSize;
115 assert(m_filling ==
false);
119 #ifdef DSVN_USE_ATOMIC 120 m_dataSize.store(rh.
m_dataSize.exchange(m_dataSize.load()));
145 #ifdef DSVN_USE_ATOMIC 149 return offset.compare_exchange_strong(expected, -2);
156 bool uninitialized()
const {
return (-1) ==
offset; }
160 operator id_type()
const {
return id; }
163 #ifdef DSVN_USE_ATOMIC 165 bool expected =
false;
166 if (!m_filling.compare_exchange_strong(expected,
true))
171 bool ready()
const {
return true; }
188 template <
typename T>
205 typedef typename IdContainer::iterator
IdIter;
206 typedef typename std::vector<data_type>::iterator
DataIter;
221 m_detset.
set(*m_v, item, m_update);
232 typedef std::pair<const_iterator, const_iterator>
Range;
250 : m_v(iv), m_item(m_v.ready() ? m_v.push_back(id) :
dummy()), m_saveEmpty(isaveEmpty) {
256 : m_v(iv), m_item(it), m_saveEmpty(isaveEmpty) {
260 m_item.offset =
int(m_v.m_data.size());
263 if (!m_saveEmpty && m_item.size == 0) {
264 m_v.pop_back(m_item.id);
266 assert(m_v.m_filling ==
true);
267 m_v.m_filling =
false;
271 m_v.pop_back(m_item.id);
276 if (m_v.onDemand() && m_v.m_data.size() == m_v.m_data.capacity())
281 if (m_v.onDemand() && m_v.m_data.size() + s > m_v.m_data.capacity())
286 if (m_item.offset + s <= m_v.m_data.capacity())
290 m_v.m_data.reserve(m_item.offset + s);
294 checkCapacityExausted(s);
295 m_v.m_data.resize(m_item.offset + s);
296 m_v.m_dataSize = m_v.m_data.size();
300 id_type
id()
const {
return m_item.id; }
301 size_type
size()
const {
return m_item.size; }
302 bool empty()
const {
return m_item.size == 0; }
304 data_type&
operator[](size_type
i) {
return m_v.m_data[m_item.offset +
i]; }
305 DataIter
begin() {
return m_v.m_data.begin() + m_item.offset; }
309 checkCapacityExausted();
310 m_v.m_data.push_back(d);
315 checkCapacityExausted();
321 data_type&
back() {
return m_v.m_data.back(); }
325 friend class ::TestDetSet;
341 #ifdef DSVN_USE_ATOMIC 350 assert(m_v.m_filling ==
true);
351 m_v.m_filling =
false;
356 bool expected =
false;
357 while (!m_v.m_filling.compare_exchange_weak(expected,
true)) {
359 nanosleep(
nullptr,
nullptr);
361 int offset = m_v.m_data.size();
362 if (m_v.onDemand() &&
full()) {
363 m_v.m_filling =
false;
366 std::move(m_lv.begin(), m_lv.end(), std::back_inserter(m_v.m_data));
367 m_item.size = m_lv.size();
370 m_v.m_dataSize = m_v.m_data.size();
371 assert(m_v.m_filling ==
true);
372 m_v.m_filling =
false;
378 int offset = m_v.m_dataSize;
379 return m_v.m_data.capacity() < offset + m_lv.size();
388 id_type
id()
const {
return m_item.id; }
389 size_type
size()
const {
return m_lv.size(); }
390 bool empty()
const {
return m_lv.empty(); }
393 DataIter
begin() {
return m_lv.begin(); }
394 DataIter
end() {
return m_lv.end(); }
399 data_type&
back() {
return m_lv.back(); }
403 friend class ::TestDetSet;
421 #ifdef DSVN_USE_ATOMIC 423 bool expected =
false;
424 while (!iContainer.
m_filling.compare_exchange_weak(expected,
true, std::memory_order_acq_rel)) {
426 nanosleep(
nullptr,
nullptr);
457 bool onDemand()
const {
return !m_getter.empty(); }
466 void swap(IdContainer& iic, DataContainer& idc) {
472 m_ids.reserve(isize);
473 m_data.reserve(dsize);
478 m_ids.shrink_to_fit();
479 m_data.shrink_to_fit();
482 void resize(
size_t isize,
size_t dsize) {
484 m_data.resize(dsize);
485 m_dataSize = m_data.size();
489 m_ids.erase(std::remove_if(m_ids.begin(), m_ids.end(), [](Item
const&
m) {
return 0 ==
m.size; }), m_ids.end());
493 DetSet
insert(id_type iid, data_type
const* idata, size_type isize) {
494 Item&
item = addItem(iid, isize);
495 m_data.resize(m_data.size() + isize);
497 m_dataSize = m_data.size();
498 return DetSet(*
this, item,
false);
501 DetSet
insert(id_type iid, size_type isize) {
502 Item&
item = addItem(iid, isize);
503 m_data.resize(m_data.size() + isize);
504 m_dataSize = m_data.size();
505 return DetSet(*
this, item,
false);
509 Item&
push_back(id_type iid) {
return addItem(iid, 0); }
513 const_IdIter
p = findItem(iid);
514 if (p == m_ids.end())
517 if ((*p).isValid() && (*p).size > 0 && m_data.size() == (*p).offset + (*p).size) {
518 m_data.resize((*p).offset);
519 m_dataSize = m_data.size();
521 m_ids.erase(m_ids.begin() + (p - m_ids.begin()));
526 Item it(iid,
size_type(m_data.size()), isize);
527 IdIter
p = std::lower_bound(m_ids.begin(), m_ids.end(), it);
528 if (p != m_ids.end() && !(it < *
p))
536 bool exists(id_type
i)
const {
return findItem(i) != m_ids.end(); }
539 const_IdIter
p = findItem(i);
540 return p != m_ids.end() && (*p).isValid();
552 const_IdIter
p = findItem(i);
553 if (p == m_ids.end())
555 return DetSet(*
this, *p,
true);
561 const_IdIter
p = findItem(i);
562 return (p == m_ids.end()) ?
end() : boost::make_transform_iterator(p,
IterHelp(*
this,
update));
567 std::pair<const_IdIter, const_IdIter>
p = std::equal_range(m_ids.begin(), m_ids.end(), Item(i));
568 return (p.first != p.second) ? p.first : m_ids.end();
573 return boost::make_transform_iterator(m_ids.begin(),
IterHelp(*
this,
update));
578 return boost::make_transform_iterator(m_ids.end(),
IterHelp(*
this,
update));
582 template <
typename CMP>
585 std::pair<const_IdIter, const_IdIter>
p = std::equal_range(m_ids.begin(), m_ids.end(),
i,
cmp);
587 boost::make_transform_iterator(p.second,
IterHelp(*
this,
update)));
592 bool empty()
const {
return m_ids.empty(); }
596 size_type
size()
const {
return m_ids.size(); }
602 data_type
const*
data(
size_t cell)
const {
return &m_data[m_ids[cell].offset]; }
604 size_type
detsetSize(
size_t cell)
const {
return m_ids[cell].size; }
606 id_type
id(
size_t cell)
const {
return m_ids[cell].id; }
608 Item
const&
item(
size_t cell)
const {
return m_ids[cell]; }
612 IdContainer
const&
ids()
const {
return m_ids; }
613 DataContainer
const&
data()
const {
return m_data; }
620 friend class ::TestDetSet;
630 std::vector<Trans::Item> m_ids;
635 template <
typename T>
643 template <
typename T>
645 const std::vector<det_id_type>& iDets,
647 : m_subdetId(isubdet) {
650 m_ids.reserve(iDets.size());
651 det_id_type sanityCheck = 0;
652 for (std::vector<det_id_type>::const_iterator itDetId = iDets.begin(), itDetIdEnd = iDets.end();
653 itDetId != itDetIdEnd;
655 assert(sanityCheck < *itDetId &&
"vector of det_id_type was not ordered");
656 sanityCheck = *itDetId;
657 m_ids.push_back(*itDetId);
661 #ifdef DSVN_USE_ATOMIC 662 template <
typename T>
665 if (m_getter.empty()) {
673 (*boost::any_cast<std::shared_ptr<Getter> >(&m_getter))->fill(ff);
680 #ifdef DSVN_USE_ATOMIC 681 template <
typename T>
687 nanosleep(
nullptr,
nullptr);
688 m_data = &icont.
data();
697 #include <type_traits> 702 namespace refhelper {
703 template <
typename T>
711 namespace refhelper {
712 template <
typename T>
719 return &(iContainer[iIndex]);
723 template <
typename T>
733 template <
class HandleT>
736 const HandleT& iHandle,
typename HandleT::element_type::value_type::const_iterator itIter) {
737 static_assert(std::is_same<
typename HandleT::element_type,
739 "Handle and DetSetVector do not have compatible types.");
740 auto index = itIter - &iHandle->data().front();
742 iHandle.id(), &(*itIter),
index);
749 template <
typename T>
754 static size_t size(
const edmNew::DetSetVector<T>* iContainer) {
return iContainer->
dataSize(); }
755 static unsigned int indexFor(
const value_type* iElement,
const edmNew::DetSetVector<T>* iContainer) {
756 return iElement - &(iContainer->
data().front());
761 #ifdef DSVN_USE_ATOMIC 762 #undef DSVN_USE_ATOMIC
bool isValid(id_type i) const
DataContainer const & data() const
static unsigned int indexFor(const value_type *iElement, const edmNew::DetSetVector< T > *iContainer)
void swap(DetSetVectorTrans &rh)
void push_back(data_type const &d)
void update(Item const &item) const
const_iterator end(bool update=false) const
static DetSetVector< T >::Item & dummy()
size_type dataSize() const
DetSetVector< T >::id_type id_type
void reserve(size_type s)
result_type operator()(first_argument_type iContainer, second_argument_type iIndex)
Range equal_range(id_type i, CMP cmp, bool update=false) const
std::vector< data_type > DataContainer
std::pair< const_iterator, const_iterator > Range
Item & push_back(id_type iid)
const_IdIter findItem(id_type i) const
std::vector< Trans::Item > m_ids
PixelRecoRange< float > Range
bool any(const std::vector< T > &v, const T &what)
IdContainer::iterator IdIter
void checkCapacityExausted(size_type s) const
void set(Container const &icont, typename Container::Item const &item, bool update=true)
void push_back(data_type &&d)
void throwCapacityExausted()
void reserve(size_type s)
edmNew::DetSetVector< T >::FindForDetSetVector value
std::vector< data_type >::const_iterator const_DataIter
void push_back(data_type &&d)
void swap(DetSetVector &rh)
void throw_range(det_id_type iid)
Item & operator=(Item &&rh)
DetSetVector(int isubdet=0)
TSFastFiller(DetSetVector< T > const &iv, typename DetSetVector< T >::Item const &it)
void pop_back(id_type iid)
#define CMS_CLASS_VERSION(_version_)
id_type id(size_t cell) const
DetSetVectorTrans(const DetSetVectorTrans &rh)
DetSetVector< T > const & m_v
bool operator<(Item const &rh) const
void swap(Association< C > &lhs, Association< C > &rhs)
DetSetVector< T > const * m_v
dstvdetails::DetSetVectorTrans Trans
static void clean(char *s)
Item const & item(size_t cell) const
void checkCapacityExausted() const
IterHelp(DetSetVector< T > const &iv, bool iup)
Ref< typename HandleT::element_type, typename HandleT::element_type::value_type::value_type > makeRefTo(const HandleT &iHandle, det_id_type iDetID, typename HandleT::element_type::value_type::const_iterator itIter)
Item & addItem(id_type iid, size_type isize)
DetSetVector< T >::data_type value_type
static DetSetVector< T >::Item const & dummy()
DetSetVectorTrans & operator=(DetSetVectorTrans &&rh)
unsigned int second_argument_type
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
DetSetVector< T >::Item & m_item
bool exists(id_type i) const
void push_back(data_type const &d)
IdContainer const & ids() const
bool initializing() const
data_type const * data(size_t cell) const
FastFiller(DetSetVector< T > &iv, id_type id, bool isaveEmpty=false)
#define CMS_THREAD_GUARD(_var_)
FindSetForNewDetSetVector< T > value
DetSetVector< T >::size_type size_type
Item(id_type i=0, int io=-1, size_type is=0)
DetSetVector< T >::Item const & m_item
void swap(IdContainer &iic, DataContainer &idc)
DetSetVector< T >::id_type key_type
static size_t size(const edmNew::DetSetVector< T > *iContainer)
void resize(size_t isize, size_t dsize)
TSFastFiller(DetSetVector< T > &iv, id_type id)
size_type detsetSize(size_t cell) const
std::atomic< int > offset
DetSetVector()
Create an empty DetSetVector.
DetSet insert(id_type iid, data_type const *idata, size_type isize)
std::pair< IdIter, DataIter > IterPair
Namespace of DDCMS conversion namespace.
unsigned int second_argument_type
std::vector< Item > IdContainer
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
dslv::LazyGetter< T > Getter
DetSetVector< T >::id_type key_type
void errorIdExists(det_id_type iid)
result_type operator()(first_argument_type iContainer, second_argument_type iIndex)
void reserve(size_t isize, size_t dsize)
data_type & operator[](size_type i)
std::vector< data_type >::iterator DataIter
result_type & operator()(Item const &item) const
const_iterator find(id_type i, bool update=false) const
CapacityExaustedException()
Item & operator=(Item const &rh)
DetSet insert(id_type iid, size_type isize)
std::atomic< size_type > m_dataSize
std::atomic< bool > m_filling
IdContainer::const_iterator const_IdIter
data_type operator()(size_t cell, size_t frame) const
edmNew::DetSet< T > DetSet
std::pair< const_IdIter, const_DataIter > const_IterPair
FastFiller(DetSetVector< T > &iv, typename DetSetVector< T >::Item &it, bool isaveEmpty=false)
data_type & operator[](size_type i)
edm::refhelper::FindForNewDetSetVector< data_type > RefFinder
DetSetVectorTrans(DetSetVectorTrans &&rh)
DetSetVector< T >::size_type size_type
DetSetVector< T >::id_type id_type
DetSet operator[](id_type i) const
DetSetVector< T >::data_type value_type
const_iterator begin(bool update=false) const