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

std.bitmanip.FixedBitArray #9610

Open
dlangBugzillaToGithub opened this issue Jul 15, 2013 · 0 comments
Open

std.bitmanip.FixedBitArray #9610

dlangBugzillaToGithub opened this issue Jul 15, 2013 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-07-15T17:00:54Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=10650

Description

This is a struct with no defined constructor that uses a bit array of statically known size:


import std.bitmanip: BitArray;
struct Foo {
    enum nBits = 1_024;
    size_t[nBits / size_t.sizeof] buffer;
    BitArray bitSet;
    bool isInitialized = false;

    void bar() /*pure nothrow*/ {
        if (!isInitialized) {
             bitSet.init(buffer, nBits);
             isInitialized = true;
         }

        // .......
    }
}
void main() {}



A statically known size is useful to reduce pressure a bit on the GC, to increase cache locality, etc. So I suggest to introduce in std.bitmanip a simple FixedBitArray based on BitArray that offers a simpler usage for statically known sizes of bit arrays:


struct FixedBitArray(size_t nBits) {
    private size_t[nBits / size_t.sizeof + (nBits % size_t.sizeof) ? 1 : 0] buffer;
...
}


import std.bitmanip: FixedBitArray;
struct Foo {
    FixedBitArray!(1_024) bitSet;

    void bar() pure nothrow {
        // .......
    }
}
void main() {}


An alternative name is "BoundedBitArray" as in the Ada 2012 bounded collections.

An alternative is to modify BitArray to allow both usages nicely.
@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants