focus/compare/compare.go

324 lines
4.3 KiB
Go
Raw Normal View History

new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
package compare
import "time"
// Compare 如下
// k1 > k2 --> 1
// k1 == k2 --> 0
// k1 < k2 --> -1
type Compare func(k1, k2 interface{}) int
2020-03-04 03:24:36 +00:00
// RuneArray []rune compare
func RuneArray(k1, k2 interface{}) int {
s1 := k1.([]rune)
s2 := k2.([]rune)
switch {
case len(s1) > len(s2):
for i := 0; i < len(s2); i++ {
if s1[i] != s2[i] {
if s1[i] > s2[i] {
return 1
}
return -1
}
}
return 1
case len(s1) < len(s2):
for i := 0; i < len(s1); i++ {
if s1[i] != s2[i] {
if s1[i] > s2[i] {
return 1
}
return -1
}
}
return -1
default:
for i := 0; i < len(s1); i++ {
if s1[i] != s2[i] {
if s1[i] > s2[i] {
return 1
}
return -1
}
}
return 0
}
}
// ByteArray []byte compare
func ByteArray(k1, k2 interface{}) int {
s1 := k1.([]byte)
s2 := k2.([]byte)
switch {
case len(s1) > len(s2):
for i := 0; i < len(s2); i++ {
if s1[i] != s2[i] {
if s1[i] > s2[i] {
return 1
}
return -1
}
}
return 1
case len(s1) < len(s2):
for i := 0; i < len(s1); i++ {
if s1[i] != s2[i] {
if s1[i] > s2[i] {
return 1
}
return -1
}
}
return -1
default:
for i := 0; i < len(s1); i++ {
if s1[i] != s2[i] {
if s1[i] > s2[i] {
return 1
}
return -1
}
}
return 0
}
}
new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
// String comp
func String(k1, k2 interface{}) int {
s1 := k1.(string)
s2 := k2.(string)
switch {
case len(s1) > len(s2):
for i := 0; i < len(s2); i++ {
if s1[i] != s2[i] {
if s1[i] > s2[i] {
return 1
}
return -1
}
}
return 1
case len(s1) < len(s2):
for i := 0; i < len(s1); i++ {
if s1[i] != s2[i] {
if s1[i] > s2[i] {
return 1
}
return -1
}
}
return -1
default:
for i := 0; i < len(s1); i++ {
if s1[i] != s2[i] {
if s1[i] > s2[i] {
return 1
}
return -1
}
}
return 0
}
}
func Int(k1, k2 interface{}) int {
c1 := k1.(int)
c2 := k2.(int)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func Int8(k1, k2 interface{}) int {
c1 := k1.(int8)
c2 := k2.(int8)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func Int16(k1, k2 interface{}) int {
c1 := k1.(int16)
c2 := k2.(int16)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func Int32(k1, k2 interface{}) int {
c1 := k1.(int32)
c2 := k2.(int32)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func Int64(k1, k2 interface{}) int {
c1 := k1.(int64)
c2 := k2.(int64)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func UInt(k1, k2 interface{}) int {
c1 := k1.(uint)
c2 := k2.(uint)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func UInt8(k1, k2 interface{}) int {
c1 := k1.(uint8)
c2 := k2.(uint8)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func UInt16(k1, k2 interface{}) int {
c1 := k1.(uint16)
c2 := k2.(uint16)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func UInt32(k1, k2 interface{}) int {
c1 := k1.(uint32)
c2 := k2.(uint32)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func UInt64(k1, k2 interface{}) int {
c1 := k1.(uint64)
c2 := k2.(uint64)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func Float32(k1, k2 interface{}) int {
c1 := k1.(float32)
c2 := k2.(float32)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func Float64(k1, k2 interface{}) int {
c1 := k1.(float64)
c2 := k2.(float64)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func Byte(k1, k2 interface{}) int {
c1 := k1.(byte)
c2 := k2.(byte)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func Rune(k1, k2 interface{}) int {
c1 := k1.(rune)
c2 := k2.(rune)
switch {
case c1 > c2:
return 1
case c1 < c2:
return -1
default:
return 0
}
}
func Time(k1, k2 interface{}) int {
c1 := k1.(time.Time)
c2 := k2.(time.Time)
switch {
case c1.After(c2):
return 1
case c1.Before(c2):
return -1
default:
return 0
}
}