@@ -374,7 +374,7 @@ var FuseIndex = class FuseIndex {
374374 } ;
375375 }
376376} ;
377- function createIndex ( keys , docs , { getFn = Config . getFn , fieldNormWeight = Config . fieldNormWeight } = { } ) {
377+ function createIndex$1 ( keys , docs , { getFn = Config . getFn , fieldNormWeight = Config . fieldNormWeight } = { } ) {
378378 const myIndex = new FuseIndex ( {
379379 getFn,
380380 fieldNormWeight
@@ -1042,84 +1042,181 @@ function createAnalyzer({ isCaseSensitive = false, ignoreDiacritics = false, tok
10421042
10431043//#endregion
10441044//#region src/search/token/InvertedIndex.ts
1045- function addField ( index , text , docIdx , analyzer ) {
1046- const tokens = analyzer . tokenize ( text ) ;
1047- if ( ! tokens . length ) return ;
1048- index . fieldCount ++ ;
1049- index . docFieldCount . set ( docIdx , ( index . docFieldCount . get ( docIdx ) || 0 ) + 1 ) ;
1050- const distinctTerms = new Set ( tokens ) ;
1051- let perDocTerms = index . docTermFieldHits . get ( docIdx ) ;
1052- if ( ! perDocTerms ) {
1053- perDocTerms = /* @__PURE__ */ new Map ( ) ;
1054- index . docTermFieldHits . set ( docIdx , perDocTerms ) ;
1055- }
1056- for ( const term of distinctTerms ) {
1057- perDocTerms . set ( term , ( perDocTerms . get ( term ) || 0 ) + 1 ) ;
1058- index . df . set ( term , ( index . df . get ( term ) || 0 ) + 1 ) ;
1045+ const HITS_BITS = 4 ;
1046+ const HITS_ESCAPE = ( 1 << HITS_BITS ) - 1 ;
1047+ const MAX_TERM_ID = 2 ** ( 32 - HITS_BITS ) - 1 ;
1048+ function createIndex ( ) {
1049+ return {
1050+ fieldCount : 0 ,
1051+ termInfo : /* @__PURE__ */ new Map ( ) ,
1052+ termsById : [ ] ,
1053+ get df ( ) {
1054+ const snapshot = /* @__PURE__ */ new Map ( ) ;
1055+ for ( const info of this . termsById ) if ( info . df > 0 ) snapshot . set ( info . term , info . df ) ;
1056+ return snapshot ;
1057+ } ,
1058+ pool : /* @__PURE__ */ new Uint32Array ( 1024 ) ,
1059+ poolLen : 0 ,
1060+ deadSlots : 0 ,
1061+ docStart : [ ] ,
1062+ docLen : [ ] ,
1063+ docFields : [ ]
1064+ } ;
1065+ }
1066+ function internTerm ( index , term ) {
1067+ let info = index . termInfo . get ( term ) ;
1068+ if ( info === void 0 ) {
1069+ const id = index . termsById . length ;
1070+ if ( id > MAX_TERM_ID ) throw new Error ( "Token search vocabulary exceeds the supported size" ) ;
1071+ info = {
1072+ id,
1073+ term,
1074+ df : 0
1075+ } ;
1076+ index . termInfo . set ( term , info ) ;
1077+ index . termsById . push ( info ) ;
10591078 }
1079+ return info ;
10601080}
1061- function ingestRecord ( index , record , keyCount , analyzer ) {
1062- const { i : docIdx , v, $ : fields } = record ;
1063- if ( v !== void 0 ) {
1064- addField ( index , v , docIdx , analyzer ) ;
1065- return ;
1081+ function reservePool ( index , slots ) {
1082+ const needed = index . poolLen + slots ;
1083+ if ( needed <= index . pool . length ) return ;
1084+ let capacity = index . pool . length ;
1085+ while ( capacity < needed ) capacity *= 2 ;
1086+ const next = new Uint32Array ( capacity ) ;
1087+ next . set ( index . pool . subarray ( 0 , index . poolLen ) ) ;
1088+ index . pool = next ;
1089+ }
1090+ function ensureDoc ( index , docIdx ) {
1091+ for ( let d = index . docLen . length ; d <= docIdx ; d += 1 ) {
1092+ index . docStart . push ( 0 ) ;
1093+ index . docLen . push ( 0 ) ;
1094+ index . docFields . push ( 0 ) ;
10661095 }
1067- if ( ! fields ) return ;
1068- for ( let keyIdx = 0 ; keyIdx < keyCount ; keyIdx ++ ) {
1069- const value = fields [ keyIdx ] ;
1096+ }
1097+ function collectRecord ( record , keyCount , analyzer ) {
1098+ const hits = /* @__PURE__ */ new Map ( ) ;
1099+ let fields = 0 ;
1100+ const addField = ( text ) => {
1101+ const tokens = analyzer . tokenize ( text ) ;
1102+ if ( ! tokens . length ) return ;
1103+ fields += 1 ;
1104+ for ( const term of new Set ( tokens ) ) hits . set ( term , ( hits . get ( term ) || 0 ) + 1 ) ;
1105+ } ;
1106+ const { v, $ : values } = record ;
1107+ if ( v !== void 0 ) addField ( v ) ;
1108+ else if ( values ) for ( let keyIdx = 0 ; keyIdx < keyCount ; keyIdx += 1 ) {
1109+ const value = values [ keyIdx ] ;
10701110 if ( ! value ) continue ;
1071- if ( Array . isArray ( value ) ) for ( const sub of value ) addField ( index , sub . v , docIdx , analyzer ) ;
1072- else addField ( index , value . v , docIdx , analyzer ) ;
1111+ if ( Array . isArray ( value ) ) for ( const sub of value ) addField ( sub . v ) ;
1112+ else addField ( value . v ) ;
10731113 }
1114+ return {
1115+ fields,
1116+ hits
1117+ } ;
1118+ }
1119+ function ingestRecord ( index , record , keyCount , analyzer ) {
1120+ const { fields, hits } = collectRecord ( record , keyCount , analyzer ) ;
1121+ if ( ! fields ) return ;
1122+ const docIdx = record . i ;
1123+ ensureDoc ( index , docIdx ) ;
1124+ index . fieldCount += fields ;
1125+ reservePool ( index , hits . size * 2 ) ;
1126+ const start = index . poolLen ;
1127+ const pool = index . pool ;
1128+ let p = start ;
1129+ for ( const [ term , count ] of hits ) {
1130+ const info = internTerm ( index , term ) ;
1131+ info . df += count ;
1132+ const id = info . id ;
1133+ if ( count < HITS_ESCAPE ) pool [ p ++ ] = id << HITS_BITS | count ;
1134+ else {
1135+ pool [ p ++ ] = id << HITS_BITS | HITS_ESCAPE ;
1136+ pool [ p ++ ] = count ;
1137+ }
1138+ }
1139+ index . poolLen = p ;
1140+ index . docStart [ docIdx ] = start ;
1141+ index . docLen [ docIdx ] = p - start ;
1142+ index . docFields [ docIdx ] = fields ;
1143+ }
1144+ function forEachEntry ( index , docIdx , fn ) {
1145+ const { pool } = index ;
1146+ const end = index . docStart [ docIdx ] + index . docLen [ docIdx ] ;
1147+ let p = index . docStart [ docIdx ] ;
1148+ while ( p < end ) {
1149+ const entry = pool [ p ++ ] ;
1150+ const id = entry >>> HITS_BITS ;
1151+ let count = entry & HITS_ESCAPE ;
1152+ if ( count === HITS_ESCAPE ) count = pool [ p ++ ] ;
1153+ fn ( id , count ) ;
1154+ }
1155+ }
1156+ function subtractDoc ( index , docIdx ) {
1157+ if ( docIdx >= index . docLen . length || ! index . docFields [ docIdx ] ) return ;
1158+ index . fieldCount -= index . docFields [ docIdx ] ;
1159+ forEachEntry ( index , docIdx , ( id , count ) => {
1160+ const info = index . termsById [ id ] ;
1161+ info . df = Math . max ( 0 , info . df - count ) ;
1162+ } ) ;
1163+ index . deadSlots += index . docLen [ docIdx ] ;
1164+ }
1165+ function compactPool ( index ) {
1166+ const remap = new Int32Array ( index . termsById . length ) . fill ( - 1 ) ;
1167+ const liveTerms = [ ] ;
1168+ for ( const info of index . termsById ) if ( info . df > 0 ) {
1169+ remap [ info . id ] = liveTerms . length ;
1170+ info . id = liveTerms . length ;
1171+ liveTerms . push ( info ) ;
1172+ } else index . termInfo . delete ( info . term ) ;
1173+ index . termsById = liveTerms ;
1174+ const live = index . poolLen - index . deadSlots ;
1175+ const next = new Uint32Array ( Math . max ( 1024 , live * 2 ) ) ;
1176+ const { pool } = index ;
1177+ let p = 0 ;
1178+ for ( let d = 0 ; d < index . docLen . length ; d += 1 ) {
1179+ const len = index . docLen [ d ] ;
1180+ if ( ! len ) continue ;
1181+ const end = index . docStart [ d ] + len ;
1182+ let q = index . docStart [ d ] ;
1183+ index . docStart [ d ] = p ;
1184+ while ( q < end ) {
1185+ const entry = pool [ q ++ ] ;
1186+ const hits = entry & HITS_ESCAPE ;
1187+ next [ p ++ ] = remap [ entry >>> HITS_BITS ] << HITS_BITS | hits ;
1188+ if ( hits === HITS_ESCAPE ) next [ p ++ ] = pool [ q ++ ] ;
1189+ }
1190+ }
1191+ index . pool = next ;
1192+ index . poolLen = p ;
1193+ index . deadSlots = 0 ;
10741194}
10751195function buildInvertedIndex ( records , keyCount , analyzer ) {
1076- const index = {
1077- fieldCount : 0 ,
1078- df : /* @__PURE__ */ new Map ( ) ,
1079- docFieldCount : /* @__PURE__ */ new Map ( ) ,
1080- docTermFieldHits : /* @__PURE__ */ new Map ( )
1081- } ;
1196+ const index = createIndex ( ) ;
10821197 for ( const record of records ) ingestRecord ( index , record , keyCount , analyzer ) ;
1198+ if ( index . pool . length > index . poolLen + 1024 ) index . pool = index . pool . slice ( 0 , index . poolLen + 1024 ) ;
10831199 return index ;
10841200}
10851201function addToInvertedIndex ( index , record , keyCount , analyzer ) {
10861202 ingestRecord ( index , record , keyCount , analyzer ) ;
10871203}
1088- function removeFromInvertedIndex ( index , docIdx ) {
1089- const fieldCount = index . docFieldCount . get ( docIdx ) ;
1090- if ( fieldCount === void 0 ) return ;
1091- index . fieldCount -= fieldCount ;
1092- index . docFieldCount . delete ( docIdx ) ;
1093- const perDocTerms = index . docTermFieldHits . get ( docIdx ) ;
1094- if ( ! perDocTerms ) return ;
1095- for ( const [ term , hits ] of perDocTerms ) {
1096- const next = ( index . df . get ( term ) || 0 ) - hits ;
1097- if ( next <= 0 ) index . df . delete ( term ) ;
1098- else index . df . set ( term , next ) ;
1099- }
1100- index . docTermFieldHits . delete ( docIdx ) ;
1101- }
11021204function removeAndShiftInvertedIndex ( index , removedIndices ) {
11031205 if ( removedIndices . length === 0 ) return ;
1104- const sorted = Array . from ( new Set ( removedIndices ) ) . sort ( ( a , b ) => a - b ) ;
1105- for ( const idx of sorted ) removeFromInvertedIndex ( index , idx ) ;
1106- const shift = ( oldIdx ) => {
1107- let lo = 0 ;
1108- let hi = sorted . length ;
1109- while ( lo < hi ) {
1110- const mid = lo + hi >>> 1 ;
1111- if ( sorted [ mid ] < oldIdx ) lo = mid + 1 ;
1112- else hi = mid ;
1113- }
1114- return oldIdx - lo ;
1115- } ;
1116- const firstRemoved = sorted [ 0 ] ;
1117- const shiftedDocFieldCount = /* @__PURE__ */ new Map ( ) ;
1118- for ( const [ oldKey , count ] of index . docFieldCount ) shiftedDocFieldCount . set ( oldKey > firstRemoved ? shift ( oldKey ) : oldKey , count ) ;
1119- index . docFieldCount = shiftedDocFieldCount ;
1120- const shiftedDocTermFieldHits = /* @__PURE__ */ new Map ( ) ;
1121- for ( const [ oldKey , terms ] of index . docTermFieldHits ) shiftedDocTermFieldHits . set ( oldKey > firstRemoved ? shift ( oldKey ) : oldKey , terms ) ;
1122- index . docTermFieldHits = shiftedDocTermFieldHits ;
1206+ const removed = new Set ( removedIndices ) ;
1207+ for ( const idx of removed ) subtractDoc ( index , idx ) ;
1208+ let w = 0 ;
1209+ for ( let r = 0 ; r < index . docLen . length ; r += 1 ) {
1210+ if ( removed . has ( r ) ) continue ;
1211+ index . docStart [ w ] = index . docStart [ r ] ;
1212+ index . docLen [ w ] = index . docLen [ r ] ;
1213+ index . docFields [ w ] = index . docFields [ r ] ;
1214+ w += 1 ;
1215+ }
1216+ index . docStart . length = w ;
1217+ index . docLen . length = w ;
1218+ index . docFields . length = w ;
1219+ if ( index . deadSlots > index . poolLen - index . deadSlots ) compactPool ( index ) ;
11231220}
11241221
11251222//#endregion
@@ -1155,7 +1252,7 @@ var Fuse = class {
11551252 this . _docs = docs ;
11561253 if ( index && ! ( index instanceof FuseIndex ) ) throw new Error ( INCORRECT_INDEX_TYPE ) ;
11571254 this . _ownsIndex = ! index ;
1158- this . _myIndex = index || createIndex ( this . options . keys , this . _docs , {
1255+ this . _myIndex = index || createIndex$1 ( this . options . keys , this . _docs , {
11591256 getFn : this . options . getFn ,
11601257 fieldNormWeight : this . options . fieldNormWeight
11611258 } ) ;
@@ -1410,7 +1507,7 @@ var Fuse = class {
14101507//#endregion
14111508//#region src/entry.ts
14121509Fuse . version = "7.6.0-beta.4" ;
1413- Fuse . createIndex = createIndex ;
1510+ Fuse . createIndex = createIndex$1 ;
14141511Fuse . parseIndex = parseIndex ;
14151512Fuse . config = Config ;
14161513Fuse . match = function ( pattern , text , options ) {
0 commit comments