LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
artg4tk::StringIDs Class Reference

#include "StringIDs.hh"

Public Member Functions

 StringIDs ()
 
virtual ~StringIDs ()
 
void initialize ()
 
unsigned int idGivenString (const std::string &s)
 
void reset (StringIDs const &desired)
 
const std::string & stringGivenID (unsigned int id) const
 
unsigned int size () const
 

Private Attributes

std::vector< std::string > stringVec_
 
std::map< std::string, unsigned int > stringToIdMap_
 

Detailed Description

Definition at line 26 of file StringIDs.hh.

Constructor & Destructor Documentation

artg4tk::StringIDs::StringIDs ( )

Definition at line 10 of file StringIDs.cc.

10 {}
virtual artg4tk::StringIDs::~StringIDs ( )
inlinevirtual

Definition at line 33 of file StringIDs.hh.

References idGivenString(), initialize(), and reset().

33 {}

Member Function Documentation

unsigned int artg4tk::StringIDs::idGivenString ( const std::string &  s)

Definition at line 31 of file StringIDs.cc.

References stringToIdMap_, and stringVec_.

Referenced by ~StringIDs().

32 {
33  // Do we already have s?
34  unsigned int val = 0;
35  bool found = false;
36 
37  // If the string vector is not empty, then we have to look in the map
38  if (!stringVec_.empty()) {
39 
40  // If the map is empty, then it hasn't been initialized. Throw.
41  if (stringToIdMap_.empty()) {
42  throw cet::exception("STRINGIDS") << "You forgot to initialize the StringIds object"
43  << "\n";
44  }
45 
46  // Look in the map for the ID number
47  auto mapIter = stringToIdMap_.find(s);
48  if (mapIter != stringToIdMap_.end()) {
49  // Found it
50  val = mapIter->second;
51  found = true;
52  }
53  }
54 
55  if (!found) {
56  // String not found, add it
57  val = stringVec_.size();
58  stringVec_.push_back(s);
59  stringToIdMap_[s] = val;
60  }
61 
62  return val;
63 }
std::vector< std::string > stringVec_
Definition: StringIDs.hh:65
std::map< std::string, unsigned int > stringToIdMap_
Definition: StringIDs.hh:69
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void artg4tk::StringIDs::initialize ( )

Definition at line 17 of file StringIDs.cc.

References stringToIdMap_, and stringVec_.

Referenced by reset(), and ~StringIDs().

18 {
19 
20  // If the string is not empty, then create the map
21  if (!stringVec_.empty()) {
22  unsigned int count = 0;
23  for (auto entry : stringVec_) {
24  stringToIdMap_.insert(std::pair<std::string, unsigned int>(entry, count));
25  }
26  }
27 }
std::vector< std::string > stringVec_
Definition: StringIDs.hh:65
std::map< std::string, unsigned int > stringToIdMap_
Definition: StringIDs.hh:69
void artg4tk::StringIDs::reset ( StringIDs const &  desired)

Definition at line 67 of file StringIDs.cc.

References initialize(), and stringVec_.

Referenced by ~StringIDs().

68 {
69 
70  // Replace contents
71  stringVec_ = desired.stringVec_;
72 
73  // Re-initialize
74  initialize();
75 }
std::vector< std::string > stringVec_
Definition: StringIDs.hh:65
unsigned int artg4tk::StringIDs::size ( void  ) const
inline

Definition at line 58 of file StringIDs.hh.

References stringVec_.

59  {
60  return stringVec_.size();
61  }
std::vector< std::string > stringVec_
Definition: StringIDs.hh:65
const std::string& artg4tk::StringIDs::stringGivenID ( unsigned int  id) const
inline

Definition at line 51 of file StringIDs.hh.

References stringVec_.

52  {
53  return stringVec_.at(id);
54  }
std::vector< std::string > stringVec_
Definition: StringIDs.hh:65

Member Data Documentation

std::map<std::string, unsigned int> artg4tk::StringIDs::stringToIdMap_
private

Definition at line 69 of file StringIDs.hh.

Referenced by idGivenString(), and initialize().

std::vector<std::string> artg4tk::StringIDs::stringVec_
private

Definition at line 65 of file StringIDs.hh.

Referenced by idGivenString(), initialize(), reset(), size(), and stringGivenID().


The documentation for this class was generated from the following files: