@@ -5,8 +5,17 @@ import (
5
5
)
6
6
7
7
// Set defines a unique set of IPv4 addresses using a simple []uint32
8
+ //
9
+ // Since Set is a alias of []unit32, one can use
10
+ // `make(Set, length, capacity)` or use the NewSet constructor
11
+ //
8
12
type Set []uint32
9
13
14
+ // NewSet creates a Set with a given initial capacity.
15
+ func NewSet (capacity int ) Set {
16
+ return make (Set , 0 , capacity )
17
+ }
18
+
10
19
// Len returns a length, part of the Sort.Interface
11
20
func (m Set ) Len () int {
12
21
return len (m )
@@ -76,26 +85,6 @@ func (m *Set) AddAll(ipv4dots []string) bool {
76
85
return true
77
86
}
78
87
79
- func (m * Set ) sort () {
80
- in := * m
81
- sort .Sort (in )
82
-
83
- // inplace sort
84
- // https://github.com/golang/go/wiki/SliceTricks#in-place-deduplicate-comparable
85
- j := 0
86
- for i := 1 ; i < len (in ); i ++ {
87
- if in [j ] == in [i ] {
88
- continue
89
- }
90
- j ++
91
- // preserve the original data
92
- // in[i], in[j] = in[j], in[i]
93
- // only set what is required
94
- in [j ] = in [i ]
95
- }
96
- in = in [:j + 1 ]
97
- }
98
-
99
88
// Valid return true if the internal storage is in sorted form and unique
100
89
func (m Set ) Valid () bool {
101
90
if len (m ) == 0 {
@@ -111,10 +100,31 @@ func (m Set) Valid() bool {
111
100
return true
112
101
}
113
102
103
+ // ToDots returns the IP set as a list of dotted-notation strings
114
104
func (m Set ) ToDots () []string {
115
105
out := make ([]string , len (m ))
116
106
for i , val := range m {
117
107
out [i ] = ToDots (val )
118
108
}
119
109
return out
120
110
}
111
+
112
+ func (m * Set ) sort () {
113
+ in := * m
114
+ sort .Sort (in )
115
+
116
+ // inplace sort
117
+ // https://github.com/golang/go/wiki/SliceTricks#in-place-deduplicate-comparable
118
+ j := 0
119
+ for i := 1 ; i < len (in ); i ++ {
120
+ if in [j ] == in [i ] {
121
+ continue
122
+ }
123
+ j ++
124
+ // preserve the original data
125
+ // in[i], in[j] = in[j], in[i]
126
+ // only set what is required
127
+ in [j ] = in [i ]
128
+ }
129
+ in = in [:j + 1 ]
130
+ }
0 commit comments