CBMC
boolbv_map.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "boolbv_map.h"
10 
11 #include <util/threeval.h>
12 
13 #include <solvers/prop/prop.h>
14 
15 #ifdef DEBUG
16 #include <iostream>
17 #endif
18 
19 std::string boolbv_mapt::map_entryt::get_value(const propt &prop) const
20 {
21  std::string result;
22 
23  result.reserve(literal_map.size());
24 
25  for(const auto &literal : literal_map)
26  {
27  const tvt value = prop.l_get(literal);
28 
29  result += (value.is_true() ? '1' : (value.is_false() ? '0' : '?'));
30  }
31 
32  return result;
33 }
34 
35 void boolbv_mapt::show(std::ostream &out) const
36 {
37  for(const auto &pair : mapping)
38  out << pair.first << "=" << pair.second.get_value(prop) << '\n';
39 }
40 
42  const irep_idt &identifier,
43  const typet &type,
44  std::size_t width)
45 {
46  std::pair<mappingt::iterator, bool> result=
47  mapping.insert(std::pair<irep_idt, map_entryt>(
48  identifier, map_entryt()));
49 
50  map_entryt &map_entry=result.first->second;
51 
52  if(result.second)
53  { // actually inserted
54  map_entry.type=type;
55  map_entry.literal_map.reserve(width);
56 
57  for(std::size_t bit = 0; bit < width; ++bit)
58  {
59  map_entry.literal_map.push_back(prop.new_variable());
60 
61 #ifdef DEBUG
62  std::cout << "NEW: " << identifier << ":" << bit << "="
63  << map_entry.literal_map.back() << '\n';
64 #endif
65  }
66  }
67 
68  INVARIANT(
69  map_entry.literal_map.size() == width,
70  "number of literals in the literal map shall equal the bitvector width");
71 
72  return map_entry.literal_map;
73 }
74 
76  const irep_idt &identifier,
77  const typet &type,
78  const bvt &literals)
79 {
80  std::pair<mappingt::iterator, bool> result =
81  mapping.insert(std::pair<irep_idt, map_entryt>(identifier, map_entryt()));
82 
83  map_entryt &map_entry = result.first->second;
84 
85  if(result.second)
86  { // actually inserted
87  map_entry.type = type;
88 
89  for(const auto &literal : literals)
90  {
91  INVARIANT(
92  literal.is_constant() || literal.var_no() < prop.no_variables(),
93  "variable number of non-constant literals shall be within bounds");
94  }
95 
96  map_entry.literal_map = literals;
97  }
98  else
99  {
100  for(auto it = literals.begin(); it != literals.end(); ++it)
101  {
102  const literalt &literal = *it;
103 
104  INVARIANT(
105  literal.is_constant() || literal.var_no() < prop.no_variables(),
106  "variable number of non-constant literals shall be within bounds");
107 
108  const std::size_t bit = it - literals.begin();
109 
110  INVARIANT(
111  bit < map_entry.literal_map.size(), "bit index shall be within bounds");
112 
113  prop.set_equal(map_entry.literal_map[bit], literal);
114  }
115  }
116 }
117 
119  const irep_idt &identifier,
120  const typet &)
121 {
122  mapping.erase(identifier);
123 }
std::string get_value(const propt &) const
Definition: boolbv_map.cpp:19
propt & prop
Definition: boolbv_map.h:74
mappingt mapping
Definition: boolbv_map.h:73
void erase_literals(const irep_idt &identifier, const typet &type)
Definition: boolbv_map.cpp:118
void set_literals(const irep_idt &identifier, const typet &type, const bvt &literals)
Definition: boolbv_map.cpp:75
void show(std::ostream &out) const
Definition: boolbv_map.cpp:35
const bvt & get_literals(const irep_idt &identifier, const typet &type, std::size_t width)
Definition: boolbv_map.cpp:41
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
bool is_constant() const
Definition: literal.h:166
var_not var_no() const
Definition: literal.h:83
TO_BE_DOCUMENTED.
Definition: prop.h:25
virtual size_t no_variables() const =0
virtual void set_equal(literalt a, literalt b)
asserts a==b in the propositional formula
Definition: prop.cpp:12
virtual literalt new_variable()=0
virtual tvt l_get(literalt a) const =0
Definition: threeval.h:20
bool is_false() const
Definition: threeval.h:26
bool is_true() const
Definition: threeval.h:25
The type of an expression, extends irept.
Definition: type.h:29
std::vector< literalt > bvt
Definition: literal.h:201