Skip to content

Commit 6cbd518

Browse files
committed
Refactoring de nombres. Agrego linter
1 parent 52205e7 commit 6cbd518

File tree

6 files changed

+111
-103
lines changed

6 files changed

+111
-103
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22

33
.env
44

5+
*.out
6+
7+
*.sum
58

69
.vscode/

dfa_estructures_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ package DFA
44

55
import testing "testing"
66

7-
87
func TestPartitionNotEmpty(t *testing.T) {
98
q0 := 0
109

11-
partition := Partition{ q0 }
10+
partition := Partition{q0}
1211

1312
if partition.IsEmpty() {
1413
t.Error()
@@ -31,7 +30,7 @@ func TestPartitionEmpty(t *testing.T) {
3130

3231
func TestPartitionExtractElem(t *testing.T) {
3332
q0 := 0
34-
partition := Partition{ q0 }
33+
partition := Partition{q0}
3534

3635
partition.ExtractElem(q0)
3736

@@ -56,12 +55,12 @@ func TestPartitionExtractPartitionWithNoIntersection(t *testing.T) {
5655
q3 := 3
5756
q4 := 4
5857

59-
p1 := Partition{ q1, q2 }
60-
p2 := Partition{ q3, q4 }
58+
p1 := Partition{q1, q2}
59+
p2 := Partition{q3, q4}
6160
oldSizeSum := p1.Size() + p2.Size()
6261

6362
p1.Extract(p2)
64-
if oldSizeSum != p1.Size() + p2.Size() {
63+
if oldSizeSum != p1.Size()+p2.Size() {
6564
t.Error()
6665
}
6766
}
@@ -71,11 +70,11 @@ func TestPartitionExtractPartitionWithIntersection(t *testing.T) {
7170
q3 := 3
7271
q4 := 4
7372

74-
p1 := Partition{ q1, q2, q4 }
75-
p2 := Partition{ q3, q4 }
73+
p1 := Partition{q1, q2, q4}
74+
p2 := Partition{q3, q4}
7675
oldSizeP1 := p1.Size()
7776
p1.Extract(p2)
78-
if oldSizeP1 != p1.Size() + 1 {
77+
if oldSizeP1 != p1.Size()+1 {
7978
t.Error()
8079
}
8180
}
@@ -86,11 +85,10 @@ func TestPartitionExtractPartitionBiggerBecomesEmpty(t *testing.T) {
8685
q3 := 3
8786
q4 := 4
8887

89-
p1 := Partition{ q1, q2, q3, q4 }
90-
p2 := Partition{ q3, q4 }
88+
p1 := Partition{q1, q2, q3, q4}
89+
p2 := Partition{q3, q4}
9190
p2.Extract(p1)
9291
if !p2.IsEmpty() {
9392
t.Error()
9493
}
9594
}
96-

dfa_min_data.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
package DFA
1+
package dfa
22

3+
// Partition represents a set of states
34
type Partition []State
5+
6+
// State represent automata state with a numeric value
47
type State = int // Alias to int
58

6-
type Splitter struct {
7-
Partition Partition
8-
Symbol int
9+
type splitter struct {
10+
partition Partition
11+
input int
912
}
1013

14+
// DFA represents a finite automata
1115
type DFA struct {
12-
States Partition
13-
Alphabet []int
16+
States Partition
17+
Alphabet []int
1418
InitialState State
15-
FinalStates Partition
16-
Delta map[State]map[int]State // Given state and symbol returns the state
19+
FinalStates Partition
20+
Delta map[State]map[int]State // Given state and symbol returns the state
1721
}
1822

1923
// Size returns the number of states of the automata
20-
func (M *DFA) Size() int{
24+
func (M *DFA) Size() int {
2125
return M.States.Size()
2226
}
2327

2428
// SplitBy returns R1 and R2, where R1 are the states from the partition that has transitions to sp.Partition with sp.Symbol
2529
// and R2 are the states from the partition that do not have transitions with sp.Symbol to sp.Partition
26-
func (P *Partition) SplitBy(sp *Splitter, A *DFA) (R1 Partition, R2 Partition, splitted bool) {
30+
func (P *Partition) SplitBy(sp *splitter, A *DFA) (R1 Partition, R2 Partition, splitted bool) {
2731
splitted = true
28-
a := sp.Symbol
29-
partitionSp := sp.Partition
30-
for _, t := range (*P) {
32+
a := sp.input
33+
partitionSp := sp.partition
34+
for _, t := range *P {
3135
if partitionSp.Includes((A.Delta[t][a])) {
3236
R1.Add(t)
3337
} else {
@@ -54,8 +58,9 @@ func (P *Partition) StatesWithIncomingTransitionWith(a int, A *DFA) Partition {
5458
}
5559
}
5660
return *newPartition
57-
}
61+
}
5862

63+
// Set interface wraps the basic actions that a math set should have
5964
type Set interface {
6065
Extract(Q Partition)
6166
ExtractElem(q *State)
@@ -66,22 +71,21 @@ type Set interface {
6671
Equals(Q Partition) bool
6772
}
6873

69-
7074
// Equals returns true if Q has the same elements that the partition
7175
func (P *Partition) Equals(Q Partition) bool {
7276
if Q.Size() != P.Size() {
7377
return false
7478
}
75-
for _, p := range (*P) {
79+
for _, p := range *P {
7680
Q.ExtractElem(p)
77-
}
81+
}
7882
return Q.IsEmpty()
7983
}
8084

8185
// Add adds a new state to the partition if it does not exist
8286
func (P *Partition) Add(q State) {
83-
for _, t := range (*P) {
84-
if t == q {
87+
for _, t := range *P {
88+
if t == q {
8589
return
8690
}
8791
}
@@ -100,7 +104,7 @@ func (P *Partition) Size() int {
100104

101105
// Includes returns true if q is in the partition
102106
func (P *Partition) Includes(q State) bool {
103-
for _, p := range (*P) {
107+
for _, p := range *P {
104108
if q == p {
105109
return true
106110
}
@@ -125,6 +129,3 @@ func (P *Partition) ExtractElem(q State) {
125129
}
126130
}
127131
}
128-
129-
130-

dfa_min_hopcroft.go

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
package DFA
1+
package dfa
22

3+
// HopcroftDFAMin Given A DFA returns a new DFA with minimum states
34
func HopcroftDFAMin(A DFA) DFA {
45
var pi []Partition
56
pi = append(pi, A.FinalStates)
67
pi = append(pi, A.States)
78
pi[1].Extract(pi[0])
89

9-
var worklist []Splitter
10+
var worklist []splitter
1011

1112
for _, symb := range A.Alphabet { // Step 4
12-
var a0 = pi[0].StatesWithIncomingTransitionWith(symb, &A)
13+
var a0 = pi[0].StatesWithIncomingTransitionWith(symb, &A)
1314
var a1 = pi[1].StatesWithIncomingTransitionWith(symb, &A)
1415
if a0.Size() < a1.Size() {
1516
// Use partition with finals states
16-
worklist = append(worklist, Splitter{Partition: pi[0], Symbol: symb})
17+
worklist = append(worklist, splitter{partition: pi[0], input: symb})
1718
} else {
1819
// Use partition with non finals states
19-
worklist = append(worklist, Splitter{Partition: pi[1], Symbol: symb})
20+
worklist = append(worklist, splitter{partition: pi[1], input: symb})
2021
}
2122
}
2223
for len(worklist) > 0 {
2324
// Pick a splitter <Partition P, Symbol a> from the splitter set (and delete it)
24-
currentSplitter := PickOneAndRemove(&worklist)
25+
currentSplitter := pickOneAndRemove(&worklist)
2526
for i, R := range pi { //Step 7
2627
R1, R2, splitted := R.SplitBy(&currentSplitter, &A)
2728
if splitted { // Hay refinamiento
@@ -30,17 +31,17 @@ func HopcroftDFAMin(A DFA) DFA {
3031
// R2 == B''j ==> Bk
3132
pi = append(pi, R2)
3233
for _, c := range A.Alphabet {
33-
spR := Splitter{ Partition: R, Symbol: c}
34-
spR1 := Splitter{ Partition: R1, Symbol: c}
35-
spR2 := Splitter{ Partition: R2, Symbol: c}
36-
if !ReplaceInWorklistIfSplitterExists(&worklist, spR, spR1, spR2) {
37-
ar1 := R1.StatesWithIncomingTransitionWith(c, &A)
38-
ar2 := R2.StatesWithIncomingTransitionWith(c, &A)
39-
if ( (ar1.Size() < ar2.Size())) {
40-
worklist = append(worklist, Splitter{Partition: R1, Symbol: c})
41-
}else {
42-
worklist = append(worklist, Splitter{Partition: R2, Symbol: c})
43-
}
34+
spR := splitter{partition: R, input: c}
35+
spR1 := splitter{partition: R1, input: c}
36+
spR2 := splitter{partition: R2, input: c}
37+
if !RefinedPartitionReplacedInWorklist(&worklist, spR, spR1, spR2) {
38+
ar1 := R1.StatesWithIncomingTransitionWith(c, &A)
39+
ar2 := R2.StatesWithIncomingTransitionWith(c, &A)
40+
if ar1.Size() < ar2.Size() {
41+
worklist = append(worklist, splitter{partition: R1, input: c})
42+
} else {
43+
worklist = append(worklist, splitter{partition: R2, input: c})
44+
}
4445
}
4546
}
4647
pi = append(pi, R1)
@@ -54,40 +55,40 @@ func HopcroftDFAMin(A DFA) DFA {
5455
var initialStateMinim State
5556
DeltaMin := make(map[State]map[int]State)
5657
for i, part := range pi {
57-
statesMinim.Add(i)
58-
AddTransitions(&DeltaMin, &A, pi, i)
59-
partitionHasFinalStates := false
60-
partitionHasInitialState := false
61-
for _, s := range part {
62-
if A.FinalStates.Includes(s) {
63-
partitionHasFinalStates = true
64-
}
65-
if(A.InitialState == s) {
66-
partitionHasInitialState = true
67-
continue
68-
}
58+
statesMinim.Add(i)
59+
addTransitions(&DeltaMin, &A, pi, i)
60+
partitionHasFinalStates := false
61+
partitionHasInitialState := false
62+
for _, s := range part {
63+
if A.FinalStates.Includes(s) {
64+
partitionHasFinalStates = true
6965
}
70-
if partitionHasFinalStates {
71-
finalStatesMinim.Add(i)
66+
if A.InitialState == s {
67+
partitionHasInitialState = true
68+
continue
7269
}
73-
if partitionHasInitialState {
74-
initialStateMinim = i
75-
}
70+
}
71+
if partitionHasFinalStates {
72+
finalStatesMinim.Add(i)
73+
}
74+
if partitionHasInitialState {
75+
initialStateMinim = i
76+
}
7677
}
7778
Ar := DFA{States: *statesMinim, Alphabet: A.Alphabet, InitialState: initialStateMinim, FinalStates: *finalStatesMinim, Delta: DeltaMin}
7879
return Ar
7980
}
8081

81-
func AddTransitions(DeltaMin *map[State]map[int]State, A *DFA, pi []Partition, i int) {
82+
func addTransitions(DeltaMin *map[State]map[int]State, A *DFA, pi []Partition, i int) {
8283
for _, c := range A.Alphabet {
8384
transitionState, ok := A.Delta[pi[i][0]][c]
8485
if !ok {
8586
continue
8687
}
8788
partitionOfState := getPartitionOfState(&pi, transitionState)
8889
if (*DeltaMin)[i] == nil {
89-
(*DeltaMin)[i] = map[int]State{c: partitionOfState}
90-
}else {
90+
(*DeltaMin)[i] = map[int]State{c: partitionOfState}
91+
} else {
9192
(*DeltaMin)[i][c] = partitionOfState
9293
}
9394
}
@@ -103,11 +104,13 @@ func getPartitionOfState(pi *[]Partition, q State) int {
103104
return 0
104105
}
105106

106-
func ReplaceInWorklistIfSplitterExists(worklist *[]Splitter, spR Splitter, spR1 Splitter, spR2 Splitter) bool {
107+
// RefinedPartitionReplacedInWorklist if the splitted partition was in the worklist it has to be replaced.
108+
// If did not exist in the worklist return false
109+
func RefinedPartitionReplacedInWorklist(worklist *[]splitter, spR splitter, spR1 splitter, spR2 splitter) bool {
107110
newWorklist := *worklist
108111
worklistSize := len(*worklist)
109-
for i, w := range (*worklist) {
110-
if w.Partition.Equals(spR.Partition) && w.Symbol == spR.Symbol {
112+
for i, w := range *worklist {
113+
if w.partition.Equals(spR.partition) && w.input == spR.input {
111114
newWorklist[i] = (*worklist)[worklistSize-1]
112115
newWorklist = append(newWorklist, spR1)
113116
newWorklist = append(newWorklist, spR2)
@@ -118,10 +121,10 @@ func ReplaceInWorklistIfSplitterExists(worklist *[]Splitter, spR Splitter, spR1
118121
return false
119122
}
120123

121-
func PickOneAndRemove(worklist *[]Splitter) Splitter {
124+
func pickOneAndRemove(worklist *[]splitter) splitter {
122125
worklistSize := len(*worklist)
123126
newWorklist := *worklist
124-
sp := (*worklist)[worklistSize - 1]
127+
sp := (*worklist)[worklistSize-1]
125128
newWorklist = (*worklist)[:worklistSize-1]
126129
*worklist = newWorklist
127130
return sp

0 commit comments

Comments
 (0)