SHOW:
|
|
- or go back to the newest paste.
1 | // map.go | |
2 | package main | |
3 | ||
4 | import ( | |
5 | "bytes" | |
6 | "fmt" | |
7 | "strconv" | |
8 | ||
9 | "github.com/alphadose/haxmap" | |
10 | - | type MSD map[string]float64 |
10 | + | |
11 | ||
12 | const MAX_DATA = 12000000 | |
13 | ||
14 | var i2ch []byte | |
15 | ||
16 | func init() { | |
17 | i2ch = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'B', 'c', 'D', 'e', 'F'} | |
18 | } | |
19 | func get_first_digit(d float64) int { | |
20 | for d > 10 { | |
21 | d /= 10 | |
22 | } | |
23 | return int(d) | |
24 | } | |
25 | func to_rhex(v int) string { | |
26 | hex := bytes.Buffer{} | |
27 | for v > 0 { | |
28 | hex.WriteByte(i2ch[v%16]) | |
29 | v /= 16 | |
30 | } | |
31 | return hex.String() | |
32 | } | |
33 | - | func set_or_inc(m map[string]float64, key string, set, inc int, ctr *int) { |
33 | + | func set_or_inc(m *haxmap.Map[string, float64], key string, set, inc int, ctr *int) { |
34 | - | if _, ok := m[key]; !ok { |
34 | + | if old, ok := m.Get(key); !ok { |
35 | - | m[key] = float64(set) |
35 | + | m.Set(key, float64(set)) |
36 | } else { | |
37 | - | m[key] += float64(inc) |
37 | + | m.Set(key, old+float64(inc)) |
38 | *ctr += 1 | |
39 | } | |
40 | } | |
41 | func main() { | |
42 | - | m := MSD{} |
42 | + | m := haxmap.New[string, float64]() |
43 | m.Grow(MAX_DATA) | |
44 | dup1, dup2, dup3 := 0, 0, 0 | |
45 | for z := MAX_DATA; z > 0; z-- { | |
46 | val2 := MAX_DATA - z | |
47 | val3 := MAX_DATA*2 - z | |
48 | key1 := strconv.Itoa(z) | |
49 | key2 := strconv.Itoa(val2) | |
50 | key3 := to_rhex(val3) | |
51 | set_or_inc(m, key1, z, val2, &dup1) | |
52 | set_or_inc(m, key2, val2, val3, &dup2) | |
53 | set_or_inc(m, key3, val3, z, &dup3) | |
54 | } | |
55 | fmt.Println(dup1, dup2, dup3) | |
56 | - | for k, v := range m { |
56 | + | |
57 | m.ForEach(func(k string, v float64) bool { | |
58 | total += get_first_digit(v) | |
59 | verify += len(k) | |
60 | count += 1 | |
61 | return true | |
62 | }) | |
63 | fmt.Println(total, verify, count) | |
64 | } | |
65 |