38#include <unordered_map>
39#include <unordered_set>
69 for(
int i = 0; i < 8; ++i) {
70 u.
hi = (u.
hi << 8) | p[i];
71 u.
lo = (u.
lo << 8) | p[i + 8];
78 for(
int i = 0; i < 8; ++i) {
79 p[i] =
static_cast<unsigned char>(
hi >> (56 - 8 * i));
80 p[i + 8] =
static_cast<unsigned char>(
lo >> (56 - 8 * i));
96 std::uint64_t w[2] = {0, 0};
98 for(std::size_t i = 0; i < 36; ++i) {
100 if(i == 8 || i == 13 || i == 18 || i == 23) {
106 if(c >=
'0' && c <=
'9')
108 else if(c >=
'a' && c <=
'f')
112 w[n / 16] = (w[n / 16] << 4) | d;
122 static const char digits[] =
"0123456789abcdef";
123 std::string s(36,
'-');
125 for(
int n = 0; n < 32; ++n) {
126 if(pos == 8 || pos == 13 || pos == 18 || pos == 23)
128 const std::uint64_t w = n < 16 ?
hi :
lo;
129 s[pos++] = digits[(w >> (60 - 4 * (n % 16))) & 0xF];
140 return !(*
this == o);
148 template<
class Archive>
void serialize(Archive &ar,
const unsigned int) {
157 return static_cast<std::size_t
>(u.
hi ^ (u.
lo * 0x9E3779B97F4A7C15ULL));
171template<
class gateType>
179enum :
unsigned char {
188std::unordered_map<gate_uuid, gate_t, gate_uuid_hash>
uuid2id;
191std::unordered_map<std::string, gate_t>
name2id;
192std::unordered_map<gate_t, std::string>
id2name;
196 const auto i =
static_cast<std::underlying_type<gate_t>::type
>(g);
216std::vector<std::vector<gate_t> >
wires;
225 gates[
static_cast<std::underlying_type<gate_t>::type
>(g)]=t;
292 u =
id2uuid[
static_cast<std::underlying_type<gate_t>::type
>(g)];
303 return gates[
static_cast<std::underlying_type<gate_t>::type
>(g)];
313 return wires[
static_cast<std::underlying_type<gate_t>::type
>(g)];
323 return wires[
static_cast<std::underlying_type<gate_t>::type
>(g)];
396virtual char const *
what() const noexcept {
428const char *
tag() const noexcept {
439 return g=
gate_t{
static_cast<std::underlying_type<gate_t>::type
>(g)+1};
450 return static_cast<std::underlying_type<gate_t>::type
>(t)<u;
459 return std::to_string(
static_cast<std::underlying_type<gate_t>::type
>(g));
470 std::underlying_type<gate_t>::type u;
484 return o << static_cast<std::underlying_type<gate_t>::type>(g);
std::istream & operator>>(std::istream &i, gate_t &g)
Read a gate_t from an input stream.
gate_t & operator++(gate_t &g)
Pre-increment operator for gate_t.
std::ostream & operator<<(std::ostream &o, gate_t g)
Write a gate_t to an output stream as its decimal value.
bool operator<(gate_t t, std::vector< gate_t >::size_type u)
Compare a gate_t against a std::vector size type.
gate_t
Strongly-typed gate identifier.
std::string to_string(gate_t g)
Convert a gate_t to its decimal string representation.
std::string message
Human-readable description of the error.
virtual char const * what() const noexcept
Return the error message as a C-string.
CircuitException(const std::string &m)
Construct with a descriptive error message.
const char * scope_
PROVSQL_DELIBERATE, PROVSQL_GAP, ...
const char * scope() const noexcept
The kind of the refusal.
CircuitRefusal(const char *scope, const char *tag, const std::string &m)
Construct a refusal.
const char * tag_
Stable short tag of the refusal.
const char * tag() const noexcept
The tag of the refusal.
unsigned char nameKind(gate_t g) const
What gate g is named by.
std::vector< unsigned char > name_kind
What each gate is named by.
std::string uuid
UUID type used in this circuit (always std::string).
std::vector< gate_t > & getWires(gate_t g)
Return a mutable reference to the child-wire list of gate g.
@ UUID_NAME
A UUID, in id2uuid.
@ OTHER_NAME
Another string, in id2name.
gateType getGateType(gate_t g) const
Return the type of gate g.
gate_t getGate(const gate_uuid &u)
Return (or create) the gate associated with UUID u.
Circuit & operator=(const Circuit &)=default
void dropName(gate_t g)
Remove the name of gate g, if any.
const std::vector< gate_t > & getWires(gate_t g) const
Return a const reference to the child-wire list of gate g.
bool hasGate(const gate_uuid &u) const
Test whether a gate with UUID u exists.
virtual gate_t setGate(const uuid &u, gateType type)
Create or update the gate associated with UUID u.
Circuit(const Circuit &)=default
virtual std::string toString(gate_t g) const =0
Return a textual description of gate g for debugging.
void addWire(gate_t f, gate_t t)
Add a directed wire from gate f (parent) to gate t (child).
std::unordered_map< gate_uuid, gate_t, gate_uuid_hash > uuid2id
UUID → gate index.
std::unordered_map< gate_t, std::string > id2name
Gate index → other name.
void setGateType(gate_t g, gateType t)
Update the type of an existing gate.
Circuit & operator=(Circuit &&)=default
void pointName(gate_t g, gate_t target)
Make the name of gate g, if any, name gate target.
std::vector< gateType > gates
Gate type for each gate.
std::vector< gate_uuid > id2uuid
UUID of each gate (if UUID_NAME).
uuid getUUID(gate_t g) const
Return the UUID string associated with gate g.
std::vector< std::vector< gate_t > > wires
Child wire lists for each gate.
Circuit(Circuit &&)=default
std::pair< gate_t, bool > findOrAddGate(const gate_uuid &u)
Return (or create) the gate associated with UUID u, and whether it was created.
gate_t getGate(const uuid &u)
Return (or create) the gate associated with UUID u.
bool getUUID(gate_t g, gate_uuid &u) const
Read the UUID of gate g into u.
bool hasGate(const uuid &u) const
Test whether a gate with UUID u exists.
void bindName(gate_t g, const gate_uuid &u)
Name gate g by the UUID u.
std::vector< gate_t >::size_type getNbGates() const
Return the total number of gates in the circuit.
std::unordered_map< std::string, gate_t > name2id
Other name → gate index.
void moveName(gate_t from, gate_t to)
Give the name of gate from, if any, to gate to.
virtual gate_t addGate()
Allocate a new gate with a default-initialised type.
Hash of a gate_uuid: its bytes are already uniformly spread.
std::size_t operator()(const gate_uuid &u) const
The hash of u.
A UUID as its 16 bytes, read as two big-endian words.
void toBytes(unsigned char *p) const
Write the 16 bytes of the UUID to p.
static gate_uuid fromBytes(const unsigned char *p)
The UUID whose 16 bytes start at p.
bool operator<(const gate_uuid &o) const
The order of the bytes.
std::string str() const
The text form of the UUID, as PostgreSQL prints it.
void serialize(Archive &ar, const unsigned int)
Boost serialisation.
bool operator==(const gate_uuid &o) const
Equality of the 16 bytes.
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)...
std::uint64_t lo
The last eight bytes, big-endian.
bool operator!=(const gate_uuid &o) const
Inequality of the 16 bytes.
std::uint64_t hi
The first eight bytes, big-endian.