bitmaps icon indicating copy to clipboard operation
bitmaps copied to clipboard

impl: add {set,clear}_range

Open pskrgag opened this issue 2 years ago • 4 comments

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]

pskrgag avatar Dec 10 '22 16:12 pskrgag

Would it make sense instead to support a RangeBound?

arlyon avatar Dec 12 '22 23:12 arlyon

@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!

pskrgag avatar Dec 14 '22 20:12 pskrgag

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 avatar Dec 20 '22 10:12 arlyon

@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!

pskrgag avatar Dec 21 '22 16:12 pskrgag