Skip to content
Snippets Groups Projects
Commit 4e3600d3 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

algo: use ext containers

parent 94c38ad5
No related branches found
No related tags found
1 merge request!206Merge jt
......@@ -3,7 +3,7 @@
 
namespace {
 
auto MinimizeVerboseNFA = registration::AbstractRegister < automaton::simplify::MinimizeVerbose, ext::vector < ext::map < std::pair < DefaultStateType, DefaultStateType >, ext::map < DefaultSymbolType, DefaultStateType > > >, const automaton::DFA < > & > ( automaton::simplify::MinimizeVerbose::minimize, "dfa" ).setDocumentation (
auto MinimizeVerboseNFA = registration::AbstractRegister < automaton::simplify::MinimizeVerbose, ext::vector < ext::map < ext::pair < DefaultStateType, DefaultStateType >, ext::map < DefaultSymbolType, DefaultStateType > > >, const automaton::DFA < > & > ( automaton::simplify::MinimizeVerbose::minimize, "dfa" ).setDocumentation (
"Minimizes deterministic finite autmaton, also reports the middle steps of the computation.\n\
\n\
@param dfa deterministic finite automaton to minimize.\n\
......
......@@ -52,16 +52,16 @@ public:
* @return trace of minimisation of the automaton
*/
template < class SymbolType, class StateType >
static ext::vector < ext::map < std::pair < StateType, StateType >, ext::map < SymbolType, StateType > > > minimize(const automaton::DFA < SymbolType, StateType >& dfa );
static ext::vector < ext::map < ext::pair < StateType, StateType >, ext::map < SymbolType, StateType > > > minimize(const automaton::DFA < SymbolType, StateType >& dfa );
 
private:
template < class SymbolType, class StateType >
static ext::map < std::pair < StateType, StateType >, ext::map < SymbolType, StateType > > print_progress ( const ext::map<std::pair<StateType, ext::set<std::pair<SymbolType, StateType> > >, ext::set<StateType> >& minimizedTransitionFunction);
static ext::map < ext::pair < StateType, StateType >, ext::map < SymbolType, StateType > > print_progress ( const ext::map<ext::pair<StateType, ext::set<ext::pair<SymbolType, StateType> > >, ext::set<StateType> >& minimizedTransitionFunction);
};
 
template < class SymbolType, class StateType >
ext::vector < ext::map < std::pair < StateType, StateType >, ext::map < SymbolType, StateType > > > MinimizeVerbose::minimize(const automaton::DFA < SymbolType, StateType >& dfa ) {
ext::vector < ext::map < std::pair < StateType, StateType >, ext::map < SymbolType, StateType > > > res;
ext::vector < ext::map < ext::pair < StateType, StateType >, ext::map < SymbolType, StateType > > > MinimizeVerbose::minimize(const automaton::DFA < SymbolType, StateType >& dfa ) {
ext::vector < ext::map < ext::pair < StateType, StateType >, ext::map < SymbolType, StateType > > > res;
 
ext::map<StateType, ext::map<SymbolType, StateType > > refactor;
 
......@@ -72,7 +72,7 @@ ext::vector < ext::map < std::pair < StateType, StateType >, ext::map < SymbolTy
refactor[transition.first.first].insert(std::make_pair(transition.first.second, transition.second));
 
ext::map<StateType, StateType> toEquivalentStates; //original state to equivalent state
ext::map<std::pair<StateType, ext::set<std::pair<SymbolType, StateType> > >, ext::set<StateType> > minimizedTransitionFunction; //mapped to the original state
ext::map<ext::pair<StateType, ext::set<ext::pair<SymbolType, StateType> > >, ext::set<StateType> > minimizedTransitionFunction; //mapped to the original state
 
const StateType *firstFinal = nullptr;
const StateType *firstNonfinal = nullptr;
......@@ -92,12 +92,12 @@ ext::vector < ext::map < std::pair < StateType, StateType >, ext::map < SymbolTy
while ( true ) {
for(const std::pair<const StateType, ext::map<SymbolType, StateType> >& transition : refactor) {
const StateType& from = toEquivalentStates.find(transition.first)->second;
ext::set<std::pair<SymbolType, StateType> > transitionFunction;
ext::set<ext::pair<SymbolType, StateType> > transitionFunction;
 
for(const std::pair<const SymbolType, StateType> & transitionFromState : transition.second)
transitionFunction.insert(std::make_pair(transitionFromState.first, toEquivalentStates.find(transitionFromState.second)->second));
transitionFunction.insert(ext::make_pair(transitionFromState.first, toEquivalentStates.find(transitionFromState.second)->second));
 
minimizedTransitionFunction[std::make_pair(from, transitionFunction)].insert(transition.first);
minimizedTransitionFunction[ext::make_pair(from, transitionFunction)].insert(transition.first);
}
 
res.push_back ( print_progress ( minimizedTransitionFunction) );
......@@ -108,7 +108,7 @@ ext::vector < ext::map < std::pair < StateType, StateType >, ext::map < SymbolTy
prevSize = minimizedTransitionFunction.size();
toEquivalentStates.clear();
 
for(const std::pair<const std::pair<StateType, ext::set<std::pair<SymbolType, StateType> > >, ext::set<StateType> >& transition : minimizedTransitionFunction)
for(const std::pair<const ext::pair<StateType, ext::set < ext::pair<SymbolType, StateType> > >, ext::set<StateType> >& transition : minimizedTransitionFunction)
for(const StateType& target : transition.second)
toEquivalentStates.insert(std::make_pair(target, *(transition.second.begin())));
 
......@@ -121,17 +121,17 @@ ext::vector < ext::map < std::pair < StateType, StateType >, ext::map < SymbolTy
#define RESETSS(x) {(x).clear(); (x).str("");}
 
template < class SymbolType, class StateType >
ext::map<std::pair<StateType, StateType>, ext::map<SymbolType, StateType>> MinimizeVerbose::print_progress ( const ext::map<std::pair<StateType, ext::set<std::pair<SymbolType, StateType> > >, ext::set<StateType> >& minimizedTransitionFunction) {
ext::map<ext::pair<StateType, StateType>, ext::map<SymbolType, StateType>> MinimizeVerbose::print_progress ( const ext::map<ext::pair<StateType, ext::set<ext::pair<SymbolType, StateType> > >, ext::set<StateType> >& minimizedTransitionFunction) {
/* need to restruct this first so we have table like: orig state | new state | trans_symb_1 | trans_symb_2 | ... | trans_symb_n */
// we surely have DFA here (transition map hence)
ext::map<std::pair<StateType, StateType>, ext::map<SymbolType, StateType>> printMap;
ext::map<ext::pair<StateType, StateType>, ext::map<SymbolType, StateType>> printMap;
for(const auto& kv: minimizedTransitionFunction) {
for(const auto& state : kv.second) {
ext::map<SymbolType, StateType> stateTransMap;
for(const auto& transition : kv.first.second) {
stateTransMap.insert(std::make_pair(transition.first, transition.second));
}
printMap.insert(std::make_pair(std::make_pair(state, kv.first.first), stateTransMap));
printMap.insert(std::make_pair(ext::make_pair(state, kv.first.first), stateTransMap));
}
}
return printMap;
......
......@@ -43,7 +43,7 @@ static Cut fordfulkerson_impl_dir(const DirectedGraph &graph,
// mark nodes, which are reachable from source
ext::unordered_map<node::Node, int> state;
std::queue<node::Node> open;
ext::vector<std::pair<node::Node, node::Node>> candidates;
ext::vector<ext::pair<node::Node, node::Node>> candidates;
for (const node::Node &node : graph.getNodes())
state[node] = 0;
state[source] = 1;
......@@ -70,7 +70,7 @@ static Cut fordfulkerson_impl_dir(const DirectedGraph &graph,
}
 
// cut are those edges, which lead from nodes reachable from source to nodes unreachable from source
for (const std::pair<node::Node, node::Node> & edge : candidates) {
for (const ext::pair<node::Node, node::Node> & edge : candidates) {
if ((state[edge.first] == 1 && state[edge.second] == 0)
|| (state[edge.first] == 0 && state[edge.second] == 1))
cut.insert(edge);
......@@ -91,7 +91,7 @@ static Cut fordfulkerson_impl_undir(const UndirectedGraph &ugraph,
// mark nodes, which are reachable from source
ext::unordered_map<node::Node, int> state;
std::queue<node::Node> open;
ext::vector<std::pair<node::Node, node::Node>> candidates;
ext::vector<ext::pair<node::Node, node::Node>> candidates;
for (const node::Node &node : ugraph.getNodes())
state[node] = 0;
state[source] = 1;
......@@ -110,7 +110,7 @@ static Cut fordfulkerson_impl_undir(const UndirectedGraph &ugraph,
}
 
// cut are those edges, which lead from nodes reachable from source to nodes unreachable from source
for (const std::pair<node::Node, node::Node> & edge : candidates) {
for (const ext::pair<node::Node, node::Node> & edge : candidates) {
if ((state[edge.first] == 1 && state[edge.second] == 0)
|| (state[edge.first] == 0 && state[edge.second] == 1))
cut.insert(edge);
......
......@@ -10,8 +10,8 @@
 
namespace std {
template<>
struct hash<std::pair<node::Node, node::Node> > {
std::size_t operator()(const std::pair<node::Node, node::Node> &p) const {
struct hash<ext::pair<node::Node, node::Node> > {
std::size_t operator()(const ext::pair<node::Node, node::Node> &p) const {
return std::hash<std::string>()(ext::to_string(p.first) + ext::to_string(p.second));
}
};
......@@ -23,7 +23,7 @@ namespace graph {
 
namespace minimum_cut {
 
typedef ext::unordered_set<std::pair<node::Node, node::Node> > Cut;
typedef ext::unordered_set<ext::pair<node::Node, node::Node> > Cut;
 
// Old implementation without templates
using UndirectedGraph = graph::UndirectedGraph<node::Node, edge::CapacityEdge<node::Node, int>>;
......
......@@ -126,7 +126,7 @@ void testDirNetwork(unsigned netID, const Type1 *netDef, const Type2 cutDef, uns
 
CHECK ( cutSize == cut.size ( ) );
for (i = 0; i < cutSize; i++) {
CHECK(cut.find(std::make_pair(id2node[cutDef[i].node1ID], id2node[cutDef[i].node2ID])) != cut.end());
CHECK(cut.find(ext::make_pair(id2node[cutDef[i].node1ID], id2node[cutDef[i].node2ID])) != cut.end());
}
}
 
......@@ -156,7 +156,7 @@ void testUndirNetwork(unsigned netID, const Type1 *netDef, const Type2 cutDef, u
 
CHECK (cutSize == cut.size ( ) );
for (i = 0; i < cutSize; i++) {
CHECK(cut.find(std::make_pair(id2node[cutDef[i].node1ID], id2node[cutDef[i].node2ID])) != cut.end());
CHECK(cut.find(ext::make_pair(id2node[cutDef[i].node1ID], id2node[cutDef[i].node2ID])) != cut.end());
}
}
 
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment