1
2
3
4
5 package ssa
6
7 import (
8 "math"
9 )
10
11
12
13
14
15
16 type biasedSparseMap struct {
17 s *sparseMap
18 first int
19 }
20
21
22 func newBiasedSparseMap(first, last int) *biasedSparseMap {
23 if first > last {
24 return &biasedSparseMap{first: math.MaxInt32, s: nil}
25 }
26 return &biasedSparseMap{first: first, s: newSparseMap(1 + last - first)}
27 }
28
29
30 func (s *biasedSparseMap) cap() int {
31 if s == nil || s.s == nil {
32 return 0
33 }
34 return s.s.cap() + int(s.first)
35 }
36
37
38 func (s *biasedSparseMap) size() int {
39 if s == nil || s.s == nil {
40 return 0
41 }
42 return s.s.size()
43 }
44
45
46 func (s *biasedSparseMap) contains(x uint) bool {
47 if s == nil || s.s == nil {
48 return false
49 }
50 if int(x) < s.first {
51 return false
52 }
53 if int(x) >= s.cap() {
54 return false
55 }
56 return s.s.contains(ID(int(x) - s.first))
57 }
58
59
60
61 func (s *biasedSparseMap) get(x uint) (int32, bool) {
62 if s == nil || s.s == nil {
63 return 0, false
64 }
65 if int(x) < s.first {
66 return 0, false
67 }
68 if int(x) >= s.cap() {
69 return 0, false
70 }
71 k := ID(int(x) - s.first)
72 if !s.s.contains(k) {
73 return 0, false
74 }
75 return s.s.get(k)
76 }
77
78
79
80 func (s *biasedSparseMap) getEntry(i int) (x uint, v int32) {
81 e := s.s.contents()[i]
82 x = uint(int(e.key) + s.first)
83 v = e.val
84 return
85 }
86
87
88 func (s *biasedSparseMap) add(x uint) {
89 if int(x) < s.first || int(x) >= s.cap() {
90 return
91 }
92 s.s.set(ID(int(x)-s.first), 0)
93 }
94
95
96 func (s *biasedSparseMap) set(x uint, v int32) {
97 if int(x) < s.first || int(x) >= s.cap() {
98 return
99 }
100 s.s.set(ID(int(x)-s.first), v)
101 }
102
103
104 func (s *biasedSparseMap) remove(x uint) {
105 if int(x) < s.first || int(x) >= s.cap() {
106 return
107 }
108 s.s.remove(ID(int(x) - s.first))
109 }
110
111 func (s *biasedSparseMap) clear() {
112 if s.s != nil {
113 s.s.clear()
114 }
115 }
116
View as plain text