-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
36 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
package lib | ||
|
||
// AbsInt64 calculates the absolute value of a given int | ||
func AbsInt64(n int64) int64 { | ||
import "golang.org/x/exp/constraints" | ||
|
||
// Abs calculates the absolute value of a given int | ||
func Abs[T constraints.Signed](n T) T { | ||
if n < 0 { | ||
return -n | ||
} | ||
|
||
return n | ||
} | ||
|
||
// ModInt64 calculates the modulus value (as opposed to the remainder) | ||
// Mod calculates the modulus value (as opposed to the remainder) | ||
// https://github.com/golang/go/issues/448 | ||
func ModInt64(x, y int64) int64 { | ||
func Mod[T constraints.Signed](x, y T) T { | ||
return ((x % y) + y) % y | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
package lib | ||
|
||
import "strconv" | ||
import ( | ||
"strconv" | ||
"unsafe" | ||
|
||
// ParseInt32 parses the string as a base-10 int | ||
func ParseInt32(text string) (int32, error) { | ||
value, err := strconv.ParseInt(text, 10, 32) | ||
"golang.org/x/exp/constraints" | ||
) | ||
|
||
// ParseInt64 parses the string as a base-10 int64 | ||
func ParseInt[T constraints.Signed](text string) (T, error) { | ||
size := unsafe.Sizeof(T(0)) | ||
value, err := strconv.ParseInt(text, 10, int(size*8)) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return int32(value), nil | ||
} | ||
|
||
// ParseInt64 parses the string as a base-10 int64 | ||
func ParseInt64(text string) (int64, error) { | ||
return strconv.ParseInt(text, 10, 64) | ||
return T(value), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters