SortedCollection

StrictWeakOrdering

has_fillView

has_setPtr

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
#ifndef DataFormats_Common_SortedCollection_h
#define DataFormats_Common_SortedCollection_h

/*----------------------------------------------------------------------

SortedCollection: A collection of homogeneous objects that can be used
for an EDProduct. SortedCollection is *not* designed for use as a base
class (it has no virtual functions).

SortedCollection is in some ways similar to a sequence
(e.g. std::vector and std::list), and in other ways is similar to an
associative collection (e.g. std::map and std::set). SortedCollection
provides keyed access (via operator[]() and find()) to its contained
values. In normal usage, the values contained in a SortedCollection
are sorted according to the order imposed by the ordering of the keys.

CAVEATS:

1. The user must make sure that two VALUES with the same KEY are not
never inserted into the sequence. The SortedCollection does not
prevent this, nor does it detect this. However, 'find' will be
unreliable if such duplicate entries are made.

**************** Much more is needed here! ****************

----------------------------------------------------------------------*/

#include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
#include "DataFormats/Common/interface/fillPtrVector.h"
#include "DataFormats/Common/interface/FillView.h"
#include "DataFormats/Common/interface/setPtr.h"
#include "DataFormats/Common/interface/traits.h"
#include "DataFormats/Provenance/interface/ProductID.h"
#include "FWCore/Utilities/interface/EDMException.h"

#include <algorithm>
#include <typeinfo>
#include <vector>

namespace edm {

  //------------------------------------------------------------
  // Forward declarations
  //

  template <typename T>
  struct StrictWeakOrdering;
  template <typename T, typename SORT = StrictWeakOrdering<T> >
  class SortedCollection;

  template <typename T, typename SORT>
  struct has_fillView<edm::SortedCollection<T, SORT> > {
    static bool const value = true;
  };

  template <typename T, typename SORT>
  struct has_setPtr<edm::SortedCollection<T, SORT> > {
    static bool const value = true;
  };

  template <typename T>
  struct StrictWeakOrdering {
    typedef typename T::key_type key_type;

    // Each of the following comparisons are needed (at least with GCC's library).
    bool operator()(key_type a, T const& b) const { return a < b.id(); }
    bool operator()(T const& a, key_type b) const { return a.id() < b; }
    bool operator()(T const& a, T const& b) const { return a.id() < b.id(); }

    // This final comparison is not needed (at least with GCC's library).
    // bool operator()(key_type a, key_type b) const { return a < b; }
  };

  template <typename T, typename SORT>
  class SortedCollection {
  public:
    typedef T value_type;      // the values we contain
    typedef SORT key_compare;  // function object for sorting

    typedef typename std::vector<T>::const_iterator const_iterator;
    typedef typename std::vector<T>::iterator iterator;
    typedef typename std::vector<T>::const_reference const_reference;
    typedef typename std::vector<T>::reference reference;

    typedef typename std::vector<T>::size_type size_type;

    // This needs to be turned into a template parameter, perhaps with
    // a default --- if there is a way to slip in the default without
    // growing any dependence on the code supplying the key!
    typedef typename key_compare::key_type key_type;

    SortedCollection();
    explicit SortedCollection(size_type n);
    explicit SortedCollection(std::vector<T> const& vec);
    SortedCollection(SortedCollection const& h);

    // Add the following when needed
    //template<typename InputIterator>
    //SortedCollection(InputIterator b, InputIterator e);

    void push_back(T const& t);
#if defined(__GXX_EXPERIMENTAL_CXX0X__)
    void push_back(T&& t) { obj.push_back(t); }

    template <typename... Args>
    void emplace_back(Args&&... args) {
      obj.emplace_back(args...);
    }
#endif
    void pop_back() { obj.pop_back(); }

    void swap(SortedCollection& other);

    void swap_contents(std::vector<T>& other);

    SortedCollection& operator=(SortedCollection const& rhs);

    bool empty() const;
    size_type size() const;
    size_type capacity() const;
    void reserve(size_type n);

    // Return a reference to the i'th item in the collection.
    // Note that the argument is an *integer*, not an object of
    //   type key_type
    reference operator[](size_type i);
    const_reference operator[](size_type i) const;

    // Find the item with key matching k. If no such item is found,
    // return end();
    iterator find(key_type k);
    const_iterator find(key_type k) const;

    const_iterator begin() const;
    const_iterator end() const;

    iterator begin();
    iterator end();

    const_reference front() const;
    reference front();
    const_reference back() const;
    reference back();

    // Sort the elements of the vector, in the order determined by the
    // keys. Note that the Event will make sure to call this function
    // after the SortedCollection has been put into the Event, so
    // there is no need to call it in user code (unless one needs the
    // collection sorted *before* it is inserted into the Event).
    void sort();

    // This function will be called by the edm::Event after the
    // SortedCollection has been inserted into the Event.
    void post_insert();

    void fillView(ProductID const& id, std::vector<void const*>& pointers, FillViewHelperVector& helpers) const;

    void setPtr(std::type_info const& toType, unsigned long index, void const*& ptr) const;

    void fillPtrVector(std::type_info const& toType,
                       std::vector<unsigned long> const& indices,
                       std::vector<void const*>& ptrs) const;

    //Used by ROOT storage
    CMS_CLASS_VERSION(10)

  private:
    typedef std::vector<T> collection_type;
    typedef typename collection_type::const_iterator const_inner_iterator;
    typedef typename collection_type::iterator inner_iterator;

    collection_type obj;
  };

  template <typename T, typename SORT>
  inline SortedCollection<T, SORT>::SortedCollection() : obj() {}

  template <typename T, typename SORT>
  inline SortedCollection<T, SORT>::SortedCollection(size_type n) : obj(n) {}

  template <typename T, typename SORT>
  inline SortedCollection<T, SORT>::SortedCollection(std::vector<T> const& vec) : obj(vec) {}

  template <typename T, typename SORT>
  inline SortedCollection<T, SORT>::SortedCollection(SortedCollection<T, SORT> const& h) : obj(h.obj) {}

  template <typename T, typename SORT>
  inline void SortedCollection<T, SORT>::push_back(T const& t) {
    obj.push_back(t);
  }

  template <typename T, typename SORT>
  inline void SortedCollection<T, SORT>::swap(SortedCollection<T, SORT>& other) {
    obj.swap(other.obj);
  }

  template <typename T, typename SORT>
  inline void SortedCollection<T, SORT>::swap_contents(std::vector<T>& other) {
    obj.swap(other);
  }

  template <typename T, typename SORT>
  inline SortedCollection<T, SORT>& SortedCollection<T, SORT>::operator=(SortedCollection<T, SORT> const& rhs) {
    SortedCollection<T, SORT> temp(rhs);
    this->swap(temp);
    return *this;
  }

  template <typename T, typename SORT>
  inline bool SortedCollection<T, SORT>::empty() const {
    return obj.empty();
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::size_type SortedCollection<T, SORT>::size() const {
    return obj.size();
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::size_type SortedCollection<T, SORT>::capacity() const {
    return obj.capacity();
  }

  template <typename T, typename SORT>
  inline void SortedCollection<T, SORT>::reserve(typename SortedCollection<T, SORT>::size_type n) {
    obj.reserve(n);
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::reference SortedCollection<T, SORT>::operator[](size_type i) {
    return obj[i];
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::const_reference SortedCollection<T, SORT>::operator[](size_type i) const {
    return obj[i];
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::iterator SortedCollection<T, SORT>::find(key_type key) {
    // This fails if the SortedCollection has not been sorted. It is
    // up to the user (with the help of the Event) to make sure this
    // has been done.
    key_compare comp;
    inner_iterator last = obj.end();
    inner_iterator loc = std::lower_bound(obj.begin(), last, key, comp);
    return loc == last || comp(key, *loc) ? last : loc;
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::const_iterator SortedCollection<T, SORT>::find(key_type key) const {
    // This fails if the SortedCollection has not been sorted. It is
    // up to the user (with the help of the Event) to make sure this
    // has been done.
    key_compare comp;
    const_inner_iterator last = obj.end();
    const_inner_iterator loc = std::lower_bound(obj.begin(), last, key, comp);
    return loc == last || comp(key, *loc) ? last : loc;
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::const_iterator SortedCollection<T, SORT>::begin() const {
    return obj.begin();
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::const_iterator SortedCollection<T, SORT>::end() const {
    return obj.end();
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::iterator SortedCollection<T, SORT>::begin() {
    return obj.begin();
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::iterator SortedCollection<T, SORT>::end() {
    return obj.end();
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::const_reference SortedCollection<T, SORT>::front() const {
    return obj.front();
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::reference SortedCollection<T, SORT>::front() {
    return obj.front();
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::const_reference SortedCollection<T, SORT>::back() const {
    return obj.back();
  }

  template <typename T, typename SORT>
  inline typename SortedCollection<T, SORT>::reference SortedCollection<T, SORT>::back() {
    return obj.back();
  }

  template <typename T, typename SORT>
  inline void SortedCollection<T, SORT>::sort() {
    key_compare comp;
    std::sort(obj.begin(), obj.end(), comp);
  }

  template <typename T, typename SORT>
  inline void SortedCollection<T, SORT>::post_insert() {
    // After insertion, we make sure our contents are sorted.
    sort();
  }

  template <typename T, typename SORT>
  inline void SortedCollection<T, SORT>::fillView(ProductID const& id,
                                                  std::vector<void const*>& pointers,
                                                  FillViewHelperVector& helpers) const {
    detail::reallyFillView(*this, id, pointers, helpers);
  }

  template <typename T, typename SORT>
  inline void SortedCollection<T, SORT>::setPtr(std::type_info const& toType,
                                                unsigned long index,
                                                void const*& ptr) const {
    detail::reallySetPtr(*this, toType, index, ptr);
  }

  template <typename T, typename SORT>
  inline void SortedCollection<T, SORT>::fillPtrVector(std::type_info const& toType,
                                                       std::vector<unsigned long> const& indices,
                                                       std::vector<void const*>& ptrs) const {
    detail::reallyfillPtrVector(*this, toType, indices, ptrs);
  }

  // Free swap function
  template <typename T, typename SORT>
  inline void swap(SortedCollection<T, SORT>& a, SortedCollection<T, SORT>& b) {
    a.swap(b);
  }

  //----------------------------------------------------------------------
  //
  // Free function templates to support comparisons.

  // The two function templates below are not written as a single
  // function template in order to avoid inadvertent matches with
  // inappropriate template arguments. Template template parameters
  // were avoided to stay away from generic programming problems
  // sometimes encountered in the presence of such parameters.  If
  // support for comparison between SortedCollections and other sorts
  // of collections is needed, it can be added.

  // comparison with vector tests to see whether the entries in the
  // SortedCollection are the same as the entries in the vector, *and
  // in the same order.
  // operator==(T const& a, T const& b) is used to compare the elements in
  // the collections.

  template <typename T, typename SORT, typename ALLOC>
  inline bool operator==(SortedCollection<T, SORT> const& c, std::vector<T, ALLOC> const& v) {
    return c.size() == v.size() && std::equal(v.begin(), v.end(), c.begin());
  }

  // comparison of two SortedCollections is done by comparing the
  // collected elements, in order for equality.
  // operator==(T const& a, T const& b) is used to compare the elements.

  template <typename T, typename SORT>
  inline bool operator==(SortedCollection<T, SORT> const& a, SortedCollection<T, SORT> const& b) {
    return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
  }

  //----------------------------------------------------------------------
  //
  // Free function template to support creation of Views.

  template <typename T, typename SORT>
  inline void fillView(SortedCollection<T, SORT> const& obj,
                       ProductID const& id,
                       std::vector<void const*>& pointers,
                       FillViewHelperVector& helpers) {
    obj.fillView(id, pointers, helpers);
  }

  // Free function templates to support the use of edm::Ptr.
  template <typename T, typename SORT>
  inline void setPtr(SortedCollection<T, SORT> const& obj,
                     std::type_info const& toType,
                     unsigned long index,
                     void const*& ptr) {
    obj.setPtr(toType, index, ptr);
  }

  template <typename T, typename SORT>
  inline void fillPtrVector(SortedCollection<T, SORT> const& obj,
                            std::type_info const& toType,
                            std::vector<unsigned long> const& indices,
                            std::vector<void const*>& ptrs) {
    obj.fillPtrVector(toType, indices, ptrs);
  }
}  // namespace edm

#endif