LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
StringIDs.hh
Go to the documentation of this file.
1 // Manage String IDs
2 //
3 // The idea here is that you have string identifiers that appear lots of times.
4 // Instead of storing the strings multiple times in the event (or in many events),
5 // you store an identifier that represents the string.
6 //
7 // This object allows for easy creation and management of the ID numbers.
8 // This object can be directly stored in Root - it will just store the internal vector.
9 //
10 // For easy lookups, there is also a map -- you need to initialize this object
11 // before you can use it. We don't want Root to know anything about the map,
12 // hence the @__GCCXML__@ ifdefs.
13 
14 #ifndef artg4tk_util_StringIDs_hh
15 #define artg4tk_util_StringIDs_hh
16 
17 #include <string>
18 #include <vector>
19 
20 #ifndef __GCCXML__
21 #include <map>
22 #endif
23 
24 namespace artg4tk {
25 
26  class StringIDs {
27 
28  public:
29  // Default c'tor
30  StringIDs();
31 
32  // Default d'tor
33  virtual ~StringIDs() {}
34 
35 #ifndef __GCCXML__
36 
37  // Initialize everything. You must call this after you
38  // create it if you want to add
39  void initialize();
40 
41  // Given a string, return the ID and add it to the list
42  unsigned int idGivenString(const std::string& s);
43 
44  // Reset - put in a new string vector (presumedly because you've read one in)
45  void reset(StringIDs const& desired);
46 
47 #endif // __GCCXML__
48 
49  // Given an ID, return the string
50  const std::string&
51  stringGivenID(unsigned int id) const
52  {
53  return stringVec_.at(id);
54  }
55 
56  // Return the number of entries
57  unsigned int
58  size() const
59  {
60  return stringVec_.size();
61  }
62 
63  private:
64  // The vector that holds the strings
65  std::vector<std::string> stringVec_;
66 
67 #ifndef __GCCXML__
68  // Auxillary map
69  std::map<std::string, unsigned int> stringToIdMap_;
70 #endif
71  };
72 }
73 
74 #endif /* artg4tk_util_StringIDs_hh */
void reset(StringIDs const &desired)
Definition: StringIDs.cc:67
std::vector< std::string > stringVec_
Definition: StringIDs.hh:65
virtual ~StringIDs()
Definition: StringIDs.hh:33
const std::string & stringGivenID(unsigned int id) const
Definition: StringIDs.hh:51
std::map< std::string, unsigned int > stringToIdMap_
Definition: StringIDs.hh:69
unsigned int size() const
Definition: StringIDs.hh:58
unsigned int idGivenString(const std::string &s)
Definition: StringIDs.cc:31