1
Fork 0
mirror of https://github.com/Jaxan/hybrid-ads.git synced 2025-04-27 15:07:45 +02:00

Creates stronger types for states, inputs and outputs

This commit is contained in:
Joshua Moerman 2015-02-17 09:44:01 +01:00
parent b62d3a1a17
commit f7fb5def0c
5 changed files with 35 additions and 26 deletions

View file

@ -1,2 +0,0 @@
static int x=x;

View file

@ -1,9 +1,15 @@
#pragma once
#include "phantom.hpp"
#include <map>
#include <string>
#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.
* Everything is indexed by size_t's, so that we can index vectors
@ -13,13 +19,13 @@
*/
struct Mealy {
struct edge {
size_t to = -1;
size_t output = -1;
state to = -1;
output output = -1;
};
std::map<std::string, size_t> nodes_indices;
std::map<std::string, size_t> input_indices;
std::map<std::string, size_t> output_indices;
std::map<std::string, state> nodes_indices;
std::map<std::string, input> input_indices;
std::map<std::string, output> output_indices;
// state -> input -> (output, state)
std::vector<std::vector<edge>> graph;
@ -31,19 +37,19 @@ struct Mealy {
};
inline auto is_complete(const Mealy & m){
for(int n = 0; n < m.graph_size; ++n){
if(m.graph[n].size() != m.input_size) return false;
for(auto && e : m.graph[n]) if(e.to == -1 || e.output == -1) return false;
for(state n = 0; n < m.graph_size; ++n){
if(m.graph[n.base()].size() != m.input_size) return false;
for(auto && e : m.graph[n.base()]) if(e.to == -1 || e.output == -1) return false;
}
return true;
}
inline auto apply(Mealy const & m, size_t state, size_t input){
return m.graph[state][input];
inline auto apply(Mealy const & m, state state, input input){
return m.graph[state.base()][input.base()];
}
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;
while(b != e){
ret = apply(m, state, *b++);

View file

@ -5,7 +5,13 @@
template <typename Base, typename T>
struct phantom : boost::operators<phantom<Base, T>>, boost::shiftable<phantom<Base, T>> {
phantom() = default;
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; }
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>
std::istream & operator>>(std::istream & in, phantom<B, T> & p){ return in >> p.x; }

View file

@ -55,9 +55,9 @@ Mealy read_mealy_from_dot(istream& in, int verbose){
// add edge
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[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){

View file

@ -19,9 +19,9 @@ struct splijtboom {
iota(begin(states), end(states), 0);
}
vector<size_t> states;
vector<state> states;
vector<splijtboom> children;
vector<size_t> seperator;
vector<input> seperator;
int mark = 0;
};
@ -95,9 +95,9 @@ int main(int argc, char *argv[]){
}
// First try to split on output
for(size_t symbol = 0; symbol < P; ++symbol){
auto new_blocks = part.refine(block, [symbol, &g](size_t state){
return apply(g, state, symbol).output;
for(input symbol = 0; symbol < P; ++symbol){
auto new_blocks = part.refine(block, [symbol, &g](state state){
return apply(g, state, symbol).output.base();
}, Q);
if(elems_in(new_blocks) == 1){
@ -129,14 +129,14 @@ int main(int argc, char *argv[]){
}
// 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);
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{
return successor_states[state];
auto & oboom = lca(root, [&successor_states](state state) -> bool{
return successor_states[state.base()];
});
if(oboom.children.empty()){
@ -163,7 +163,7 @@ int main(int argc, char *argv[]){
copy(begin(oboom.seperator), end(oboom.seperator), it);
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);
if(elems_in(new_blocks) == 1){