Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-17 23:57:19

0001 #include <vector>
0002 #include <iostream>
0003 #include <cassert>
0004 using namespace std;
0005 
0006 template <class T>
0007 BXVector<T>::BXVector(unsigned size,  // number of objects per BX
0008                       int bxFirst,    // first BX stored
0009                       int bxLast)     // last BX stored
0010     : bxFirst_(std::min(0, bxFirst)), bxLast_(std::max(0, bxLast)), data_(std::vector<T>(size * numBX())) {
0011   assert(bxFirst_ <= bxLast_);
0012   itrs_.clear();
0013   for (unsigned i = 0; i < numBX(); i++) {
0014     itrs_.push_back(i * size);
0015   }
0016 }
0017 
0018 // set BX range, adds an empty bx so you must push_pack to see anything
0019 template <class T>
0020 void BXVector<T>::setBXRange(int bxFirst, int bxLast) {
0021   int bxF = bxFirst_;
0022   int bxL = bxLast_;
0023   if (bxFirst < bxFirst_) {
0024     for (int i = 0; i < bxF - bxFirst; i++) {
0025       itrs_.insert(itrs_.begin(), itrs_[0]);
0026     }
0027   }
0028   if (bxFirst > bxFirst_) {
0029     for (int i = 0; i < bxFirst - bxF; i++) {
0030       deleteBX(bxF + i);
0031     }
0032   }
0033   if (bxLast > bxLast_) {
0034     for (int i = 0; i < bxLast - bxL; i++) {
0035       addBX();
0036     }
0037   }
0038   if (bxLast < bxLast_) {
0039     for (int i = 0; i < bxL - bxLast; i++) {
0040       deleteBX(bxL - i);
0041     }
0042   }
0043   bxFirst_ = bxFirst;
0044   bxLast_ = bxLast;
0045 }
0046 
0047 // add a BX, empty bx
0048 template <class T>
0049 void BXVector<T>::addBX() {
0050   itrs_.push_back(data_.size());
0051   bxLast_ = bxLast_ + 1;
0052 }
0053 
0054 // delete a BX
0055 template <class T>
0056 void BXVector<T>::deleteBX(int bx) {
0057   clearBX(bx);
0058   itrs_.erase(itrs_.begin() + indexFromBX(bx));
0059   if (bx == getFirstBX()) {
0060     bxFirst_ = bxFirst_ + 1;
0061   }
0062   if (bx == getLastBX()) {
0063     bxLast_ = bxLast_ - 1;
0064   }
0065 }
0066 // set size for a given BX
0067 template <class T>
0068 void BXVector<T>::resize(int bx, unsigned max) {
0069   T k;
0070   unsigned s = size(bx);
0071   if (size(bx) < max) {
0072     for (unsigned i = 0; i < max - s; i++) {
0073       push_back(bx, k);
0074     }
0075   } else {
0076     for (unsigned i = 0; i < s - max; i++) {
0077       erase(bx, s - i - 1);
0078     }
0079   }
0080 }
0081 
0082 // set size for all BXs
0083 template <class T>
0084 void BXVector<T>::resizeAll(unsigned size) {
0085   for (unsigned i = 0; i < itrs_.size(); ++i) {
0086     resize(i, size);
0087   }
0088 }
0089 
0090 // iterator access by BX
0091 template <class T>
0092 typename BXVector<T>::const_iterator BXVector<T>::begin(int bx) const {
0093   assert(!itrs_.empty());
0094   return data_.begin() + itrs_[indexFromBX(bx)];
0095 }
0096 
0097 // iterator access by BX
0098 template <class T>
0099 typename BXVector<T>::const_iterator BXVector<T>::end(int bx) const {
0100   return data_.begin() + itrs_[indexFromBX(bx)] + size(bx);
0101 }
0102 
0103 // get the first BX stored
0104 template <class T>
0105 int BXVector<T>::getFirstBX() const {
0106   return bxFirst_;
0107 }
0108 
0109 // get the last BX stored
0110 template <class T>
0111 int BXVector<T>::getLastBX() const {
0112   return bxLast_;
0113 }
0114 
0115 // get N objects for a given BX
0116 template <class T>
0117 unsigned BXVector<T>::size(int bx) const {
0118   if (isEmpty(bx)) {
0119     return 0;
0120   }
0121 
0122   if (indexFromBX(bx) == (itrs_.size() - 1)) {
0123     return (data_.size() - itrs_[itrs_.size() - 1]);
0124   } else {
0125     return (itrs_[indexFromBX(bx + 1)] - itrs_[indexFromBX(bx)]);
0126   }
0127 }
0128 
0129 // add element with given BX index
0130 template <class T>
0131 void BXVector<T>::push_back(int bx, T object) {
0132   data_.insert(data_.begin() + itrs_[indexFromBX(bx)] + size(bx), object);
0133   for (unsigned k = 0; k < itrs_.size(); k++) {
0134     if (k > indexFromBX(bx)) {
0135       itrs_[k]++;
0136     }
0137   }
0138 }
0139 
0140 // clear entire BXVector
0141 template <class T>
0142 void BXVector<T>::clear() {
0143   data_.clear();
0144   for (unsigned k = 0; k < itrs_.size(); k++) {
0145     itrs_[k] = 0;
0146   }
0147 }
0148 
0149 // insert data in location i of bx
0150 template <class T>
0151 void BXVector<T>::insert(int bx, unsigned i, T object) {
0152   if (i > size(bx)) {
0153     //cout<<"ERROR: bx "<<bx<<" is only "<<size(bx)<<" long"<<endl;
0154   } else {
0155     data_.insert(data_.begin() + itrs_[indexFromBX(bx)] + i, object);
0156 
0157     for (unsigned k = 0; k < itrs_.size(); k++) {
0158       if (k > indexFromBX(bx)) {
0159         itrs_[k]++;
0160       }
0161     }
0162   }
0163 }
0164 
0165 // erase data in location i of bx
0166 template <class T>
0167 void BXVector<T>::erase(int bx, unsigned i) {
0168   if (i >= size(bx)) {
0169     //cout<<"ERROR: bx "<<bx<<" is only "<<size(bx)<<" long"<<endl;
0170   } else {
0171     data_.erase(data_.begin() + itrs_[indexFromBX(bx)] + i);
0172     for (unsigned k = 0; k < itrs_.size(); k++) {
0173       if (k > indexFromBX(bx)) {
0174         itrs_[k]--;
0175       }
0176     }
0177   }
0178 }
0179 
0180 // clear information for given bx but keeps record of the empty BX
0181 template <class T>
0182 void BXVector<T>::clearBX(int bx) {
0183   unsigned si = size(bx);
0184   for (unsigned i = 0; i < si; i++) {
0185     erase(bx, 0);
0186   }
0187 }
0188 
0189 // access data at a given location i in bunch crossing bx
0190 template <class T>
0191 const T& BXVector<T>::at(int bx, unsigned i) const {
0192   return data_.at(itrs_[indexFromBX(bx)] + i);
0193 }
0194 
0195 // set data at a given location i in bunch crossing bx
0196 template <class T>
0197 void BXVector<T>::set(int bx, unsigned i, const T& object) {
0198   if (i >= size(bx)) {
0199     //cout<<"ERROR: bx "<<bx<<" is only "<<size(bx)<<" long"<<endl;
0200   } else {
0201     data_.at(itrs_[indexFromBX(bx)] + i) = object;
0202   }
0203 }
0204 
0205 // redefine bunch crossing starting from 0
0206 template <class T>
0207 unsigned BXVector<T>::indexFromBX(int bx) const {
0208   return bx - bxFirst_;
0209 }
0210 
0211 // check to see if bx is empty
0212 template <class T>
0213 bool BXVector<T>::isEmpty(int bx) const {
0214   if (itrs_[indexFromBX(bx)] == data_.size()) {
0215     return true;
0216   }
0217 
0218   if (indexFromBX(bx + 1) >= itrs_.size()) {
0219     return false;
0220   }
0221 
0222   if (itrs_[indexFromBX(bx)] == itrs_[indexFromBX(bx + 1)]) {
0223     return true;
0224   }
0225 
0226   return false;
0227 }
0228 
0229 // edm::View support
0230 template <class T>
0231 inline void BXVector<T>::fillView(edm::ProductID const& id,
0232                                   std::vector<void const*>& pointers,
0233                                   edm::FillViewHelperVector& helpers) const {
0234   edm::detail::reallyFillView(*this, id, pointers, helpers);
0235 }
0236 //
0237 namespace edm {
0238   template <class T>
0239   inline void fillView(BXVector<T> const& obj,
0240                        edm::ProductID const& id,
0241                        std::vector<void const*>& pointers,
0242                        edm::FillViewHelperVector& helpers) {
0243     obj.fillView(id, pointers, helpers);
0244   }
0245   template <class T>
0246   struct has_fillView<BXVector<T> > {
0247     static bool const value = true;
0248   };
0249 }  // namespace edm
0250 
0251 // edm::Ptr support
0252 template <class T>
0253 inline void BXVector<T>::setPtr(std::type_info const& toType, unsigned long index, void const*& ptr) const {
0254   edm::detail::reallySetPtr<BXVector<T> >(*this, toType, index, ptr);
0255 }
0256 template <class T>
0257 inline void BXVector<T>::fillPtrVector(std::type_info const& toType,
0258                                        std::vector<unsigned long> const& indices,
0259                                        std::vector<void const*>& ptrs) const {
0260   edm::detail::reallyfillPtrVector(*this, toType, indices, ptrs);
0261 }
0262 //
0263 template <class T>
0264 inline void setPtr(BXVector<T> const& obj, std::type_info const& toType, unsigned long index, void const*& ptr) {
0265   obj.setPtr(toType, index, ptr);
0266 }
0267 template <class T>
0268 inline void fillPtrVector(BXVector<T> const& obj,
0269                           std::type_info const& toType,
0270                           std::vector<unsigned long> const& indices,
0271                           std::vector<void const*>& ptrs) {
0272   obj.fillPtrVector(toType, indices, ptrs);
0273 }
0274 namespace edm {
0275   template <class T>
0276   struct has_setPtr<BXVector<T> > {
0277     static bool const value = true;
0278   };
0279 }  // namespace edm
0280 
0281 // Local Variables:
0282 // mode: c++
0283 // End: