ATLAS Offline Software
ResolvedCollection.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef DCMATH_RESOLVEDCOLLECTION_H
6 #define DCMATH_RESOLVEDCOLLECTION_H
7 
8 #include <iostream>
9 #include <vector>
10 
11 namespace TrkDriftCircleMath {
12 
13  template <class Data, class IsSubset> class ResolvedCollection {
14  public:
15  typedef std::vector<Data> DataVec;
16  enum SubsetState { SubSet = 0, SuperSet = 1, Different = 2 };
17 
18  public:
19  const DataVec& data() const { return m_data; }
20  DataVec& data() { return m_data; }
21 
22  void clear() { m_data.clear(); }
23 
24  void set(DataVec& data) { m_data.swap(data); }
25 
26  bool insert(const Data& data) {
27  // add data if collection is empty
28  if (m_data.empty()) {
29  m_data.push_back(data);
30  return true;
31  }
32 
33  IsSubset isSubset;
34 
35  bool inserted(false);
36 
37  // loop over data vector compare new element with existing ones
38  for (unsigned int i = 0; i < m_data.size(); ++i) {
39  // get subset state
40  int state = isSubset(data, m_data[i]);
41 
42 
43  // do nothing in case the new element is a subset of an already inserted element
44  if (state == SubSet) {
45  return false;
46 
47  // if the new element is a super set of an existing on replace it
48  // check all existing elements and keep track of number of replaces
49  } else if (state == SuperSet) {
50  // if not inserted yet replace the subset
51  if (!inserted) {
52  inserted = true;
53  m_data[i] = data;
54  } else {
55  // replace current element with last and restart loop
56  if (i == m_data.size() - 1) {
57  m_data.pop_back();
58  } else {
59  m_data[i] = m_data.back();
60  m_data.pop_back();
61  --i; // here we go back to also check the copied element
62  }
63  }
64  }
65  }
66 
67  // check if new element was SuperSet of existing one
68  if (!inserted) {
69  // insert new element
70  m_data.push_back(data);
71  }
72  return true;
73  }
74 
75  private:
76  std::vector<Data> m_data{};
77  };
78 
79 } // namespace TrkDriftCircleMath
80 
81 #endif
Data
@ Data
Definition: BaseObject.h:11
TrkDriftCircleMath::ResolvedCollection::clear
void clear()
Definition: ResolvedCollection.h:22
TrkDriftCircleMath
Function object to check whether two Segments are sub/super sets or different.
Definition: IMdtSegmentFinder.h:13
TrkDriftCircleMath::ResolvedCollection::SuperSet
@ SuperSet
Definition: ResolvedCollection.h:16
TrkDriftCircleMath::ResolvedCollection::set
void set(DataVec &data)
Definition: ResolvedCollection.h:24
TrkDriftCircleMath::ResolvedCollection::data
DataVec & data()
Definition: ResolvedCollection.h:20
TrkDriftCircleMath::ResolvedCollection::DataVec
std::vector< Data > DataVec
Definition: ResolvedCollection.h:15
TrkDriftCircleMath::ResolvedCollection::SubSet
@ SubSet
Definition: ResolvedCollection.h:16
lumiFormat.i
int i
Definition: lumiFormat.py:92
TrkDriftCircleMath::ResolvedCollection::SubsetState
SubsetState
Definition: ResolvedCollection.h:16
TrkDriftCircleMath::ResolvedCollection::m_data
std::vector< Data > m_data
Definition: ResolvedCollection.h:76
TrkDriftCircleMath::ResolvedCollection::insert
bool insert(const Data &data)
Definition: ResolvedCollection.h:26
TrkDriftCircleMath::ResolvedCollection::Different
@ Different
Definition: ResolvedCollection.h:16
TrkDriftCircleMath::ResolvedCollection
Definition: ResolvedCollection.h:13
TrkDriftCircleMath::ResolvedCollection::data
const DataVec & data() const
Definition: ResolvedCollection.h:19