ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Circuit.hpp
Go to the documentation of this file.
1/**
2 * @file Circuit.hpp
3 * @brief Out-of-line template method implementations for @c Circuit<gateType>.
4 *
5 * This file provides the definitions of the template methods declared in
6 * @c Circuit.h that cannot be placed in a @c .cpp file (since they must
7 * be instantiated by the compiler in each translation unit that uses them).
8 *
9 * Implemented methods:
10 * - @c hasGate(): UUID membership test.
11 * - @c getGate(), @c findOrAddGate(): UUID → gate_t lookup, allocating a
12 * new gate on miss.
13 * - @c getUUID(): gate_t → UUID string lookup.
14 * - @c bindName(), @c pointName(), @c dropName(), @c moveName(): naming
15 * gates.
16 * - @c addGate(): default gate allocation (extends @c gates and @c wires vectors).
17 * - @c setGate(gateType): allocate a new gate and set its type.
18 * - @c setGate(const uuid&, gateType): create/update a UUID-named gate.
19 * - @c addWire(): append a directed edge.
20 *
21 * Included by subclass headers that need these implementations (e.g.
22 * @c BooleanCircuit.h, @c DotCircuit.h).
23 */
24#ifndef CIRCUIT_HPP
25#define CIRCUIT_HPP
26
27#include "Circuit.h"
28
29template<class gateType>
30bool Circuit<gateType>::hasGate(const uuid &u) const
31{
32 gate_uuid k;
33 if(gate_uuid::parse(u, k))
34 return hasGate(k);
35 return name2id.find(u)!=name2id.end();
36}
37
38template<class gateType>
39std::pair<gate_t, bool> Circuit<gateType>::findOrAddGate(const gate_uuid &u)
40{
41 auto [it, fresh] = uuid2id.try_emplace(u);
42 if(fresh) {
43 it->second = addGate();
44 bindName(it->second, u);
45 }
46 return {it->second, fresh};
47}
48
49template<class gateType>
51{
52 gate_uuid k;
53 if(gate_uuid::parse(u, k))
54 return getGate(k);
55 auto it=name2id.find(u);
56 if(it==name2id.end()) {
57 gate_t id=addGate();
58 bindName(id, u);
59 return id;
60 } else
61 return it->second;
62}
63
64template<class gateType>
66{
67 switch(nameKind(g)) {
68 case UUID_NAME:
69 return id2uuid[static_cast<std::underlying_type<gate_t>::type>(g)].str();
70 case OTHER_NAME:
71 return id2name.at(g);
72 default:
73 return "";
74 }
75}
76
77template<class gateType>
79{
80 const auto i = static_cast<std::underlying_type<gate_t>::type>(g);
81 if(i >= name_kind.size()) {
82 name_kind.resize(i + 1, NO_NAME);
83 id2uuid.resize(i + 1);
84 }
85 if(name_kind[i] == OTHER_NAME)
86 id2name.erase(g);
87 uuid2id[u] = g;
88 id2uuid[i] = u;
90}
91
92template<class gateType>
94{
95 gate_uuid k;
96 if(gate_uuid::parse(u, k)) {
97 bindName(g, k);
98 return;
99 }
100 const auto i = static_cast<std::underlying_type<gate_t>::type>(g);
101 if(i >= name_kind.size()) {
102 name_kind.resize(i + 1, NO_NAME);
103 id2uuid.resize(i + 1);
104 }
105 name2id[u] = g;
106 id2name[g] = u;
108}
109
110template<class gateType>
112{
113 const auto i = static_cast<std::underlying_type<gate_t>::type>(g);
114 switch(nameKind(g)) {
115 case UUID_NAME:
116 uuid2id[id2uuid[i]] = target;
117 break;
118 case OTHER_NAME:
119 name2id[id2name[g]] = target;
120 break;
121 default:
122 break;
123 }
124}
125
126template<class gateType>
128{
129 const auto i = static_cast<std::underlying_type<gate_t>::type>(g);
130 switch(nameKind(g)) {
131 case UUID_NAME:
132 uuid2id.erase(id2uuid[i]);
133 break;
134 case OTHER_NAME:
135 name2id.erase(id2name[g]);
136 id2name.erase(g);
137 break;
138 default:
139 return;
140 }
141 name_kind[i] = NO_NAME;
142}
143
144template<class gateType>
146{
147 const auto i = static_cast<std::underlying_type<gate_t>::type>(from);
148 switch(nameKind(from)) {
149 case UUID_NAME: {
150 const gate_uuid u = id2uuid[i];
151 name_kind[i] = NO_NAME;
152 bindName(to, u);
153 break;
154 }
155 case OTHER_NAME: {
156 const std::string u = id2name[from];
157 id2name.erase(from);
158 name_kind[i] = NO_NAME;
159 bindName(to, u);
160 break;
161 }
162 default:
163 break;
164 }
165}
166
167template<class gateType>
169{
170 gate_t id{gates.size()};
171 gates.push_back(gateType());
172 wires.push_back({});
173 id2uuid.emplace_back();
174 name_kind.push_back(NO_NAME);
175 return id;
176}
177
178template<class gateType>
180{
181 gate_t id = addGate();
182 gates[static_cast<std::underlying_type<gate_t>::type>(id)] = type;
183 return id;
184}
185
186template<class gateType>
187gate_t Circuit<gateType>::setGate(const uuid &u, gateType type)
188{
189 gate_t id = getGate(u);
190 gates[static_cast<std::underlying_type<gate_t>::type>(id)] = type;
191 return id;
192}
193
194template<class gateType>
196{
197 getWires(f).push_back(t);
198}
199
200#endif /* CIRCUIT_HPP */
Generic directed-acyclic-graph circuit template and gate identifier.
gate_t
Strongly-typed gate identifier.
Definition Circuit.h:53
unsigned char nameKind(gate_t g) const
What gate g is named by.
Definition Circuit.h:195
std::vector< unsigned char > name_kind
What each gate is named by.
Definition Circuit.h:190
std::string uuid
UUID type used in this circuit (always std::string).
Definition Circuit.h:175
std::vector< gate_t > & getWires(gate_t g)
Return a mutable reference to the child-wire list of gate g.
Definition Circuit.h:311
@ UUID_NAME
A UUID, in id2uuid.
Definition Circuit.h:181
@ OTHER_NAME
Another string, in id2name.
Definition Circuit.h:182
@ NO_NAME
No name.
Definition Circuit.h:180
void dropName(gate_t g)
Remove the name of gate g, if any.
Definition Circuit.hpp:127
virtual gate_t setGate(const uuid &u, gateType type)
Create or update the gate associated with UUID u.
Definition Circuit.hpp:187
void addWire(gate_t f, gate_t t)
Add a directed wire from gate f (parent) to gate t (child).
Definition Circuit.hpp:195
std::unordered_map< gate_uuid, gate_t, gate_uuid_hash > uuid2id
UUID → gate index.
Definition Circuit.h:188
std::unordered_map< gate_t, std::string > id2name
Gate index → other name.
Definition Circuit.h:192
void pointName(gate_t g, gate_t target)
Make the name of gate g, if any, name gate target.
Definition Circuit.hpp:111
std::vector< gateType > gates
Gate type for each gate.
Definition Circuit.h:215
std::vector< gate_uuid > id2uuid
UUID of each gate (if UUID_NAME).
Definition Circuit.h:189
uuid getUUID(gate_t g) const
Return the UUID string associated with gate g.
Definition Circuit.hpp:65
std::vector< std::vector< gate_t > > wires
Child wire lists for each gate.
Definition Circuit.h:216
std::pair< gate_t, bool > findOrAddGate(const gate_uuid &u)
Return (or create) the gate associated with UUID u, and whether it was created.
Definition Circuit.hpp:39
gate_t getGate(const uuid &u)
Return (or create) the gate associated with UUID u.
Definition Circuit.hpp:50
bool hasGate(const uuid &u) const
Test whether a gate with UUID u exists.
Definition Circuit.hpp:30
void bindName(gate_t g, const gate_uuid &u)
Name gate g by the UUID u.
Definition Circuit.hpp:78
std::unordered_map< std::string, gate_t > name2id
Other name → gate index.
Definition Circuit.h:191
void moveName(gate_t from, gate_t to)
Give the name of gate from, if any, to gate to.
Definition Circuit.hpp:145
virtual gate_t addGate()
Allocate a new gate with a default-initialised type.
Definition Circuit.hpp:168
A UUID as its 16 bytes, read as two big-endian words.
Definition Circuit.h:62
static bool parse(const std::string &s, gate_uuid &u)
Read s as a UUID in the text form PostgreSQL prints (lowercase, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)...
Definition Circuit.h:93