@@ -1053,9 +1053,17 @@ class RoaringBitmap {
10531053 contains ( keyvalue ) {
10541054 const key = keyvalue >> 16 ;
10551055 const value = keyvalue & 0xFFFF ;
1056- for ( let i = 0 ; i < this . keys . length ; ++ i ) {
1057- if ( this . keys [ i ] === key ) {
1058- return this . containers [ i ] . contains ( value ) ;
1056+ let left = 0 ;
1057+ let right = this . keys . length - 1 ;
1058+ while ( left <= right ) {
1059+ const mid = Math . floor ( ( left + right ) / 2 ) ;
1060+ const x = this . keys [ mid ] ;
1061+ if ( x < key ) {
1062+ left = mid + 1 ;
1063+ } else if ( x > key ) {
1064+ right = mid - 1 ;
1065+ } else {
1066+ return this . containers [ mid ] . contains ( value ) ;
10591067 }
10601068 }
10611069 return false ;
@@ -1068,11 +1076,18 @@ class RoaringBitmapRun {
10681076 this . array = array ;
10691077 }
10701078 contains ( value ) {
1071- const l = this . runcount * 4 ;
1072- for ( let i = 0 ; i < l ; i += 4 ) {
1079+ let left = 0 ;
1080+ let right = this . runcount - 1 ;
1081+ while ( left <= right ) {
1082+ const mid = Math . floor ( ( left + right ) / 2 ) ;
1083+ const i = mid * 4 ;
10731084 const start = this . array [ i ] | ( this . array [ i + 1 ] << 8 ) ;
10741085 const lenm1 = this . array [ i + 2 ] | ( this . array [ i + 3 ] << 8 ) ;
1075- if ( value >= start && value <= ( start + lenm1 ) ) {
1086+ if ( ( start + lenm1 ) < value ) {
1087+ left = mid + 1 ;
1088+ } else if ( start > value ) {
1089+ right = mid - 1 ;
1090+ } else {
10761091 return true ;
10771092 }
10781093 }
@@ -1085,10 +1100,17 @@ class RoaringBitmapArray {
10851100 this . array = array ;
10861101 }
10871102 contains ( value ) {
1088- const l = this . cardinality * 2 ;
1089- for ( let i = 0 ; i < l ; i += 2 ) {
1090- const start = this . array [ i ] | ( this . array [ i + 1 ] << 8 ) ;
1091- if ( value === start ) {
1103+ let left = 0 ;
1104+ let right = this . cardinality - 1 ;
1105+ while ( left <= right ) {
1106+ const mid = Math . floor ( ( left + right ) / 2 ) ;
1107+ const i = mid * 2 ;
1108+ const x = this . array [ i ] | ( this . array [ i + 1 ] << 8 ) ;
1109+ if ( x < value ) {
1110+ left = mid + 1 ;
1111+ } else if ( x > value ) {
1112+ right = mid - 1 ;
1113+ } else {
10921114 return true ;
10931115 }
10941116 }
0 commit comments