Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conventions and cleanup for Bitv and BitvSet #19216

Merged
merged 6 commits into from
Dec 23, 2014
Merged

Conversation

Gankra
Copy link
Contributor

@Gankra Gankra commented Nov 22, 2014

Part of #18424

This commit changes the semantics of reserve and capacity for Bitv and BitvSet to match conventions. It also introduces the notion of reserve_index and reserve_index_exact for collections with maximum-index-based capacity semantics.

Deprecates free function constructors in favour of functions on Bitv itself.

Changes Bitv::pop to return an Option rather than panicking.

Deprecates and renames several methods in favour of conventions.

Marks several blessed methods as unstable.

This commit also substantially refactors Bitv and BitvSet's implementations. The new implementation is simpler, cleaner, better documented, and more robust against overflows. It also reduces coupling between Bitv and BitvSet. Tests have been seperated into seperate submodules.

Fixes #16958

[breaking-change]

@Gankra
Copy link
Contributor Author

Gankra commented Nov 22, 2014

r? @aturon @huonw @apoelstra

@aturon
Copy link
Member

aturon commented Nov 22, 2014

Part of #18428

That looks like the wrong reference.

@Gankra
Copy link
Contributor Author

Gankra commented Nov 22, 2014

Whoops, fixed.

/// false, false, false, true,
/// false, false, true, false]));
/// ```
pub fn from_bytes(bytes: &[u8]) -> Bitv {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could consider providing FromIterator/Extend instances for bool, u8, u16, u32 and u64 instead. That would give you maximal flexibility while keeping the API surface thin and standard.

(I suppose this would put further pressure on the performance of these traits, though. Perhaps it's worth spending time on that soon.)

@aturon
Copy link
Member

aturon commented Nov 22, 2014

I've reviewed the API changes and left a few comments. The biggest thing is that, in the long run, I'd really like to reduce the number of ad hoc from and grow functions in favor of iterators (both here and in Vec), but need to investigate ideas about performance first. I'm happy to land these API changes in the meantime.

I didn't have time right now to review the implementation changes in any detail -- maybe one of the other reviewers you mentioned can do that. But I'm happy with the API.

/// Iterator over the underlying blocks of data
fn blocks(&self) -> Take<Cloned<Items<u32>>> {
let blocks = blocks_for_bits(self.len());
self.storage.iter().cloned().take(blocks)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these would be more efficient as self.storage.slice_to(blocks).iter().cloned(). That just stores two pointers (and updates one), while .take stores the pointers and a uint and updates a pointer and the uint on each step (LLVM may optimise this out, but there's no guarantee).

cuviper referenced this pull request in Gankra/rust Dec 7, 2014
Part of rust-lang#18424

This commit changes the semantics of `reserve` and `capacity` for Bitv and BitvSet to match conventions. It also introduces the notion of `reserve_index` and `reserve_index_exact` for collections with maximum-index-based capacity semantics.

Deprecates free function constructors in favour of functions on Bitv itself.

Changes `Bitv::pop` to return an Option rather than panicking.

Deprecates and renames several methods in favour of conventions.

Marks several blessed methods as unstable.

This commit also substantially refactors Bitv and BitvSet's implementations. The new implementation is simpler, cleaner, better documented, and more robust against overflows. It also reduces coupling between Bitv and BitvSet. Tests have been seperated into seperate submodules.

Fixes rust-lang#16958

[breaking-change]
use std::hash;

use vec::Vec;

// FIXME(conventions): look, we just need to refactor this whole thing. Inside and out.
type Blocks<'a> = Cloned<Items<'a, u32>>
type MutBlocks<'a> MutItems<'a, u32>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing ; on Blocks and = in MutBlocks.

@cuviper
Copy link
Member

cuviper commented Dec 7, 2014

After I fix the above noted things, I can build your branch, and indeed my test_32_elements() addition also fails here.

@Gankra
Copy link
Contributor Author

Gankra commented Dec 8, 2014

@cuviper Oh, woah thanks! I've had this branch on the backburner for school work and because it needs an RFC for some of the changes. Thanks for looking at this!

Feel free to send a PR with all your fixes!

@Gankra
Copy link
Contributor Author

Gankra commented Dec 10, 2014

Merged in @cuviper's fixes. Still need to wait on /rust-lang/rfcs/pull/509 to merge this, though.

(not bothering with rebasing until then)

@Gankra Gankra force-pushed the bitv branch 3 times, most recently from 4765043 to 2f52dfb Compare December 15, 2014 16:04
@Gankra
Copy link
Contributor Author

Gankra commented Dec 15, 2014

I have rebased on to master with some fixes from @tbu-

Thanks everyone!

@Gankra Gankra force-pushed the bitv branch 2 times, most recently from 9620f4d to 7781058 Compare December 18, 2014 20:24
@Gankra
Copy link
Contributor Author

Gankra commented Dec 18, 2014

@huonw I've rebased and fixed up stuff to match the collections reform part 2 RFC, which has been merged.

I've also squashed the commits as much as I consider appropriate. The first commit is all the previously reviewed work.

r?

@Gankra
Copy link
Contributor Author

Gankra commented Dec 18, 2014

(Full disclosure: I have not had time to make check this at all)

I checked this pretty far but got a spurious error. May re-run if I find time.

@Gankra Gankra force-pushed the bitv branch 4 times, most recently from f9c5548 to 78c8b3f Compare December 20, 2014 05:28
Gankra and others added 5 commits December 20, 2014 09:10
Part of rust-lang#18424

This commit changes the semantics of `reserve` and `capacity` for Bitv and BitvSet to match conventions. It also introduces the notion of `reserve_index` and `reserve_index_exact` for collections with maximum-index-based capacity semantics.

Deprecates free function constructors in favour of functions on Bitv itself.

Changes `Bitv::pop` to return an Option rather than panicking.

Deprecates and renames several methods in favour of conventions.

Marks several blessed methods as unstable.

This commit also substantially refactors Bitv and BitvSet's implementations. The new implementation is simpler, cleaner, better documented, and more robust against overflows. It also reduces coupling between Bitv and BitvSet. Tests have been seperated into seperate submodules.

Fixes rust-lang#16958

[breaking-change]
- Fix typos on Blocks and MutBlocks.
- Use slice_to_mut() for creating blocks_mut().
- Deref the block parameter in get().
- Access nbits separately from mutating set in pop().
The old logic would be ok with *either* 0 or all 1s in the last word,
because it didn't compute a proper mask for the case where nbits is an
exact multiple of u32::BITS.

Add mask_for_bits() to compute this properly, and use it in all().  Add
all/none assertions to most of the tests.  Note in particular, the all-zero
bitv in test_32_elements() was incorrectly all()==true before this patch.
The length of the underlying vector must now be exactly as long as it needs to
be.
Also fix up some tests from last commit.
@Gankra
Copy link
Contributor Author

Gankra commented Dec 20, 2014

Rebased

@homu homu mentioned this pull request Dec 22, 2014
barosl added a commit to barosl/rust that referenced this pull request Dec 22, 2014
@alexcrichton
Copy link
Member

Looks great to me, thanks @gankro! r=me with a squashing of those last few commits.

bors added a commit that referenced this pull request Dec 22, 2014
Part of #18424

This commit changes the semantics of `reserve` and `capacity` for Bitv and BitvSet to match conventions. It also introduces the notion of `reserve_index` and `reserve_index_exact` for collections with maximum-index-based capacity semantics.

Deprecates free function constructors in favour of functions on Bitv itself.

Changes `Bitv::pop` to return an Option rather than panicking.

Deprecates and renames several methods in favour of conventions.

Marks several blessed methods as unstable.

This commit also substantially refactors Bitv and BitvSet's implementations. The new implementation is simpler, cleaner, better documented, and more robust against overflows. It also reduces coupling between Bitv and BitvSet. Tests have been seperated into seperate submodules.

Fixes #16958

[breaking-change]
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Dec 22, 2014
Part of rust-lang#18424

This commit changes the semantics of `reserve` and `capacity` for Bitv and BitvSet to match conventions. It also introduces the notion of `reserve_index` and `reserve_index_exact` for collections with maximum-index-based capacity semantics.

Deprecates free function constructors in favour of functions on Bitv itself.

Changes `Bitv::pop` to return an Option rather than panicking.

Deprecates and renames several methods in favour of conventions.

Marks several blessed methods as unstable.

This commit also substantially refactors Bitv and BitvSet's implementations. The new implementation is simpler, cleaner, better documented, and more robust against overflows. It also reduces coupling between Bitv and BitvSet. Tests have been seperated into seperate submodules.

Fixes rust-lang#16958

[breaking-change]
@alexcrichton
Copy link
Member

Gonna try to push the rollup ahead of this (this PR is in the rollup)

Rebasing that rollup is gonna be awful if it comes to that...

bors added a commit that referenced this pull request Dec 22, 2014
Part of #18424

This commit changes the semantics of `reserve` and `capacity` for Bitv and BitvSet to match conventions. It also introduces the notion of `reserve_index` and `reserve_index_exact` for collections with maximum-index-based capacity semantics.

Deprecates free function constructors in favour of functions on Bitv itself.

Changes `Bitv::pop` to return an Option rather than panicking.

Deprecates and renames several methods in favour of conventions.

Marks several blessed methods as unstable.

This commit also substantially refactors Bitv and BitvSet's implementations. The new implementation is simpler, cleaner, better documented, and more robust against overflows. It also reduces coupling between Bitv and BitvSet. Tests have been seperated into seperate submodules.

Fixes #16958

[breaking-change]
@bors bors merged commit 20d7a5f into rust-lang:master Dec 23, 2014
lnicola added a commit to lnicola/rust that referenced this pull request Mar 3, 2025
internal: Downgrade to ubuntu-22.04 for aarch64 and arm builds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bitv internal nbits counter silently overflows for huge bitvs on 32-bit platforms
8 participants