Russ Cox | bcd2187 | 2022-04-11 13:08:42 -0400 | [diff] [blame] | 1 | //go:build ignore |
Andrew Gerrand | e3933f0 | 2016-01-14 15:01:18 +1100 | [diff] [blame] | 2 | // +build ignore |
| 3 | |
Russ Cox | a87cfef | 2016-01-06 12:26:18 -0500 | [diff] [blame] | 4 | /* |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | |
| 8 | * Redistributions of source code must retain the above copyright |
| 9 | notice, this list of conditions and the following disclaimer. |
| 10 | |
| 11 | * Redistributions in binary form must reproduce the above copyright |
| 12 | notice, this list of conditions and the following disclaimer in the |
| 13 | documentation and/or other materials provided with the distribution. |
| 14 | |
| 15 | * Neither the name of "The Computer Language Benchmarks Game" nor the |
| 16 | name of "The Computer Language Shootout Benchmarks" nor the names of |
| 17 | its contributors may be used to endorse or promote products derived |
| 18 | from this software without specific prior written permission. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 24 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | /* The Computer Language Benchmarks Game |
| 34 | * http://shootout.alioth.debian.org/ |
| 35 | * |
| 36 | * contributed by The Go Authors. |
| 37 | */ |
| 38 | |
| 39 | package main |
| 40 | |
| 41 | import ( |
| 42 | "bufio" |
| 43 | "os" |
| 44 | ) |
| 45 | |
| 46 | const lineSize = 60 |
| 47 | |
| 48 | var complement = [256]uint8{ |
| 49 | 'A': 'T', 'a': 'T', |
| 50 | 'C': 'G', 'c': 'G', |
| 51 | 'G': 'C', 'g': 'C', |
| 52 | 'T': 'A', 't': 'A', |
| 53 | 'U': 'A', 'u': 'A', |
| 54 | 'M': 'K', 'm': 'K', |
| 55 | 'R': 'Y', 'r': 'Y', |
| 56 | 'W': 'W', 'w': 'W', |
| 57 | 'S': 'S', 's': 'S', |
| 58 | 'Y': 'R', 'y': 'R', |
| 59 | 'K': 'M', 'k': 'M', |
| 60 | 'V': 'B', 'v': 'B', |
| 61 | 'H': 'D', 'h': 'D', |
| 62 | 'D': 'H', 'd': 'H', |
| 63 | 'B': 'V', 'b': 'V', |
| 64 | 'N': 'N', 'n': 'N', |
| 65 | } |
| 66 | |
| 67 | func main() { |
| 68 | in := bufio.NewReader(os.Stdin) |
| 69 | buf := make([]byte, 1024*1024) |
| 70 | line, err := in.ReadSlice('\n') |
| 71 | for err == nil { |
| 72 | os.Stdout.Write(line) |
| 73 | |
| 74 | // Accumulate reversed complement in buf[w:] |
| 75 | nchar := 0 |
| 76 | w := len(buf) |
| 77 | for { |
| 78 | line, err = in.ReadSlice('\n') |
| 79 | if err != nil || line[0] == '>' { |
| 80 | break |
| 81 | } |
| 82 | line = line[0 : len(line)-1] |
| 83 | nchar += len(line) |
| 84 | if len(line)+nchar/60+128 >= w { |
| 85 | nbuf := make([]byte, len(buf)*5) |
| 86 | copy(nbuf[len(nbuf)-len(buf):], buf) |
| 87 | w += len(nbuf) - len(buf) |
| 88 | buf = nbuf |
| 89 | } |
| 90 | |
| 91 | // This loop is the bottleneck. |
| 92 | for _, c := range line { |
| 93 | w-- |
| 94 | buf[w] = complement[c] |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Copy down to beginning of buffer, inserting newlines. |
| 99 | // The loop left room for the newlines and 128 bytes of padding. |
| 100 | i := 0 |
| 101 | for j := w; j < len(buf); j += 60 { |
| 102 | n := copy(buf[i:i+60], buf[j:]) |
| 103 | buf[i+n] = '\n' |
| 104 | i += n + 1 |
| 105 | } |
| 106 | os.Stdout.Write(buf[0:i]) |
| 107 | } |
| 108 | } |