bodil / bitmaps Goto Github PK
View Code? Open in Web Editor NEWBitmap types for Rust
License: Mozilla Public License 2.0
Bitmap types for Rust
License: Mozilla Public License 2.0
How do I serialize bitmaps (to bytes)? If I use bincode. I can't implement the Serialize interface for it.
thanks
checking for is_full with Bitmap of size 32 and all bits set to true, always panics.
let mut bitmap = Bitmap::<32>::mask(32);
let z = bitmap.is_full();
println!("Yes full {:?}", z);
thread 'main' panicked at 'attempt to shift left with overflow', /Users/kumargu/.cargo/registry/src/github.com-1ecc6299db9ec823/bitmaps-3.2.0/src/types.rs:550:1
This is because boolean values don't support all bit patterns.
An object with the boolean type has a size and alignment of 1 each. The value false has the bit pattern 0x00 and the value true has the bit pattern 0x01. It is undefined behavior for an object with the boolean type to have any other bit pattern.
https://doc.rust-lang.org/reference/types/boolean.html
Both these tests fail when run with Miri:
#[test]
fn test1() {
let _ = Bitmap::<1>::try_from([0xff].as_slice());
}
#[test]
fn test2() {
let mut bm = Bitmap::<1>::new();
bm.as_mut()[0] = 0xff;
let _ = bm.into_value();
}
test test1 ... error: Undefined Behavior: constructing invalid value at .value: encountered 0xff, but expected a boolean
--> /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/bitmaps-3.2.1/src/bitmap.rs:144:27
|
144 | data: data.assume_init(),
| ^^^^^^^^^^^^^^^^^^ constructing invalid value at .value: encountered 0xff, but expected a boolean
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
= note: BACKTRACE on thread `test1`:
= note: inside `<bitmaps::Bitmap<1> as std::convert::TryFrom<&[u8]>>::try_from` at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/bitmaps-3.2.1/src/bitmap.rs:144:27: 144:45
test test2 ... error: Undefined Behavior: constructing invalid value at .data: encountered 0xff, but expected a boolean
--> lib.rs:9:10
|
9 | let _ = bm.into_value();
| ^^ constructing invalid value at .data: encountered 0xff, but expected a boolean
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
= note: BACKTRACE on thread `test2`:
= note: inside `test2` at lib.rs:9:10: 9:12
For all uXXX
there's leading_zeroes and trailing_zeroes, but this last operation is not useable in bitmaps.
I'd like to use these operations for getting the last entry of the bitmap, but this is currently impossible to do in this manner. Finding the last entry is difficult.
Similarly, for iterating over the entries in the bitmap, it would be tremendously helpful if either an iterator were implemented for Bitmap, or a set of operations next_index / prev_index would be implemented.
next_index(after: usize)
can technically be derived from (mask(index).invert() & map).first_index()
, but that's a waste of cycles making, inverting and applying the mask, if this same operation could be done efficiently on the bitmap store itself.
For prev_index (next_index but now backwards) I could not determine an efficient method for determining a previous index based on some offset, due to the lack of last_index-support.
Hello,
Would it be possible to have a method as_value()
on Bitmap
?
I guess the implementation would simply be:
pub fn as_value(&self) -> &Size::Store {
&self.data
}
Besides, the reason I am trying to get the underlying value is to be able to implement PartialOrd
, Eq
, Ord
, and Hash
on a custom type that uses a Bitmap
.
Is there any reason why those implementations are not available out of the box even though the underlying value implement them ?
The following test failes for the last assert. The iterator yields 0,1,2,3 which is out of range.
#[test]
fn iterate_in_range() {
let mut m = bitmaps::Bitmap::<3>::new();
m.invert();
let mut iter = m.into_iter();
assert_eq!(iter.next(), Some(0));
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
// assert_eq!(iter.next(), Some(3));
assert_eq!(iter.next(), None);
}
When using .invert()
on a Bitmap<SIZE>
and then iterating over the indices, SIZE
appears as an index.
Minimal working example:
use bitmaps::{
Bitmap,
};
fn main() {
let mut my_map: Bitmap<4> = Bitmap::new();
my_map.set(0,true);
my_map.set(2,true);
println!("== TRUE =============");
for index in &my_map{
println!("index: {}", index);
//println!("value: {}", my_map.get(index));
}
my_map.invert();
println!("== FALSE ============");
for index in &my_map{
println!("index: {}", index);
//println!("value: {}", my_map.get(index));
}
}
Expected Output:
== TRUE =============
index: 0
index: 2
== FALSE ============
index: 1
index: 3
Actual Output:
== TRUE =============
index: 0
index: 2
== FALSE ============
index: 1
index: 3
index: 4
This becomes especially dramatic when you un-comment lines 12
and 19
as it results in the program panicking.
Cargo.toml:
[Dependencies]
bitmaps = "3.2.0"
I want build a bitmap from a array of u8. Like this:
let data = [u8; 1024];
//read data from file
let bmap = BitMpa::from(data)
I'm unnerved by passing around naked booleans, for fear the sense will be misinterpreted, incompatible boolean parameters silently swapped, etc.
Could there be a sensible way to allow symbolic values to be used in a bitmap
?
In my case, to represent whether a pushbutton key is pressed or not, I defined:
pub enum KeyPosition {
Up,
Down,
}
But to use this in a bitmap
I have to keep converting to and from bool
. Could bitmap
be extended to hold something like my enum? (Or is this possible already and I don't spot it?)
Thanks,
-kb
This was noticed due to a complete audit of all source code licenses shipped as part of Fedora Linux.
The package.license
field in Cargo.toml
is interpreted as an "SPDX 2.1 license expression", and must contain only values from the "SPDX license list 3.11" according to the cargo reference docs:
https://doc.rust-lang.org/cargo/reference/manifest.html#the-license-and-license-file-fields
This crate specifies its license as MPL-2.0+
(I assume this is supposed to mean "MPL-2.0 or any later version"), but this is not a valid SPDX identifier.
The official list of all valid identifiers is available here: https://spdx.org/licenses/
This list does not contain a variant of MPL-2.0 that also accepts any later versions.
Would it be possible to change the specified license to just "MPL-2.0"
? It is the closest valid match.
Thank you!
Please correct me if I'm wrong, but isn't the assertion of mask()
slightly wrong
Line 126 in 6534d3b
debug_assert!(bits <= SIZE);
instead?
As I understand it, the assertions currently prevent the masking of the complete Bitset as the last index cannot be included.
Looking forward to your opinion.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.