...
1
18
19 package resolver
20
21 type addressMapEntry struct {
22 addr Address
23 value any
24 }
25
26
27
28
29
30 type AddressMap struct {
31
32
33
34
35
36
37
38
39
40
41
42
43
44 m map[Address]addressMapEntryList
45 }
46
47 func toMapKey(addr *Address) Address {
48 return Address{Addr: addr.Addr, ServerName: addr.ServerName}
49 }
50
51 type addressMapEntryList []*addressMapEntry
52
53
54 func NewAddressMap() *AddressMap {
55 return &AddressMap{m: make(map[Address]addressMapEntryList)}
56 }
57
58
59
60 func (l addressMapEntryList) find(addr Address) int {
61 for i, entry := range l {
62
63
64 if entry.addr.Attributes.Equal(addr.Attributes) {
65 return i
66 }
67 }
68 return -1
69 }
70
71
72 func (a *AddressMap) Get(addr Address) (value any, ok bool) {
73 addrKey := toMapKey(&addr)
74 entryList := a.m[addrKey]
75 if entry := entryList.find(addr); entry != -1 {
76 return entryList[entry].value, true
77 }
78 return nil, false
79 }
80
81
82 func (a *AddressMap) Set(addr Address, value any) {
83 addrKey := toMapKey(&addr)
84 entryList := a.m[addrKey]
85 if entry := entryList.find(addr); entry != -1 {
86 entryList[entry].value = value
87 return
88 }
89 a.m[addrKey] = append(entryList, &addressMapEntry{addr: addr, value: value})
90 }
91
92
93 func (a *AddressMap) Delete(addr Address) {
94 addrKey := toMapKey(&addr)
95 entryList := a.m[addrKey]
96 entry := entryList.find(addr)
97 if entry == -1 {
98 return
99 }
100 if len(entryList) == 1 {
101 entryList = nil
102 } else {
103 copy(entryList[entry:], entryList[entry+1:])
104 entryList = entryList[:len(entryList)-1]
105 }
106 a.m[addrKey] = entryList
107 }
108
109
110 func (a *AddressMap) Len() int {
111 ret := 0
112 for _, entryList := range a.m {
113 ret += len(entryList)
114 }
115 return ret
116 }
117
118
119 func (a *AddressMap) Keys() []Address {
120 ret := make([]Address, 0, a.Len())
121 for _, entryList := range a.m {
122 for _, entry := range entryList {
123 ret = append(ret, entry.addr)
124 }
125 }
126 return ret
127 }
128
129
130 func (a *AddressMap) Values() []any {
131 ret := make([]any, 0, a.Len())
132 for _, entryList := range a.m {
133 for _, entry := range entryList {
134 ret = append(ret, entry.value)
135 }
136 }
137 return ret
138 }
139
140 type endpointNode struct {
141 addrs map[string]struct{}
142 }
143
144
145
146 func (en *endpointNode) Equal(en2 *endpointNode) bool {
147 if len(en.addrs) != len(en2.addrs) {
148 return false
149 }
150 for addr := range en.addrs {
151 if _, ok := en2.addrs[addr]; !ok {
152 return false
153 }
154 }
155 return true
156 }
157
158 func toEndpointNode(endpoint Endpoint) endpointNode {
159 en := make(map[string]struct{})
160 for _, addr := range endpoint.Addresses {
161 en[addr.Addr] = struct{}{}
162 }
163 return endpointNode{
164 addrs: en,
165 }
166 }
167
168
169
170
171
172 type EndpointMap struct {
173 endpoints map[*endpointNode]any
174 }
175
176
177 func NewEndpointMap() *EndpointMap {
178 return &EndpointMap{
179 endpoints: make(map[*endpointNode]any),
180 }
181 }
182
183
184 func (em *EndpointMap) Get(e Endpoint) (value any, ok bool) {
185 en := toEndpointNode(e)
186 if endpoint := em.find(en); endpoint != nil {
187 return em.endpoints[endpoint], true
188 }
189 return nil, false
190 }
191
192
193 func (em *EndpointMap) Set(e Endpoint, value any) {
194 en := toEndpointNode(e)
195 if endpoint := em.find(en); endpoint != nil {
196 em.endpoints[endpoint] = value
197 return
198 }
199 em.endpoints[&en] = value
200 }
201
202
203 func (em *EndpointMap) Len() int {
204 return len(em.endpoints)
205 }
206
207
208
209
210
211
212 func (em *EndpointMap) Keys() []Endpoint {
213 ret := make([]Endpoint, 0, len(em.endpoints))
214 for en := range em.endpoints {
215 var endpoint Endpoint
216 for addr := range en.addrs {
217 endpoint.Addresses = append(endpoint.Addresses, Address{Addr: addr})
218 }
219 ret = append(ret, endpoint)
220 }
221 return ret
222 }
223
224
225 func (em *EndpointMap) Values() []any {
226 ret := make([]any, 0, len(em.endpoints))
227 for _, val := range em.endpoints {
228 ret = append(ret, val)
229 }
230 return ret
231 }
232
233
234
235
236 func (em EndpointMap) find(e endpointNode) *endpointNode {
237 for endpoint := range em.endpoints {
238 if e.Equal(endpoint) {
239 return endpoint
240 }
241 }
242 return nil
243 }
244
245
246 func (em *EndpointMap) Delete(e Endpoint) {
247 en := toEndpointNode(e)
248 if entry := em.find(en); entry != nil {
249 delete(em.endpoints, entry)
250 }
251 }
252
View as plain text