bitmaps
bitmaps copied to clipboard
impl: add {set,clear}_range
Setting bits with for loop one by one is not performant. Creating mask for bit_and/bit_or to achieve the same goal is not trivial task especially for "big" bitmasks.
To make life easier let's introduce new methods to set and clear range of bits by creating needed mask
Signed-off-by: Pavel Skripkin [email protected]
Would it make sense instead to support a RangeBound
?
@arlyon Thank you for taking a look!
I am far from expert in Rust :( I don't get how implementing this trait would help solving set/clear for bit range. IIUC this trait allows to find range bounds, so it may help with finding if bit is set. However I don't see how to manipulate ranges via this trait
I wound appreciate if you will help me understand your suggestion.
Thanks!
RangeBound is implemented by 0..3 (Range) 0..=3 (RangeInclusive), ..4 (RangeTo), 5.. (RangeFrom) and .. (RangeFull) so something that is generic over these would allow you to do any of the following:
bytes.set_range(..);
bytes.clear_range(..);
bytes.set_range(..5);
bytes.clear_range(3..=6);
You can then choose which syntax you want based on the situation (and also removes the need to check whether the method is inclusive or not!). Then set_range
and clear_range
checks the lower and upper bound on the range you give it to determine which bits to set or clear.
@arlyon Thank you so much for explanations!
Your idea is clear to me now, and I think it will make code more "rusty". I will update the PR soon.
Thanks!