-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregular.hpp
More file actions
87 lines (80 loc) · 2.63 KB
/
regular.hpp
File metadata and controls
87 lines (80 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* mini-cp is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3
* as published by the Free Software Foundation.
*
* mini-cp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mini-cp. If not, see http://www.gnu.org/licenses/lgpl-3.0.en.html
*
* Copyright (c) 2018. by Laurent Michel, Pierre Schaus, Pascal Van Hentenryck
*/
#ifndef __REGULAR_H
#define __REGULAR_H
#include <set>
#include <array>
#include <map>
#include <algorithm>
#include <iomanip>
#include <stdint.h>
#include "matrix.hpp"
#include "intvar.hpp"
#include "acstr.hpp"
#include "bitset.hpp"
#include "range.hpp"
struct Transition {
int from;
int symbol;
int to;
};
class Automaton {
CPSolver::Ptr _cp;
Range<int> _smb; // first to last symbol range (alphabet for the FSA)
Range<int> _stt; // first to last state range (set of state identifiers in FSA)
int _start; // start state
std::set<int> _finals; // all final states in the FSA
std::vector<Transition> _t; // transition table for the FSA
public:
typedef handle_ptr<Automaton> Ptr;
Automaton(CPSolver::Ptr s,int f,int l,int fState,int lState,
int start,std::set<int> finals,const std::vector<Transition>& tf)
: _cp(s),_smb(f,l),_stt(fState,lState),
_start(start),_finals(finals),_t(tf)
{}
CPSolver::Ptr getSolver() { return _cp;}
auto getStates() const { return _stt;}
auto getSymbols()const { return _smb;}
auto getStart() const { return _start;}
auto getFinals() const {return _finals;}
std::vector<std::vector<int>> transition() const;
};
class Regular : public Constraint {
Automaton::Ptr _A;
Factory::Veci _x;
public:
template <class Vec> Regular(const Vec& x,Automaton::Ptr A)
: Constraint(x[0]->getSolver()),_A(A),
_x(x.size(),Factory::alloci(x[0]->getStore()))
{
int i = 0;
for(auto& xi : x)
_x[i++] = xi;
}
void post() override;
};
namespace Factory {
inline Automaton::Ptr automaton(CPSolver::Ptr cp,int f,int l,int fs,int ls,
int start,
std::set<int> finals,
const std::vector<Transition>& tf)
{
return new (cp) Automaton(cp,f,l,fs,ls,start,finals,tf);
}
template <class Vec> Constraint::Ptr regular(const Vec& x,Automaton::Ptr aPtr) {
return new (aPtr->getSolver()) Regular(x,aPtr);
}
}
#endif