如何实现位向量(bitset)(在Java中)?

发布于 2024-12-11 03:54:27 字数 206 浏览 0 评论 0原文

是否有一些好的文本、书籍、pdf 或网站解释如何实现位向量,尤其是在 Java 中?

我问这个问题是因为我想用 Java 实现我自己的 BitSet 实现。原因是我想添加额外的功能和调整,如果我修改 java.util 中的 BitSet Java 类,则无法完成这些功能和调整。此外,我想制作自己的实现,以便我可以在我的开源项目中使用它,而无需处理许可证。

谢谢!

Is there some good text, books, pdf or website that explains how to implement a bit vector, especially in Java?

I ask this question because I would like to make my own BitSet implementation in Java. The reason is that I want to add aditional features and tweak that cannot be done if I modify the BitSet Java class from java.util. Moreover, I want to make my own implementation so that I can use it in my open-source project without having to deal with licenses.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀梦 2024-12-18 03:54:27

如果您想要位向量或位集具有出色的性能或其他出色的功能,那么正如有人已经建议的那样,您应该继承位向量/位集的现有实现。或者,您可以参考一些开源实现。不过,如果你想了解位向量的机制,那就相当简单了。以下是一个实现示例:

class BitSet{
    private Byte[] p;

    private BitSet(){
        p = null;
    }

    public BitSet(int n){
        assert n > 0;
        p = new Byte[(n - 1) >> 3 + 1];
    }

    public BitSet Complement(){
        BitSet bs = new BitSet();
        bs.p = new Byte[p.length];
        for(int i = 0; i < p.length; i++){
            bs.p[i] = ~ p[i];
        }
        return bs;
    }

    public BitSet Union(BitSet bs2){
        assert p.length == bs2.p.length;
        BitSet bs = new BitSet();
        bs.p = new Byte[p.length];
        for(int i = 0; i < p.length; i++){
            bs.p[i] = p[i] | bs2.p[i];
        }
        return bs;
    }

    public BitSet Intersection(BitSet bs2){
        assert p.length == bs2.p.length;
        BitSet bs = new BitSet();
        bs.p = new Byte[p.length];
        for(int i = 0; i < p.length; i++){
            bs.p[i] = p[i] & bs2.p[i];
        }
        return bs;
    }
}

您可以在上面的示例中实现并添加您自己的 set-wise 操作功能。

If you want fancy performance or other fancy features for your bit vector or bit set, then as some one already suggested, you should inherit an existing implementation of bit vector/set. Or, you may refer to some open source implementations. However, if you want to learn the mechanism of bit vector, it is rather simple. Here's an implementation as example:

class BitSet{
    private Byte[] p;

    private BitSet(){
        p = null;
    }

    public BitSet(int n){
        assert n > 0;
        p = new Byte[(n - 1) >> 3 + 1];
    }

    public BitSet Complement(){
        BitSet bs = new BitSet();
        bs.p = new Byte[p.length];
        for(int i = 0; i < p.length; i++){
            bs.p[i] = ~ p[i];
        }
        return bs;
    }

    public BitSet Union(BitSet bs2){
        assert p.length == bs2.p.length;
        BitSet bs = new BitSet();
        bs.p = new Byte[p.length];
        for(int i = 0; i < p.length; i++){
            bs.p[i] = p[i] | bs2.p[i];
        }
        return bs;
    }

    public BitSet Intersection(BitSet bs2){
        assert p.length == bs2.p.length;
        BitSet bs = new BitSet();
        bs.p = new Byte[p.length];
        for(int i = 0; i < p.length; i++){
            bs.p[i] = p[i] & bs2.p[i];
        }
        return bs;
    }
}

You may implement and add your own set-wise operation features into the above example.

扬花落满肩 2024-12-18 03:54:27

快速实施您的要求。希望有帮助。

    public class BitSet
    {
        int[] numbers;
        public BitSet(int k){
            numbers = new int[(k >> 5) + 1];
        }
        public void set(int k)
        {
            int remender = k & 0x1F;
            int devide = k >> 5;
            result[devide] = result[devide] | (1<<remender);
        }

        public void unset(int k)
        {
            int remender = k & 0x1F;
            int devide = k >> 5;
            result[devide] = result[devide] & (~(1<<remender));
        }

        public boolean isSet(int k)
        {
            int remender = k & 0x1F;
            int devide = k >> 5;
            return (result[devide] & (1<<remender))!=0;
        }
    }

A quick implementation for your requirement. Hope it helps.

    public class BitSet
    {
        int[] numbers;
        public BitSet(int k){
            numbers = new int[(k >> 5) + 1];
        }
        public void set(int k)
        {
            int remender = k & 0x1F;
            int devide = k >> 5;
            result[devide] = result[devide] | (1<<remender);
        }

        public void unset(int k)
        {
            int remender = k & 0x1F;
            int devide = k >> 5;
            result[devide] = result[devide] & (~(1<<remender));
        }

        public boolean isSet(int k)
        {
            int remender = k & 0x1F;
            int devide = k >> 5;
            return (result[devide] & (1<<remender))!=0;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文