Skip to content

Commit

Permalink
refactor: use sort.Slice
Browse files Browse the repository at this point in the history
  • Loading branch information
suzaku committed Dec 7, 2021
1 parent cf68462 commit fc7f720
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
2 changes: 1 addition & 1 deletion source/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var drivers = make(map[string]Driver)
// * Drivers are supposed to be read only.
// * Ideally don't load any contents (into memory) in Open or WithInstance.
type Driver interface {
// Open returns a a new driver instance configured with parameters
// Open returns a new driver instance configured with parameters
// coming from the URL string. Migrate will call this function
// only once per instance.
Open(url string) (Driver, error)
Expand Down
18 changes: 4 additions & 14 deletions source/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ func (i *Migrations) Append(m *Migration) (ok bool) {
}

func (i *Migrations) buildIndex() {
i.index = make(uintSlice, 0)
i.index = make(uintSlice, 0, len(i.migrations))
for version := range i.migrations {
i.index = append(i.index, version)
}
sort.Sort(i.index)
sort.Slice(i.index, func(x, y int) bool {
return i.index[x] < i.index[y]
})
}

func (i *Migrations) First() (version uint, ok bool) {
Expand Down Expand Up @@ -126,18 +128,6 @@ func (i *Migrations) findPos(version uint) int {

type uintSlice []uint

func (s uintSlice) Len() int {
return len(s)
}

func (s uintSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func (s uintSlice) Less(i, j int) bool {
return s[i] < s[j]
}

func (s uintSlice) Search(x uint) int {
return sort.Search(len(s), func(i int) bool { return s[i] >= x })
}

0 comments on commit fc7f720

Please sign in to comment.