mirror of
https://github.com/emirpasic/gods
synced 2024-11-20 03:25:45 +00:00
Merge pull request #17 from emirpasic/timsort_replace
- replace timsort with go's sort
This commit is contained in:
commit
c874c09c6d
13
README.md
13
README.md
@ -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.
|
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
|
### License
|
||||||
|
|
||||||
This library is distributed under the BSD-style license found in the [LICENSE](https://github.com/emirpasic/gods/blob/master/LICENSE) file.
|
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
|
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) {
|
func Sort(values []interface{}, comparator Comparator) {
|
||||||
less := func(a, b interface{}) bool { return comparator(a, b) < 0 }
|
sort.Sort(sortable{values, comparator})
|
||||||
timsort.Sort(values, less)
|
}
|
||||||
|
|
||||||
|
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
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -98,5 +99,28 @@ func TestSortStructs(t *testing.T) {
|
|||||||
t.Errorf("Not sorted!")
|
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…
Reference in New Issue
Block a user