87 void toString(std::ostringstream& os)
const;
91 static void*
operator new(size_t);
92 static void operator delete(
void*);
106 REG::Exp::operator
new(
size_t s) {
107 return heap.ralloc(s);
110 REG::Exp::operator
delete(
void*) {
115 REG::Exp::dispose(
void) {
119 while (!todo.empty()) {
124 if ((e->data.kids[1] !=
nullptr) && (--e->data.kids[1]->use_cnt == 0))
125 todo.push(e->data.kids[1]);
128 if ((e->data.kids[0] !=
nullptr) && (--e->data.kids[0]->use_cnt == 0))
129 todo.push(e->data.kids[0]);
143 if ((e !=
nullptr) && (--e->use_cnt == 0))
150 return (e !=
nullptr) ? e->_n_pos : 0;
157 os <<
"[" <<
data.symbol <<
"]";
161 bool par = ((
data.kids[0] !=
nullptr) &&
164 os << (par ?
"*(" :
"*");
165 if (
data.kids[0]==
nullptr) {
168 data.kids[0]->toString(os);
170 os << (par ?
")" :
"");
175 bool par0 = ((
data.kids[0] !=
nullptr) &&
177 os << (par0 ?
"(" :
"");
178 if (
data.kids[0]==
nullptr) {
181 data.kids[0]->toString(os);
183 os << (par0 ?
")+" :
"+");
184 bool par1 = ((
data.kids[1] !=
nullptr) &&
186 os << (par1 ?
"(" :
"");
187 if (
data.kids[1]==
nullptr) {
190 data.kids[1]->toString(os);
192 os << (par1 ?
")" :
"");
196 if (
data.kids[0]==
nullptr) {
199 data.kids[0]->toString(os);
202 if (
data.kids[1]==
nullptr) {
205 data.kids[1]->toString(os);
216 std::ostringstream os;
264 for (
int i=n; i--; ) {
269 a[i]->data.symbol = x[i];
272 for (
int m=n; m>1; ) {
281 a[0]->data.kids[0] = e1;
282 a[0]->data.kids[1] = e2;
285 for (
int i=0; i<m; i++) {
292 a[i]->data.kids[0] = e1;
293 a[i]->data.kids[1] = e2;
330 if (e ==
nullptr)
return r2;
331 if (r2.e ==
nullptr)
return *
this;
376 if ((n>m) || (m == 0))
389 unsigned int i = m-n;
426 REG::toString(
void)
const {
476 for (
const PosSet* ps =
this; ps !=
nullptr; ps = ps->next)
479 }
else if (ps->pos < p) {
487 while ((ps1 !=
nullptr) && (ps2 !=
nullptr)) {
505 while ((ps1 !=
nullptr) && (ps2 !=
nullptr)) {
510 *p = n; p = &n->
next;
511 if (ps1->
pos == ps2->
pos) {
514 }
else if (ps1->
pos > ps2->
pos) {
520 *p = (ps1 !=
nullptr) ? ps1 : ps2;
580 if (todo.
top().exp ==
nullptr) {
582 done.
push(NodeInfo(
true,
nullptr,
nullptr));
584 switch (todo.
top().exp->type) {
587 pi[p].symbol = todo.
pop().exp->data.symbol;
588 PosSet* ps =
new (psm) PosSet(p++);
589 done.
push(NodeInfo(
false,ps,ps));
593 if (todo.
top().open) {
595 todo.
top().open =
false;
596 todo.
push(todo.
top().exp->data.kids[0]);
599 NodeInfo ni = done.
pop();
600 for (PosSet* ps = ni.lastpos; ps !=
nullptr; ps = ps->next)
601 pi[ps->pos].followpos =
602 PosSet::cup(psm,
pi[ps->pos].followpos,ni.firstpos);
603 done.
push(NodeInfo(
true,ni.firstpos,ni.lastpos));
607 if (todo.
top().open) {
609 todo.
top().open =
false;
611 todo.
push(e->data.kids[1]);
612 todo.
push(e->data.kids[0]);
615 NodeInfo ni1 = done.
pop();
616 NodeInfo ni0 = done.
pop();
617 for (PosSet* ps = ni0.lastpos; ps !=
nullptr; ps = ps->next)
618 pi[ps->pos].followpos =
619 PosSet::cup(psm,
pi[ps->pos].followpos,ni1.firstpos);
620 done.
push(NodeInfo(ni0.nullable & ni1.nullable,
622 PosSet::cup(psm,ni0.firstpos,ni1.firstpos) : ni0.firstpos,
624 PosSet::cup(psm,ni0.lastpos,ni1.lastpos) : ni1.lastpos));
628 if (todo.
top().open) {
630 todo.
top().open =
false;
632 todo.
push(e->data.kids[1]);
633 todo.
push(e->data.kids[0]);
636 NodeInfo ni1 = done.
pop();
637 NodeInfo ni0 = done.
pop();
638 done.
push(NodeInfo(ni0.nullable | ni1.nullable,
639 PosSet::cup(psm,ni0.firstpos,ni1.firstpos),
640 PosSet::cup(psm,ni0.lastpos,ni1.lastpos)));
646 }
while (!todo.
empty());
647 return done.
top().firstpos;
685 bool empty(
void)
const;
699 root.right =
nullptr;
713 return next ==
nullptr;
727 }
while (n !=
nullptr);
744 operator ()(
int x,
int y) {
765 void add(
int,
int,
int);
775 t[n].i_state = i_state;
776 t[n].symbol = symbol;
777 t[n].o_state = o_state;
843 PosSetAllocator psm(region);
844 StatePoolAllocator spm(
heap);
848 PosInfo*
pi = region.
alloc<PosInfo>(n_pos);
849 for (
int i=n_pos; i--; )
850 pi[i].followpos =
nullptr;
852 PosSet* firstpos = r.e->followpos(psm,&
pi[0]);
855 int* symbols = region.
alloc<
int>(n_pos);
856 for (
int i=n_pos; i--; )
857 symbols[i] =
pi[i].symbol;
859 SymbolsInc::sort(&symbols[0],n_pos-1);
861 for (
int i = 1; i<n_pos-1; i++)
862 if (symbols[i-1] != symbols[i])
863 symbols[n_symbols++] = symbols[i];
867 StatePool sp(firstpos);
868 while (!sp.empty()) {
869 StateNode* sn = sp.pop();
870 for (
int i = n_symbols; i--; ) {
872 for (PosSet* ps = sn->pos; ps !=
nullptr; ps = ps->next)
873 if (
pi[ps->pos].symbol == symbols[i])
874 u = PosSet::cup(psm,u,
pi[ps->pos].followpos);
876 tb.add(sn->state,symbols[i],sp.state(spm,u));
883 for (StateNode* n = sp.all; n !=
nullptr; n = n->next)
884 if (n->pos->in(n_pos-1))
888 return DFA(0,tb.transitions(),fb.finals(),
true);
int size(void) const
Return size of array (number of elements).
Specification of a DFA transition.
Deterministic finite automaton (DFA).
Passing integer arguments.
ExpInfo(REG::Exp *e=nullptr)
For collecting final states while constructing a DFA.
Node information computed during traversal of the expressions.
NodeInfo(bool n=false, PosSet *fp=nullptr, PosSet *lp=nullptr)
Information on positions collected during traversal.
static PosSet * cup(PosSetAllocator &, PosSet *, PosSet *)
static PosSetCmp cmp(PosSet *, PosSet *)
Node together with state information
State pool combines a tree of states together with yet unprocessed states
int state(StatePoolAllocator &, PosSet *)
static void sort(int s[], int n)
Exception: Too few arguments available in argument array
For collecting transitions while constructing a DFA.
DFA::Transition * transitions(void)
Implementation of the actual expression tree.
std::string toString(void) const
Print expression.
MiniModel::PosSet * followpos(MiniModel::PosSetAllocator &, MiniModel::PosInfo *)
Compute the follow positions.
Exp * kids[2]
Subexpressions.
ExpType type
Type of regular expression.
static int n_pos(Exp *e)
Return number of positions of e.
unsigned int use_cnt
Reference counter.
union Gecode::REG::Exp::@006363043156204252347047222243062210172322043104 data
Symbol or subexpressions.
static void dec(Exp *e)
Decrement use counter of e.
void toString(std::ostringstream &os) const
Print expression to os.
static void inc(Exp *e)
Increment use counter of e.
int _n_pos
Number of positions.
ExpType
Type of regular expression.
Regular expressions over integer values.
REG operator*(void)
Return expression for: this expression arbitrarily often (Kleene star).
const REG & operator=(const REG &r)
Assign to regular expression r.
REG & operator+=(const REG &r)
This expression is followed by r.
REG & operator|=(const REG &r)
This expression or r.
friend class MiniModel::ExpInfo
REG(void)
Initialize as empty sequence (epsilon).
REG operator()(unsigned int n, unsigned int m)
Return expression for: this expression at least n and at most m times.
REG operator+(void)
Return expression for: this expression at least once.
REG operator|(const REG &r)
Return expression for: this expression or r.
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Manage memory organized into block lists (allocator).
Client for block allocator of type T.
Array with arbitrary number of elements.
Stack with arbitrary number of elements.
void push(const T &x)
Push element x on top of stack.
bool empty(void) const
Test whether stack is empty.
T pop(void)
Pop topmost element from stack and return it.
T & top(void) const
Return element on top of stack.
Heap heap
The single global heap.
const int max
Largest allowed integer value.
Minimalistic modeling support.
Support::BlockAllocator< PosSet, Region > PosSetAllocator
Allocator for position sets.
Support::BlockAllocator< StateNode, Heap > StatePoolAllocator
Allocator for state nodes.
PosSetCmp
Order on position sets.
void quicksort(Type *l, Type *r, Less &less)
Standard quick sort.
Gecode toplevel namespace
#define GECODE_NEVER
Assert that this command is never executed.