1
0
mirror of https://github.com/golang/go synced 2024-11-22 05:04:40 -07:00

gofmt -r 'α[β:len(α)] -> α[β:]' -w test/bench

except chameneosredux which i know is being edited

require gofmt for test/bench

R=r
https://golang.org/cl/157110
This commit is contained in:
Russ Cox 2009-11-20 13:11:42 -08:00
parent 7ada6018a7
commit 19dae0799b
15 changed files with 257 additions and 262 deletions

View File

@ -607,7 +607,7 @@ def RelativePath(path, cwd):
# Check that gofmt run on the list of files does not change them # Check that gofmt run on the list of files does not change them
def CheckGofmt(ui, repo, files, just_warn=False): def CheckGofmt(ui, repo, files, just_warn=False):
files = [f for f in files if f.startswith('src/') and f.endswith('.go')] files = [f for f in files if (f.startswith('src/') or f.startswith('test/bench/')) and f.endswith('.go')]
if not files: if not files:
return return
cwd = os.getcwd() cwd = os.getcwd()
@ -1082,6 +1082,7 @@ def sync(ui, repo, **opts):
err = commands.postincoming(ui, repo, modheads, True, "tip") err = commands.postincoming(ui, repo, modheads, True, "tip")
if err: if err:
return err return err
commands.update(ui, repo)
sync_changes(ui, repo) sync_changes(ui, repo)
def sync_note(msg): def sync_note(msg):

View File

@ -44,12 +44,12 @@ import (
var n = flag.Int("n", 15, "depth") var n = flag.Int("n", 15, "depth")
type Node struct { type Node struct {
item int; item int;
left, right *Node; left, right *Node;
} }
type Arena struct { type Arena struct {
head *Node head *Node;
} }
var arena Arena var arena Arena
@ -67,9 +67,9 @@ func (n *Node) free() {
func (a *Arena) New(item int, left, right *Node) *Node { func (a *Arena) New(item int, left, right *Node) *Node {
if a.head == nil { if a.head == nil {
nodes := make([]Node, 3 << uint(*n)); nodes := make([]Node, 3<<uint(*n));
for i := 0; i < len(nodes)-1; i++ { for i := 0; i < len(nodes)-1; i++ {
nodes[i].left = &nodes[i+1]; nodes[i].left = &nodes[i+1]
} }
a.head = &nodes[0]; a.head = &nodes[0];
} }
@ -81,11 +81,11 @@ func (a *Arena) New(item int, left, right *Node) *Node {
return n; return n;
} }
func bottomUpTree(item, depth int) *Node { func bottomUpTree(item, depth int) *Node {
if depth <= 0 { if depth <= 0 {
return arena.New(item, nil, nil) return arena.New(item, nil, nil)
} }
return arena.New(item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1)) return arena.New(item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1));
} }
func (n *Node) itemCheck() int { func (n *Node) itemCheck() int {
@ -95,13 +95,13 @@ func (n *Node) itemCheck() int {
return n.item + n.left.itemCheck() - n.right.itemCheck(); return n.item + n.left.itemCheck() - n.right.itemCheck();
} }
const minDepth = 4; const minDepth = 4
func main() { func main() {
flag.Parse(); flag.Parse();
maxDepth := *n; maxDepth := *n;
if minDepth + 2 > *n { if minDepth+2 > *n {
maxDepth = minDepth + 2 maxDepth = minDepth + 2
} }
stretchDepth := maxDepth + 1; stretchDepth := maxDepth + 1;
@ -111,15 +111,15 @@ func main() {
longLivedTree := bottomUpTree(0, maxDepth); longLivedTree := bottomUpTree(0, maxDepth);
for depth := minDepth; depth <= maxDepth; depth+=2 { for depth := minDepth; depth <= maxDepth; depth += 2 {
iterations := 1 << uint(maxDepth - depth + minDepth); iterations := 1 << uint(maxDepth-depth+minDepth);
check = 0; check = 0;
for i := 1; i <= iterations; i++ { for i := 1; i <= iterations; i++ {
t := bottomUpTree(i,depth); t := bottomUpTree(i, depth);
check += t.itemCheck(); check += t.itemCheck();
t.free(); t.free();
t = bottomUpTree(-i,depth); t = bottomUpTree(-i, depth);
check += t.itemCheck(); check += t.itemCheck();
t.free(); t.free();
} }

View File

@ -44,15 +44,15 @@ import (
var n = flag.Int("n", 15, "depth") var n = flag.Int("n", 15, "depth")
type Node struct { type Node struct {
item int; item int;
left, right *Node; left, right *Node;
} }
func bottomUpTree(item, depth int) *Node { func bottomUpTree(item, depth int) *Node {
if depth <= 0 { if depth <= 0 {
return &Node{item: item} return &Node{item: item}
} }
return &Node{ item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1) } return &Node{item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1)};
} }
func (n *Node) itemCheck() int { func (n *Node) itemCheck() int {
@ -62,13 +62,13 @@ func (n *Node) itemCheck() int {
return n.item + n.left.itemCheck() - n.right.itemCheck(); return n.item + n.left.itemCheck() - n.right.itemCheck();
} }
const minDepth = 4; const minDepth = 4
func main() { func main() {
flag.Parse(); flag.Parse();
maxDepth := *n; maxDepth := *n;
if minDepth + 2 > *n { if minDepth+2 > *n {
maxDepth = minDepth + 2 maxDepth = minDepth + 2
} }
stretchDepth := maxDepth + 1; stretchDepth := maxDepth + 1;
@ -78,13 +78,13 @@ func main() {
longLivedTree := bottomUpTree(0, maxDepth); longLivedTree := bottomUpTree(0, maxDepth);
for depth := minDepth; depth <= maxDepth; depth+=2 { for depth := minDepth; depth <= maxDepth; depth += 2 {
iterations := 1 << uint(maxDepth - depth + minDepth); iterations := 1 << uint(maxDepth-depth+minDepth);
check = 0; check = 0;
for i := 1; i <= iterations; i++ { for i := 1; i <= iterations; i++ {
check += bottomUpTree(i,depth).itemCheck(); check += bottomUpTree(i, depth).itemCheck();
check += bottomUpTree(-i,depth).itemCheck(); check += bottomUpTree(-i, depth).itemCheck();
} }
fmt.Printf("%d\t trees of depth %d\t check: %d\n", iterations*2, depth, check); fmt.Printf("%d\t trees of depth %d\t check: %d\n", iterations*2, depth, check);
} }

View File

@ -46,7 +46,7 @@ var n = flag.Int("n", 7, "count")
func fannkuch(n int) int { func fannkuch(n int) int {
if n < 1 { if n < 1 {
return 0; return 0
} }
n1 := n - 1; n1 := n - 1;
@ -55,43 +55,45 @@ func fannkuch(n int) int {
count := make([]int, n); count := make([]int, n);
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
perm1[i] = i; // initial (trivial) permutation perm1[i] = i // initial (trivial) permutation
} }
r := n; r := n;
didpr := 0; didpr := 0;
flipsMax := 0; flipsMax := 0;
for { for {
if didpr < 30 { if didpr < 30 {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
fmt.Printf("%d", 1+perm1[i]); fmt.Printf("%d", 1+perm1[i])
} }
fmt.Printf("\n"); fmt.Printf("\n");
didpr++; didpr++;
} }
for ; r != 1; r-- { for ; r != 1; r-- {
count[r-1] = r; count[r-1] = r
} }
if perm1[0] != 0 && perm1[n1] != n1 { if perm1[0] != 0 && perm1[n1] != n1 {
flips := 0; flips := 0;
for i := 1; i < n; i++ { // perm = perm1 for i := 1; i < n; i++ { // perm = perm1
perm[i] = perm1[i]; perm[i] = perm1[i]
} }
k := perm1[0]; // cache perm[0] in k k := perm1[0]; // cache perm[0] in k
for { // k!=0 ==> k>0 for { // k!=0 ==> k>0
for i, j := 1, k-1; i < j; i, j = i+1, j-1 { for i, j := 1, k-1; i < j; i, j = i+1, j-1 {
perm[i], perm[j] = perm[j], perm[i]; perm[i], perm[j] = perm[j], perm[i]
} }
flips++; flips++;
// Now exchange k (caching perm[0]) and perm[k]... with care! // Now exchange k (caching perm[0]) and perm[k]... with care!
j := perm[k]; perm[k] = k; k = j; j := perm[k];
perm[k] = k;
k = j;
if k == 0 { if k == 0 {
break break
} }
} }
if flipsMax < flips { if flipsMax < flips {
flipsMax = flips; flipsMax = flips
} }
} }
@ -99,19 +101,19 @@ func fannkuch(n int) int {
// rotate down perm[0..r] by one // rotate down perm[0..r] by one
perm0 := perm1[0]; perm0 := perm1[0];
for i := 0; i < r; i++ { for i := 0; i < r; i++ {
perm1[i] = perm1[i+1]; perm1[i] = perm1[i+1]
} }
perm1[r] = perm0; perm1[r] = perm0;
count[r]--; count[r]--;
if count[r] > 0 { if count[r] > 0 {
break; break
} }
} }
if r == n { if r == n {
return flipsMax; return flipsMax
} }
} }
return 0 return 0;
} }
func main() { func main() {

View File

@ -91,7 +91,7 @@ func RepeatFasta(s []byte, count int) {
pos := 0; pos := 0;
s2 := make([]byte, len(s)+WIDTH); s2 := make([]byte, len(s)+WIDTH);
copy(s2, s); copy(s2, s);
copy(s2[len(s):len(s2)], s); copy(s2[len(s):], s);
for count > 0 { for count > 0 {
line := min(WIDTH, count); line := min(WIDTH, count);
out.Write(s2[pos : pos+line]); out.Write(s2[pos : pos+line]);

View File

@ -47,18 +47,18 @@ import (
var in *bufio.Reader var in *bufio.Reader
func count(data string, n int) map[string] int { func count(data string, n int) map[string]int {
counts := make(map[string] int); counts := make(map[string]int);
top := len(data) - n; top := len(data) - n;
for i := 0; i <= top; i++ { for i := 0; i <= top; i++ {
s := data[i:i+n]; s := data[i : i+n];
if k, ok := counts[s]; ok { if k, ok := counts[s]; ok {
counts[s] = k+1 counts[s] = k + 1
} else { } else {
counts[s] = 1 counts[s] = 1
} }
} }
return counts return counts;
} }
func countOne(data string, s string) int { func countOne(data string, s string) int {
@ -66,7 +66,7 @@ func countOne(data string, s string) int {
if i, ok := counts[s]; ok { if i, ok := counts[s]; ok {
return i return i
} }
return 0 return 0;
} }
@ -77,16 +77,16 @@ type kNuc struct {
type kNucArray []kNuc type kNucArray []kNuc
func (kn kNucArray) Len() int { return len(kn) } func (kn kNucArray) Len() int { return len(kn) }
func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] } func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] }
func (kn kNucArray) Less(i, j int) bool { func (kn kNucArray) Less(i, j int) bool {
if kn[i].count == kn[j].count { if kn[i].count == kn[j].count {
return kn[i].name > kn[j].name // sort down return kn[i].name > kn[j].name // sort down
} }
return kn[i].count > kn[j].count return kn[i].count > kn[j].count;
} }
func sortedArray(m map[string] int) kNucArray { func sortedArray(m map[string]int) kNucArray {
kn := make(kNucArray, len(m)); kn := make(kNucArray, len(m));
i := 0; i := 0;
for k, v := range m { for k, v := range m {
@ -98,14 +98,14 @@ func sortedArray(m map[string] int) kNucArray {
return kn; return kn;
} }
func print(m map[string] int) { func print(m map[string]int) {
a := sortedArray(m); a := sortedArray(m);
sum := 0; sum := 0;
for _, kn := range a { for _, kn := range a {
sum += kn.count; sum += kn.count
} }
for _, kn := range a { for _, kn := range a {
fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum)); fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum))
} }
} }
@ -119,7 +119,7 @@ func main() {
os.Exit(2); os.Exit(2);
} }
if line[0] == '>' && bytes.Equal(line[0:len(three)], three) { if line[0] == '>' && bytes.Equal(line[0:len(three)], three) {
break; break
} }
} }
data, err := io.ReadAll(in); data, err := io.ReadAll(in);
@ -132,7 +132,7 @@ func main() {
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
if data[i] != '\n' { if data[i] != '\n' {
data[j] = data[i] &^ ' '; // upper case data[j] = data[i] &^ ' '; // upper case
j++ j++;
} }
} }
str := string(data[0:j]); str := string(data[0:j]);
@ -145,6 +145,6 @@ func main() {
interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"}; interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"};
for _, s := range interests { for _, s := range interests {
fmt.Printf("%d %s\n", countOne(str, s), s); fmt.Printf("%d %s\n", countOne(str, s), s)
} }
} }

View File

@ -32,7 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
* *
* contributed by The Go Authors. * contributed by The Go Authors.
* Based on mandelbrot.c contributed by Greg Buchholz * Based on mandelbrot.c contributed by Greg Buchholz
*/ */
package main package main
@ -61,7 +61,7 @@ func main() {
fmt.Fprintf(out, "P4\n%d %d\n", w, h); fmt.Fprintf(out, "P4\n%d %d\n", w, h);
for y := 0; y < h; y++ { for y := 0; y < h; y++ {
for x := 0; x<w; x++ { for x := 0; x < w; x++ {
Zr, Zi, Tr, Ti := Zero, Zero, Zero, Zero; Zr, Zi, Tr, Ti := Zero, Zero, Zero, Zero;
Cr := (2*float64(x)/float64(w) - 1.5); Cr := (2*float64(x)/float64(w) - 1.5);
Ci := (2*float64(y)/float64(h) - 1.0); Ci := (2*float64(y)/float64(h) - 1.0);
@ -75,7 +75,7 @@ func main() {
byte_acc <<= 1; byte_acc <<= 1;
if Tr+Ti <= Limit*Limit { if Tr+Ti <= Limit*Limit {
byte_acc |= 0x01; byte_acc |= 0x01
} }
bit_num++; bit_num++;
@ -85,7 +85,7 @@ func main() {
byte_acc = 0; byte_acc = 0;
bit_num = 0; bit_num = 0;
} else if x == w-1 { } else if x == w-1 {
byte_acc <<= uint(8-w%8); byte_acc <<= uint(8 - w%8);
out.WriteByte(byte_acc); out.WriteByte(byte_acc);
byte_acc = 0; byte_acc = 0;
bit_num = 0; bit_num = 0;

View File

@ -48,7 +48,7 @@ func boolInt(b bool) int8 {
if b { if b {
return 1 return 1
} }
return 0 return 0;
} }
/* The board is a 50 cell hexagonal pattern. For . . . . . /* The board is a 50 cell hexagonal pattern. For . . . . .
@ -63,7 +63,7 @@ func boolInt(b bool) int8 {
* . . . . . * . . . . .
*/ */
var board uint64 = 0xFFFC000000000000 var board uint64 = 0xFFFC000000000000
/* The puzzle pieces must be specified by the path followed /* The puzzle pieces must be specified by the path followed
* from one end to the other along 12 hexagonal directions. * from one end to the other along 12 hexagonal directions.
@ -87,7 +87,7 @@ var board uint64 = 0xFFFC000000000000
*/ */
const ( const (
E = iota; E = iota;
ESE; ESE;
SE; SE;
S; S;
@ -102,17 +102,17 @@ const (
PIVOT; PIVOT;
) )
var piece_def = [10][4]int8 { var piece_def = [10][4]int8{
[4]int8{ E, E, E, SE}, [4]int8{E, E, E, SE},
[4]int8{ SE, E, NE, E}, [4]int8{SE, E, NE, E},
[4]int8{ E, E, SE, SW}, [4]int8{E, E, SE, SW},
[4]int8{ E, E, SW, SE}, [4]int8{E, E, SW, SE},
[4]int8{ SE, E, NE, S}, [4]int8{SE, E, NE, S},
[4]int8{ E, E, SW, E}, [4]int8{E, E, SW, E},
[4]int8{ E, SE, SE, NE}, [4]int8{E, SE, SE, NE},
[4]int8{ E, SE, SE, W}, [4]int8{E, SE, SE, W},
[4]int8{ E, SE, E, E}, [4]int8{E, SE, E, E},
[4]int8{ E, E, E, SW} [4]int8{E, E, E, SW},
} }
@ -127,20 +127,16 @@ var piece_def = [10][4]int8 {
* location to reduce the burden on the solve function. * location to reduce the burden on the solve function.
*/ */
var ( var (
pieces[10][50][12] uint64; pieces [10][50][12]uint64;
piece_counts[10][50] int; piece_counts [10][50]int;
next_cell[10][50][12] int8; next_cell [10][50][12]int8;
) )
/* Returns the direction rotated 60 degrees clockwise */ /* Returns the direction rotated 60 degrees clockwise */
func rotate(dir int8) int8 { func rotate(dir int8) int8 { return (dir + 2) % PIVOT }
return (dir + 2) % PIVOT;
}
/* Returns the direction flipped on the horizontal axis */ /* Returns the direction flipped on the horizontal axis */
func flip(dir int8) int8 { func flip(dir int8) int8 { return (PIVOT - dir) % PIVOT }
return (PIVOT - dir) % PIVOT;
}
/* Returns the new cell index from the specified cell in the /* Returns the new cell index from the specified cell in the
@ -151,60 +147,60 @@ func flip(dir int8) int8 {
func shift(cell, dir int8) int8 { func shift(cell, dir int8) int8 {
switch dir { switch dir {
case E: case E:
return cell + 1; return cell + 1
case ESE: case ESE:
if ((cell / 5) % 2) != 0 { if ((cell / 5) % 2) != 0 {
return cell + 7; return cell + 7
} else { } else {
return cell + 6; return cell + 6
} }
case SE: case SE:
if ((cell / 5) % 2) != 0 { if ((cell / 5) % 2) != 0 {
return cell + 6; return cell + 6
} else { } else {
return cell + 5; return cell + 5
} }
case S: case S:
return cell + 10; return cell + 10
case SW: case SW:
if ((cell / 5) % 2) != 0 { if ((cell / 5) % 2) != 0 {
return cell + 5; return cell + 5
} else { } else {
return cell + 4; return cell + 4
} }
case WSW: case WSW:
if ((cell / 5) % 2) != 0 { if ((cell / 5) % 2) != 0 {
return cell + 4; return cell + 4
} else { } else {
return cell + 3; return cell + 3
} }
case W: case W:
return cell - 1; return cell - 1
case WNW: case WNW:
if ((cell / 5) % 2) != 0{ if ((cell / 5) % 2) != 0 {
return cell - 6; return cell - 6
} else { } else {
return cell - 7; return cell - 7
} }
case NW: case NW:
if ((cell / 5) % 2) != 0{ if ((cell / 5) % 2) != 0 {
return cell - 5; return cell - 5
} else { } else {
return cell - 6; return cell - 6
} }
case N: case N:
return cell - 10; return cell - 10
case NE: case NE:
if ((cell / 5) % 2) != 0{ if ((cell / 5) % 2) != 0 {
return cell - 4; return cell - 4
} else { } else {
return cell - 5; return cell - 5
} }
case ENE: case ENE:
if ((cell / 5) % 2) != 0{ if ((cell / 5) % 2) != 0 {
return cell - 3; return cell - 3
} else { } else {
return cell - 4; return cell - 4
} }
} }
return cell; return cell;
@ -215,32 +211,32 @@ func shift(cell, dir int8) int8 {
* location or not. * location or not.
*/ */
func out_of_bounds(cell, dir int8) bool { func out_of_bounds(cell, dir int8) bool {
switch(dir) { switch dir {
case E: case E:
return cell % 5 == 4; return cell%5 == 4
case ESE: case ESE:
i := cell % 10; i := cell % 10;
return i == 4 || i == 8 || i == 9 || cell >= 45; return i == 4 || i == 8 || i == 9 || cell >= 45;
case SE: case SE:
return cell % 10 == 9 || cell >= 45; return cell%10 == 9 || cell >= 45
case S: case S:
return cell >= 40; return cell >= 40
case SW: case SW:
return cell % 10 == 0 || cell >= 45; return cell%10 == 0 || cell >= 45
case WSW: case WSW:
i := cell % 10; i := cell % 10;
return i == 0 || i == 1 || i == 5 || cell >= 45; return i == 0 || i == 1 || i == 5 || cell >= 45;
case W: case W:
return cell % 5 == 0; return cell%5 == 0
case WNW: case WNW:
i := cell % 10; i := cell % 10;
return i == 0 || i == 1 || i == 5 || cell < 5; return i == 0 || i == 1 || i == 5 || cell < 5;
case NW: case NW:
return cell % 10 == 0 || cell < 5; return cell%10 == 0 || cell < 5
case N: case N:
return cell < 10; return cell < 10
case NE: case NE:
return cell % 10 == 9 || cell < 5; return cell%10 == 9 || cell < 5
case ENE: case ENE:
i := cell % 10; i := cell % 10;
return i == 4 || i == 8 || i == 9 || cell < 5; return i == 4 || i == 8 || i == 9 || cell < 5;
@ -251,14 +247,14 @@ func out_of_bounds(cell, dir int8) bool {
/* Rotate a piece 60 degrees clockwise */ /* Rotate a piece 60 degrees clockwise */
func rotate_piece(piece int) { func rotate_piece(piece int) {
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
piece_def[piece][i] = rotate(piece_def[piece][i]); piece_def[piece][i] = rotate(piece_def[piece][i])
} }
} }
/* Flip a piece along the horizontal axis */ /* Flip a piece along the horizontal axis */
func flip_piece(piece int) { func flip_piece(piece int) {
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
piece_def[piece][i] = flip(piece_def[piece][i]); piece_def[piece][i] = flip(piece_def[piece][i])
} }
} }
@ -266,16 +262,16 @@ func flip_piece(piece int) {
func calc_cell_indices(cell []int8, piece int, index int8) { func calc_cell_indices(cell []int8, piece int, index int8) {
cell[0] = index; cell[0] = index;
for i := 1; i < 5; i++ { for i := 1; i < 5; i++ {
cell[i] = shift(cell[i-1], piece_def[piece][i-1]); cell[i] = shift(cell[i-1], piece_def[piece][i-1])
} }
} }
/* Convenience function to quickly calculate if a piece fits on the board */ /* Convenience function to quickly calculate if a piece fits on the board */
func cells_fit_on_board(cell []int8, piece int) bool { func cells_fit_on_board(cell []int8, piece int) bool {
return !out_of_bounds(cell[0], piece_def[piece][0]) && return !out_of_bounds(cell[0], piece_def[piece][0]) &&
!out_of_bounds(cell[1], piece_def[piece][1]) && !out_of_bounds(cell[1], piece_def[piece][1]) &&
!out_of_bounds(cell[2], piece_def[piece][2]) && !out_of_bounds(cell[2], piece_def[piece][2]) &&
!out_of_bounds(cell[3], piece_def[piece][3]); !out_of_bounds(cell[3], piece_def[piece][3])
} }
/* Returns the lowest index of the cells of a piece. /* Returns the lowest index of the cells of a piece.
@ -299,9 +295,9 @@ func minimum_of_cells(cell []int8) int8 {
func first_empty_cell(cell []int8, minimum int8) int8 { func first_empty_cell(cell []int8, minimum int8) int8 {
first_empty := minimum; first_empty := minimum;
for first_empty == cell[0] || first_empty == cell[1] || for first_empty == cell[0] || first_empty == cell[1] ||
first_empty == cell[2] || first_empty == cell[3] || first_empty == cell[2] || first_empty == cell[3] ||
first_empty == cell[4] { first_empty == cell[4] {
first_empty++; first_empty++
} }
return first_empty; return first_empty;
} }
@ -312,7 +308,7 @@ func first_empty_cell(cell []int8, minimum int8) int8 {
func bitmask_from_cells(cell []int8) uint64 { func bitmask_from_cells(cell []int8) uint64 {
var piece_mask uint64; var piece_mask uint64;
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
piece_mask |= 1 << uint(cell[i]); piece_mask |= 1 << uint(cell[i])
} }
return piece_mask; return piece_mask;
} }
@ -332,26 +328,26 @@ func record_piece(piece int, minimum int8, first_empty int8, piece_mask uint64)
*/ */
func fill_contiguous_space(board []int8, index int8) { func fill_contiguous_space(board []int8, index int8) {
if board[index] == 1 { if board[index] == 1 {
return; return
} }
board[index] = 1; board[index] = 1;
if !out_of_bounds(index, E) { if !out_of_bounds(index, E) {
fill_contiguous_space(board, shift(index, E)); fill_contiguous_space(board, shift(index, E))
} }
if !out_of_bounds(index, SE) { if !out_of_bounds(index, SE) {
fill_contiguous_space(board, shift(index, SE)); fill_contiguous_space(board, shift(index, SE))
} }
if !out_of_bounds(index, SW) { if !out_of_bounds(index, SW) {
fill_contiguous_space(board, shift(index, SW)); fill_contiguous_space(board, shift(index, SW))
} }
if !out_of_bounds(index, W) { if !out_of_bounds(index, W) {
fill_contiguous_space(board, shift(index, W)); fill_contiguous_space(board, shift(index, W))
} }
if !out_of_bounds(index, NW) { if !out_of_bounds(index, NW) {
fill_contiguous_space(board, shift(index, NW)); fill_contiguous_space(board, shift(index, NW))
} }
if !out_of_bounds(index, NE) { if !out_of_bounds(index, NE) {
fill_contiguous_space(board, shift(index, NE)); fill_contiguous_space(board, shift(index, NE))
} }
} }
@ -366,22 +362,22 @@ func has_island(cell []int8, piece int) bool {
temp_board := make([]int8, 50); temp_board := make([]int8, 50);
var i int; var i int;
for i = 0; i < 5; i++ { for i = 0; i < 5; i++ {
temp_board[cell[i]] = 1; temp_board[cell[i]] = 1
} }
i = 49; i = 49;
for temp_board[i] == 1 { for temp_board[i] == 1 {
i--; i--
} }
fill_contiguous_space(temp_board, int8(i)); fill_contiguous_space(temp_board, int8(i));
c := 0; c := 0;
for i = 0; i < 50; i++ { for i = 0; i < 50; i++ {
if temp_board[i] == 0 { if temp_board[i] == 0 {
c++; c++
} }
} }
if c == 0 || (c == 5 && piece == 8) || (c == 40 && piece == 8) || if c == 0 || (c == 5 && piece == 8) || (c == 40 && piece == 8) ||
(c % 5 == 0 && piece == 0) { (c%5 == 0 && piece == 0) {
return false; return false
} }
return true; return true;
} }
@ -422,23 +418,24 @@ func calc_pieces() {
} }
/* Calculate all 32 possible states for a 5-bit row and all rows that will /* Calculate all 32 possible states for a 5-bit row and all rows that will
* create islands that follow any of the 32 possible rows. These pre- * create islands that follow any of the 32 possible rows. These pre-
* calculated 5-bit rows will be used to find islands in a partially solved * calculated 5-bit rows will be used to find islands in a partially solved
* board in the solve function. * board in the solve function.
*/ */
const ( const (
ROW_MASK = 0x1F; ROW_MASK = 0x1F;
TRIPLE_MASK = 0x7FFF; TRIPLE_MASK = 0x7FFF;
) )
var ( var (
all_rows = [32]int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, all_rows = [32]int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
bad_even_rows [32][32]int8; };
bad_odd_rows [32][32]int8; bad_even_rows [32][32]int8;
bad_even_triple [32768]int8; bad_odd_rows [32][32]int8;
bad_odd_triple [32768]int8; bad_even_triple [32768]int8;
bad_odd_triple [32768]int8;
) )
func rows_bad(row1, row2 int8, even bool) int8 { func rows_bad(row1, row2 int8, even bool) int8 {
@ -446,34 +443,34 @@ func rows_bad(row1, row2 int8, even bool) int8 {
var row2_shift int8; var row2_shift int8;
/* Test for blockages at same index and shifted index */ /* Test for blockages at same index and shifted index */
if even { if even {
row2_shift = ((row2 << 1) & ROW_MASK) | 0x01; row2_shift = ((row2 << 1) & ROW_MASK) | 0x01
} else { } else {
row2_shift = (row2 >> 1) | 0x10; row2_shift = (row2 >> 1) | 0x10
} }
block := ((row1 ^ row2) & row2) & ((row1 ^ row2_shift) & row2_shift); block := ((row1 ^ row2) & row2) & ((row1 ^ row2_shift) & row2_shift);
/* Test for groups of 0's */ /* Test for groups of 0's */
in_zeroes := false; in_zeroes := false;
group_okay := false; group_okay := false;
for i := uint8(0); i < 5; i++ { for i := uint8(0); i < 5; i++ {
if row1 & (1 << i) != 0 { if row1&(1<<i) != 0 {
if in_zeroes { if in_zeroes {
if !group_okay { if !group_okay {
return 1; return 1
} }
in_zeroes = false; in_zeroes = false;
group_okay = false; group_okay = false;
} }
} else { } else {
if !in_zeroes { if !in_zeroes {
in_zeroes = true; in_zeroes = true
} }
if (block & (1 << i)) == 0 { if (block & (1 << i)) == 0 {
group_okay = true; group_okay = true
} }
} }
} }
if in_zeroes { if in_zeroes {
return boolInt(!group_okay); return boolInt(!group_okay)
} }
return 0; return 0;
} }
@ -490,9 +487,9 @@ func triple_is_okay(row1, row2, row3 int, even bool) bool {
* row3: 011?? 00110 ????? ????? * row3: 011?? 00110 ????? ?????
*/ */
return ((row1 == 0x03) && (row2 == 0x0B) && ((row3 & 0x1C) == 0x0C)) || return ((row1 == 0x03) && (row2 == 0x0B) && ((row3 & 0x1C) == 0x0C)) ||
((row1 == 0x01) && (row2 == 0x05) && (row3 == 0x06)) || ((row1 == 0x01) && (row2 == 0x05) && (row3 == 0x06)) ||
((row1 == 0x19) && (row2 == 0x11)) || ((row1 == 0x19) && (row2 == 0x11)) ||
((row1 == 0x15) && (row2 == 0x11)); ((row1 == 0x15) && (row2 == 0x11))
} }
/* There are two cases: /* There are two cases:
* row1: 10011 10101 * row1: 10011 10101
@ -500,7 +497,7 @@ func triple_is_okay(row1, row2, row3 int, even bool) bool {
* row3: ????? ????? * row3: ????? ?????
*/ */
return ((row1 == 0x13) && (row2 == 0x11)) || return ((row1 == 0x13) && (row2 == 0x11)) ||
((row1 == 0x15) && (row2 == 0x11)); ((row1 == 0x15) && (row2 == 0x11));
} }
func calc_rows() { func calc_rows() {
@ -515,18 +512,18 @@ func calc_rows() {
for row3 := 0; row3 < 32; row3++ { for row3 := 0; row3 < 32; row3++ {
result1 := bad_even_rows[row1][row2]; result1 := bad_even_rows[row1][row2];
result2 := bad_odd_rows[row2][row3]; result2 := bad_odd_rows[row2][row3];
if result1==0 && result2!=0 && triple_is_okay(row1, row2, row3, true) { if result1 == 0 && result2 != 0 && triple_is_okay(row1, row2, row3, true) {
bad_even_triple[row1+(row2*32)+(row3*1024)] = 0; bad_even_triple[row1+(row2*32)+(row3*1024)] = 0
} else { } else {
bad_even_triple[row1+(row2*32)+(row3*1024)] = boolInt(result1!=0 || result2!=0); bad_even_triple[row1+(row2*32)+(row3*1024)] = boolInt(result1 != 0 || result2 != 0)
} }
result1 = bad_odd_rows[row1][row2]; result1 = bad_odd_rows[row1][row2];
result2 = bad_even_rows[row2][row3]; result2 = bad_even_rows[row2][row3];
if result1==0 && result2!=0 && triple_is_okay(row1, row2, row3, false) { if result1 == 0 && result2 != 0 && triple_is_okay(row1, row2, row3, false) {
bad_odd_triple[row1+(row2*32)+(row3*1024)] = 0; bad_odd_triple[row1+(row2*32)+(row3*1024)] = 0
} else { } else {
bad_odd_triple[row1+(row2*32)+(row3*1024)] = boolInt(result1!=0 || result2!=0); bad_odd_triple[row1+(row2*32)+(row3*1024)] = boolInt(result1 != 0 || result2 != 0)
} }
} }
} }
@ -534,17 +531,16 @@ func calc_rows() {
} }
/* Calculate islands while solving the board. /* Calculate islands while solving the board.
*/ */
func boardHasIslands(cell int8) int8 { func boardHasIslands(cell int8) int8 {
/* Too low on board, don't bother checking */ /* Too low on board, don't bother checking */
if cell >= 40 { if cell >= 40 {
return 0; return 0
} }
current_triple := (board >> uint((cell / 5) * 5)) & TRIPLE_MASK; current_triple := (board >> uint((cell/5)*5)) & TRIPLE_MASK;
if (cell / 5) % 2 != 0 { if (cell/5)%2 != 0 {
return bad_odd_triple[current_triple]; return bad_odd_triple[current_triple]
} }
return bad_even_triple[current_triple]; return bad_even_triple[current_triple];
} }
@ -557,18 +553,18 @@ func boardHasIslands(cell int8) int8 {
* array if a solution is found. * array if a solution is found.
*/ */
var ( var (
avail uint16 = 0x03FF; avail uint16 = 0x03FF;
sol_nums [10]int8; sol_nums [10]int8;
sol_masks [10]uint64; sol_masks [10]uint64;
solutions [2100][50]int8; solutions [2100][50]int8;
solution_count = 0; solution_count = 0;
) )
func record_solution() { func record_solution() {
for sol_no := 0; sol_no < 10; sol_no++ { for sol_no := 0; sol_no < 10; sol_no++ {
sol_mask := sol_masks[sol_no]; sol_mask := sol_masks[sol_no];
for index := 0; index < 50; index++ { for index := 0; index < 50; index++ {
if sol_mask & 1 == 1 { if sol_mask&1 == 1 {
solutions[solution_count][index] = sol_nums[sol_no]; solutions[solution_count][index] = sol_nums[sol_no];
/* Board rotated 180 degrees is a solution too! */ /* Board rotated 180 degrees is a solution too! */
solutions[solution_count+1][49-index] = sol_nums[sol_no]; solutions[solution_count+1][49-index] = sol_nums[sol_no];
@ -581,23 +577,23 @@ func record_solution() {
func solve(depth, cell int8) { func solve(depth, cell int8) {
if solution_count >= *max_solutions { if solution_count >= *max_solutions {
return; return
} }
for board & (1 << uint(cell)) != 0 { for board&(1<<uint(cell)) != 0 {
cell++; cell++
} }
for piece := int8(0); piece < 10; piece++ { for piece := int8(0); piece < 10; piece++ {
var piece_no_mask uint16 = 1 << uint(piece); var piece_no_mask uint16 = 1 << uint(piece);
if avail & piece_no_mask == 0 { if avail&piece_no_mask == 0 {
continue; continue
} }
avail ^= piece_no_mask; avail ^= piece_no_mask;
max_rots := piece_counts[piece][cell]; max_rots := piece_counts[piece][cell];
piece_mask := pieces[piece][cell]; piece_mask := pieces[piece][cell];
for rotation := 0; rotation < max_rots; rotation++ { for rotation := 0; rotation < max_rots; rotation++ {
if board & piece_mask[rotation] == 0 { if board&piece_mask[rotation] == 0 {
sol_nums[depth] = piece; sol_nums[depth] = piece;
sol_masks[depth] = piece_mask[rotation]; sol_masks[depth] = piece_mask[rotation];
if depth == 9 { if depth == 9 {
@ -608,7 +604,7 @@ func solve(depth, cell int8) {
} }
board |= piece_mask[rotation]; board |= piece_mask[rotation];
if boardHasIslands(next_cell[piece][cell][rotation]) == 0 { if boardHasIslands(next_cell[piece][cell][rotation]) == 0 {
solve(depth + 1, next_cell[piece][cell][rotation]); solve(depth+1, next_cell[piece][cell][rotation])
} }
board ^= piece_mask[rotation]; board ^= piece_mask[rotation];
} }
@ -621,8 +617,8 @@ func solve(depth, cell int8) {
func pretty(b *[50]int8) { func pretty(b *[50]int8) {
for i := 0; i < 50; i += 10 { for i := 0; i < 50; i += 10 {
fmt.Printf("%c %c %c %c %c \n %c %c %c %c %c \n", b[i]+'0', b[i+1]+'0', fmt.Printf("%c %c %c %c %c \n %c %c %c %c %c \n", b[i]+'0', b[i+1]+'0',
b[i+2]+'0', b[i+3]+'0', b[i+4]+'0', b[i+5]+'0', b[i+6]+'0', b[i+2]+'0', b[i+3]+'0', b[i+4]+'0', b[i+5]+'0', b[i+6]+'0',
b[i+7]+'0', b[i+8]+'0', b[i+9]+'0'); b[i+7]+'0', b[i+8]+'0', b[i+9]+'0')
} }
fmt.Printf("\n"); fmt.Printf("\n");
} }
@ -639,7 +635,7 @@ func smallest_largest() (smallest, largest *[50]int8) {
continue continue
} }
if c < s { if c < s {
smallest = candidate; smallest = candidate
} }
break; break;
} }
@ -649,7 +645,7 @@ func smallest_largest() (smallest, largest *[50]int8) {
continue continue
} }
if c > s { if c > s {
largest = candidate; largest = candidate
} }
break; break;
} }

View File

@ -45,15 +45,15 @@ import (
var n = flag.Int("n", 1000, "number of iterations") var n = flag.Int("n", 1000, "number of iterations")
type Body struct { type Body struct {
x, y, z, vx, vy, vz, mass float64 x, y, z, vx, vy, vz, mass float64;
} }
const ( const (
solarMass = 4 * math.Pi * math.Pi; solarMass = 4 * math.Pi * math.Pi;
daysPerYear = 365.24; daysPerYear = 365.24;
) )
func (b *Body) offsetMomentum(px, py, pz float64){ func (b *Body) offsetMomentum(px, py, pz float64) {
b.vx = -px / solarMass; b.vx = -px / solarMass;
b.vy = -py / solarMass; b.vy = -py / solarMass;
b.vz = -pz / solarMass; b.vz = -pz / solarMass;
@ -82,7 +82,7 @@ func (sys System) energy() float64 {
for i, body := range sys { for i, body := range sys {
e += 0.5 * body.mass * e += 0.5 * body.mass *
(body.vx*body.vx + body.vy*body.vy + body.vz*body.vz); (body.vx*body.vx + body.vy*body.vy + body.vz*body.vz);
for j := i+1; j < len(sys); j++ { for j := i + 1; j < len(sys); j++ {
body2 := sys[j]; body2 := sys[j];
dx := body.x - body2.x; dx := body.x - body2.x;
dy := body.y - body2.y; dy := body.y - body2.y;
@ -96,7 +96,7 @@ func (sys System) energy() float64 {
func (sys System) advance(dt float64) { func (sys System) advance(dt float64) {
for i, body := range sys { for i, body := range sys {
for j := i+1; j < len(sys); j++ { for j := i + 1; j < len(sys); j++ {
body2 := sys[j]; body2 := sys[j];
dx := body.x - body2.x; dx := body.x - body2.x;
dy := body.y - body2.y; dy := body.y - body2.y;
@ -124,7 +124,7 @@ func (sys System) advance(dt float64) {
} }
var ( var (
jupiter = Body { jupiter = Body{
x: 4.84143144246472090e+00, x: 4.84143144246472090e+00,
y: -1.16032004402742839e+00, y: -1.16032004402742839e+00,
z: -1.03622044471123109e-01, z: -1.03622044471123109e-01,
@ -133,7 +133,7 @@ var (
vz: -6.90460016972063023e-05 * daysPerYear, vz: -6.90460016972063023e-05 * daysPerYear,
mass: 9.54791938424326609e-04 * solarMass, mass: 9.54791938424326609e-04 * solarMass,
}; };
saturn = Body { saturn = Body{
x: 8.34336671824457987e+00, x: 8.34336671824457987e+00,
y: 4.12479856412430479e+00, y: 4.12479856412430479e+00,
z: -4.03523417114321381e-01, z: -4.03523417114321381e-01,
@ -142,7 +142,7 @@ var (
vz: 2.30417297573763929e-05 * daysPerYear, vz: 2.30417297573763929e-05 * daysPerYear,
mass: 2.85885980666130812e-04 * solarMass, mass: 2.85885980666130812e-04 * solarMass,
}; };
uranus = Body { uranus = Body{
x: 1.28943695621391310e+01, x: 1.28943695621391310e+01,
y: -1.51111514016986312e+01, y: -1.51111514016986312e+01,
z: -2.23307578892655734e-01, z: -2.23307578892655734e-01,
@ -151,7 +151,7 @@ var (
vz: -2.96589568540237556e-05 * daysPerYear, vz: -2.96589568540237556e-05 * daysPerYear,
mass: 4.36624404335156298e-05 * solarMass, mass: 4.36624404335156298e-05 * solarMass,
}; };
neptune = Body { neptune = Body{
x: 1.53796971148509165e+01, x: 1.53796971148509165e+01,
y: -2.59193146099879641e+01, y: -2.59193146099879641e+01,
z: 1.79258772950371181e-01, z: 1.79258772950371181e-01,
@ -160,9 +160,9 @@ var (
vz: -9.51592254519715870e-05 * daysPerYear, vz: -9.51592254519715870e-05 * daysPerYear,
mass: 5.15138902046611451e-05 * solarMass, mass: 5.15138902046611451e-05 * solarMass,
}; };
sun = Body { sun = Body{
mass: solarMass mass: solarMass,
} };
) )
func main() { func main() {

View File

@ -43,20 +43,20 @@ import (
"fmt"; "fmt";
) )
var n = flag.Int("n", 27, "number of digits"); var n = flag.Int("n", 27, "number of digits")
var silent = flag.Bool("s", false, "don't print result"); var silent = flag.Bool("s", false, "don't print result")
var ( var (
tmp1 *bignum.Integer; tmp1 *bignum.Integer;
tmp2 *bignum.Integer; tmp2 *bignum.Integer;
numer = bignum.Int(1); numer = bignum.Int(1);
accum = bignum.Int(0); accum = bignum.Int(0);
denom = bignum.Int(1); denom = bignum.Int(1);
) )
func extract_digit() int64 { func extract_digit() int64 {
if numer.Cmp(accum) > 0 { if numer.Cmp(accum) > 0 {
return -1; return -1
} }
// Compute (numer * 3 + accum) / denom // Compute (numer * 3 + accum) / denom
@ -70,7 +70,7 @@ func extract_digit() int64 {
// ... is normalized, then the two divisions have the same result. // ... is normalized, then the two divisions have the same result.
if tmp2.Cmp(denom) >= 0 { if tmp2.Cmp(denom) >= 0 {
return -1; return -1
} }
return tmp1.Value(); return tmp1.Value();
@ -94,7 +94,7 @@ func eliminate_digit(d int64) {
func printf(s string, arg ...) { func printf(s string, arg ...) {
if !*silent { if !*silent {
fmt.Printf(s, arg); fmt.Printf(s, arg)
} }
} }
@ -110,20 +110,20 @@ func main() {
d = extract_digit(); d = extract_digit();
} }
printf("%c", d + '0'); printf("%c", d+'0');
i++; i++;
m = i%10; m = i % 10;
if m == 0 { if m == 0 {
printf("\t:%d\n", i); printf("\t:%d\n", i)
} }
if i >= *n { if i >= *n {
break; break
} }
eliminate_digit(d); eliminate_digit(d);
} }
if m > 0 { if m > 0 {
printf("%s\t:%d\n", " "[m : 10], *n); printf("%s\t:%d\n", " "[m:10], *n)
} }
} }

View File

@ -43,7 +43,7 @@ import (
"strings"; "strings";
) )
var variants = []string { var variants = []string{
"agggtaaa|tttaccct", "agggtaaa|tttaccct",
"[cgt]gggtaaa|tttaccc[acg]", "[cgt]gggtaaa|tttaccc[acg]",
"a[act]ggtaaa|tttacc[agt]t", "a[act]ggtaaa|tttacc[agt]t",
@ -56,21 +56,21 @@ var variants = []string {
} }
type Subst struct { type Subst struct {
pat, repl string pat, repl string;
} }
var substs = [] Subst { var substs = []Subst{
Subst {"B", "(c|g|t)"}, Subst{"B", "(c|g|t)"},
Subst {"D", "(a|g|t)"}, Subst{"D", "(a|g|t)"},
Subst {"H", "(a|c|t)"}, Subst{"H", "(a|c|t)"},
Subst {"K", "(g|t)"}, Subst{"K", "(g|t)"},
Subst {"M", "(a|c)"}, Subst{"M", "(a|c)"},
Subst {"N", "(a|c|g|t)"}, Subst{"N", "(a|c|g|t)"},
Subst {"R", "(a|g)"}, Subst{"R", "(a|g)"},
Subst {"S", "(c|g)"}, Subst{"S", "(c|g)"},
Subst {"V", "(a|c|g)"}, Subst{"V", "(a|c|g)"},
Subst {"W", "(a|t)"}, Subst{"W", "(a|t)"},
Subst {"Y", "(c|t)"}, Subst{"Y", "(c|t)"},
} }
func countMatches(pat string, bytes []byte) int { func countMatches(pat string, bytes []byte) int {
@ -79,10 +79,10 @@ func countMatches(pat string, bytes []byte) int {
for { for {
e := re.Execute(bytes); e := re.Execute(bytes);
if len(e) == 0 { if len(e) == 0 {
break; break
} }
n++; n++;
bytes = bytes[e[1]:len(bytes)]; bytes = bytes[e[1]:];
} }
return n; return n;
} }
@ -98,10 +98,10 @@ func main() {
bytes = regexp.MustCompile("(>[^\n]+)?\n").ReplaceAll(bytes, []byte{}); bytes = regexp.MustCompile("(>[^\n]+)?\n").ReplaceAll(bytes, []byte{});
clen := len(bytes); clen := len(bytes);
for _, s := range variants { for _, s := range variants {
fmt.Printf("%s %d\n", s, countMatches(s, bytes)); fmt.Printf("%s %d\n", s, countMatches(s, bytes))
} }
for _, sub := range substs { for _, sub := range substs {
bytes = regexp.MustCompile(sub.pat).ReplaceAll(bytes, strings.Bytes(sub.repl)); bytes = regexp.MustCompile(sub.pat).ReplaceAll(bytes, strings.Bytes(sub.repl))
} }
fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, len(bytes)); fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, len(bytes));
} }

View File

@ -103,13 +103,13 @@ func main() {
for { for {
line, err = in.ReadSlice('\n'); line, err = in.ReadSlice('\n');
if err != nil || line[0] == '>' { if err != nil || line[0] == '>' {
break; break
} }
line = line[0:len(line)-1]; line = line[0 : len(line)-1];
nchar += len(line); nchar += len(line);
if len(line)+nchar/60+128 >= w { if len(line)+nchar/60+128 >= w {
nbuf := make([]byte, len(buf)*5); nbuf := make([]byte, len(buf)*5);
copy(nbuf[len(nbuf)-len(buf):len(nbuf)], buf); copy(nbuf[len(nbuf)-len(buf):], buf);
w += len(nbuf) - len(buf); w += len(nbuf) - len(buf);
buf = nbuf; buf = nbuf;
} }
@ -123,9 +123,9 @@ func main() {
// The loop left room for the newlines and 128 bytes of padding. // The loop left room for the newlines and 128 bytes of padding.
i := 0; i := 0;
for j := w; j < len(buf); j += 60 { for j := w; j < len(buf); j += 60 {
n := copy(buf[i:i+60], buf[j:len(buf)]); n := copy(buf[i:i+60], buf[j:]);
buf[i+n] = '\n'; buf[i+n] = '\n';
i += n+1; i += n + 1;
} }
os.Stdout.Write(buf[0:i]); os.Stdout.Write(buf[0:i]);
} }

View File

@ -46,9 +46,7 @@ import (
var n = flag.Int("n", 2000, "count") var n = flag.Int("n", 2000, "count")
var nCPU = flag.Int("ncpu", 4, "number of cpus") var nCPU = flag.Int("ncpu", 4, "number of cpus")
func evalA(i, j int) float64 { func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }
return 1 / float64(((i + j)*(i + j + 1)/2 + i + 1));
}
type Vec []float64 type Vec []float64
@ -56,7 +54,7 @@ func (v Vec) Times(i, n int, u Vec, c chan int) {
for ; i < n; i++ { for ; i < n; i++ {
v[i] = 0; v[i] = 0;
for j := 0; j < len(u); j++ { for j := 0; j < len(u); j++ {
v[i] += evalA(i, j)*u[j]; v[i] += evalA(i, j) * u[j]
} }
} }
c <- 1; c <- 1;
@ -66,7 +64,7 @@ func (v Vec) TimesTransp(i, n int, u Vec, c chan int) {
for ; i < n; i++ { for ; i < n; i++ {
v[i] = 0; v[i] = 0;
for j := 0; j < len(u); j++ { for j := 0; j < len(u); j++ {
v[i] += evalA(j, i)*u[j]; v[i] += evalA(j, i) * u[j]
} }
} }
c <- 1; c <- 1;
@ -82,11 +80,11 @@ func (v Vec) ATimesTransp(u Vec) {
x := make(Vec, len(u)); x := make(Vec, len(u));
c := make(chan int, *nCPU); c := make(chan int, *nCPU);
for i := 0; i < *nCPU; i++ { for i := 0; i < *nCPU; i++ {
go x.Times(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, u, c); go x.Times(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, u, c)
} }
wait(c); wait(c);
for i := 0; i < *nCPU; i++ { for i := 0; i < *nCPU; i++ {
go v.TimesTransp(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, x, c); go v.TimesTransp(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, x, c)
} }
wait(c); wait(c);
} }
@ -97,7 +95,7 @@ func main() {
N := *n; N := *n;
u := make(Vec, N); u := make(Vec, N);
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
u[i] = 1; u[i] = 1
} }
v := make(Vec, N); v := make(Vec, N);
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
@ -106,8 +104,8 @@ func main() {
} }
var vBv, vv float64; var vBv, vv float64;
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
vBv += u[i]*v[i]; vBv += u[i] * v[i];
vv += v[i]*v[i]; vv += v[i] * v[i];
} }
fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv)); fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv));
} }

View File

@ -44,9 +44,7 @@ import (
var n = flag.Int("n", 2000, "count") var n = flag.Int("n", 2000, "count")
func evalA(i, j int) float64 { func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }
return 1 / float64(((i + j)*(i + j + 1)/2+ i + 1));
}
type Vec []float64 type Vec []float64
@ -54,7 +52,7 @@ func (v Vec) Times(u Vec) {
for i := 0; i < len(v); i++ { for i := 0; i < len(v); i++ {
v[i] = 0; v[i] = 0;
for j := 0; j < len(u); j++ { for j := 0; j < len(u); j++ {
v[i] += evalA(i, j)*u[j]; v[i] += evalA(i, j) * u[j]
} }
} }
} }
@ -63,7 +61,7 @@ func (v Vec) TimesTransp(u Vec) {
for i := 0; i < len(v); i++ { for i := 0; i < len(v); i++ {
v[i] = 0; v[i] = 0;
for j := 0; j < len(u); j++ { for j := 0; j < len(u); j++ {
v[i] += evalA(j, i)*u[j]; v[i] += evalA(j, i) * u[j]
} }
} }
} }
@ -79,7 +77,7 @@ func main() {
N := *n; N := *n;
u := make(Vec, N); u := make(Vec, N);
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
u[i] = 1; u[i] = 1
} }
v := make(Vec, N); v := make(Vec, N);
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
@ -88,8 +86,8 @@ func main() {
} }
var vBv, vv float64; var vBv, vv float64;
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
vBv += u[i]*v[i]; vBv += u[i] * v[i];
vv += v[i]*v[i]; vv += v[i] * v[i];
} }
fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv)); fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv));
} }

View File

@ -52,7 +52,7 @@ func f(i int, in <-chan int, out chan<- int) {
fmt.Printf("%d\n", i); fmt.Printf("%d\n", i);
os.Exit(0); os.Exit(0);
} }
out <- n - 1 out <- n-1;
} }
} }