CBMC
string_container.cpp
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 #include "string_container.h"
13 
14 #include <cstring>
15 #include <iostream>
16 #include <numeric>
17 
18 string_ptrt::string_ptrt(const char *_s):s(_s), len(strlen(_s))
19 {
20 }
21 
22 bool string_ptrt::operator==(const string_ptrt &other) const
23 {
24  if(len!=other.len)
25  return false;
26 
27  return len==0 || memcmp(s, other.s, len)==0;
28 }
29 
31 {
32 }
33 
34 unsigned string_containert::get(const char *s)
35 {
36  string_ptrt string_ptr(s);
37 
38  hash_tablet::iterator it=hash_table.find(string_ptr);
39 
40  if(it!=hash_table.end())
41  return it->second;
42 
43  size_t r=hash_table.size();
44 
45  // these are stable
46  string_list.push_back(std::string(s));
47  string_ptrt result(string_list.back());
48 
49  hash_table[result]=r;
50 
51  // these are not
52  string_vector.push_back(&string_list.back());
53 
54  return r;
55 }
56 
57 unsigned string_containert::get(const std::string &s)
58 {
59  string_ptrt string_ptr(s);
60 
61  hash_tablet::iterator it=hash_table.find(string_ptr);
62 
63  if(it!=hash_table.end())
64  return it->second;
65 
66  size_t r=hash_table.size();
67 
68  // these are stable
69  string_list.push_back(s);
70  string_ptrt result(string_list.back());
71 
72  hash_table[result]=r;
73 
74  // these are not
75  string_vector.push_back(&string_list.back());
76 
77  return r;
78 }
79 
80 void string_container_statisticst::dump_on_stream(std::ostream &out) const
81 {
82  auto total_memory_usage = strings_memory_usage + vector_memory_usage +
84  out << "String container statistics:"
85  << "\n string count: " << string_count
86  << "\n string memory usage: " << strings_memory_usage.to_string()
87  << "\n vector memory usage: " << vector_memory_usage.to_string()
88  << "\n map memory usage: " << map_memory_usage.to_string()
89  << "\n list memory usage: " << list_memory_usage.to_string()
90  << "\n total memory usage: " << total_memory_usage.to_string() << '\n';
91 }
92 
94 {
96  result.string_count = string_vector.size();
98  sizeof(string_vector) +
99  sizeof(string_vectort::value_type) * string_vector.capacity());
100  result.strings_memory_usage = memory_sizet::from_bytes(std::accumulate(
101  begin(string_vector),
102  end(string_vector),
103  std::size_t(0),
104  [](std::size_t sz, const std::string *s) { return sz + s->capacity(); }));
106  sizeof(hash_table) + hash_table.size() * sizeof(hash_tablet::value_type));
107 
109  sizeof(string_list) + 2 * sizeof(void *) * string_list.size());
110  return result;
111 }
std::string to_string() const
static memory_sizet from_bytes(std::size_t bytes)
unsigned get(const char *s)
string_vectort string_vector
hash_tablet hash_table
string_listt string_list
string_container_statisticst compute_statistics() const
static int8_t r
Definition: irep_hash.h:60
int memcmp(const void *s1, const void *s2, size_t n)
Definition: string.c:923
size_t strlen(const char *s)
Definition: string.c:561
Container for C-Strings.
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 * s