调整 java BitSet 的大小

发布于 2024-09-27 09:48:53 字数 630 浏览 11 评论 0原文

我对 BitSet 类进行了子类化以添加一些附加方法。其中之一称为“折叠”。 它将 BitSet 分成两半,并用 or 将它们“组合”。 (增加信息密度)

这可行,但折叠后的 BitSet 的大小 (1024) 仍然是原始大小。

代码:

    BitSet firstHalf;
    BitSet secondHalf;
    for(int i = nrOfTimes; i > 0; i-- ){
        firstHalf = this.get(0, this.size()/2);
        secondHalf = this.get(this.size()/2, this.size());
        firstHalf.or(secondHalf);
        this.clear();
        this.or(firstHalf);
    }

可能可以返回所需长度的新 BitSet,但只能通过为每次迭代创建一个新的较小的 BitSet,但您仍然需要重新分配它(myClass = myClass.fold())。如果你折叠了,那么对原始版本就没有兴趣了。这个想法是节省空间(内存和数据库)。

有没有办法减少当前 BitSet 的大小? (我没有看到的“伎俩”?)

I sub classed the BitSet class to add some additional methods. One of the is called "fold".
It splits the BitSet in 2 halves and "combines" them with an or.
(increases Information density)

This works but the size (1024) of the folded BitSet is still the original size.

Code:

    BitSet firstHalf;
    BitSet secondHalf;
    for(int i = nrOfTimes; i > 0; i-- ){
        firstHalf = this.get(0, this.size()/2);
        secondHalf = this.get(this.size()/2, this.size());
        firstHalf.or(secondHalf);
        this.clear();
        this.or(firstHalf);
    }

It's probably doable to return a new BitSet of desired length but only by creating a new smaller one for each iteration but still you would need to re-assign it (myClass = myClass.fold()). If you fold, there is no interest in the original version. The idea is to save space (memory and DB).

Is there a way to reduce the size of the current BitSet? (a "trick" I'm not seeing?)

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

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

发布评论

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

评论(2

葬心 2024-10-04 09:48:53

我认为这样做myClass = myClass.fold()是可以的,你不必担心“节省空间”。

如果对旧对象不感兴趣(即没有人引用它),垃圾收集器无论如何都会为您清理内存。它针对此类用例进行了很好的优化。

这种模式存在于 java 库中的所有不可变类中。例如 str = str.substring(i);bigInt = bigInt.multiply(BigInteger.TEN); 等。

I think it's okay to do myClass = myClass.fold(), you don't have to worry about "saving space".

If there is no interest in the old object (i.e., no one has a reference to it) the garbage collector will clean up the memory for you anyway. It is well optimized for these kind of use cases.

This pattern is found in all immutable classes in the java library. Take for instance str = str.substring(i); or bigInt = bigInt.multiply(BigInteger.TEN); etc.

牵你的手,一向走下去 2024-10-04 09:48:53

事实上,你是对的,clear方法将清除所有位,但不会释放任何用于保存这些位的内部存储器。

其价值在于:如果您查看 BitSet 的源代码。这些位保存在一个名为“words”的内部数组中。该数组唯一缩小的地方是私有 trimToSize() 方法。这反过来只能从 clone()writeObject() 调用,但前提是大小粘性 - 即如果code>BitSet 不是通过调用 BitSet(int nbits) 构造函数创建的。

您建议的创建新 BitSet 并重新分配它的方法是完全可以的。无论如何,原始版本都会被垃圾收集。修改后的方法可能如下所示:

public static BitSet fold(BitSet bs, int nrOfTimes)
{
    BitSet temp;
    while (nrOfTimes-- > 0)
    {
        temp = bs.get(0, bs.size()/2);
        temp.or ( bs.get(bs.size()/2, bs.size()) );
        bs.clear();
        bs.or(temp);
    }
    return temp;
}

Indeed you are correct, the clear method will clear all the bits but will not release any internal memory used to hold the bits.

For what it's worth: If you look at the source code of BitSet. The bits are hold in an internal array called words. The only place where this array is downsize is in the private trimToSize() method. This in turn is only called from clone() and writeObject(), but only if the size is not sticky -- i.e. if the BitSet was not created by calling the BitSet(int nbits) constructor.

Your suggested approach of creating a new BitSet and reassigning it is perfectly OK. The original version will be garbage collected anyway. The modified method could look like this:

public static BitSet fold(BitSet bs, int nrOfTimes)
{
    BitSet temp;
    while (nrOfTimes-- > 0)
    {
        temp = bs.get(0, bs.size()/2);
        temp.or ( bs.get(bs.size()/2, bs.size()) );
        bs.clear();
        bs.or(temp);
    }
    return temp;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文