-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
150 lines (131 loc) · 2.37 KB
/
Copy pathutils.go
File metadata and controls
150 lines (131 loc) · 2.37 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package parts
import (
"fmt"
"strconv"
"strings"
)
func IncreasePortInAddress(addr string, index int) string {
addrParts := strings.Split(addr, ":")
port, _ := strconv.Atoi(addrParts[len(addrParts)-1])
addrParts[len(addrParts)-1] = fmt.Sprintf("%d", port+index)
return strings.Join(addrParts, ":")
}
func IndexOf(element int64, data []int64) int {
for k, v := range data {
if element == v {
return k
}
}
return -1 //not found.
}
func IndexOfMin(element int64, data []int64) int {
for k, v := range data {
if element == v {
return k
}
if element > v && len(data) == 1 {
return k
}
if element > v && k == len(data)-1 {
return k
}
if element > v && k < len(data)-1 && element < data[k+1] {
return k
}
}
return -1 //not found.
}
func RemoveFromSlice(s []int64, i int64) []int64 {
index := IndexOf(i, s)
if index == -1 {
Log.Error("Received index -1 for val ", i)
return s
}
s[index] = s[len(s)-1]
return s[:len(s)-1]
}
func RemoveFromSliceByIndex(s []int64, index int64) []int64 {
// TODO: Optimization?
return append(s[:index], s[index+1:]...)
// s[index] = s[len(s)-1]
// return s[:len(s)-1]
}
func Max64(x, y int64) int64 {
if x < y {
return y
}
return x
}
func Max(x, y int) int {
if x < y {
return y
}
return x
}
func Min(x, y int) int {
if x > y {
return y
}
return x
}
func Min64(x, y int64) int64 {
if x > y {
return y
}
return x
}
func CeilForce(x, y int64) int64 {
res := x / y
f := float64(x) / float64(y)
if f > float64(res) {
return res + 1
} else {
return res
}
}
func CeilForceInt(x, y int) int {
res := x / y
f := float64(x) / float64(y)
if f > float64(res) {
return res + 1
} else {
return res
}
}
func CeilForceInt64(x, y int64) int64 {
res := x / y
f := float64(x) / float64(y)
if f > float64(res) {
return res + 1
} else {
return res
}
}
func AppendIfMissing(slice []int64, i int64) []int64 {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}
func Sum(array []int64) int64 {
var result int64 = 0
for _, v := range array {
result += v
}
return result
}
func ByteCountSI(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}