19 using Lucene.Net.Support;
23 namespace Lucene.Net.Util
83 protected internal long[] internalbits;
84 protected internal int wlen;
93 internalbits =
new long[Bits2words(numBits)];
94 wlen = internalbits.Length;
116 this.internalbits = bits;
117 this.wlen = numWords;
126 public override bool IsCacheable
132 public virtual long Capacity()
134 return internalbits.Length << 6;
140 public virtual long Size()
146 public virtual bool IsEmpty()
148 return Cardinality() == 0;
152 [System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Performance",
"CA1819:PropertiesShouldNotReturnArrays")]
153 public virtual long[] Bits
155 set { this.internalbits = value; }
156 get {
return internalbits; }
160 public virtual int NumWords
163 set { this.wlen = value; }
168 public virtual bool Get(
int index)
173 if (i >= internalbits.Length)
176 int bit = index & 0x3f;
177 long bitmask = 1L << bit;
178 return (internalbits[i] & bitmask) != 0;
185 public virtual bool FastGet(
int index)
190 int bit = index & 0x3f;
191 long bitmask = 1L << bit;
192 return (internalbits[i] & bitmask) != 0;
198 public virtual bool Get(
long index)
200 int i = (int) (index >> 6);
201 if (i >= internalbits.Length)
203 int bit = (int) index & 0x3f;
204 long bitmask = 1L << bit;
205 return (internalbits[i] & bitmask) != 0;
211 public virtual bool FastGet(
long index)
213 int i = (int) (index >> 6);
214 int bit = (int) index & 0x3f;
215 long bitmask = 1L << bit;
216 return (internalbits[i] & bitmask) != 0;
235 public virtual int GetBit(
int index)
238 int bit = index & 0x3f;
239 return ((
int )((ulong) (internalbits[i]) >> bit)) & 0x01;
253 public virtual void Set(
long index)
255 int wordNum = ExpandingWordNum(index);
256 int bit = (int) index & 0x3f;
257 long bitmask = 1L << bit;
258 internalbits[wordNum] |= bitmask;
265 public virtual void FastSet(
int index)
267 int wordNum = index >> 6;
268 int bit = index & 0x3f;
269 long bitmask = 1L << bit;
270 internalbits[wordNum] |= bitmask;
276 public virtual void FastSet(
long index)
278 int wordNum = (int) (index >> 6);
279 int bit = (int) index & 0x3f;
280 long bitmask = 1L << bit;
281 internalbits[wordNum] |= bitmask;
291 public virtual void Set(
long startIndex,
long endIndex)
293 if (endIndex <= startIndex)
296 int startWord = (int) (startIndex >> 6);
300 int endWord = ExpandingWordNum(endIndex - 1);
302 long startmask = - 1L << (int) startIndex;
303 long endmask = (long) (0xffffffffffffffffUL >> (
int) - endIndex);
305 if (startWord == endWord)
307 internalbits[startWord] |= (startmask & endmask);
311 internalbits[startWord] |= startmask;
312 for (
int i = startWord + 1; i < endWord; i++)
313 internalbits[i] = -1L;
314 internalbits[endWord] |= endmask;
319 protected internal virtual int ExpandingWordNum(
long index)
321 int wordNum = (int) (index >> 6);
324 EnsureCapacity(index + 1);
334 public virtual void FastClear(
int index)
336 int wordNum = index >> 6;
337 int bit = index & 0x03f;
338 long bitmask = 1L << bit;
339 internalbits[wordNum] &= ~ bitmask;
352 public virtual void FastClear(
long index)
354 int wordNum = (int) (index >> 6);
355 int bit = (int) index & 0x3f;
356 long bitmask = 1L << bit;
357 internalbits[wordNum] &= ~ bitmask;
361 public virtual void Clear(
long index)
363 int wordNum = (int) (index >> 6);
366 int bit = (int) index & 0x3f;
367 long bitmask = 1L << bit;
368 internalbits[wordNum] &= ~ bitmask;
378 public virtual void Clear(
int startIndex,
int endIndex)
380 if (endIndex <= startIndex)
383 int startWord = (startIndex >> 6);
384 if (startWord >= wlen)
389 int endWord = ((endIndex - 1) >> 6);
391 long startmask = - 1L << startIndex;
392 long endmask = (long) (0xffffffffffffffffUL >> - endIndex);
395 startmask = ~ startmask;
398 if (startWord == endWord)
400 internalbits[startWord] &= (startmask | endmask);
404 internalbits[startWord] &= startmask;
406 int middle = System.Math.Min(wlen, endWord);
407 for (
int i = startWord + 1; i < middle; i++)
408 internalbits[i] = 0L;
411 internalbits[endWord] &= endmask;
423 public virtual void Clear(
long startIndex,
long endIndex)
425 if (endIndex <= startIndex)
428 int startWord = (int) (startIndex >> 6);
429 if (startWord >= wlen)
434 int endWord = (int) ((endIndex - 1) >> 6);
436 long startmask = - 1L << (int) startIndex;
437 long endmask = (long) (0xffffffffffffffffUL >> (
int) - endIndex);
440 startmask = ~ startmask;
443 if (startWord == endWord)
445 internalbits[startWord] &= (startmask | endmask);
449 internalbits[startWord] &= startmask;
451 int middle = System.Math.Min(wlen, endWord);
452 for (
int i = startWord + 1; i < middle; i++)
453 internalbits[i] = 0L;
456 internalbits[endWord] &= endmask;
465 public virtual bool GetAndSet(
int index)
467 int wordNum = index >> 6;
468 int bit = index & 0x3f;
469 long bitmask = 1L << bit;
470 bool val = (internalbits[wordNum] & bitmask) != 0;
471 internalbits[wordNum] |= bitmask;
478 public virtual bool GetAndSet(
long index)
480 int wordNum = (int) (index >> 6);
481 int bit = (int) index & 0x3f;
482 long bitmask = 1L << bit;
483 bool val = (internalbits[wordNum] & bitmask) != 0;
484 internalbits[wordNum] |= bitmask;
491 public virtual void FastFlip(
int index)
493 int wordNum = index >> 6;
494 int bit = index & 0x3f;
495 long bitmask = 1L << bit;
496 internalbits[wordNum] ^= bitmask;
502 public virtual void FastFlip(
long index)
504 int wordNum = (int) (index >> 6);
505 int bit = (int) index & 0x3f;
506 long bitmask = 1L << bit;
507 internalbits[wordNum] ^= bitmask;
511 public virtual void Flip(
long index)
513 int wordNum = ExpandingWordNum(index);
514 int bit = (int) index & 0x3f;
515 long bitmask = 1L << bit;
516 internalbits[wordNum] ^= bitmask;
522 public virtual bool FlipAndGet(
int index)
524 int wordNum = index >> 6;
525 int bit = index & 0x3f;
526 long bitmask = 1L << bit;
527 internalbits[wordNum] ^= bitmask;
528 return (internalbits[wordNum] & bitmask) != 0;
534 public virtual bool FlipAndGet(
long index)
536 int wordNum = (int) (index >> 6);
537 int bit = (int) index & 0x3f;
538 long bitmask = 1L << bit;
539 internalbits[wordNum] ^= bitmask;
540 return (internalbits[wordNum] & bitmask) != 0;
550 public virtual void Flip(
long startIndex,
long endIndex)
552 if (endIndex <= startIndex)
554 int startWord = (int) (startIndex >> 6);
558 int endWord = ExpandingWordNum(endIndex - 1);
567 long startmask = - 1L << (int) startIndex;
568 long endmask = (long) (0xffffffffffffffffUL >> (
int) - endIndex);
570 if (startWord == endWord)
572 internalbits[startWord] ^= (startmask & endmask);
576 internalbits[startWord] ^= startmask;
578 for (
int i = startWord + 1; i < endWord; i++)
580 internalbits[i] = ~ internalbits[i];
583 internalbits[endWord] ^= endmask;
612 public virtual long Cardinality()
622 return BitUtil.
Pop_intersect(a.internalbits, b.internalbits, 0, System.Math.Min(a.wlen, b.wlen));
630 long tot =
BitUtil.
Pop_union(a.internalbits, b.internalbits, 0, System.Math.Min(a.wlen, b.wlen));
635 else if (a.wlen > b.wlen)
648 long tot =
BitUtil.
Pop_andnot(a.internalbits, b.internalbits, 0, System.Math.Min(a.wlen, b.wlen));
661 long tot =
BitUtil.
Pop_xor(a.internalbits, b.internalbits, 0, System.Math.Min(a.wlen, b.wlen));
666 else if (a.wlen > b.wlen)
677 public virtual int NextSetBit(
int index)
682 int subIndex = index & 0x3f;
683 long word = internalbits[i] >> subIndex;
687 return (i << 6) + subIndex +
BitUtil.
Ntz(word);
692 word = internalbits[i];
703 public virtual long NextSetBit(
long index)
705 int i = (int) (index >> 6);
708 int subIndex = (int) index & 0x3f;
709 long word = (long) ((ulong) internalbits[i] >> subIndex);
713 return (((
long) i) << 6) + (subIndex +
BitUtil.
Ntz(word));
718 word = internalbits[i];
729 public virtual System.Object Clone()
738 catch (System.Exception e)
740 throw new System.SystemException(e.Message, e);
747 int newLen = System.Math.Min(this.wlen, other.wlen);
748 long[] thisArr = this.internalbits;
749 long[] otherArr = other.internalbits;
754 thisArr[pos] &= otherArr[pos];
756 if (this.wlen > newLen)
759 for (
int i = newLen; i < this.wlen; i++)
760 internalbits[i] = 0L;
768 int newLen = System.Math.Max(wlen, other.wlen);
769 EnsureCapacityWords(newLen);
771 long[] thisArr = this.internalbits;
772 long[] otherArr = other.internalbits;
773 int pos = System.Math.Min(wlen, other.wlen);
776 thisArr[pos] |= otherArr[pos];
778 if (this.wlen < newLen)
780 Array.Copy(otherArr, this.wlen, thisArr, this.wlen, newLen - this.wlen);
789 int idx = System.Math.Min(wlen, other.wlen);
790 long[] thisArr = this.internalbits;
791 long[] otherArr = other.internalbits;
794 thisArr[idx] &= ~ otherArr[idx];
801 int newLen = System.Math.Max(wlen, other.wlen);
802 EnsureCapacityWords(newLen);
804 long[] thisArr = this.internalbits;
805 long[] otherArr = other.internalbits;
806 int pos = System.Math.Min(wlen, other.wlen);
809 thisArr[pos] ^= otherArr[pos];
811 if (this.wlen < newLen)
813 Array.Copy(otherArr, this.wlen, thisArr, this.wlen, newLen - this.wlen);
842 int pos = System.Math.Min(this.wlen, other.wlen);
843 long[] thisArr = this.internalbits;
844 long[] otherArr = other.internalbits;
847 if ((thisArr[pos] & otherArr[pos]) != 0)
858 public virtual void EnsureCapacityWords(
int numWords)
860 if (internalbits.Length < numWords)
869 public virtual void EnsureCapacity(
long numBits)
871 EnsureCapacityWords(Bits2words(numBits));
877 public virtual void TrimTrailingZeros()
880 while (idx >= 0 && internalbits[idx] == 0)
886 public static int Bits2words(
long numBits)
888 return (
int) ((((numBits - 1) >> 6)) + 1);
893 public override bool Equals(System.Object o)
902 if (b.wlen >
this.wlen)
912 for (
int i = a.wlen - 1; i >= b.wlen; i--)
914 if (a.internalbits[i] != 0)
918 for (
int i = b.wlen - 1; i >= 0; i--)
920 if (a.internalbits[i] != b.internalbits[i])
927 public override int GetHashCode()
932 for (
int i = internalbits.Length; --i >= 0; )
934 h ^= internalbits[i];
939 return (
int)(((h >> 32) ^ h) + 0x98761234);