-
Notifications
You must be signed in to change notification settings - Fork 313
/
Utils.go
106 lines (90 loc) · 1.92 KB
/
Utils.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
package main
import (
"container/list"
"fmt"
"math/rand"
"strings"
"time"
)
const (
letterIdxBits = 6
letterIdxMask = 1<<letterIdxBits - 1
letterIdxMax = 63 / letterIdxBits
)
type speedPair struct {
index uint64
speed float64
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var SpeedQueue = list.New()
var SpeedIndex uint64 = 0
type header struct {
key, value string
}
type headersList []header
func (h *headersList) String() string {
return fmt.Sprint(*h)
}
func (h *headersList) IsCumulative() bool {
return true
}
func (h *headersList) Set(value string) error {
res := strings.SplitN(value, ":", 2)
if len(res) != 2 {
return nil
}
*h = append(*h, header{
res[0], strings.Trim(res[1], " "),
})
return nil
}
type ipArray []string
func (i *ipArray) String() string {
return strings.Join(*i, ",")
}
func (i *ipArray) Set(value string) (err error) {
*i = append(*i, strings.TrimSpace(value))
return nil
}
func RandStringBytesMaskImpr(n int) string {
b := make([]byte, n)
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func generateRandomIPAddress() string {
rand.Seed(time.Now().Unix())
ip := fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
return ip
}
func LeastSquares(x []float64, y []float64) (a float64, b float64) {
xi := float64(0)
x2 := float64(0)
yi := float64(0)
xy := float64(0)
if len(x) != len(y) {
a = 0
b = 0
return
} else {
length := float64(len(x))
for i := 0; i < len(x); i++ {
xi += x[i]
x2 += x[i] * x[i]
yi += y[i]
xy += x[i] * y[i]
}
a = (yi*xi - xy*length) / (xi*xi - x2*length)
b = (yi*x2 - xy*xi) / (x2*length - xi*xi)
}
return
}