github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/test/bench/shootout/reverse-complement.go (about) 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 "bufio" 40 "os" 41 ) 42 43 const lineSize = 60 44 45 var complement = [256]uint8{ 46 'A': 'T', 'a': 'T', 47 'C': 'G', 'c': 'G', 48 'G': 'C', 'g': 'C', 49 'T': 'A', 't': 'A', 50 'U': 'A', 'u': 'A', 51 'M': 'K', 'm': 'K', 52 'R': 'Y', 'r': 'Y', 53 'W': 'W', 'w': 'W', 54 'S': 'S', 's': 'S', 55 'Y': 'R', 'y': 'R', 56 'K': 'M', 'k': 'M', 57 'V': 'B', 'v': 'B', 58 'H': 'D', 'h': 'D', 59 'D': 'H', 'd': 'H', 60 'B': 'V', 'b': 'V', 61 'N': 'N', 'n': 'N', 62 } 63 64 func main() { 65 in := bufio.NewReader(os.Stdin) 66 buf := make([]byte, 1024*1024) 67 line, err := in.ReadSlice('\n') 68 for err == nil { 69 os.Stdout.Write(line) 70 71 // Accumulate reversed complement in buf[w:] 72 nchar := 0 73 w := len(buf) 74 for { 75 line, err = in.ReadSlice('\n') 76 if err != nil || line[0] == '>' { 77 break 78 } 79 line = line[0 : len(line)-1] 80 nchar += len(line) 81 if len(line)+nchar/60+128 >= w { 82 nbuf := make([]byte, len(buf)*5) 83 copy(nbuf[len(nbuf)-len(buf):], buf) 84 w += len(nbuf) - len(buf) 85 buf = nbuf 86 } 87 88 // This loop is the bottleneck. 89 for _, c := range line { 90 w-- 91 buf[w] = complement[c] 92 } 93 } 94 95 // Copy down to beginning of buffer, inserting newlines. 96 // The loop left room for the newlines and 128 bytes of padding. 97 i := 0 98 for j := w; j < len(buf); j += 60 { 99 n := copy(buf[i:i+60], buf[j:]) 100 buf[i+n] = '\n' 101 i += n + 1 102 } 103 os.Stdout.Write(buf[0:i]) 104 } 105 }