-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_utils.go
More file actions
52 lines (45 loc) · 906 Bytes
/
Copy pathnumber_utils.go
File metadata and controls
52 lines (45 loc) · 906 Bytes
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
package chart
import (
"strconv"
"sync"
)
var (
itoaCache = make(map[int]string)
itoaMutex = &sync.Mutex{}
ftoa1Cache = make(map[float64]string)
ftoa1Mutex = &sync.Mutex{}
ftoa2Cache = make(map[float64]string)
ftoa2Mutex = &sync.Mutex{}
)
// Implement a caching itoa function.
func itoa(i int) string {
itoaMutex.Lock()
defer itoaMutex.Unlock()
if s, ok := itoaCache[i]; ok {
return s
}
s := strconv.FormatInt(int64(i), 10)
itoaCache[i] = s
return s
}
// Implement a caching ftoa function.
func ftoa1(f float64) string {
ftoa1Mutex.Lock()
defer ftoa1Mutex.Unlock()
if s, ok := ftoa1Cache[f]; ok {
return s
}
s := strconv.FormatFloat(f, 'f', 1, 64)
ftoa1Cache[f] = s
return s
}
func ftoa2(f float64) string {
ftoa2Mutex.Lock()
defer ftoa2Mutex.Unlock()
if s, ok := ftoa2Cache[f]; ok {
return s
}
s := strconv.FormatFloat(f, 'f', 2, 64)
ftoa2Cache[f] = s
return s
}