-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit drops all the non-generic, reflection-based code, including all previous subpackages. It also renames several functions, and moves the float code into a new subpackage. This represents a large change to the API and thus is a new major version.
- Loading branch information
1 parent
e5b8c98
commit d9207db
Showing
32 changed files
with
804 additions
and
1,284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
zermelo/floats | ||
============== | ||
This subpackage handles sorting float slices. | ||
|
||
Example | ||
------- | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/shawnsmithdev/zermelo/floats" | ||
"something" | ||
) | ||
|
||
func main() { | ||
var x []float64 | ||
x = something.GetFloatData() | ||
floats.SortFloats(x) | ||
} | ||
``` | ||
|
||
Sorter Example | ||
-------------- | ||
todo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package floats | ||
|
||
import ( | ||
"github.com/shawnsmithdev/zermelo/v2" | ||
"golang.org/x/exp/constraints" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
// cutoffSorter is a Sorter with adjustable comparison sort cutoff, for testing. | ||
type cutoffSorter[F constraints.Float] interface { | ||
zermelo.Sorter[F] | ||
withCutoff(int) cutoffSorter[F] | ||
} | ||
|
||
type floatSorter[F constraints.Float, U constraints.Unsigned] struct { | ||
uintSorter zermelo.Sorter[U] | ||
compSortCutoff int | ||
topBit U | ||
} | ||
|
||
func (s *floatSorter[F, U]) Sort(x []F) { | ||
x = sortNaNs(x) | ||
if len(x) < 2 { | ||
return | ||
} | ||
if len(x) < s.compSortCutoff { | ||
slices.Sort(x) | ||
return | ||
} | ||
|
||
y := unsafeSliceConvert[F, U](x) | ||
floatFlip[U](y, s.topBit) | ||
s.uintSorter.Sort(y) | ||
floatUnflip[U](y, s.topBit) | ||
} | ||
|
||
func (s *floatSorter[F, U]) withCutoff(cutoff int) cutoffSorter[F] { | ||
s.compSortCutoff = cutoff | ||
return s | ||
} | ||
|
||
// NewFloatSorter creates a new Sorter for float slices that will use radix sort on large slices and reuses buffers. | ||
// The first sort creates a buffer the same size as the slice being sorted and keeps it for future use. | ||
// Later sorts may grow this buffer as needed. The FloatSorter returned is not thread safe. | ||
// Using this sorter can be much faster than repeat calls to SortFloats. | ||
func NewFloatSorter[F constraints.Float]() zermelo.Sorter[F] { | ||
return newFloatSorter[F]() | ||
} | ||
|
||
func newFloatSorter[F constraints.Float]() cutoffSorter[F] { | ||
if isFloat32[F]() { | ||
return &floatSorter[F, uint32]{ | ||
uintSorter: zermelo.NewSorter[uint32](), | ||
compSortCutoff: compSortCutoffFloat32, | ||
topBit: uint32(1) << 31, | ||
} | ||
} | ||
return &floatSorter[F, uint64]{ | ||
uintSorter: zermelo.NewSorter[uint64](), | ||
compSortCutoff: compSortCutoffFloat64, | ||
topBit: uint64(1) << 63, | ||
} | ||
} |
Oops, something went wrong.