调整 java BitSet 的大小
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这样做
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);
orbigInt = bigInt.multiply(BigInteger.TEN);
etc.事实上,你是对的,
clear
方法将清除所有位,但不会释放任何用于保存这些位的内部存储器。其价值在于:如果您查看
BitSet
的源代码。这些位保存在一个名为“words”的内部数组中。该数组唯一缩小的地方是私有trimToSize()
方法。这反过来只能从clone()
和writeObject()
调用,但前提是大小不粘性 - 即如果code>BitSet 不是通过调用BitSet(int nbits)
构造函数创建的。您建议的创建新
BitSet
并重新分配它的方法是完全可以的。无论如何,原始版本都会被垃圾收集。修改后的方法可能如下所示: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 calledwords
. The only place where this array is downsize is in the privatetrimToSize()
method. This in turn is only called fromclone()
andwriteObject()
, but only if the size is not sticky -- i.e. if theBitSet
was not created by calling theBitSet(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: