-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsorted_index.go
121 lines (105 loc) · 3.51 KB
/
sorted_index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package slices
import (
"reflect"
"github.com/golodash/godash/internal"
)
// Uses a binary search to determine the lowest index at which value should be
// inserted into slice in order to maintain its sort order.
//
// Complexity: O(log(n))
func SortedIndex(slice, value interface{}) int {
if !internal.SliceCheck(slice) {
panic("passed 'slice' variable is not slice type")
}
if !internal.IsNumber(value) {
panic("'value' is not a number")
}
val := reflect.ValueOf(value)
sType := reflect.TypeOf(slice)
if !sType.Elem().ConvertibleTo(val.Type()) {
panic("'value' is not comparable with 'slice'")
}
if reflect.ValueOf(slice).Len() == 0 {
return 0
}
return whereToPutInSliceLowerEqual(slice, value, compareLowerEqual)
}
// Compare function for SortedIndex function
func compareLowerEqual(midValue, value interface{}) bool {
mid := reflect.ValueOf(midValue)
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Float64:
return v.Interface().(float64) <= mid.Interface().(float64)
case reflect.Float32:
return v.Interface().(float32) <= mid.Interface().(float32)
case reflect.Int:
return v.Interface().(int) <= mid.Interface().(int)
case reflect.Int8:
return v.Interface().(int8) <= mid.Interface().(int8)
case reflect.Int16:
return v.Interface().(int16) <= mid.Interface().(int16)
case reflect.Int32:
return v.Interface().(int32) <= mid.Interface().(int32)
case reflect.Int64:
return v.Interface().(int64) <= mid.Interface().(int64)
case reflect.Uint:
return v.Interface().(uint) <= mid.Interface().(uint)
case reflect.Uint8:
return v.Interface().(uint8) <= mid.Interface().(uint8)
case reflect.Uint16:
return v.Interface().(uint16) <= mid.Interface().(uint16)
case reflect.Uint32:
return v.Interface().(uint32) <= mid.Interface().(uint32)
case reflect.Uint64:
return v.Interface().(uint64) <= mid.Interface().(uint64)
case reflect.Uintptr:
return v.Interface().(uintptr) <= mid.Interface().(uintptr)
}
return false
}
// Based on binary search, searches on where to put the sent value in the passed
// slice based on isLowerEqualFunction result.
//
// Complexity: O(log(n))
func whereToPutInSliceLowerEqual(slice, value, isLowerEqualFunction interface{}) int {
sliceValue := reflect.ValueOf(slice)
len := sliceValue.Len()
if len == 0 {
return 0
} else if len == 1 {
item := sliceValue.Index(0)
if res := reflect.ValueOf(isLowerEqualFunction).Call([]reflect.Value{item, reflect.ValueOf(value)}); res[0].Bool() {
return 0
} else {
return 1
}
} else if len == 2 {
item := sliceValue.Index(0)
if res := reflect.ValueOf(isLowerEqualFunction).Call([]reflect.Value{item, reflect.ValueOf(value)}); res[0].Bool() {
return 0
} else {
var result int
if result = whereToPutInSliceLowerEqual(sliceValue.Slice(1, 2).Interface(), value, isLowerEqualFunction); result == -1 {
return -1
}
return result + 1
}
}
item := sliceValue.Index(len / 2).Interface()
if !internal.AreComparable(item, value) {
panic("couldn't compare 'value' with all items in passed slice")
}
var result int
if res := reflect.ValueOf(isLowerEqualFunction).Call([]reflect.Value{reflect.ValueOf(item), reflect.ValueOf(value)}); res[0].Bool() {
if result = whereToPutInSliceLowerEqual(sliceValue.Slice(0, (len/2)+1).Interface(), value, isLowerEqualFunction); result == -1 {
return -1
}
return result
} else {
if result = whereToPutInSliceLowerEqual(sliceValue.Slice(len/2, len).Interface(), value, isLowerEqualFunction); result == -1 {
return -1
}
return result + (len / 2)
}
}