@@ -7,70 +7,60 @@ package ssa
7
7
// from https://research.swtch.com/sparse
8
8
// in turn, from Briggs and Torczon
9
9
10
- type sparseEntry struct {
11
- key ID
12
- val int32
10
+ // sparseKey needs to be something we can index a slice with.
11
+ type sparseKey interface { ~ int | ~ int32 }
12
+
13
+ type sparseEntry [K sparseKey , V any ] struct {
14
+ key K
15
+ val V
13
16
}
14
17
15
- type sparseMap struct {
16
- dense []sparseEntry
18
+ type genericSparseMap [ K sparseKey , V any ] struct {
19
+ dense []sparseEntry [ K , V ]
17
20
sparse []int32
18
21
}
19
22
20
- // newSparseMap returns a sparseMap that can map
21
- // integers between 0 and n-1 to int32s .
22
- func newSparseMap (n int ) * sparseMap {
23
- return & sparseMap {dense : nil , sparse : make ([]int32 , n )}
23
+ // newGenericSparseMap returns a sparseMap that can map
24
+ // integers between 0 and n-1 to a value type .
25
+ func newGenericSparseMap [ K sparseKey , V any ] (n int ) * genericSparseMap [ K , V ] {
26
+ return & genericSparseMap [ K , V ] {dense : nil , sparse : make ([]int32 , n )}
24
27
}
25
28
26
- func (s * sparseMap ) cap () int {
29
+ func (s * genericSparseMap [ K , V ] ) cap () int {
27
30
return len (s .sparse )
28
31
}
29
32
30
- func (s * sparseMap ) size () int {
33
+ func (s * genericSparseMap [ K , V ] ) size () int {
31
34
return len (s .dense )
32
35
}
33
36
34
- func (s * sparseMap ) contains (k ID ) bool {
37
+ func (s * genericSparseMap [ K , V ] ) contains (k K ) bool {
35
38
i := s .sparse [k ]
36
39
return i < int32 (len (s .dense )) && s .dense [i ].key == k
37
40
}
38
41
39
- // get returns the value for key k, or -1 if k does
40
- // not appear in the map.
41
- func (s * sparseMap ) get (k ID ) int32 {
42
+ // get returns the value for key k, or the zero V
43
+ // if k does not appear in the map.
44
+ func (s * genericSparseMap [ K , V ] ) get (k K ) V {
42
45
i := s .sparse [k ]
43
46
if i < int32 (len (s .dense )) && s .dense [i ].key == k {
44
47
return s .dense [i ].val
45
48
}
46
- return - 1
49
+ var v V
50
+ return v
47
51
}
48
52
49
- func (s * sparseMap ) set (k ID , v int32 ) {
53
+ func (s * genericSparseMap [ K , V ] ) set (k K , v V ) {
50
54
i := s .sparse [k ]
51
55
if i < int32 (len (s .dense )) && s .dense [i ].key == k {
52
56
s .dense [i ].val = v
53
57
return
54
58
}
55
- s .dense = append (s .dense , sparseEntry {k , v })
56
- s .sparse [k ] = int32 (len (s .dense )) - 1
57
- }
58
-
59
- // setBit sets the v'th bit of k's value, where 0 <= v < 32
60
- func (s * sparseMap ) setBit (k ID , v uint ) {
61
- if v >= 32 {
62
- panic ("bit index too large." )
63
- }
64
- i := s .sparse [k ]
65
- if i < int32 (len (s .dense )) && s .dense [i ].key == k {
66
- s .dense [i ].val |= 1 << v
67
- return
68
- }
69
- s .dense = append (s .dense , sparseEntry {k , 1 << v })
59
+ s .dense = append (s .dense , sparseEntry [K , V ]{k , v })
70
60
s .sparse [k ] = int32 (len (s .dense )) - 1
71
61
}
72
62
73
- func (s * sparseMap ) remove (k ID ) {
63
+ func (s * genericSparseMap [ K , V ] ) remove (k K ) {
74
64
i := s .sparse [k ]
75
65
if i < int32 (len (s .dense )) && s .dense [i ].key == k {
76
66
y := s .dense [len (s .dense )- 1 ]
@@ -80,10 +70,18 @@ func (s *sparseMap) remove(k ID) {
80
70
}
81
71
}
82
72
83
- func (s * sparseMap ) clear () {
73
+ func (s * genericSparseMap [ K , V ] ) clear () {
84
74
s .dense = s .dense [:0 ]
85
75
}
86
76
87
- func (s * sparseMap ) contents () []sparseEntry {
77
+ func (s * genericSparseMap [ K , V ] ) contents () []sparseEntry [ K , V ] {
88
78
return s .dense
89
79
}
80
+
81
+ type sparseMap = genericSparseMap [ID , int32 ]
82
+
83
+ // newSparseMap returns a sparseMap that can map
84
+ // integers between 0 and n-1 to int32s.
85
+ func newSparseMap (n int ) * sparseMap {
86
+ return newGenericSparseMap [ID , int32 ](n )
87
+ }
0 commit comments