IndexedSet.h
Go to the documentation of this file.
1 #ifndef WIRECELLUTIL_INDEXEDSET
2 #define WIRECELLUTIL_INDEXEDSET
3 
4 #include <unordered_map>
5 #include <vector>
6 
7 namespace WireCell {
8 
9  /** IndexedSet - maintain a collection of exactly one thing in a
10  * first added order */
11 
12  template<class TYPE>
13  class IndexedSet {
14  public:
15  // the passed objects in order of first seen
16 
17  typedef std::unordered_map<TYPE, int> index_type;
18  typedef std::vector<TYPE> collection_type;
19  typedef typename collection_type::size_type size_type;
20 
21  index_type index;
22  collection_type collection;
23  size_type size() { return collection.size(); }
24 
25  int operator()(const TYPE& obj) const {
26  auto mit = index.find(obj);
27  if (mit != index.end()) {
28  return mit->second;
29  }
30  return -1;
31  }
32  int operator()(const TYPE& obj) {
33  auto mit = index.find(obj);
34  if (mit != index.end()) {
35  return mit->second;
36  }
37  int index_number = collection.size();
38  index[obj] = index_number;
39  collection.push_back(obj);
40  return index_number;
41  }
42 
43  bool has(const TYPE& obj) {
44  auto mit = index.find(obj);
45  return mit != index.end();
46  }
47 
48  };
49 
50 }
51 
52 #endif
collection_type collection
Definition: IndexedSet.h:22
index_type index
Definition: IndexedSet.h:21
collection_type::size_type size_type
Definition: IndexedSet.h:19
int operator()(const TYPE &obj)
Definition: IndexedSet.h:32
int operator()(const TYPE &obj) const
Definition: IndexedSet.h:25
size_type size()
Definition: IndexedSet.h:23
std::unordered_map< TYPE, int > index_type
Definition: IndexedSet.h:17
Definition: Main.h:22
std::vector< TYPE > collection_type
Definition: IndexedSet.h:18
bool has(const TYPE &obj)
Definition: IndexedSet.h:43