0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 package org.apache.spark.unsafe.bitset;
0019
0020 import org.apache.spark.unsafe.Platform;
0021
0022
0023
0024
0025
0026
0027
0028
0029 public final class BitSetMethods {
0030
0031 private static final long WORD_SIZE = 8;
0032
0033 private BitSetMethods() {
0034
0035 }
0036
0037
0038
0039
0040 public static void set(Object baseObject, long baseOffset, int index) {
0041 assert index >= 0 : "index (" + index + ") should >= 0";
0042 final long mask = 1L << (index & 0x3f);
0043 final long wordOffset = baseOffset + (index >> 6) * WORD_SIZE;
0044 final long word = Platform.getLong(baseObject, wordOffset);
0045 Platform.putLong(baseObject, wordOffset, word | mask);
0046 }
0047
0048
0049
0050
0051 public static void unset(Object baseObject, long baseOffset, int index) {
0052 assert index >= 0 : "index (" + index + ") should >= 0";
0053 final long mask = 1L << (index & 0x3f);
0054 final long wordOffset = baseOffset + (index >> 6) * WORD_SIZE;
0055 final long word = Platform.getLong(baseObject, wordOffset);
0056 Platform.putLong(baseObject, wordOffset, word & ~mask);
0057 }
0058
0059
0060
0061
0062 public static boolean isSet(Object baseObject, long baseOffset, int index) {
0063 assert index >= 0 : "index (" + index + ") should >= 0";
0064 final long mask = 1L << (index & 0x3f);
0065 final long wordOffset = baseOffset + (index >> 6) * WORD_SIZE;
0066 final long word = Platform.getLong(baseObject, wordOffset);
0067 return (word & mask) != 0;
0068 }
0069
0070
0071
0072
0073 public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInWords) {
0074 long addr = baseOffset;
0075 for (int i = 0; i < bitSetWidthInWords; i++, addr += WORD_SIZE) {
0076 if (Platform.getLong(baseObject, addr) != 0) {
0077 return true;
0078 }
0079 }
0080 return false;
0081 }
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 public static int nextSetBit(
0102 Object baseObject,
0103 long baseOffset,
0104 int fromIndex,
0105 int bitsetSizeInWords) {
0106 int wi = fromIndex >> 6;
0107 if (wi >= bitsetSizeInWords) {
0108 return -1;
0109 }
0110
0111
0112 final int subIndex = fromIndex & 0x3f;
0113 long word = Platform.getLong(baseObject, baseOffset + wi * WORD_SIZE) >> subIndex;
0114 if (word != 0) {
0115 return (wi << 6) + subIndex + java.lang.Long.numberOfTrailingZeros(word);
0116 }
0117
0118
0119 wi += 1;
0120 while (wi < bitsetSizeInWords) {
0121 word = Platform.getLong(baseObject, baseOffset + wi * WORD_SIZE);
0122 if (word != 0) {
0123 return (wi << 6) + java.lang.Long.numberOfTrailingZeros(word);
0124 }
0125 wi += 1;
0126 }
0127
0128 return -1;
0129 }
0130 }