Skip to content
This repository has been archived by the owner on Mar 18, 2021. It is now read-only.

Add MinSliceIndex/MaxSliceIndex #6

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ MaxN returns the largest int in the set provided.
If no values are provided, Max returns 0.


## func MaxSliceIndex
``` go
func MaxSliceIndex(slice interface{}, less func(i, j int) bool) int
```
MaxSliceIndex returns the largest element index in the slice.
If slice is empty, MaxSliceIndex returns -1


## func MaxUint
``` go
func MaxUint(a, b uint) uint
Expand Down Expand Up @@ -316,6 +324,14 @@ MinN returns the smallest int in the set provided.
If no values are provided, Min returns 0.


## func MinSliceIndex
``` go
func MinSliceIndex(slice interface{}, less func(i, j int) bool) int
```
MinSliceIndex returns the smallest element index in the slice.
If slice is empty, MinSliceIndex returns -1


## func MinUint
``` go
func MinUint(a, b uint) uint
Expand Down
35 changes: 35 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package math

import "reflect"

// MinSliceIndex returns the smallest element index in the slice.
// If slice is empty, MinSliceIndex returns -1
func MinSliceIndex(slice interface{}, less func(i, j int) bool) int {
v := reflect.ValueOf(slice)
if v.Len() == 0 {
return -1
}
minIndex := 0
for i := 1; i < v.Len(); i++ {
if less(i, minIndex) {
minIndex = i
}
}
return minIndex
}

// MaxSliceIndex returns the largest element index in the slice.
// If slice is empty, MaxSliceIndex returns -1
func MaxSliceIndex(slice interface{}, less func(i, j int) bool) int {
v := reflect.ValueOf(slice)
if v.Len() == 0 {
return -1
}
maxIndex := 0
for i := 1; i < v.Len(); i++ {
if less(maxIndex, i) {
maxIndex = i
}
}
return maxIndex
}
63 changes: 63 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package math

import (
"testing"
)

var minSliceIndexTests = []struct {
v []struct{ n int }
want int
}{
{
v: []struct{ n int }{{1}},
want: 0,
},
{
v: []struct{ n int }{{7}, {2}},
want: 1,
},
{
v: []struct{ n int }{{12}, {18}, {17}, {9}},
want: 3,
},
}

func TestMinSliceIndex(t *testing.T) {
for i, tt := range minSliceIndexTests {
got := MinSliceIndex(tt.v, func(i, j int) bool {
return tt.v[i].n < tt.v[j].n
})
if tt.want != got {
t.Errorf("%d: MinSliceIndex(%v) = %v, want %v", i+1, tt.v, got, tt.want)
}
}
}

var maxSliceIndexTests = []struct {
v []struct{ n int }
want int
}{
{
v: []struct{ n int }{{1}},
want: 0,
},
{
v: []struct{ n int }{{7}, {2}},
want: 0,
},
{
v: []struct{ n int }{{12}, {18}, {17}, {19}},
want: 3,
},
}

func TestMaxSliceIndex(t *testing.T) {
for i, tt := range maxSliceIndexTests {
got := MaxSliceIndex(tt.v, func(i, j int) bool {
return tt.v[i].n < tt.v[j].n
})
if tt.want != got {
t.Errorf("%d: MaxSliceIndex(%v) = %v, want %v", i+1, tt.v, got, tt.want)
}
}
}