mirror of
https://github.com/Jaxan/hybrid-ads.git
synced 2025-04-28 07:27:45 +02:00
Creates stronger types for states, inputs and outputs
This commit is contained in:
parent
b62d3a1a17
commit
f7fb5def0c
5 changed files with 35 additions and 26 deletions
|
@ -1,2 +0,0 @@
|
||||||
static int x=x;
|
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "phantom.hpp"
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
using state = phantom<size_t, struct state_tag>;
|
||||||
|
using input = phantom<size_t, struct input_tag>;
|
||||||
|
using output = phantom<size_t, struct output_tag>;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Structure used for reading mealy files from dot files.
|
* Structure used for reading mealy files from dot files.
|
||||||
* Everything is indexed by size_t's, so that we can index vectors
|
* Everything is indexed by size_t's, so that we can index vectors
|
||||||
|
@ -13,13 +19,13 @@
|
||||||
*/
|
*/
|
||||||
struct Mealy {
|
struct Mealy {
|
||||||
struct edge {
|
struct edge {
|
||||||
size_t to = -1;
|
state to = -1;
|
||||||
size_t output = -1;
|
output output = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::map<std::string, size_t> nodes_indices;
|
std::map<std::string, state> nodes_indices;
|
||||||
std::map<std::string, size_t> input_indices;
|
std::map<std::string, input> input_indices;
|
||||||
std::map<std::string, size_t> output_indices;
|
std::map<std::string, output> output_indices;
|
||||||
|
|
||||||
// state -> input -> (output, state)
|
// state -> input -> (output, state)
|
||||||
std::vector<std::vector<edge>> graph;
|
std::vector<std::vector<edge>> graph;
|
||||||
|
@ -31,19 +37,19 @@ struct Mealy {
|
||||||
};
|
};
|
||||||
|
|
||||||
inline auto is_complete(const Mealy & m){
|
inline auto is_complete(const Mealy & m){
|
||||||
for(int n = 0; n < m.graph_size; ++n){
|
for(state n = 0; n < m.graph_size; ++n){
|
||||||
if(m.graph[n].size() != m.input_size) return false;
|
if(m.graph[n.base()].size() != m.input_size) return false;
|
||||||
for(auto && e : m.graph[n]) if(e.to == -1 || e.output == -1) return false;
|
for(auto && e : m.graph[n.base()]) if(e.to == -1 || e.output == -1) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline auto apply(Mealy const & m, size_t state, size_t input){
|
inline auto apply(Mealy const & m, state state, input input){
|
||||||
return m.graph[state][input];
|
return m.graph[state.base()][input.base()];
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Iterator>
|
template <typename Iterator>
|
||||||
auto apply(Mealy const & m, size_t state, Iterator b, Iterator e){
|
auto apply(Mealy const & m, state state, Iterator b, Iterator e){
|
||||||
Mealy::edge ret;
|
Mealy::edge ret;
|
||||||
while(b != e){
|
while(b != e){
|
||||||
ret = apply(m, state, *b++);
|
ret = apply(m, state, *b++);
|
||||||
|
|
|
@ -5,7 +5,13 @@
|
||||||
|
|
||||||
template <typename Base, typename T>
|
template <typename Base, typename T>
|
||||||
struct phantom : boost::operators<phantom<Base, T>>, boost::shiftable<phantom<Base, T>> {
|
struct phantom : boost::operators<phantom<Base, T>>, boost::shiftable<phantom<Base, T>> {
|
||||||
|
phantom() = default;
|
||||||
phantom(Base y) : x(y) {}
|
phantom(Base y) : x(y) {}
|
||||||
|
phantom(phantom const &) = default;
|
||||||
|
phantom& operator=(phantom const &) = default;
|
||||||
|
phantom(phantom &&) = default;
|
||||||
|
phantom& operator=(phantom &&) = default;
|
||||||
|
|
||||||
explicit operator Base() const { return x; }
|
explicit operator Base() const { return x; }
|
||||||
Base base() const { return x; }
|
Base base() const { return x; }
|
||||||
|
|
||||||
|
@ -41,4 +47,3 @@ std::ostream & operator<<(std::ostream & out, phantom<B, T> p){ return out << p.
|
||||||
|
|
||||||
template <typename B, typename T>
|
template <typename B, typename T>
|
||||||
std::istream & operator>>(std::istream & in, phantom<B, T> & p){ return in >> p.x; }
|
std::istream & operator>>(std::istream & in, phantom<B, T> & p){ return in >> p.x; }
|
||||||
|
|
||||||
|
|
|
@ -55,9 +55,9 @@ Mealy read_mealy_from_dot(istream& in, int verbose){
|
||||||
|
|
||||||
// add edge
|
// add edge
|
||||||
m.graph.resize(m.graph_size);
|
m.graph.resize(m.graph_size);
|
||||||
auto & v = m.graph[m.nodes_indices[lh]];
|
auto & v = m.graph[m.nodes_indices[lh].base()];
|
||||||
v.resize(m.input_size);
|
v.resize(m.input_size);
|
||||||
v[m.input_indices[input]] = {m.nodes_indices[rh], m.output_indices[output]};
|
v[m.input_indices[input].base()] = {m.nodes_indices[rh], m.output_indices[output]};
|
||||||
}
|
}
|
||||||
|
|
||||||
if(verbose >= 1){
|
if(verbose >= 1){
|
||||||
|
|
20
src/main.cpp
20
src/main.cpp
|
@ -19,9 +19,9 @@ struct splijtboom {
|
||||||
iota(begin(states), end(states), 0);
|
iota(begin(states), end(states), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<size_t> states;
|
vector<state> states;
|
||||||
vector<splijtboom> children;
|
vector<splijtboom> children;
|
||||||
vector<size_t> seperator;
|
vector<input> seperator;
|
||||||
int mark = 0;
|
int mark = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -95,9 +95,9 @@ int main(int argc, char *argv[]){
|
||||||
}
|
}
|
||||||
|
|
||||||
// First try to split on output
|
// First try to split on output
|
||||||
for(size_t symbol = 0; symbol < P; ++symbol){
|
for(input symbol = 0; symbol < P; ++symbol){
|
||||||
auto new_blocks = part.refine(block, [symbol, &g](size_t state){
|
auto new_blocks = part.refine(block, [symbol, &g](state state){
|
||||||
return apply(g, state, symbol).output;
|
return apply(g, state, symbol).output.base();
|
||||||
}, Q);
|
}, Q);
|
||||||
|
|
||||||
if(elems_in(new_blocks) == 1){
|
if(elems_in(new_blocks) == 1){
|
||||||
|
@ -129,14 +129,14 @@ int main(int argc, char *argv[]){
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then try to split on state
|
// Then try to split on state
|
||||||
for(size_t symbol = 0; symbol < P; ++symbol){
|
for(input symbol = 0; symbol < P; ++symbol){
|
||||||
vector<bool> successor_states(N, false);
|
vector<bool> successor_states(N, false);
|
||||||
for(auto && state : *block){
|
for(auto && state : *block){
|
||||||
successor_states[g.graph[state][symbol].to] = true;
|
successor_states[apply(g, state, symbol).to.base()] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto & oboom = lca(root, [&successor_states](size_t state) -> bool{
|
auto & oboom = lca(root, [&successor_states](state state) -> bool{
|
||||||
return successor_states[state];
|
return successor_states[state.base()];
|
||||||
});
|
});
|
||||||
|
|
||||||
if(oboom.children.empty()){
|
if(oboom.children.empty()){
|
||||||
|
@ -163,7 +163,7 @@ int main(int argc, char *argv[]){
|
||||||
copy(begin(oboom.seperator), end(oboom.seperator), it);
|
copy(begin(oboom.seperator), end(oboom.seperator), it);
|
||||||
|
|
||||||
auto new_blocks = part.refine(block, [&boom, &g](size_t state){
|
auto new_blocks = part.refine(block, [&boom, &g](size_t state){
|
||||||
return apply(g, state, begin(boom.seperator), end(boom.seperator)).output;
|
return apply(g, state, begin(boom.seperator), end(boom.seperator)).output.base();
|
||||||
}, Q);
|
}, Q);
|
||||||
|
|
||||||
if(elems_in(new_blocks) == 1){
|
if(elems_in(new_blocks) == 1){
|
||||||
|
|
Loading…
Add table
Reference in a new issue