blob: 89686c47d4d22eb1281f2847f5ed42e767d0c199 (
plain) (
blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.util.automata
import scala.collection.{immutable, mutable, Set, Map}
/** A nondeterministic automaton. States are integers, where
* 0 is always the only initial state. Transitions are represented
* in the delta function. Default transitions are transitions that
* are taken when no other transitions can be applied.
* All states are reachable. Accepting states are those for which
* the partial function 'finals' is defined.
*/
abstract class NondetWordAutom[T <: AnyRef] {
val nstates: Int
val labels: Seq[T]
val finals: Array[Int] // 0 means not final
val delta: Array[Map[T, immutable.BitSet]]
val default: Array[immutable.BitSet]
/** returns true if the state is final */
final def isFinal(state: Int) = finals(state) > 0
/** returns tag of final state */
final def finalTag(state: Int) = finals(state)
/** returns true if the set of states contains at least one final state */
final def containsFinal(Q: immutable.BitSet): Boolean = {
val it = Q.elements
while (it.hasNext)
if (isFinal(it.next))
return true
return false
}
/** returns true if there are no accepting states */
final def isEmpty = {
var r = true
var j = 0; while(r && (j < nstates)) {
if (isFinal(j))
r = false
}
r
}
/** returns a bitset with the next states for given state and label */
def next(q: Int, a: T): immutable.BitSet = {
delta(q).get(a) match {
case Some(bs) => bs
case _ => default(q)
}
}
/** returns a bitset with the next states for given state and label */
def next(Q: immutable.BitSet, a: T): immutable.BitSet = {
val x = new mutable.BitSet(nstates)
for (q <- Q) {
for (i <- next(q,a)) {
x += i
}
}
x.toImmutable
}
def nextDefault(Q: immutable.BitSet): immutable.BitSet = {
val x = new mutable.BitSet(nstates)
for (q <- Q) {
for (i <- default(q)) { //@todo: OR
x += i
}
}
x.toImmutable
}
override def toString = {
val sb = new StringBuilder("[NondetWordAutom nstates=")
sb.append(nstates)
sb.append(" finals=")
var map = scala.collection.immutable.Map[Int,Int]()
var j = 0; while (j < nstates) {
if (isFinal(j))
map = map.add(j, finals(j));
j += 1
}
sb.append(map.toString)
sb.append(" delta=\n")
for (i <- 0 until nstates) {
sb.append(" ")
sb.append( i )
sb.append("->")
sb.append(delta(i).toString)
sb.append("\n ")
sb.append(" _>")
sb.append(default(i).toString)
sb.append('\n')
}
sb.toString()
}
}
|