Skip to content

Commit

Permalink
Partial support for comparing and sorting slices of complex numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
feyeleanor committed Jul 9, 2011
1 parent fb3483d commit ccfb2b9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
33 changes: 32 additions & 1 deletion complex128.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slices

import "fmt"
import "sort"

func C128List(n... complex128) *C128Slice {
return (*C128Slice)(&n)
Expand All @@ -25,10 +26,40 @@ func (s C128Slice) Subtract(i, j int) { s[i] -= s[j] }
func (s C128Slice) Multiply(i, j int) { s[i] *= s[j] }
func (s C128Slice) Divide(i, j int) { s[i] /= s[j] }

func (s C128Slice) Same(i, j int) bool { return s[i] == s[j] }
func (s C128Slice) Less(i, j int) bool { return real(s[i]) < real(s[j]) }
func (s C128Slice) AtLeast(i, j int) bool { return real(s[i]) <= real(s[j]) }
func (s C128Slice) Same(i, j int) bool { return real(s[i]) == real(s[j]) }
func (s C128Slice) AtMost(i, j int) bool { return real(s[i]) >= real(s[j]) }
func (s C128Slice) More(i, j int) bool { return real(s[i]) > real(s[j]) }

func (s C128Slice) ZeroLess(i int) bool { return 0 < real(s[i]) }
func (s C128Slice) ZeroAtLeast(i, j int) bool { return 0 <= real(s[j]) }
func (s C128Slice) ZeroSame(i int) bool { return 0 == real(s[i]) }
func (s C128Slice) ZeroAtMost(i, j int) bool { return 0 >= real(s[j]) }
func (s C128Slice) ZeroMore(i int) bool { return 0 > real(s[i]) }

func (s C128Slice) Sort() { sort.Sort(s) }

func (s *C128Slice) RestrictTo(i, j int) { *s = (*s)[i:j] }

func (s C128Slice) Compare(i, j int) (r int) {
switch x, y := real(s[i]), real(s[j]); {
case x < y: r = IS_LESS_THAN
case x > y: r = IS_GREATER_THAN
default: r = IS_SAME_AS
}
return
}

func (s C128Slice) ZeroCompare(i int) (r int) {
switch x := real(s[i]); {
case 0 < x: r = IS_LESS_THAN
case 0 > x: r = IS_GREATER_THAN
default: r = IS_SAME_AS
}
return
}

func (s *C128Slice) Cut(i, j int) {
a := *s
l := len(a)
Expand Down
12 changes: 9 additions & 3 deletions complex128_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ func TestC128SliceRplacd(t *testing.T) {

func TestC128SliceSetIntersection(t *testing.T) {
ConfirmSetIntersection := func(s, o, r *C128Slice) {
if x := s.SetIntersection(*o); !r.Equal(x) {
x := s.SetIntersection(*o)
x.Sort()
if !r.Equal(x) {
t.Fatalf("%v.SetIntersection(%v) should be %v but is %v", s, o, r, x)
}
}
Expand All @@ -382,7 +384,9 @@ func TestC128SliceSetIntersection(t *testing.T) {

func TestC128SliceSetUnion(t *testing.T) {
ConfirmSetUnion := func(s, o, r *C128Slice) {
if x := s.SetUnion(*o); !r.Equal(x) {
x := s.SetUnion(*o)
x.Sort()
if !r.Equal(x) {
t.Fatalf("%v.SetUnion(%v) should be %v but is %v", s, o, r, x)
}
}
Expand All @@ -395,7 +399,9 @@ func TestC128SliceSetUnion(t *testing.T) {

func TestC128SliceSetDifference(t *testing.T) {
ConfirmSetUnion := func(s, o, r *C128Slice) {
if x := s.SetDifference(*o); !r.Equal(x) {
x := s.SetDifference(*o)
x.Sort()
if !r.Equal(x) {
t.Fatalf("%v.SetUnion(%v) should be %v but is %v", s, o, r, x)
}
}
Expand Down
34 changes: 33 additions & 1 deletion complex64.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slices

import "fmt"
import "sort"

func C64List(n... complex64) *C64Slice {
return (*C64Slice)(&n)
Expand All @@ -25,9 +26,40 @@ func (s C64Slice) Subtract(i, j int) { s[i] -= s[j] }
func (s C64Slice) Multiply(i, j int) { s[i] *= s[j] }
func (s C64Slice) Divide(i, j int) { s[i] /= s[j] }

func (s C64Slice) Same(i, j int) bool { return s[i] == s[j] }
func (s C64Slice) Less(i, j int) bool { return real(s[i]) < real(s[j]) }
func (s C64Slice) AtLeast(i, j int) bool { return real(s[i]) <= real(s[j]) }
func (s C64Slice) Same(i, j int) bool { return real(s[i]) == real(s[j]) }
func (s C64Slice) AtMost(i, j int) bool { return real(s[i]) >= real(s[j]) }
func (s C64Slice) More(i, j int) bool { return real(s[i]) > real(s[j]) }

func (s C64Slice) ZeroLess(i int) bool { return 0 < real(s[i]) }
func (s C64Slice) ZeroAtLeast(i, j int) bool { return 0 <= real(s[j]) }
func (s C64Slice) ZeroSame(i int) bool { return 0 == real(s[i]) }
func (s C64Slice) ZeroAtMost(i, j int) bool { return 0 >= real(s[j]) }
func (s C64Slice) ZeroMore(i int) bool { return 0 > real(s[i]) }

func (s C64Slice) Sort() { sort.Sort(s) }

func (s *C64Slice) RestrictTo(i, j int) { *s = (*s)[i:j] }

func (s C64Slice) Compare(i, j int) (r int) {
switch x, y := real(s[i]), real(s[j]); {
case x < y: r = IS_LESS_THAN
case x > y: r = IS_GREATER_THAN
default: r = IS_SAME_AS
}
return
}

func (s C64Slice) ZeroCompare(i int) (r int) {
switch x := real(s[i]); {
case 0 < x: r = IS_LESS_THAN
case 0 > x: r = IS_GREATER_THAN
default: r = IS_SAME_AS
}
return
}

func (s *C64Slice) Cut(i, j int) {
a := *s
l := len(a)
Expand Down
12 changes: 9 additions & 3 deletions complex64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ func TestC64SliceRplacd(t *testing.T) {

func TestC64SliceSetIntersection(t *testing.T) {
ConfirmSetIntersection := func(s, o, r *C64Slice) {
if x := s.SetIntersection(*o); !r.Equal(x) {
x := s.SetIntersection(*o)
x.Sort()
if !r.Equal(x) {
t.Fatalf("%v.SetIntersection(%v) should be %v but is %v", s, o, r, x)
}
}
Expand All @@ -382,7 +384,9 @@ func TestC64SliceSetIntersection(t *testing.T) {

func TestC64SliceSetUnion(t *testing.T) {
ConfirmSetUnion := func(s, o, r *C64Slice) {
if x := s.SetUnion(*o); !r.Equal(x) {
x := s.SetUnion(*o)
x.Sort()
if !r.Equal(x) {
t.Fatalf("%v.SetUnion(%v) should be %v but is %v", s, o, r, x)
}
}
Expand All @@ -395,7 +399,9 @@ func TestC64SliceSetUnion(t *testing.T) {

func TestC64SliceSetDifference(t *testing.T) {
ConfirmSetUnion := func(s, o, r *C64Slice) {
if x := s.SetDifference(*o); !r.Equal(x) {
x := s.SetDifference(*o)
x.Sort()
if !r.Equal(x) {
t.Fatalf("%v.SetUnion(%v) should be %v but is %v", s, o, r, x)
}
}
Expand Down

0 comments on commit ccfb2b9

Please sign in to comment.