![](/img/trans.png)
[英]What would be the fastest way to intersect two bitsets into a new BitSet in Java?
[英]Java BitSet which allows easy Concatenation of BitSets
我需要一個BitSet,它可以輕松地將多個BitSet串聯起來以創建一個新的BitSet。 默認實現沒有這種方法。
你們中任何人都知道某個外部庫中有任何實現可以輕松串聯的實現嗎?
例如,假設我有一個位數組11111和另一個位數組010101。我想要附加功能。 因此,連接后將得到11111010101。
好吧,因為沒有左移方法,所以無法實現這種極其有效的性能(即性能和內存)。
您可以使用明顯的nextSetBit
進行循環-速度慢,但內存效率高。
大概更快的方法是在一個toLongArray
上使用toLongArray
,將其正確復制到足夠大的數組中,然后從該數組中創建一個位集,或與另一個數組一起創建一個位集。 這樣,您就不會對單個位進行任何移位,而是對字化的塊進行處理。
這為我工作:
BitSet concatenate_vectors(BitSet vector_1_in, BitSet vector_2_in) {
BitSet vector_1_in_clone = (BitSet)vector_1_in.clone();
BitSet vector_2_in_clone = (BitSet)vector_2_in.clone();
int n = 5;//_desired length of the first (leading) vector
int index = -1;
while (index < (vector_2_in_clone.length() - 1)) {
index = vector_2_in_clone.nextSetBit((index + 1));
vector_1_in_clone.set((index + n));
}
return vector_1_in_clone;
}
結果:11111010101
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.