CMS 3D CMS Logo

SortedCollection.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_SortedCollection_h
2 #define DataFormats_Common_SortedCollection_h
3 
4 /*----------------------------------------------------------------------
5 
6 SortedCollection: A collection of homogeneous objects that can be used
7 for an EDProduct. SortedCollection is *not* designed for use as a base
8 class (it has no virtual functions).
9 
10 SortedCollection is in some ways similar to a sequence
11 (e.g. std::vector and std::list), and in other ways is similar to an
12 associative collection (e.g. std::map and std::set). SortedCollection
13 provides keyed access (via operator[]() and find()) to its contained
14 values. In normal usage, the values contained in a SortedCollection
15 are sorted according to the order imposed by the ordering of the keys.
16 
17 CAVEATS:
18 
19 1. The user must make sure that two VALUES with the same KEY are not
20 never inserted into the sequence. The SortedCollection does not
21 prevent this, nor does it detect this. However, 'find' will be
22 unreliable if such duplicate entries are made.
23 
24 **************** Much more is needed here! ****************
25 
26 ----------------------------------------------------------------------*/
27 
35 
37 
38 #include <algorithm>
39 #include <typeinfo>
40 #include <vector>
41 
42 namespace edm {
43 
44  //------------------------------------------------------------
45  // Forward declarations
46  //
47 
48  template<typename T> struct StrictWeakOrdering;
49  template<typename T, typename SORT = StrictWeakOrdering<T> >
51 
52  template<typename T, typename SORT>
53  struct has_fillView<edm::SortedCollection<T, SORT> > {
54  static bool const value = true;
55  };
56 
57  template<typename T, typename SORT>
58  struct has_setPtr<edm::SortedCollection<T, SORT> > {
59  static bool const value = true;
60  };
61 
62  template<typename T>
63  struct StrictWeakOrdering {
64  typedef typename T::key_type key_type;
65 
66  // Each of the following comparisons are needed (at least with GCC's library).
67  bool operator()(key_type a, T const& b) const { return a < b.id(); }
68  bool operator()(T const& a, key_type b) const { return a.id() < b; }
69  bool operator()(T const& a, T const& b) const { return a.id() < b.id(); }
70 
71  // This final comparison is not needed (at least with GCC's library).
72  // bool operator()(key_type a, key_type b) const { return a < b; }
73  };
74 
75 
76  template<typename T, typename SORT>
77  class SortedCollection {
78  public:
79  typedef T value_type; // the values we contain
80  typedef SORT key_compare; // function object for sorting
81 
82  typedef typename std::vector<T>::const_iterator const_iterator;
83  typedef typename std::vector<T>::iterator iterator;
84  typedef typename std::vector<T>::const_reference const_reference;
86 
88 
89  // This needs to be turned into a template parameter, perhaps with
90  // a default --- if there is a way to slip in the default without
91  // growing any dependence on the code supplying the key!
92  typedef typename key_compare::key_type key_type;
93 
95  explicit SortedCollection(size_type n);
96  explicit SortedCollection(std::vector<T> const& vec);
98 
99  // Add the following when needed
100  //template<typename InputIterator>
101  //SortedCollection(InputIterator b, InputIterator e);
102 
103  void push_back(T const& t);
104 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
105  void push_back(T && t) { obj.push_back(t);}
106 
107  template<typename... Args >
108  void emplace_back( Args&&... args ) { obj.emplace_back(args...);}
109 #endif
110  void pop_back() { obj.pop_back(); }
111 
112  void swap(SortedCollection& other);
113 
114  void swap_contents(std::vector<T>& other);
115 
116  SortedCollection& operator=(SortedCollection const& rhs);
117 
118  bool empty() const;
119  size_type size() const;
120  size_type capacity() const;
121  void reserve(size_type n);
122 
123  // Return a reference to the i'th item in the collection.
124  // Note that the argument is an *integer*, not an object of
125  // type key_type
126  reference operator[](size_type i);
127  const_reference operator[](size_type i) const;
128 
129  // Find the item with key matching k. If no such item is found,
130  // return end();
131  iterator find(key_type k);
132  const_iterator find(key_type k) const;
133 
134  const_iterator begin() const;
135  const_iterator end() const;
136 
137  iterator begin();
138  iterator end();
139 
140  const_reference front() const;
141  reference front();
142  const_reference back() const;
143  reference back();
144 
145  // Sort the elements of the vector, in the order determined by the
146  // keys. Note that the Event will make sure to call this function
147  // after the SortedCollection has been put into the Event, so
148  // there is no need to call it in user code (unless one needs the
149  // collection sorted *before* it is inserted into the Event).
150  void sort();
151 
152  // This function will be called by the edm::Event after the
153  // SortedCollection has been inserted into the Event.
154  void post_insert();
155 
156  void fillView(ProductID const& id,
157  std::vector<void const*>& pointers,
159 
160  void setPtr(std::type_info const& toType,
161  unsigned long index,
162  void const*& ptr) const;
163 
164  void fillPtrVector(std::type_info const& toType,
165  std::vector<unsigned long> const& indices,
166  std::vector<void const*>& ptrs) const;
167 
168  //Used by ROOT storage
170 
171  private:
172 
173  typedef std::vector<T> collection_type;
174  typedef typename collection_type::const_iterator const_inner_iterator;
175  typedef typename collection_type::iterator inner_iterator;
176 
177  collection_type obj;
178  };
179 
180  template<typename T, typename SORT>
181  inline
182  SortedCollection<T, SORT>::SortedCollection() : obj() {}
183 
184  template<typename T, typename SORT>
185  inline
187 
188  template<typename T, typename SORT>
189  inline
190  SortedCollection<T, SORT>::SortedCollection(std::vector<T> const& vec) : obj(vec) {}
191 
192  template<typename T, typename SORT>
193  inline
195 
196  template<typename T, typename SORT>
197  inline
198  void
200  obj.push_back(t);
201  }
202 
203  template<typename T, typename SORT>
204  inline
205  void
207  obj.swap(other.obj);
208  }
209 
210  template<typename T, typename SORT>
211  inline
212  void
214  obj.swap(other);
215  }
216 
217  template<typename T, typename SORT>
218  inline
222  this->swap(temp);
223  return *this;
224  }
225 
226  template<typename T, typename SORT>
227  inline
228  bool
230  return obj.empty();
231  }
232 
233  template<typename T, typename SORT>
234  inline
237  return obj.size();
238  }
239 
240  template<typename T, typename SORT>
241  inline
244  return obj.capacity();
245  }
246 
247  template<typename T, typename SORT>
248  inline
249  void
251  obj.reserve(n);
252  }
253 
254  template<typename T, typename SORT>
255  inline
258  return obj[i];
259  }
260 
261  template<typename T, typename SORT>
262  inline
265  return obj[i];
266  }
267 
268  template<typename T, typename SORT>
269  inline
272  // This fails if the SortedCollection has not been sorted. It is
273  // up to the user (with the help of the Event) to make sure this
274  // has been done.
275  key_compare comp;
276  inner_iterator last = obj.end();
278  last,
279  key,
280  comp);
281  return loc == last || comp(key, *loc) ? last : loc;
282  }
283 
284  template<typename T, typename SORT>
285  inline
288  // This fails if the SortedCollection has not been sorted. It is
289  // up to the user (with the help of the Event) to make sure this
290  // has been done.
291  key_compare comp;
292  const_inner_iterator last = obj.end();
294  last,
295  key,
296  comp);
297  return loc == last || comp(key, *loc) ? last : loc;
298  }
299 
300  template<typename T, typename SORT>
301  inline
304  return obj.begin();
305  }
306 
307  template<typename T, typename SORT>
308  inline
311  return obj.end();
312  }
313 
314  template<typename T, typename SORT>
315  inline
318  return obj.begin();
319  }
320 
321  template<typename T, typename SORT>
322  inline
325  return obj.end();
326  }
327 
328  template<typename T, typename SORT>
329  inline
332  return obj.front();
333  }
334 
335  template<typename T, typename SORT>
336  inline
339  return obj.front();
340  }
341 
342  template<typename T, typename SORT>
343  inline
346  return obj.back();
347  }
348 
349  template<typename T, typename SORT>
350  inline
353  return obj.back();
354  }
355 
356  template<typename T, typename SORT>
357  inline
358  void
360  key_compare comp;
361  std::sort(obj.begin(), obj.end(), comp);
362  }
363 
364  template<typename T, typename SORT>
365  inline
366  void
368  // After insertion, we make sure our contents are sorted.
369  sort();
370  }
371 
372  template<typename T, typename SORT>
373  inline
374  void
376  std::vector<void const*>& pointers,
377  FillViewHelperVector& helpers) const {
378  detail::reallyFillView(*this, id, pointers, helpers);
379  }
380 
381  template<typename T, typename SORT>
382  inline
383  void
384  SortedCollection<T, SORT>::setPtr(std::type_info const& toType,
385  unsigned long index,
386  void const*& ptr) const {
387  detail::reallySetPtr(*this, toType, index, ptr);
388  }
389 
390  template<typename T, typename SORT>
391  inline
392  void
393  SortedCollection<T, SORT>::fillPtrVector(std::type_info const& toType,
394  std::vector<unsigned long> const& indices,
395  std::vector<void const*>& ptrs) const {
396  detail::reallyfillPtrVector(*this, toType, indices, ptrs);
397  }
398 
399  // Free swap function
400  template<typename T, typename SORT>
401  inline
402  void
404  a.swap(b);
405  }
406 
407  //----------------------------------------------------------------------
408  //
409  // Free function templates to support comparisons.
410 
411  // The two function templates below are not written as a single
412  // function template in order to avoid inadvertent matches with
413  // inappropriate template arguments. Template template parameters
414  // were avoided to stay away from generic programming problems
415  // sometimes encountered in the presence of such parameters. If
416  // support for comparison between SortedCollections and other sorts
417  // of collections is needed, it can be added.
418 
419  // comparison with vector tests to see whether the entries in the
420  // SortedCollection are the same as the entries in the vector, *and
421  // in the same order.
422  // operator==(T const& a, T const& b) is used to compare the elements in
423  // the collections.
424 
425  template<typename T, typename SORT, typename ALLOC>
426  inline
427  bool
429  std::vector<T, ALLOC> const& v) {
430  return c.size() == v.size() && std::equal(v.begin(), v.end(), c.begin());
431  }
432 
433  // comparison of two SortedCollections is done by comparing the
434  // collected elements, in order for equality.
435  // operator==(T const& a, T const& b) is used to compare the elements.
436 
437  template<typename T, typename SORT>
438  inline
439  bool
441  SortedCollection<T, SORT> const& b) {
442  return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
443  }
444 
445  //----------------------------------------------------------------------
446  //
447  // Free function template to support creation of Views.
448 
449  template<typename T, typename SORT>
450  inline
451  void
453  ProductID const& id,
454  std::vector<void const*>& pointers,
455  FillViewHelperVector& helpers) {
456  obj.fillView(id, pointers, helpers);
457  }
458 
459  // Free function templates to support the use of edm::Ptr.
460  template<typename T, typename SORT>
461  inline
462  void
464  std::type_info const& toType,
465  unsigned long index,
466  void const*& ptr) {
467  obj.setPtr(toType, index, ptr);
468  }
469 
470  template<typename T, typename SORT>
471  inline
472  void
474  std::type_info const& toType,
475  std::vector<unsigned long> const& indices,
476  std::vector<void const*>& ptrs) {
477  obj.fillPtrVector(toType, indices, ptrs);
478  }
479 }
480 
481 #endif
size
Write out results.
collection_type::iterator inner_iterator
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
bool operator()(T const &a, key_type b) const
void fillPtrVector(std::vector< T, A > const &obj, std::type_info const &iToType, std::vector< unsigned long > const &iIndicies, std::vector< void const * > &oPtr)
Definition: fillPtrVector.h:85
void reallyFillView(COLLECTION const &coll, ProductID const &id, std::vector< void const * > &ptrs, FillViewHelperVector &helpers)
Definition: FillView.h:27
std::vector< T >::const_iterator const_iterator
void swap(SortedCollection &other)
std::vector< EcalRecHit > collection_type
void push_back(T const &t)
#define CMS_CLASS_VERSION(_version_)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
uint16_t size_type
void fillView(ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers) const
std::vector< T >::size_type size_type
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
bool equal(const T &first, const T &second)
Definition: Equal.h:34
void setPtr(std::vector< T, A > const &obj, std::type_info const &iToType, unsigned long iIndex, void const *&oPtr)
Definition: setPtr.h:72
bool operator()(key_type a, T const &b) const
void reallyfillPtrVector(COLLECTION const &coll, std::type_info const &iToType, std::vector< unsigned long > const &iIndicies, std::vector< void const * > &oPtr)
Definition: fillPtrVector.h:38
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &) noexcept
void swap_contents(std::vector< T > &other)
std::vector< T >::const_reference const_reference
key_compare::key_type key_type
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers)
bool operator()(T const &a, T const &b) const
T operator[](int i) const
const_reference front() const
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
size_type capacity() const
#define end
Definition: vmac.h:39
Definition: value.py:1
void reallySetPtr(COLLECTION const &coll, std::type_info const &iToType, unsigned long iIndex, void const *&oPtr)
Definition: setPtr.h:37
reference operator[](size_type i)
std::vector< T >::iterator iterator
int k[5][pyjets_maxn]
const_iterator end() const
std::vector< T >::reference reference
double b
Definition: hdecay.h:120
iterator find(key_type k)
#define begin
Definition: vmac.h:32
HLT enums.
size_type size() const
double a
Definition: hdecay.h:121
void setPtr(std::type_info const &toType, unsigned long index, void const *&ptr) const
void reserve(size_type n)
void fillPtrVector(std::type_info const &toType, std::vector< unsigned long > const &indices, std::vector< void const * > &ptrs) const
collection_type::const_iterator const_inner_iterator
long double T
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
const_iterator begin() const
SortedCollection & operator=(SortedCollection const &rhs)
const_reference back() const