CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AbsReference.cc
Go to the documentation of this file.
1 #include <algorithm>
2 
3 #include "Alignment/Geners/interface/AbsArchive.hh"
4 #include "Alignment/Geners/interface/AbsReference.hh"
5 #include "Alignment/Geners/interface/CatalogEntry.hh"
6 #include "Alignment/Geners/interface/IOException.hh"
7 
8 // Notes on implementation
9 //
10 // The following invariants should be preserved:
11 //
12 // 0 items: must have itemId_ == 0 && idList_.empty()
13 // 1 item: must have itemId_ != 0 && idList_.empty()
14 // more items: must have itemId_ == 0 && !idList_.empty()
15 //
16 namespace gs {
17  std::istream &AbsReference::positionInputStream(const unsigned long long id) const {
18  return archive_.inputStream(id, nullptr);
19  }
20 
21  void AbsReference::initialize() const {
22  AbsReference *modifiable = const_cast<AbsReference *>(this);
23  if (searchId_) {
24  // Make sure that the searchId_ item id is going to produce
25  // a valid item
26  std::shared_ptr<const CatalogEntry> record = archive_.catalogEntry(searchId_);
27  const unsigned long long idFound = record->id();
28 
29  // Check for valid id in the archive
30  if (idFound && this->isIOCompatible(*record)) {
31  if (idFound != searchId_)
32  throw IOInvalidData("In AbsReference::initialize: catalog is corrupted");
33  modifiable->itemId_ = searchId_;
34  }
35  } else {
36  // Search for matching items in the archive
37  archive_.search(*modifiable);
38  if (!idList_.empty()) {
39  // Check if the list is sorted.
40  // Sort if it is not.
41  const unsigned long nFound = idList_.size();
42  const unsigned long long *ids = &idList_[0];
43  bool sorted = true;
44  for (unsigned long i = 1; i < nFound; ++i)
45  if (ids[i - 1] >= ids[i]) {
46  sorted = false;
47  break;
48  }
49  if (!sorted)
50  std::sort(modifiable->idList_.begin(), modifiable->idList_.end());
51  }
52  }
53  modifiable->initialized_ = true;
54  }
55 
56  bool AbsReference::isIOCompatible(const CatalogEntry &r) const {
57  return !classId_.name().empty() && classId_.name() == r.type().name() && ioProto_ == r.ioPrototype();
58  }
59 
60  bool AbsReference::empty() const {
61  if (!initialized_)
62  initialize();
63  return itemId_ == 0 && idList_.empty();
64  }
65 
66  bool AbsReference::unique() const {
67  if (!initialized_)
68  initialize();
69  return itemId_ && idList_.empty();
70  }
71 
72  unsigned long AbsReference::size() const {
73  if (!initialized_)
74  initialize();
75  return itemId_ ? 1UL : idList_.size();
76  }
77 
78  unsigned long long AbsReference::id(const unsigned long index) const {
79  if (!initialized_)
80  initialize();
81  if (itemId_ && index == 0UL)
82  return itemId_;
83  else if (index < idList_.size())
84  return idList_[index];
85  else
86  throw gs::IOOutOfRange(
87  "In gs::AbsReference::id: "
88  "index out of range");
89  }
90 
91  std::shared_ptr<const CatalogEntry> AbsReference::indexedCatalogEntry(const unsigned long index) const {
92  return archive_.catalogEntry(id(index));
93  }
94 
95  bool AbsReference::isSameIOPrototype(const CatalogEntry &r) const { return ioProto_ == r.ioPrototype(); }
96 
97  void AbsReference::addItemId(const unsigned long long idIn) {
98  if (!idIn)
99  throw gs::IOInvalidArgument("In AbsReference::addItemId: invalid item id");
100  const unsigned long mySize = itemId_ ? 1UL : idList_.size();
101  switch (mySize) {
102  case 0UL:
103  itemId_ = idIn;
104  break;
105 
106  case 1UL:
107  idList_.reserve(2);
108  idList_.push_back(itemId_);
109  idList_.push_back(idIn);
110  itemId_ = 0ULL;
111  break;
112 
113  default:
114  idList_.push_back(idIn);
115  break;
116  }
117  }
118 
119  AbsReference::AbsReference(AbsArchive &ar,
120  const ClassId &classId,
121  const char *ioPrototype,
122  const unsigned long long itemId)
123  : archive_(ar),
124  classId_(classId),
125  ioProto_(ioPrototype ? ioPrototype : ""),
126  searchId_(itemId),
127  namePattern_(nullptr),
128  categoryPattern_(nullptr),
129  itemId_(0),
130  initialized_(false) {
131  if (!itemId)
132  throw gs::IOInvalidArgument("In AbsReference constructor: invalid item id");
133  }
134 
135  AbsReference::AbsReference(AbsArchive &ar,
136  const ClassId &classId,
137  const char *ioPrototype,
138  const SearchSpecifier &namePattern,
139  const SearchSpecifier &categoryPattern)
140  : archive_(ar),
141  classId_(classId),
142  ioProto_(ioPrototype ? ioPrototype : ""),
143  searchId_(0),
144  namePattern_(namePattern),
145  categoryPattern_(categoryPattern),
146  itemId_(0),
147  initialized_(false) {}
148 } // namespace gs
static AlgebraicMatrix initialize()
uint16_t *__restrict__ id
def unique
Definition: tier0.py:24
tuple size
Write out results.