Last active
March 1, 2024 18:41
-
-
Save romsar/6f11c51ec35a4a982eed786da958aecc to your computer and use it in GitHub Desktop.
algo scan helper
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
// https://gist.github.com/romsar/6f11c51ec35a4a982eed786da958aecc | |
import ( | |
"bufio" | |
"os" | |
"strconv" | |
"strings" | |
) | |
type Scanner struct { | |
s *bufio.Scanner | |
} | |
func NewScanner() Scanner { | |
const maxCapacity = 4 * 1024 * 1024 | |
buf := make([]byte, maxCapacity) | |
s := bufio.NewScanner(os.Stdin) | |
s.Buffer(buf, maxCapacity) | |
return Scanner{s: s} | |
} | |
func (s *Scanner) String() string { | |
return s.scan() | |
} | |
func (s *Scanner) StringSlice() []string { | |
return s.scanSlice() | |
} | |
func (s *Scanner) Bool() bool { | |
return toBool(s.scan()) | |
} | |
func (s *Scanner) BoolSlice() []bool { | |
return toSlice(s.scanSlice(), toBool) | |
} | |
func toBool(str string) bool { | |
b, _ := strconv.ParseBool(str) | |
return b | |
} | |
func (s *Scanner) Int() int { | |
return toInt(s.scan()) | |
} | |
func (s *Scanner) IntSlice() []int { | |
return toSlice(s.scanSlice(), toInt) | |
} | |
func toInt(str string) int { | |
num, _ := strconv.Atoi(str) | |
return num | |
} | |
func (s *Scanner) Int8() int8 { | |
return toInt8(s.scan()) | |
} | |
func (s *Scanner) Int8Slice() []int8 { | |
return toSlice(s.scanSlice(), toInt8) | |
} | |
func toInt8(str string) int8 { | |
i, _ := strconv.ParseInt(str, 10, 8) | |
return int8(i) | |
} | |
func (s *Scanner) Int16() int16 { | |
return toInt16(s.scan()) | |
} | |
func (s *Scanner) Int16Slice() []int16 { | |
return toSlice(s.scanSlice(), toInt16) | |
} | |
func toInt16(str string) int16 { | |
i, _ := strconv.ParseInt(str, 10, 16) | |
return int16(i) | |
} | |
func (s *Scanner) Int32() int32 { | |
return toInt32(s.scan()) | |
} | |
func (s *Scanner) Int32Slice() []int32 { | |
return toSlice(s.scanSlice(), toInt32) | |
} | |
func toInt32(str string) int32 { | |
i, _ := strconv.ParseInt(str, 10, 32) | |
return int32(i) | |
} | |
func (s *Scanner) Int64() int64 { | |
return toInt64(s.scan()) | |
} | |
func (s *Scanner) Int64Slice() []int64 { | |
return toSlice(s.scanSlice(), toInt64) | |
} | |
func toInt64(str string) int64 { | |
i, _ := strconv.ParseInt(str, 10, 64) | |
return i | |
} | |
func (s *Scanner) Float32() float32 { | |
return toFloat32(s.scan()) | |
} | |
func (s *Scanner) Float32Slice() []float32 { | |
return toSlice(s.scanSlice(), toFloat32) | |
} | |
func toFloat32(str string) float32 { | |
f, _ := strconv.ParseFloat(str, 32) | |
return float32(f) | |
} | |
func (s *Scanner) Float64() float64 { | |
return toFloat64(s.scan()) | |
} | |
func (s *Scanner) Float64Slice() []float64 { | |
return toSlice(s.scanSlice(), toFloat64) | |
} | |
func toFloat64(str string) float64 { | |
f, _ := strconv.ParseFloat(str, 64) | |
return f | |
} | |
func (s *Scanner) scan() string { | |
s.s.Scan() | |
return s.s.Text() | |
} | |
func (s *Scanner) scanSlice() []string { | |
s.s.Scan() | |
return strings.Split(s.s.Text(), " ") | |
} | |
func toSlice[T any](strs []string, f func(string) T) []T { | |
res := make([]T, len(strs)) | |
for i, str := range strs { | |
res[i] = f(str) | |
} | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compact version: