CBMC
string_container.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Container for C-Strings
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_UTIL_STRING_CONTAINER_H
13 #define CPROVER_UTIL_STRING_CONTAINER_H
14 
15 #include <list>
16 #include <unordered_map>
17 #include <vector>
18 
19 #include "memory_units.h"
20 #include "string_hash.h"
21 
23 {
24  const char *s;
25  size_t len;
26 
27  const char *c_str() const
28  {
29  return s;
30  }
31 
32  explicit string_ptrt(const char *_s);
33 
34  explicit string_ptrt(const std::string &_s):s(_s.c_str()), len(_s.size())
35  {
36  }
37 
38  bool operator==(const string_ptrt &other) const;
39 };
40 
41 // NOLINTNEXTLINE(readability/identifiers)
43 {
44 public:
45  size_t operator()(const string_ptrt s) const { return hash_string(s.s); }
46 };
47 
53 {
54  std::size_t string_count;
59 
60  void dump_on_stream(std::ostream &out) const;
61 };
62 
64 {
65 public:
66  unsigned operator[](const char *s)
67  {
68  return get(s);
69  }
70 
71  unsigned operator[](const std::string &s)
72  {
73  return get(s);
74  }
75 
76  // constructor and destructor
79 
80  // the pointer is guaranteed to be stable
81  const char *c_str(size_t no) const
82  {
83  return string_vector[no]->c_str();
84  }
85 
86  // the reference is guaranteed to be stable
87  const std::string &get_string(size_t no) const
88  {
89  return *string_vector[no];
90  }
91 
93 
94 protected:
95  // the 'unsigned' ought to be size_t
96  typedef std::unordered_map<string_ptrt, unsigned, string_ptr_hash>
99 
100  unsigned get(const char *s);
101  unsigned get(const std::string &s);
102 
103  typedef std::list<std::string> string_listt;
105 
106  typedef std::vector<std::string *> string_vectort;
108 };
109 
112 {
113  static string_containert ret;
114  return ret;
115 }
116 
117 #endif // CPROVER_UTIL_STRING_CONTAINER_H
const std::string & get_string(size_t no) const
unsigned get(const char *s)
std::vector< std::string * > string_vectort
std::unordered_map< string_ptrt, unsigned, string_ptr_hash > hash_tablet
const char * c_str(size_t no) const
string_vectort string_vector
unsigned operator[](const std::string &s)
hash_tablet hash_table
std::list< std::string > string_listt
string_listt string_list
string_container_statisticst compute_statistics() const
unsigned operator[](const char *s)
size_t operator()(const string_ptrt s) const
size_t hash_string(const dstringt &s)
Definition: dstring.h:228
string_containert & get_string_container()
Get a reference to the global string container.
string hashing
Has estimated statistics about string container (estimated because this only uses public information,...
void dump_on_stream(std::ostream &out) const
string_ptrt(const char *_s)
bool operator==(const string_ptrt &other) const
const char * c_str() const
string_ptrt(const std::string &_s)
const char * s