- replace timsort with go's sort

pull/17/head
Emir Pasic 8 years ago
parent faeea55fc9
commit 7b3992bef3

@ -995,8 +995,17 @@ Biggest contribution towards this library is to use it and give us feedback for
For direct contributions, _pull request_ into master or ask to become a contributor.
Coding style:
```shell
# Install tooling:
go build github.com/golang/lint/golint
go build github.com/fzipp/gocyclo
# Fix errors and warnings:
go fmt ./... && gofmt -s -w . && go vet ./... && go get ./... && go test ./... && golint ./... && gocyclo -avg -over 15 .
```
### License
This library is distributed under the BSD-style license found in the [LICENSE](https://github.com/emirpasic/gods/blob/master/LICENSE) file.
TimSort copied from [https://github.com/psilva261/timsort](https://github.com/psilva261/timsort) with MIT [LICENSE](https://github.com/emirpasic/gods/blob/master/utils/timsort/LICENSE) file.

@ -28,10 +28,25 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package utils
import "github.com/emirpasic/gods/utils/timsort"
import "sort"
// Sort sorts values (in-place) using timsort
// Sort sorts values (in-place)
// Uses Go's sort (hybrid of quicksort for large and then insertion sort for smaller slices)
func Sort(values []interface{}, comparator Comparator) {
less := func(a, b interface{}) bool { return comparator(a, b) < 0 }
timsort.Sort(values, less)
sort.Sort(sortable{values, comparator})
}
type sortable struct {
values []interface{}
comparator Comparator
}
func (s sortable) Len() int {
return len(s.values)
}
func (s sortable) Swap(i, j int) {
s.values[i], s.values[j] = s.values[j], s.values[i]
}
func (s sortable) Less(i, j int) bool {
return s.comparator(s.values[i], s.values[j]) < 0
}

@ -27,6 +27,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package utils
import (
"math/rand"
"testing"
)
@ -98,5 +99,28 @@ func TestSortStructs(t *testing.T) {
t.Errorf("Not sorted!")
}
}
}
func TestSortRandom(t *testing.T) {
ints := []interface{}{}
for i := 0; i < 10000; i++ {
ints = append(ints, rand.Int())
}
Sort(ints, IntComparator)
for i := 1; i < len(ints); i++ {
if ints[i-1].(int) > ints[i].(int) {
t.Errorf("Not sorted!")
}
}
}
func BenchmarkGoSortRandom(b *testing.B) {
b.StopTimer()
ints := []interface{}{}
for i := 0; i < 100000; i++ {
ints = append(ints, rand.Int())
}
b.StartTimer()
Sort(ints, IntComparator)
b.StopTimer()
}

@ -1,19 +0,0 @@
Copyright (c) 2010-2011 Mike Kroutikov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save