Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / testsuite / go.test / test / bench / k-nucleotide-parallel.go
blob0234f33d113a0f28e988768ed97a394e7e4ed7e4
1 /*
2 Redistribution and use in source and binary forms, with or without
3 modification, are permitted provided that the following conditions are met:
5 * Redistributions of source code must retain the above copyright
6 notice, this list of conditions and the following disclaimer.
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.
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.
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.
30 /* The Computer Language Benchmarks Game
31 * http://shootout.alioth.debian.org/
33 * contributed by The Go Authors.
36 package main
38 import (
39 "bufio"
40 "bytes"
41 "fmt"
42 "io/ioutil"
43 "os"
44 "sort"
47 func count(data string, n int) map[string]int {
48 counts := make(map[string]int)
49 top := len(data) - n
50 for i := 0; i <= top; i++ {
51 s := data[i : i+n]
52 counts[s]++
54 return counts
57 func countOne(data string, s string) int {
58 return count(data, len(s))[s]
61 type kNuc struct {
62 name string
63 count int
66 type kNucArray []kNuc
68 func (kn kNucArray) Len() int { return len(kn) }
69 func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] }
70 func (kn kNucArray) Less(i, j int) bool {
71 if kn[i].count == kn[j].count {
72 return kn[i].name > kn[j].name // sort down
74 return kn[i].count > kn[j].count
77 func sortedArray(m map[string]int) kNucArray {
78 kn := make(kNucArray, len(m))
79 i := 0
80 for k, v := range m {
81 kn[i] = kNuc{k, v}
82 i++
84 sort.Sort(kn)
85 return kn
88 func printKnucs(a kNucArray) {
89 sum := 0
90 for _, kn := range a {
91 sum += kn.count
93 for _, kn := range a {
94 fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum))
96 fmt.Print("\n")
99 func main() {
100 in := bufio.NewReader(os.Stdin)
101 three := []byte(">THREE ")
102 for {
103 line, err := in.ReadSlice('\n')
104 if err != nil {
105 fmt.Fprintln(os.Stderr, "ReadLine err:", err)
106 os.Exit(2)
108 if line[0] == '>' && bytes.Equal(line[0:len(three)], three) {
109 break
112 data, err := ioutil.ReadAll(in)
113 if err != nil {
114 fmt.Fprintln(os.Stderr, "ReadAll err:", err)
115 os.Exit(2)
117 // delete the newlines and convert to upper case
118 j := 0
119 for i := 0; i < len(data); i++ {
120 if data[i] != '\n' {
121 data[j] = data[i] &^ ' ' // upper case
125 str := string(data[0:j])
127 var arr1, arr2 kNucArray
128 countsdone := make(chan bool)
129 go func() {
130 arr1 = sortedArray(count(str, 1))
131 countsdone <- true
133 go func() {
134 arr2 = sortedArray(count(str, 2))
135 countsdone <- true
138 interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"}
139 results := make([]chan string, len(interests))
140 for i, s := range interests {
141 ch := make(chan string)
142 results[i] = ch
143 go func(result chan string, ss string) {
144 result <- fmt.Sprintf("%d %s\n", countOne(str, ss), ss)
145 }(ch, s)
147 <-countsdone
148 <-countsdone
149 printKnucs(arr1)
150 printKnucs(arr2)
151 for _, rc := range results {
152 fmt.Print(<-rc)