~ubuntu-branches/ubuntu/utopic/gccgo-go/utopic

« back to all changes in this revision

Viewing changes to test/bench/shootout/regex-dna-parallel.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-27 09:18:55 UTC
  • Revision ID: package-import@ubuntu.com-20140127091855-zxfshmykfsyyw4b2
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Redistribution and use in source and binary forms, with or without
 
3
modification, are permitted provided that the following conditions are met:
 
4
 
 
5
    * Redistributions of source code must retain the above copyright
 
6
    notice, this list of conditions and the following disclaimer.
 
7
 
 
8
    * Redistributions in binary form must reproduce the above copyright
 
9
    notice, this list of conditions and the following disclaimer in the
 
10
    documentation and/or other materials provided with the distribution.
 
11
 
 
12
    * Neither the name of "The Computer Language Benchmarks Game" nor the
 
13
    name of "The Computer Language Shootout Benchmarks" nor the names of
 
14
    its contributors may be used to endorse or promote products derived
 
15
    from this software without specific prior written permission.
 
16
 
 
17
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
18
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
19
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
20
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
21
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
22
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
23
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
24
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
25
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
26
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
27
POSSIBILITY OF SUCH DAMAGE.
 
28
*/
 
29
 
 
30
/* The Computer Language Benchmarks Game
 
31
 * http://shootout.alioth.debian.org/
 
32
 *
 
33
 * contributed by The Go Authors.
 
34
 */
 
35
 
 
36
package main
 
37
 
 
38
import (
 
39
        "fmt"
 
40
        "io/ioutil"
 
41
        "os"
 
42
        "regexp"
 
43
        "runtime"
 
44
)
 
45
 
 
46
var variants = []string{
 
47
        "agggtaaa|tttaccct",
 
48
        "[cgt]gggtaaa|tttaccc[acg]",
 
49
        "a[act]ggtaaa|tttacc[agt]t",
 
50
        "ag[act]gtaaa|tttac[agt]ct",
 
51
        "agg[act]taaa|ttta[agt]cct",
 
52
        "aggg[acg]aaa|ttt[cgt]ccct",
 
53
        "agggt[cgt]aa|tt[acg]accct",
 
54
        "agggta[cgt]a|t[acg]taccct",
 
55
        "agggtaa[cgt]|[acg]ttaccct",
 
56
}
 
57
 
 
58
type Subst struct {
 
59
        pat, repl string
 
60
}
 
61
 
 
62
var substs = []Subst{
 
63
        Subst{"B", "(c|g|t)"},
 
64
        Subst{"D", "(a|g|t)"},
 
65
        Subst{"H", "(a|c|t)"},
 
66
        Subst{"K", "(g|t)"},
 
67
        Subst{"M", "(a|c)"},
 
68
        Subst{"N", "(a|c|g|t)"},
 
69
        Subst{"R", "(a|g)"},
 
70
        Subst{"S", "(c|g)"},
 
71
        Subst{"V", "(a|c|g)"},
 
72
        Subst{"W", "(a|t)"},
 
73
        Subst{"Y", "(c|t)"},
 
74
}
 
75
 
 
76
func countMatches(pat string, bytes []byte) int {
 
77
        re := regexp.MustCompile(pat)
 
78
        n := 0
 
79
        for {
 
80
                e := re.FindIndex(bytes)
 
81
                if e == nil {
 
82
                        break
 
83
                }
 
84
                n++
 
85
                bytes = bytes[e[1]:]
 
86
        }
 
87
        return n
 
88
}
 
89
 
 
90
func main() {
 
91
        runtime.GOMAXPROCS(4)
 
92
        bytes, err := ioutil.ReadAll(os.Stdin)
 
93
        if err != nil {
 
94
                fmt.Fprintf(os.Stderr, "can't read input: %s\n", err)
 
95
                os.Exit(2)
 
96
        }
 
97
        ilen := len(bytes)
 
98
        // Delete the comment lines and newlines
 
99
        bytes = regexp.MustCompile("(>[^\n]+)?\n").ReplaceAll(bytes, []byte{})
 
100
        clen := len(bytes)
 
101
 
 
102
        mresults := make([]chan int, len(variants))
 
103
        for i, s := range variants {
 
104
                ch := make(chan int)
 
105
                mresults[i] = ch
 
106
                go func(ss string) {
 
107
                        ch <- countMatches(ss, bytes)
 
108
                }(s)
 
109
        }
 
110
 
 
111
        lenresult := make(chan int)
 
112
        bb := bytes
 
113
        go func() {
 
114
                for _, sub := range substs {
 
115
                        bb = regexp.MustCompile(sub.pat).ReplaceAll(bb, []byte(sub.repl))
 
116
                }
 
117
                lenresult <- len(bb)
 
118
        }()
 
119
 
 
120
        for i, s := range variants {
 
121
                fmt.Printf("%s %d\n", s, <-mresults[i])
 
122
        }
 
123
        fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, <-lenresult)
 
124
}