github.com/goki/ki@v1.1.11/ints/ints.go (about) 1 // Copyright (c) 2018, The GoKi Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 /* 6 Package ints provides a standard Inter interface and basic functions 7 defined on Inter types that support core things like Max, Min, Abs. 8 Furthermore, fully generic slice sort and conversion methods in the kit type 9 kit package attempt to use this interface, before falling back on reflection. 10 If you have a struct that can be converted into an int64, then this is the 11 only way to allow it to be sorted using those generic functions, as the 12 reflect.Kind fallback will fail. 13 14 It also includes Max, Min, Abs for builtin int64, int32 types. 15 */ 16 package ints 17 18 import "math" 19 20 // Inter converts a type from an int64, used in kit.ToInt and in sorting 21 // comparisons. See also Floater in floats package. 22 type Inter interface { 23 Int() int64 24 } 25 26 // IntSetter is an Inter that can also be set from an int. Satisfying this 27 // interface requires a pointer to the underlying type. 28 type IntSetter interface { 29 Inter 30 FromInt(val int64) 31 } 32 33 //////////////////////////////////// 34 // Inter 35 36 // Max computes the maximum of the two Inter args 37 func Max(a, b Inter) Inter { 38 if a.Int() > b.Int() { 39 return a 40 } 41 return b 42 } 43 44 // Min computes the minimum of the two Inter args 45 func Min(a, b Inter) Inter { 46 if a.Int() < b.Int() { 47 return a 48 } 49 return b 50 } 51 52 // Abs computes the absolute value of the given value 53 func Abs(a Inter) int64 { 54 if a.Int() < 0 { 55 return -a.Int() 56 } 57 return a.Int() 58 } 59 60 //////////////////////////////////// 61 // int 62 63 // MaxInt computes the maximum of the two int args 64 func MaxInt(a, b int) int { 65 if a > b { 66 return a 67 } 68 return b 69 } 70 71 // MinInt computes the minimum of the two int args 72 func MinInt(a, b int) int { 73 if a < b { 74 return a 75 } 76 return b 77 } 78 79 // AbsInt computes the absolute value of the given value 80 func AbsInt(a int) int { 81 if a < 0 { 82 return -a 83 } 84 return a 85 } 86 87 // ClipInt clips int within min, max range (max exclusive, min inclusive) 88 func ClipInt(a, min, max int) int { 89 if a >= max { 90 return max - 1 91 } 92 if a < min { 93 return min 94 } 95 return a 96 } 97 98 //////////////////////////////////// 99 // int64 100 101 // Max64 computes the maximum of the two int64 args 102 func Max64(a, b int64) int64 { 103 if a > b { 104 return a 105 } 106 return b 107 } 108 109 // Min64 computes the minimum of the two int64 args 110 func Min64(a, b int64) int64 { 111 if a < b { 112 return a 113 } 114 return b 115 } 116 117 // Abs64 computes the absolute value of the given value 118 func Abs64(a int64) int64 { 119 if a < 0 { 120 return -a 121 } 122 return a 123 } 124 125 //////////////////////////////////// 126 // int32 127 128 // Max32 computes the maximum of the two int32 args 129 func Max32(a, b int32) int32 { 130 if a > b { 131 return a 132 } 133 return b 134 } 135 136 // Min32 computes the minimum of the two int32 args 137 func Min32(a, b int32) int32 { 138 if a < b { 139 return a 140 } 141 return b 142 } 143 144 // Abs32 computes the absolute value of the given value 145 func Abs32(a int32) int32 { 146 if a < 0 { 147 return -a 148 } 149 return a 150 } 151 152 // IntMultiple returns the interger multiple of mod that is always >= given value: 153 // int(Ceil(val / mod)) * mod 154 func IntMultiple(val, mod int) int { 155 return int(math.Ceil(float64(val)/float64(mod))) * mod 156 }