-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipv4set.go
52 lines (44 loc) · 956 Bytes
/
ipv4set.go
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 main
import (
"net"
"sync"
)
type IPv4Set struct {
mu sync.RWMutex
set map[string]struct{}
order []string
capacity int
}
func NewIPv4Set(capacity int) *IPv4Set {
return &IPv4Set{
set: make(map[string]struct{}),
order: make([]string, 0, capacity),
capacity: capacity,
}
}
// Add inserts an IPv4 address. Returns true if added, false if duplicate.
func (s *IPv4Set) Add(ip net.IP) bool {
ipStr := ip.To4().String()
s.mu.Lock()
defer s.mu.Unlock()
if _, exists := s.set[ipStr]; exists {
return false
}
if len(s.order) >= s.capacity {
// Remove oldest
old := s.order[0]
s.order = s.order[1:]
delete(s.set, old)
}
s.set[ipStr] = struct{}{}
s.order = append(s.order, ipStr)
return true
}
// Exists checks if an IPv4 address is in the set.
func (s *IPv4Set) Exists(ip net.IP) bool {
ipStr := ip.To4().String()
s.mu.RLock()
defer s.mu.RUnlock()
_, exists := s.set[ipStr]
return exists
}