-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(qrcode): use kmp to speed up rule3 pattern calculating. (#53)
- Loading branch information
Showing
7 changed files
with
302 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package qrcode | ||
|
||
import "github.com/yeqown/go-qrcode/v2/matrix" | ||
|
||
// kmp is variant of kmp algorithm to count the pattern been in | ||
// src slice. | ||
// TODO(@yeqown): implement this in generic way. | ||
func kmp(src, pattern []matrix.State, next []int) (count int) { | ||
if next == nil { | ||
next = kmpGetNext(pattern) | ||
} | ||
slen := len(src) | ||
plen := len(pattern) | ||
i := 0 // cursor of src | ||
j := 0 // cursor of pattern | ||
|
||
loop: | ||
for i < slen && j < plen { | ||
if j == -1 || src[i] == pattern[j] { | ||
i++ | ||
j++ | ||
} else { | ||
j = next[j] | ||
} | ||
} | ||
|
||
if j == plen { | ||
if i-j >= 0 { | ||
count++ | ||
} | ||
|
||
// reset cursor to count duplicate pattern. | ||
// such as: "aaaa" and "aa", we want 3 rather than 2. | ||
i -= plen - 1 | ||
j = 0 | ||
|
||
goto loop | ||
} | ||
|
||
return count | ||
} | ||
|
||
func kmpGetNext(pattern []matrix.State) []int { | ||
fail := make([]int, len(pattern)) | ||
fail[0] = -1 | ||
|
||
j := 0 | ||
k := -1 | ||
|
||
for j < len(pattern)-1 { | ||
if k == -1 || pattern[j] == pattern[k] { | ||
k++ | ||
j++ | ||
fail[j] = k | ||
} else { | ||
k = fail[k] | ||
} | ||
} | ||
|
||
return fail | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package qrcode | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/yeqown/go-qrcode/v2/matrix" | ||
) | ||
|
||
func Test_kmp(t *testing.T) { | ||
src := binaryToStateSlice("11001010010111001010010101011100") | ||
|
||
type args struct { | ||
src []matrix.State | ||
pattern []matrix.State | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantCount int | ||
}{ | ||
{ | ||
name: "test1", | ||
args: args{ | ||
src: src, | ||
pattern: binaryToStateSlice("0101"), | ||
}, | ||
wantCount: 6, | ||
}, | ||
{ | ||
name: "test1", | ||
args: args{ | ||
src: src, | ||
pattern: binaryToStateSlice("1001010"), | ||
}, | ||
wantCount: 3, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.wantCount, kmp(tt.args.src, tt.args.pattern, nil), "kmp(%v, %v)", tt.args.src, tt.args.pattern) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.