Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
4b590bf9
Commit
4b590bf9
authored
Jan 16, 2009
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
casify a few more
R=rsc DELTA=72 (0 added, 9 deleted, 63 changed) OCL=22918 CL=22941
parent
393df079
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
68 deletions
+59
-68
sort.go
src/lib/sort.go
+16
-16
sort_test.go
src/lib/sort_test.go
+37
-46
testing.go
src/lib/testing.go
+6
-6
No files found.
src/lib/sort.go
View file @
4b590bf9
...
...
@@ -18,7 +18,7 @@ func min(a, b int) int {
}
// Insertion sort
func
I
nsertionSort
(
data
SortInterface
,
a
,
b
int
)
{
func
i
nsertionSort
(
data
SortInterface
,
a
,
b
int
)
{
for
i
:=
a
+
1
;
i
<
b
;
i
++
{
for
j
:=
i
;
j
>
a
&&
data
.
Less
(
j
,
j
-
1
);
j
--
{
data
.
Swap
(
j
,
j
-
1
);
...
...
@@ -30,7 +30,7 @@ func InsertionSort(data SortInterface, a, b int) {
// ``Engineering a Sort Function,'' SP&E November 1993.
// Move the median of the three values data[a], data[b], data[c] into data[a].
func
M
edianOfThree
(
data
SortInterface
,
a
,
b
,
c
int
)
{
func
m
edianOfThree
(
data
SortInterface
,
a
,
b
,
c
int
)
{
m0
:=
b
;
m1
:=
a
;
m2
:=
c
;
...
...
@@ -41,22 +41,22 @@ func MedianOfThree(data SortInterface, a, b, c int) {
// now data[m0] <= data[m1] <= data[m2]
}
func
S
wapRange
(
data
SortInterface
,
a
,
b
,
n
int
)
{
func
s
wapRange
(
data
SortInterface
,
a
,
b
,
n
int
)
{
for
i
:=
0
;
i
<
n
;
i
++
{
data
.
Swap
(
a
+
i
,
b
+
i
);
}
}
func
Pivot
(
data
SortInterface
,
lo
,
hi
int
)
(
midlo
,
midhi
int
)
{
func
do
Pivot
(
data
SortInterface
,
lo
,
hi
int
)
(
midlo
,
midhi
int
)
{
m
:=
(
lo
+
hi
)
/
2
;
if
hi
-
lo
>
40
{
// Tukey's ``Ninther,'' median of three medians of three.
s
:=
(
hi
-
lo
)
/
8
;
M
edianOfThree
(
data
,
lo
,
lo
+
s
,
lo
+
2
*
s
);
M
edianOfThree
(
data
,
m
,
m
-
s
,
m
+
s
);
M
edianOfThree
(
data
,
hi
-
1
,
hi
-
1
-
s
,
hi
-
1
-
2
*
s
);
m
edianOfThree
(
data
,
lo
,
lo
+
s
,
lo
+
2
*
s
);
m
edianOfThree
(
data
,
m
,
m
-
s
,
m
+
s
);
m
edianOfThree
(
data
,
hi
-
1
,
hi
-
1
-
s
,
hi
-
1
-
2
*
s
);
}
M
edianOfThree
(
data
,
lo
,
m
,
hi
-
1
);
m
edianOfThree
(
data
,
lo
,
m
,
hi
-
1
);
// Invariants are:
// data[lo] = pivot (set up by ChoosePivot)
...
...
@@ -98,26 +98,26 @@ func Pivot(data SortInterface, lo, hi int) (midlo, midhi int) {
}
n
:=
min
(
b
-
a
,
a
-
lo
);
S
wapRange
(
data
,
lo
,
b
-
n
,
n
);
s
wapRange
(
data
,
lo
,
b
-
n
,
n
);
n
=
min
(
hi
-
d
,
d
-
c
);
S
wapRange
(
data
,
c
,
hi
-
n
,
n
);
s
wapRange
(
data
,
c
,
hi
-
n
,
n
);
return
lo
+
b
-
a
,
hi
-
(
d
-
c
);
}
func
Quicks
ort
(
data
SortInterface
,
a
,
b
int
)
{
func
quickS
ort
(
data
SortInterface
,
a
,
b
int
)
{
if
b
-
a
>
7
{
mlo
,
mhi
:=
Pivot
(
data
,
a
,
b
);
Quicks
ort
(
data
,
a
,
mlo
);
Quicks
ort
(
data
,
mhi
,
b
);
mlo
,
mhi
:=
do
Pivot
(
data
,
a
,
b
);
quickS
ort
(
data
,
a
,
mlo
);
quickS
ort
(
data
,
mhi
,
b
);
}
else
if
b
-
a
>
1
{
I
nsertionSort
(
data
,
a
,
b
);
i
nsertionSort
(
data
,
a
,
b
);
}
}
export
func
Sort
(
data
SortInterface
)
{
Quicks
ort
(
data
,
0
,
data
.
Len
());
quickS
ort
(
data
,
0
,
data
.
Len
());
}
...
...
src/lib/sort_test.go
View file @
4b590bf9
...
...
@@ -11,8 +11,6 @@ import (
"testing"
;
)
func
BentleyMcIlroyTests
();
var
ints
=
[
...
]
int
{
74
,
59
,
238
,
-
784
,
9845
,
959
,
905
,
0
,
0
,
42
,
7586
,
-
5467984
,
7586
}
var
floats
=
[
...
]
float
{
74.3
,
59.0
,
238.2
,
-
784.0
,
2.3
,
9845.768
,
-
959.7485
,
905
,
7.8
,
7.8
}
...
...
@@ -75,7 +73,7 @@ export func TestSortStrings(t *testing.T) {
}
}
export
func
TestSortLargeRandom
(
t
*
testing
.
T
)
{
export
func
TestSortLarge
_
Random
(
t
*
testing
.
T
)
{
data
:=
make
([]
int
,
1000000
);
for
i
:=
0
;
i
<
len
(
data
);
i
++
{
data
[
i
]
=
rand
.
rand
()
%
100
;
...
...
@@ -90,25 +88,25 @@ export func TestSortLargeRandom(t *testing.T) {
}
const
(
Sawtooth
=
iota
;
Rand
;
Stagger
;
Plateau
;
Shuffle
;
NDist
;
_
Sawtooth
=
iota
;
_
Rand
;
_
Stagger
;
_
Plateau
;
_
Shuffle
;
_
NDist
;
)
const
(
Copy
=
iota
;
Reverse
;
ReverseFirstHalf
;
ReverseSecondHalf
;
Sorted
;
Dither
;
NMode
;
)
;
type
T
estingData
struct
{
_
Copy
=
iota
;
_
Reverse
;
_
ReverseFirstHalf
;
_
ReverseSecondHalf
;
_
Sorted
;
_
Dither
;
_
NMode
;
)
type
t
estingData
struct
{
desc
string
;
t
*
testing
.
T
;
data
[]
int
;
...
...
@@ -116,9 +114,9 @@ type TestingData struct {
nswap
int
;
}
func
(
d
*
T
estingData
)
Len
()
int
{
return
len
(
d
.
data
);
}
func
(
d
*
T
estingData
)
Less
(
i
,
j
int
)
bool
{
return
d
.
data
[
i
]
<
d
.
data
[
j
];
}
func
(
d
*
T
estingData
)
Swap
(
i
,
j
int
)
{
func
(
d
*
t
estingData
)
Len
()
int
{
return
len
(
d
.
data
);
}
func
(
d
*
t
estingData
)
Less
(
i
,
j
int
)
bool
{
return
d
.
data
[
i
]
<
d
.
data
[
j
];
}
func
(
d
*
t
estingData
)
Swap
(
i
,
j
int
)
{
if
d
.
nswap
>=
d
.
maxswap
{
d
.
t
.
Errorf
(
"%s: used %d swaps sorting array of %d"
,
d
.
desc
,
d
.
nswap
,
len
(
d
.
data
));
d
.
t
.
FailNow
();
...
...
@@ -127,7 +125,7 @@ func (d *TestingData) Swap(i, j int) {
d
.
data
[
i
],
d
.
data
[
j
]
=
d
.
data
[
j
],
d
.
data
[
i
];
}
func
L
g
(
n
int
)
int
{
func
l
g
(
n
int
)
int
{
i
:=
0
;
for
1
<<
uint
(
i
)
<
n
{
i
++
;
...
...
@@ -135,13 +133,6 @@ func Lg(n int) int {
return
i
;
}
func
Min
(
a
,
b
int
)
int
{
if
a
<
b
{
return
a
;
}
return
b
;
}
export
func
TestBentleyMcIlroy
(
t
*
testing
.
T
)
{
sizes
:=
[]
int
{
100
,
1023
,
1024
,
1025
};
dists
:=
[]
string
{
"sawtooth"
,
"rand"
,
"stagger"
,
"plateau"
,
"shuffle"
};
...
...
@@ -150,21 +141,21 @@ export func TestBentleyMcIlroy(t *testing.T) {
for
ni
:=
0
;
ni
<
len
(
sizes
);
ni
++
{
n
:=
sizes
[
ni
];
for
m
:=
1
;
m
<
2
*
n
;
m
*=
2
{
for
dist
:=
0
;
dist
<
NDist
;
dist
++
{
for
dist
:=
0
;
dist
<
_
NDist
;
dist
++
{
j
:=
0
;
k
:=
1
;
data
:=
tmp1
[
0
:
n
];
for
i
:=
0
;
i
<
n
;
i
++
{
switch
dist
{
case
Sawtooth
:
case
_
Sawtooth
:
data
[
i
]
=
i
%
m
;
case
Rand
:
case
_
Rand
:
data
[
i
]
=
rand
.
rand
()
%
m
;
case
Stagger
:
case
_
Stagger
:
data
[
i
]
=
(
i
*
m
+
i
)
%
n
;
case
Plateau
:
data
[
i
]
=
M
in
(
i
,
m
);
case
Shuffle
:
case
_
Plateau
:
data
[
i
]
=
m
in
(
i
,
m
);
case
_
Shuffle
:
if
rand
.
rand
()
%
m
!=
0
{
j
+=
2
;
data
[
i
]
=
j
;
...
...
@@ -176,45 +167,45 @@ export func TestBentleyMcIlroy(t *testing.T) {
}
mdata
:=
tmp2
[
0
:
n
];
for
mode
:=
0
;
mode
<
NMode
;
mode
++
{
for
mode
:=
0
;
mode
<
_
NMode
;
mode
++
{
switch
mode
{
case
Copy
:
case
_
Copy
:
for
i
:=
0
;
i
<
n
;
i
++
{
mdata
[
i
]
=
data
[
i
];
}
case
Reverse
:
case
_
Reverse
:
for
i
:=
0
;
i
<
n
;
i
++
{
mdata
[
i
]
=
data
[
n
-
i
-
1
];
}
case
ReverseFirstHalf
:
case
_
ReverseFirstHalf
:
for
i
:=
0
;
i
<
n
/
2
;
i
++
{
mdata
[
i
]
=
data
[
n
/
2
-
i
-
1
];
}
for
i
:=
n
/
2
;
i
<
n
;
i
++
{
mdata
[
i
]
=
data
[
i
];
}
case
ReverseSecondHalf
:
case
_
ReverseSecondHalf
:
for
i
:=
0
;
i
<
n
/
2
;
i
++
{
mdata
[
i
]
=
data
[
i
];
}
for
i
:=
n
/
2
;
i
<
n
;
i
++
{
mdata
[
i
]
=
data
[
n
-
(
i
-
n
/
2
)
-
1
];
}
case
Sorted
:
case
_
Sorted
:
for
i
:=
0
;
i
<
n
;
i
++
{
mdata
[
i
]
=
data
[
i
];
}
// sort.SortInts is known to be correct
// because mode Sort runs after mode Copy.
// because mode Sort runs after mode
_
Copy.
sort
.
SortInts
(
mdata
);
case
Dither
:
case
_
Dither
:
for
i
:=
0
;
i
<
n
;
i
++
{
mdata
[
i
]
=
data
[
i
]
+
i
%
5
;
}
}
desc
:=
fmt
.
Sprintf
(
"n=%d m=%d dist=%s mode=%s"
,
n
,
m
,
dists
[
dist
],
modes
[
mode
]);
d
:=
&
TestingData
{
desc
,
t
,
mdata
[
0
:
n
],
n
*
L
g
(
n
)
*
12
/
10
,
0
};
d
:=
&
testingData
{
desc
,
t
,
mdata
[
0
:
n
],
n
*
l
g
(
n
)
*
12
/
10
,
0
};
sort
.
Sort
(
d
);
// If we were testing C qsort, we'd have to make a copy
...
...
src/lib/testing.go
View file @
4b590bf9
...
...
@@ -12,10 +12,10 @@ import (
var
chatty
=
flag
.
Bool
(
"chatty"
,
false
,
"chatty"
)
// Insert tabs after newlines - but not the last one
func
T
abify
(
s
string
)
string
{
func
t
abify
(
s
string
)
string
{
for
i
:=
0
;
i
<
len
(
s
)
-
1
;
i
++
{
// -1 because if last char is newline, don't bother
if
s
[
i
]
==
'\n'
{
return
s
[
0
:
i
+
1
]
+
"
\t
"
+
T
abify
(
s
[
i
+
1
:
len
(
s
)]);
return
s
[
0
:
i
+
1
]
+
"
\t
"
+
t
abify
(
s
[
i
+
1
:
len
(
s
)]);
}
}
return
s
...
...
@@ -38,11 +38,11 @@ func (t *T) FailNow() {
}
func
(
t
*
T
)
Log
(
args
...
)
{
t
.
errors
+=
"
\t
"
+
T
abify
(
fmt
.
Sprintln
(
args
));
t
.
errors
+=
"
\t
"
+
t
abify
(
fmt
.
Sprintln
(
args
));
}
func
(
t
*
T
)
Logf
(
format
string
,
args
...
)
{
t
.
errors
+=
T
abify
(
fmt
.
Sprintf
(
"
\t
"
+
format
,
args
));
t
.
errors
+=
t
abify
(
fmt
.
Sprintf
(
"
\t
"
+
format
,
args
));
l
:=
len
(
t
.
errors
);
if
l
>
0
&&
t
.
errors
[
l
-
1
]
!=
'\n'
{
t
.
errors
+=
"
\n
"
...
...
@@ -74,7 +74,7 @@ export type Test struct {
f
*
(
*
T
);
}
func
T
Runner
(
t
*
T
,
test
*
Test
)
{
func
t
Runner
(
t
*
T
,
test
*
Test
)
{
test
.
f
(
t
);
t
.
ch
<-
t
;
}
...
...
@@ -91,7 +91,7 @@ export func Main(tests []Test) {
}
t
:=
new
(
T
);
t
.
ch
=
make
(
chan
*
T
);
go
T
Runner
(
t
,
&
tests
[
i
]);
go
t
Runner
(
t
,
&
tests
[
i
]);
<-
t
.
ch
;
if
t
.
failed
{
println
(
"--- FAIL:"
,
tests
[
i
]
.
name
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment