@@ -91,4 +91,96 @@ impl<'a> USizeSetIter<'a> {
91
91
92
92
const U64_BIT_SIZE : usize = mem:: size_of :: < u64 > ( ) * 8 ;
93
93
94
- // line 113
94
+ impl < ' a > Iterator for USizeSetIter < ' a > {
95
+ type Item = usize ;
96
+
97
+ fn next ( & mut self ) -> Option < usize > {
98
+ loop {
99
+ if let Some ( bit_index) = self . current . next ( ) {
100
+ return Some ( self . offset + bit_index) ;
101
+ }
102
+
103
+ if let Some ( & next_content) = self . content . next ( ) {
104
+ self . current = BitIterator :: new ( next_content) ;
105
+ self . offset += U64_BIT_SIZE ;
106
+ }
107
+ else {
108
+ return None ;
109
+ }
110
+ }
111
+ }
112
+ }
113
+
114
+ impl USizeSet {
115
+ pub fn new ( lower : usize , upper : usize ) -> USizeSetResult < USizeSet > {
116
+ if lower > upper {
117
+ Err ( USizeSetError :: InvalidBounds )
118
+ }
119
+ else {
120
+ let required_words = ( upper - lower + 64 ) >> 6 ;
121
+ Ok ( USizeSet {
122
+ lower, upper, len : 0 , content : vec ! [ 0u64 ; required_words]
123
+ } )
124
+ }
125
+ }
126
+
127
+ pub fn singleton ( lower : usize , upper : usize , content : usize ) -> USizeSetResult < USizeSet > {
128
+ let mut result = USizeSet :: new ( lower, upper) ?;
129
+ result. insert ( content) ?;
130
+ Ok ( result)
131
+ }
132
+
133
+ pub fn range ( lower : usize , upper : usize ) -> USizeSetResult < USizeSet > {
134
+ if lower > upper {
135
+ Err ( USizeSetError :: InvalidBounds )
136
+ }
137
+ else {
138
+ let mut content = Vec :: new ( ) ;
139
+ let ones = upper - lower + 1 ;
140
+ let ones_words = ones / U64_BIT_SIZE ;
141
+
142
+ for _ in 0 ..ones_words {
143
+ content. push ( !0 ) ;
144
+ }
145
+
146
+ let remaining_ones = ones - ( ones_words << 6 ) ;
147
+
148
+ if remaining_ones > 0 {
149
+ content. push ( ( 1 << remaining_ones) - 1 ) ;
150
+ }
151
+
152
+ Ok ( USizeSet {
153
+ lower, upper, len : ones, content
154
+ } )
155
+ }
156
+ }
157
+
158
+ fn compute_index ( & self , number : usize ) -> USizeSetResult < ( usize , u64 ) > {
159
+ if number < self . lower || number > self . upper {
160
+ Err ( USizeSetError :: OutOfBounds )
161
+ }
162
+ else {
163
+ let index = number - self . lower ;
164
+ let word_index = index >> 6 ;
165
+ let sub_word_index = index & 63 ;
166
+ let mask = 1u64 << sub_word_index;
167
+ Ok ( ( word_index, mask) )
168
+ }
169
+ }
170
+
171
+ pub fn lower ( & self ) -> usize {
172
+ self . lower
173
+ }
174
+
175
+ pub fn upper ( & self ) -> usize {
176
+ self . upper
177
+ }
178
+
179
+ pub fn min ( & self ) -> Option < usize > {
180
+ for ( index, & content) in self . content . iter ( ) . enumerate ( ) {
181
+ let trailing_zeros = content. trailing_zeros ( ) as usize ;
182
+
183
+ //line 266
184
+ }
185
+ }
186
+ }
0 commit comments