@devisfuture/mega-collection 2.4.3 → 2.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-BJZg_1dI.mjs +183 -0
- package/dist/chunk-BURK5sks.mjs +505 -0
- package/dist/chunk-C5Zpu_ol.mjs +18 -0
- package/dist/filter/index.d.mts +4 -110
- package/dist/filter/index.mjs +891 -1
- package/dist/index.d.mts +4 -2
- package/dist/index.mjs +2 -1
- package/dist/merge/index.d.mts +4 -127
- package/dist/merge/index.mjs +2 -1
- package/dist/search/index.d.mts +4 -189
- package/dist/search/index.mjs +1010 -5
- package/dist/sort/index.d.mts +4 -54
- package/dist/sort/index.mjs +416 -1
- package/package.json +19 -8
- package/dist/State-2vKzwNrQ.d.mts +0 -44
- package/dist/chunk-IZXO22NQ.mjs +0 -1
- package/dist/chunk-MB56OSRQ.mjs +0 -1
- package/dist/types-DONld7xY.d.mts +0 -74
package/dist/filter/index.mjs
CHANGED
|
@@ -1 +1,891 @@
|
|
|
1
|
-
import {a,b}from'../chunk-MB56OSRQ.mjs';var v=class{constructor(e={indexes:new Map,itemPositions:new Map}){this.storage=e;}addItems(e){if(!(e.length===0||this.storage.indexes.size===0))for(let t=0;t<e.length;t++)this.addItem(e[t]);}buildIndex(e,t){let i=new Map,s=new Map;for(let n=0,r=e.length;n<r;n++){let l=e[n],a=l[t];if(a==null)continue;let d=i.get(a);if(d)d.push(l),s.get(a).set(l,d.length-1);else {i.set(a,[l]);let o=new WeakMap;o.set(l,0),s.set(a,o);}}this.storage.indexes.set(t,i),this.storage.itemPositions.set(t,s);}getByValue(e,t){let i=this.storage.indexes.get(e);return i?i.get(t)??[]:[]}getByValues(e,t){let i=this.storage.indexes.get(e);if(!i)return [];if(t.length===1)return i.get(t[0])??[];if(t.length===2){let n=i.get(t[0]),r=i.get(t[1]);return n?r?n.concat(r):n:r??[]}let s=[];for(let n=0;n<t.length;n++){let r=i.get(t[n]);if(r!==void 0)for(let l=0;l<r.length;l++)s.push(r[l]);}return s}hasIndex(e){return this.storage.indexes.has(e)}addItem(e){for(let t of this.storage.indexes.keys())this.addItemToField(t,e);}updateItem(e,t){for(let i of this.storage.indexes.keys())this.updateItemInField(i,e,t);}removeItem(e){for(let t of this.storage.indexes.keys())this.removeItemFromField(t,e);}clear(){this.storage.indexes.clear(),this.storage.itemPositions.clear();}getIndexMap(e){return this.storage.indexes.get(e)}removeItemFromField(e,t){let i=t[e];if(i==null)return;let s=this.storage.indexes.get(e),n=this.storage.itemPositions.get(e),r=s?.get(i),l=n?.get(i),a=l?.get(t);if(!s||!n||!r||!l||a===void 0)return;let d=r.length-1,o=r[d];a!==d&&(r[a]=o,l.set(o,a)),r.pop(),l.delete(t),r.length===0&&(s.delete(i),n.delete(i));}updateItemInField(e,t,i){let s=t[e],n=i[e];if(s===n){if(s==null)return;this.replaceItemReferenceInField(e,s,t,i);return}this.removeItemFromField(e,t),this.addItemToField(e,i);}replaceItemReferenceInField(e,t,i,s){let n=this.storage.indexes.get(e),r=this.storage.itemPositions.get(e),l=n?.get(t),a=r?.get(t),d=a?.get(i);!l||!a||d===void 0||(l[d]=s,a.delete(i),a.set(s,d));}addItemToField(e,t){let i=t[e];if(i==null)return;let s=this.storage.indexes.get(e),n=this.storage.itemPositions.get(e);if(!s||!n)return;let r=s.get(i),l=n.get(i);if(r&&l){r.push(t),l.set(t,r.length-1);return}s.set(i,[t]);let a=new WeakMap;a.set(t,0),n.set(i,a);}};var V=()=>({indexedFields:new Set,indexerStorage:{indexes:new Map,itemPositions:new Map},nestedStorage:{indexes:new Map,itemPositions:new Map},deferredMutationVersion:null,mutableExclude:{datasetPositions:new Map,valueCounts:new Map,duplicateValueCount:0,hasDuplicateValues:false},sequentialCache:{previousResult:null,previousCriteria:null,previousCriteriaKey:null,previousBaseData:null,previousResultsByCriteria:new Map,previousResultSet:null}});function x(h){return {value:h,enumerable:false,configurable:true,writable:true}}var T=class{constructor(e){this.callbacks=e;}create(e){let t=e;return Object.defineProperties(t,{filter:x((i,s)=>s===void 0?this.callbacks.filter(e,i):this.callbacks.filter(i,s)),add:x(i=>this.callbacks.add(i)),update:x(i=>this.callbacks.update(i)),clearIndexes:x(()=>this.callbacks.clearIndexes()),data:x(i=>this.callbacks.data(i)),getOriginData:x(()=>this.callbacks.getOriginData()),clearData:x(()=>this.callbacks.clearData()),resetFilterState:x(()=>this.callbacks.resetFilterState())}),t}};var m=class h extends Error{constructor(e){super(e),this.name="FilterEngineError";}static missingDatasetForBuildIndex(){return new h("FilterEngine: no dataset in memory. Call data() or add() before buildIndex().")}static missingDatasetForFilter(){return new h("FilterEngine: no dataset in memory. Call data() or add() before filter().")}static duplicateMutableExcludeField(e){return new h(`FilterEngine: cannot use mutable exclude on field \`${e}\` because it contains duplicate values.`)}};function y(h){let e=new Map;for(let i=0;i<h.length;i++){let s=h[i],n=Array.isArray(s.values),r=w(s.exclude),l=r!==null,a=n?P(s.values,r):null;if(!n&&!l)continue;let d=s.field,o=e.get(d);if(!o){e.set(d,{field:s.field,values:[],exclude:[],hasValues:n,hasExclude:l,includedValues:a?new Set(a):null,excludedValues:r?new Set(r):null,cacheKeySegment:""});continue}if(n)if(!o.hasValues)o.hasValues=true,o.includedValues=new Set(a);else for(let u of o.includedValues)a.has(u)||o.includedValues.delete(u);if(l)if(!o.hasExclude)o.hasExclude=true,o.excludedValues=new Set(r);else for(let u of r)o.excludedValues.add(u);}let t=[...e.values()].sort((i,s)=>i.field.localeCompare(s.field));for(let i=0;i<t.length;i++)D(t[i]);return t}function C(h,e){return !(h.hasValues&&!h.includedValues.has(e)||h.hasExclude&&h.excludedValues.has(e))}function M(h){return h.hasValues&&h.values.length===0}function w(h){return !Array.isArray(h)||h.length===0?null:new Set(h)}function P(h,e){let t=new Set;for(let i=0;i<h.length;i++){let s=h[i];e?.has(s)||t.add(s);}return t}function D(h){if(h.hasValues&&h.hasExclude)for(let e of h.excludedValues)h.includedValues.delete(e);h.values=h.includedValues?[...h.includedValues]:[],h.exclude=h.excludedValues?[...h.excludedValues]:[],h.cacheKeySegment=B(h);}function B(h){let e=h.field;return e+=`|hv:${h.hasValues?"1":"0"}|v:`,h.hasValues&&(e+=R(h.values)),e+=`|hx:${h.hasExclude?"1":"0"}|x:`,h.hasExclude&&(e+=R(h.exclude)),e}function R(h){return h.length===0?"":h.map(e=>q(e)).sort().join(",")}function q(h){if(h===null)return "null:null";let e=typeof h;return e==="object"?`object:${JSON.stringify(h)}`:`${e}:${String(h)}`}var k="deferFilterMutationIndexUpdates";var I=class{constructor(e={indexes:new Map,itemPositions:new Map}){this.storage=e;this.registeredFields=new Set;this.fieldDescriptors=new Map;}registerFields(e){if(e?.length)for(let t=0;t<e.length;t++)this.registerField(e[t]);}hasRegisteredFields(){return this.registeredFields.size>0}hasField(e){return this.registeredFields.has(e)}clearIndexes(){this.storage.indexes.clear(),this.storage.itemPositions.clear();}buildIndexes(e){this.storage.indexes.clear(),this.storage.itemPositions.clear();for(let t of this.registeredFields)this.buildIndex(e,t);}addItems(e){if(!(e.length===0||this.storage.indexes.size===0))for(let t=0;t<e.length;t++)this.addItem(e[t]);}removeItem(e){for(let t of this.storage.indexes.keys())this.removeItemFromIndex(t,e);}updateItem(e,t){for(let i of this.storage.indexes.keys())this.updateItemInIndex(i,e,t);}filter(e,t,i){let s=this.resolveCriteria(t);if(e.length===0||s.length===0)return e;let n=[],r=[];for(let a=0;a<s.length;a++){let d=s[a];if(this.storage.indexes.has(d.field)){n.push(d);continue}r.push(d);}let l=e;if(n.length>0&&(l=this.filterByIndexes(n,e,i),l.length===0))return l;for(let a=0;a<r.length;a++)if(l=this.filterLinearly(l,r[a]),l.length===0)return l;return l}resolveCriteria(e){if(e.length===0)return [];let t=e[0];return "hasValues"in t&&"hasExclude"in t&&"includedValues"in t?e:y(e)}registerField(e){let t=this.createDescriptor(e);t&&(this.registeredFields.add(e),this.fieldDescriptors.set(e,t));}createDescriptor(e){let t=e.indexOf(".");return t===-1?null:{collectionKey:e.substring(0,t),nestedKey:e.substring(t+1)}}buildIndex(e,t){let i=this.fieldDescriptors.get(t);if(!i)return;let{collectionKey:s,nestedKey:n}=i,r=new Map,l=new Map;for(let a=0,d=e.length;a<d;a++){let o=e[a],u=o[s];if(Array.isArray(u))for(let f=0;f<u.length;f++){let c=u[f][n];if(c==null)continue;let p=r.get(c);if(p){p[p.length-1]!==o&&(p.push(o),l.get(c).set(o,p.length-1));continue}r.set(c,[o]);let g=new WeakMap;g.set(o,0),l.set(c,g);}}this.storage.indexes.set(t,r),this.storage.itemPositions.set(t,l);}removeItemFromIndex(e,t){let i=this.fieldDescriptors.get(e),s=this.storage.indexes.get(e),n=this.storage.itemPositions.get(e);if(!i||!s||!n)return;let{collectionKey:r,nestedKey:l}=i,a=t[r];if(!Array.isArray(a)||a.length===0)return;let d=new Set;for(let o=0;o<a.length;o++){let u=a[o][l];u!=null&&d.add(u);}for(let o of d){let u=s.get(o),f=n.get(o),c=f?.get(t);if(!u||!f||c===void 0)continue;let p=u.length-1,g=u[p];c!==p&&(u[c]=g,f.set(g,c)),u.pop(),f.delete(t),u.length===0&&(s.delete(o),n.delete(o));}}addItem(e){for(let t of this.storage.indexes.keys())this.addItemToIndex(t,e);}addItemToIndex(e,t){let i=this.fieldDescriptors.get(e),s=this.storage.indexes.get(e),n=this.storage.itemPositions.get(e);if(!i||!s||!n)return;let{collectionKey:r,nestedKey:l}=i,a=t[r];if(!Array.isArray(a)||a.length===0)return;let d=new Set;for(let o=0;o<a.length;o++){let u=a[o][l];u!=null&&d.add(u);}for(let o of d){let u=s.get(o),f=n.get(o);if(u&&f){u.push(t),f.set(t,u.length-1);continue}s.set(o,[t]);let c=new WeakMap;c.set(t,0),n.set(o,c);}}updateItemInIndex(e,t,i){let s=this.fieldDescriptors.get(e),n=this.storage.indexes.get(e),r=this.storage.itemPositions.get(e);if(!s||!n||!r)return;let l=this.collectUniqueValues(i,s),a=this.collectUniqueValues(t,s);if(this.areValueSetsEqual(l,a)){for(let d of l){let o=n.get(d),u=r.get(d),f=u?.get(i);!o||!u||f===void 0||(o[f]=t,u.delete(i),u.set(t,f));}return}this.removeItemFromIndex(e,i),this.addItemToIndex(e,t);}collectUniqueValues(e,t){let{collectionKey:i,nestedKey:s}=t,n=e[i],r=new Set;if(!Array.isArray(n)||n.length===0)return r;for(let l=0;l<n.length;l++){let a=n[l][s];a!=null&&r.add(a);}return r}areValueSetsEqual(e,t){if(e.size!==t.size)return false;for(let i of e)if(!t.has(i))return false;return true}filterByIndexes(e,t,i){let s=e.filter(o=>o.hasValues),n=e.filter(o=>o.hasExclude),r=t===i?null:new Set(t);if(s.length===0)return this.applyIndexedExclusions(t,n);if(s.length===1){let o=this.storage.indexes.get(s[0].field);if(!o)return [];let u=this.getItemsByValues(o,s[0].values);if(u.length===0)return [];let f=[];for(let c=0;c<u.length;c++){let p=u[c];r&&!r.has(p)||f.push(p);}return this.applyIndexedExclusions(f,n)}let l=s.map(o=>({criterion:o,size:this.estimateIndexSize(o)})).sort((o,u)=>o.size-u.size),a=r,d=[];for(let o=0;o<l.length;o++){let{criterion:u}=l[o],f=this.storage.indexes.get(u.field);if(!f)return [];let c=this.getItemsByValues(f,u.values);if(c.length===0)return [];if(a===null)d=c;else {d=[];for(let p=0;p<c.length;p++){let g=c[p];a.has(g)&&d.push(g);}}if(d.length===0)return [];a=new Set(d);}return this.applyIndexedExclusions(d,n)}getItemsByValues(e,t){if(t.length===1)return e.get(t[0])??[];let i=new Set,s=[];for(let n=0;n<t.length;n++){let r=e.get(t[n]);if(r)for(let l=0;l<r.length;l++){let a=r[l];i.has(a)||(i.add(a),s.push(a));}}return s}estimateIndexSize(e){let t=this.storage.indexes.get(e.field);return t?e.values.reduce((i,s)=>{let n=t.get(s);return n?i+n.length:i},0):1/0}filterLinearly(e,t){let i=this.fieldDescriptors.get(t.field);if(!i)return e;let{collectionKey:s,nestedKey:n}=i,r=[];for(let l=0;l<e.length;l++){let a=e[l],d=a[s];if(!Array.isArray(d))continue;let o=!t.hasValues,u=false;for(let f=0;f<d.length;f++){let c=d[f][n];if(t.hasExclude&&t.excludedValues.has(c)){u=true;break}t.hasValues&&t.includedValues.has(c)&&(o=true);}!u&&o&&r.push(a);}return r}applyIndexedExclusions(e,t){if(t.length===0||e.length===0)return e;let i=new Set;for(let n=0;n<t.length;n++){let r=t[n],l=this.storage.indexes.get(r.field);if(!l)continue;let a=this.getItemsByValues(l,r.exclude);for(let d=0;d<a.length;d++)i.add(a[d]);}if(i.size===0)return e;let s=[];for(let n=0;n<e.length;n++){let r=e[n];i.has(r)||s.push(r);}return s}};var F=class{constructor(e={}){this.chainBuilder=new T({filter:(e,t)=>t===void 0?this.filter(e):this.filter(e,t),getOriginData:()=>this.getOriginData(),add:e=>this.add(e),update:e=>this.update(e),data:e=>this.data(e),clearIndexes:()=>this.clearIndexes(),clearData:()=>this.clearData(),resetFilterState:()=>this.resetFilterState()});this.mutableExcludeField=e.mutableExcludeField??null,this.state=e.state??new a(e.data??[],{filterByPreviousResult:e.filterByPreviousResult??false}),e.state&&e.filterByPreviousResult&&this.state.setFilterByPreviousResult(true),this.filterByPreviousResult=this.state.isFilterByPreviousResultEnabled(),this.namespace=this.state.createNamespace("filter"),this.indexer=new v(this.runtime.indexerStorage),this.nestedCollection=new I(this.runtime.nestedStorage),this.nestedCollection.registerFields(e.nestedFields),this.state.subscribe(s=>this.handleStateMutation(s)),this.rebuildMutableExcludeState();let t=e.fields?.length,i=this.nestedCollection.hasRegisteredFields();if(t)for(let s of e.fields)this.indexedFields.add(s);this.dataset.length>0&&(t||i)&&this.rebuildConfiguredIndexes();}get dataset(){return this.state.getOriginData()}get runtime(){return this.state.getOrCreateScopedValue(this.namespace,"runtime",V)}get mutableExcludeState(){return this.runtime.mutableExclude}get indexedFields(){return this.runtime.indexedFields}get sequentialCache(){return this.runtime.sequentialCache}shouldDeferMutationIndexUpdates(){return this.state.getScopedValue(b,k)===true}markDeferredMutationState(){this.runtime.deferredMutationVersion=this.state.getMutationVersion(),this.indexer.clear(),this.nestedCollection.clearIndexes(),this.clearMutableExcludeState(),this.resetFilterState();}ensureRuntimeReady(){this.runtime.deferredMutationVersion!==null&&(this.runtime.deferredMutationVersion=null,this.rebuildMutableExcludeState(),this.dataset.length>0&&(this.indexedFields.size>0||this.nestedCollection.hasRegisteredFields())&&this.rebuildConfiguredIndexes());}rebuildConfiguredIndexes(){this.indexer.clear(),this.nestedCollection.clearIndexes();for(let e of this.indexedFields)this.buildIndex(this.dataset,e);this.nestedCollection.hasRegisteredFields()&&this.nestedCollection.buildIndexes(this.dataset);}buildIndex(e,t){if(!Array.isArray(e)){if(!this.dataset.length)throw m.missingDatasetForBuildIndex();return this.indexer.buildIndex(this.dataset,e),this}return this.resetFilterState(),this.rebuildMutableExcludeState(),this.indexer.buildIndex(e,t),this}clearIndexes(){return this.indexer.clear(),this.nestedCollection.clearIndexes(),this}resetFilterState(){return this.sequentialCache.previousResult=null,this.sequentialCache.previousCriteria=null,this.sequentialCache.previousCriteriaKey=null,this.sequentialCache.previousBaseData=null,this.sequentialCache.previousResultsByCriteria.clear(),this.sequentialCache.previousResultSet=null,this.state.clearPreviousResult(),this}clearData(){return this.state.clearData(),this}data(e){return this.state.data(e),this}add(e){return this.state.add(e),this}update(e){return this.state.update(e),this}applyAddedItems(e,t){return e.length===0?this:(this.updateMutableExcludeStateForAddedItems(e,t),this.resetFilterState(),this.indexer.addItems(e),this.nestedCollection.addItems(e),this)}getOriginData(){return this.state.getOriginData()}filter(e,t){return t===void 0?this.withChain(this.rawFilter(e)):this.withChain(this.rawFilter(e,t))}rawFilter(e,t){this.ensureRuntimeReady();let i=t===void 0;if(i&&!this.dataset.length)throw m.missingDatasetForFilter();let s=y(i?e:t),n=i?this.dataset:e;if(i){let c=this.applyMutableExclude(s);if(c!==void 0)return c}let r=n,l=s;if(this.filterByPreviousResult){let c=this.resolveWithSequentialCache(n,i,s);if(c.isFromCache)return c.result;r=c.sourceData,l=c.executionCriteria;}if(s.length===0)return this.filterByPreviousResult&&this.resetFilterState(),n;for(let c=0;c<l.length;c++)if(M(l[c]))return this.createEmptyResult(i,n,s);let a=[],d=[];for(let c=0;c<l.length;c++){let p=l[c];this.nestedCollection.hasField(p.field)?a.push(p):d.push(p);}if(a.length>0&&(r=this.nestedCollection.filter(r,a,this.dataset),r.length===0))return this.createEmptyResult(i,n,s);if(d.length===0)return this.storePreviousResult(r,i,n,s),r;let o=[],u=[];for(let c=0;c<d.length;c++){let p=d[c];this.indexer.hasIndex(p.field)?o.push(p):u.push(p);}let f;if(o.length>0&&u.length===0)return f=this.filterViaIndex(o,r),this.storePreviousResult(f,i,n,s),f;if(o.length>0&&u.length>0){let c=this.filterViaIndex(o,r);return f=this.linearFilter(c,u),this.storePreviousResult(f,i,n,s),f}return f=this.linearFilter(r,d),this.storePreviousResult(f,i,n,s),f}resolveWithSequentialCache(e,t,i){let{previousResult:s,previousCriteria:n,previousCriteriaKey:r,previousBaseData:l,previousResultsByCriteria:a}=this.sequentialCache;if(s===null||n===null||l!==e)return {isFromCache:false,sourceData:e,executionCriteria:i};let d=this.createCriteriaCacheKey(i),o=a.get(d);if(d===r)return {isFromCache:true,result:s};if(o!==void 0)return this.storePreviousResult(o.result,t,e,i,d,o.resultSet),{isFromCache:true,result:o.result};let u=this.hasCriteriaBacktrack(n,i)||s.length===0&&!this.canApplySequentiallyToEmptyResult(n,i);return {isFromCache:false,sourceData:u?e:s,executionCriteria:u?i:this.createSequentialExecutionCriteria(n,i)}}withChain(e){return this.chainBuilder.create(e)}handleStateMutation(e){if(this.shouldDeferMutationIndexUpdates()&&(e.type==="add"||e.type==="update")){this.markDeferredMutationState();return}switch(e.type){case "add":this.applyAddedItems(e.items,e.startIndex);return;case "update":this.applyUpdatedItem(e.index,e.previousItem,e.nextItem);return;case "data":this.runtime.deferredMutationVersion=null,this.rebuildMutableExcludeState(),this.resetFilterState(),this.rebuildConfiguredIndexes();return;case "clearData":this.runtime.deferredMutationVersion=null,this.clearMutableExcludeState(),this.indexer.clear(),this.nestedCollection.clearIndexes(),this.resetFilterState();return;case "remove":this.applyRemovedItem(e.field,e.value,e.removedItem,e.removedIndex,e.movedItem);return;case "removeMany":this.applyRemovedItems(e.field,e.entries);return}}rebuildMutableExcludeState(){if(this.clearMutableExcludeState(),this.mutableExcludeField!==null)for(let e=0;e<this.dataset.length;e++){let i=this.dataset[e][this.mutableExcludeField];i!=null&&this.registerMutableExcludeValue(i,e);}}updateMutableExcludeStateForAddedItems(e,t){if(this.mutableExcludeField!==null)for(let i=0;i<e.length;i++){let s=e[i][this.mutableExcludeField];s!=null&&this.registerMutableExcludeValue(s,t+i);}}applyMutableExclude(e){if(this.mutableExcludeField===null||e.length!==1)return;let t=e[0];if(!(t.field!==this.mutableExcludeField||t.hasValues||!t.hasExclude)){if(this.mutableExcludeState.hasDuplicateValues)throw m.duplicateMutableExcludeField(this.mutableExcludeField);return this.state.removeByFieldValues(this.mutableExcludeField,t.exclude),this.resetFilterState(),this.dataset}}applyUpdatedItem(e,t,i){this.updateMutableExcludeStateForUpdatedItem(e,t,i),this.resetFilterState(),this.indexer.updateItem(t,i),this.nestedCollection.updateItem(i,t);}applyRemovedItem(e,t,i,s,n){if(e===this.mutableExcludeField&&(this.unregisterMutableExcludeValue(t),this.mutableExcludeState.datasetPositions.delete(t),n!==null&&this.mutableExcludeField!==null)){let r=n[this.mutableExcludeField];r!=null&&this.mutableExcludeState.datasetPositions.set(r,s);}this.resetFilterState(),this.indexer.removeItem(i),this.nestedCollection.removeItem(i);}applyRemovedItems(e,t){for(let i=0;i<t.length;i++){let s=t[i];if(e===this.mutableExcludeField&&(this.unregisterMutableExcludeValue(s.value),this.mutableExcludeState.datasetPositions.delete(s.value),s.movedItem!==null&&this.mutableExcludeField!==null)){let n=s.movedItem[this.mutableExcludeField];n!=null&&this.mutableExcludeState.datasetPositions.set(n,s.removedIndex);}this.indexer.removeItem(s.removedItem),this.nestedCollection.removeItem(s.removedItem);}this.resetFilterState();}updateMutableExcludeStateForUpdatedItem(e,t,i){if(this.mutableExcludeField===null)return;let s=t[this.mutableExcludeField],n=i[this.mutableExcludeField];if(s===n){n!=null&&this.mutableExcludeState.datasetPositions.set(n,e);return}s!=null&&(this.unregisterMutableExcludeValue(s),this.mutableExcludeState.datasetPositions.get(s)===e&&this.mutableExcludeState.datasetPositions.delete(s)),n!=null&&this.registerMutableExcludeValue(n,e);}clearMutableExcludeState(){this.mutableExcludeState.datasetPositions.clear(),this.mutableExcludeState.valueCounts.clear(),this.mutableExcludeState.duplicateValueCount=0,this.mutableExcludeState.hasDuplicateValues=false;}registerMutableExcludeValue(e,t){let i=(this.mutableExcludeState.valueCounts.get(e)??0)+1;i===2&&this.mutableExcludeState.duplicateValueCount++,this.mutableExcludeState.valueCounts.set(e,i),this.mutableExcludeState.datasetPositions.set(e,t),this.mutableExcludeState.hasDuplicateValues=this.mutableExcludeState.duplicateValueCount>0;}unregisterMutableExcludeValue(e){let t=this.mutableExcludeState.valueCounts.get(e);t!==void 0&&(t===2&&this.mutableExcludeState.duplicateValueCount--,t<=1?this.mutableExcludeState.valueCounts.delete(e):this.mutableExcludeState.valueCounts.set(e,t-1),this.mutableExcludeState.hasDuplicateValues=this.mutableExcludeState.duplicateValueCount>0);}linearFilter(e,t){let i=[];for(let s=0;s<e.length;s++){let n=e[s],r=true;for(let l=0;l<t.length;l++){let a=t[l];if(!C(a,n[a.field])){r=false;break}}r&&i.push(n);}return i}filterViaIndex(e,t){let i=t!==this.dataset,s=null;if(i){if(t===this.sequentialCache.previousResult&&this.sequentialCache.previousResultSet!==null)s=this.sequentialCache.previousResultSet;else if(s=new Set(t),t===this.sequentialCache.previousResult){this.sequentialCache.previousResultSet=s;let u=this.sequentialCache.previousCriteriaKey;if(u!==null){let f=this.sequentialCache.previousResultsByCriteria.get(u);f&&(f.resultSet=s);}}}let n=[],r=[];for(let u=0;u<e.length;u++){let f=e[u];f.hasValues&&n.push(f),f.hasExclude&&r.push(f);}if(n.length===0)return this.applyIndexedExclusions(t,r);if(n.length===1){let u=n[0],f=false;for(let g=0;g<r.length;g++)if(r[g].field!==u.field){f=true;break}if(s===null&&!f)return u.values.length===1?this.indexer.getByValue(u.field,u.values[0]).slice():this.indexer.getByValues(u.field,u.values);let c=this.indexer.getByValues(u.field,u.values),p=[];for(let g=0;g<c.length;g++){let b=c[g];s&&!s.has(b)||p.push(b);}return this.applyIndexedExclusions(p,r)}let l=n[0],a=this.estimateIndexSize(l);for(let u=1;u<n.length;u++){let f=n[u],c=this.estimateIndexSize(f);c<a&&(l=f,a=c);}let d=this.indexer.getByValues(l.field,l.values);if(d.length===0)return [];let o=[];for(let u=0;u<d.length;u++){let f=d[u],c=true;if(!(s&&!s.has(f))){for(let p=0;p<n.length;p++){let g=n[p];if(g!==l&&!C(g,f[g.field])){c=false;break}}c&&o.push(f);}}return this.applyIndexedExclusions(o,r)}estimateIndexSize(e){let t=this.indexer.getIndexMap(e.field);if(!t)return 1/0;let i=0;for(let s=0;s<e.values.length;s++){let n=t.get(e.values[s]);n&&(i+=n.length);}return i}applyIndexedExclusions(e,t){if(t.length===0||e.length===0)return e;let i=new Set;for(let n=0;n<t.length;n++){let r=t[n],l=this.indexer.getIndexMap(r.field);if(l)for(let a=0;a<r.exclude.length;a++){let d=l.get(r.exclude[a]);if(d)for(let o=0;o<d.length;o++)i.add(d[o]);}}if(i.size===0)return e;let s=[];for(let n=0;n<e.length;n++){let r=e[n];i.has(r)||s.push(r);}return s}createEmptyResult(e,t,i){let s=[];return this.storePreviousResult(s,e,t,i),s}storePreviousResult(e,t,i,s,n=this.createCriteriaCacheKey(s),r){if(!this.filterByPreviousResult)return;let l=r===void 0?null:r;this.sequentialCache.previousResult=e,this.sequentialCache.previousCriteria=s,this.sequentialCache.previousCriteriaKey=n,this.sequentialCache.previousBaseData=t?this.dataset:i,this.sequentialCache.previousResultSet=l,this.state.setPreviousResult(e,t?this.dataset:i),this.sequentialCache.previousResultsByCriteria.set(n,this.createSequentialCacheEntry(e,l));}createCriteriaCacheKey(e){let t="";for(let i=0;i<e.length;i++)t+=e[i].cacheKeySegment,t+=";";return t}createSequentialCacheEntry(e,t){return {result:e,resultSet:t}}createSequentialExecutionCriteria(e,t){let i=new Map;for(let s=0;s<e.length;s++){let n=e[s];i.set(n.field,n);}return t.map(s=>{let n=i.get(s.field);if(!n||!n.hasValues||!s.hasValues||n.hasExclude||s.hasExclude||s.includedValues.size<=n.includedValues.size)return s;for(let l of n.includedValues)if(!s.includedValues.has(l))return s;let r=[];for(let l of s.includedValues)n.includedValues.has(l)||r.push(l);return r.length===0?s:{...s,values:r,includedValues:new Set(r)}})}hasCriteriaBacktrack(e,t){let i=this.createCriteriaStateMap(e),s=this.createCriteriaStateMap(t);for(let[n,r]of i){let l=s.get(n);if(!l||this.isCriterionBacktracked(r,l))return true}return false}canApplySequentiallyToEmptyResult(e,t){let i=this.createCriteriaStateMap(e),s=this.createCriteriaStateMap(t);for(let[n,r]of s){let l=i.get(n);if(l){if(l.hasValues!==r.hasValues&&(l.hasValues||!r.hasValues))return false;if(l.hasValues&&r.hasValues){let a=l.includedValues,d=r.includedValues;if(!this.areSetsEqual(a,d)&&!this.isSubset(a,d))return false}if(l.hasExclude!==r.hasExclude&&(l.hasExclude||!r.hasExclude))return false;if(l.hasExclude&&r.hasExclude){let a=l.excludedValues,d=r.excludedValues;if(!this.areSetsEqual(a,d)&&!this.isSubset(a,d))return false}}}return true}createCriteriaStateMap(e){let t=new Map;for(let i=0;i<e.length;i++){let s=e[i];t.set(s.field,s);}return t}isCriterionBacktracked(e,t){if(e.hasValues&&!t.hasValues)return true;if(e.hasValues&&t.hasValues){let i=e.includedValues,s=t.includedValues;if(!this.areSetsEqual(i,s))return this.isSubset(s,i)}if(!e.hasValues&&t.hasValues)return false;if(e.hasExclude&&!t.hasExclude)return true;if(!e.hasExclude&&t.hasExclude)return false;if(e.hasExclude&&t.hasExclude){let i=e.excludedValues,s=t.excludedValues;if(!this.areSetsEqual(i,s))return this.isSubset(s,i)}return false}isSubset(e,t){for(let i of e)if(!t.has(i))return false;return true}areSetsEqual(e,t){if(e===t)return true;if(!e||!t||e.size!==t.size)return false;for(let i of e)if(!t.has(i))return false;return true}};export{F as FilterEngine};
|
|
1
|
+
import { a as e, i as t, t as n } from "../chunk-BJZg_1dI.mjs";
|
|
2
|
+
import { n as r, t as i } from "../chunk-C5Zpu_ol.mjs";
|
|
3
|
+
//#region src/indexer.ts
|
|
4
|
+
var a = class {
|
|
5
|
+
constructor(e = {
|
|
6
|
+
indexes: /* @__PURE__ */ new Map(),
|
|
7
|
+
itemPositions: /* @__PURE__ */ new Map()
|
|
8
|
+
}) {
|
|
9
|
+
this.storage = e;
|
|
10
|
+
}
|
|
11
|
+
addItems(e) {
|
|
12
|
+
if (!(e.length === 0 || this.storage.indexes.size === 0)) for (let t = 0; t < e.length; t++) this.addItem(e[t]);
|
|
13
|
+
}
|
|
14
|
+
buildIndex(e, t) {
|
|
15
|
+
let n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map();
|
|
16
|
+
for (let i = 0, a = e.length; i < a; i++) {
|
|
17
|
+
let a = e[i], o = a[t];
|
|
18
|
+
if (o == null) continue;
|
|
19
|
+
let s = n.get(o);
|
|
20
|
+
if (s) s.push(a), r.get(o).set(a, s.length - 1);
|
|
21
|
+
else {
|
|
22
|
+
n.set(o, [a]);
|
|
23
|
+
let e = /* @__PURE__ */ new WeakMap();
|
|
24
|
+
e.set(a, 0), r.set(o, e);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
this.storage.indexes.set(t, n), this.storage.itemPositions.set(t, r);
|
|
28
|
+
}
|
|
29
|
+
getByValue(e, t) {
|
|
30
|
+
let n = this.storage.indexes.get(e);
|
|
31
|
+
return n ? n.get(t) ?? [] : [];
|
|
32
|
+
}
|
|
33
|
+
getByValues(e, t) {
|
|
34
|
+
let n = this.storage.indexes.get(e);
|
|
35
|
+
if (!n) return [];
|
|
36
|
+
if (t.length === 1) return n.get(t[0]) ?? [];
|
|
37
|
+
if (t.length === 2) {
|
|
38
|
+
let e = n.get(t[0]), r = n.get(t[1]);
|
|
39
|
+
return e ? r ? e.concat(r) : e : r ?? [];
|
|
40
|
+
}
|
|
41
|
+
let r = [];
|
|
42
|
+
for (let e = 0; e < t.length; e++) {
|
|
43
|
+
let i = n.get(t[e]);
|
|
44
|
+
if (i !== void 0) for (let e = 0; e < i.length; e++) r.push(i[e]);
|
|
45
|
+
}
|
|
46
|
+
return r;
|
|
47
|
+
}
|
|
48
|
+
hasIndex(e) {
|
|
49
|
+
return this.storage.indexes.has(e);
|
|
50
|
+
}
|
|
51
|
+
addItem(e) {
|
|
52
|
+
for (let t of this.storage.indexes.keys()) this.addItemToField(t, e);
|
|
53
|
+
}
|
|
54
|
+
updateItem(e, t) {
|
|
55
|
+
for (let n of this.storage.indexes.keys()) this.updateItemInField(n, e, t);
|
|
56
|
+
}
|
|
57
|
+
removeItem(e) {
|
|
58
|
+
for (let t of this.storage.indexes.keys()) this.removeItemFromField(t, e);
|
|
59
|
+
}
|
|
60
|
+
clear() {
|
|
61
|
+
this.storage.indexes.clear(), this.storage.itemPositions.clear();
|
|
62
|
+
}
|
|
63
|
+
getIndexMap(e) {
|
|
64
|
+
return this.storage.indexes.get(e);
|
|
65
|
+
}
|
|
66
|
+
removeItemFromField(e, t) {
|
|
67
|
+
let n = t[e];
|
|
68
|
+
if (n == null) return;
|
|
69
|
+
let r = this.storage.indexes.get(e), i = this.storage.itemPositions.get(e), a = r?.get(n), o = i?.get(n), s = o?.get(t);
|
|
70
|
+
if (!r || !i || !a || !o || s === void 0) return;
|
|
71
|
+
let c = a.length - 1, l = a[c];
|
|
72
|
+
s !== c && (a[s] = l, o.set(l, s)), a.pop(), o.delete(t), a.length === 0 && (r.delete(n), i.delete(n));
|
|
73
|
+
}
|
|
74
|
+
updateItemInField(e, t, n) {
|
|
75
|
+
let r = t[e];
|
|
76
|
+
if (r === n[e]) {
|
|
77
|
+
if (r == null) return;
|
|
78
|
+
this.replaceItemReferenceInField(e, r, t, n);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
this.removeItemFromField(e, t), this.addItemToField(e, n);
|
|
82
|
+
}
|
|
83
|
+
replaceItemReferenceInField(e, t, n, r) {
|
|
84
|
+
let i = this.storage.indexes.get(e), a = this.storage.itemPositions.get(e), o = i?.get(t), s = a?.get(t), c = s?.get(n);
|
|
85
|
+
!o || !s || c === void 0 || (o[c] = r, s.delete(n), s.set(r, c));
|
|
86
|
+
}
|
|
87
|
+
addItemToField(e, t) {
|
|
88
|
+
let n = t[e];
|
|
89
|
+
if (n == null) return;
|
|
90
|
+
let r = this.storage.indexes.get(e), i = this.storage.itemPositions.get(e);
|
|
91
|
+
if (!r || !i) return;
|
|
92
|
+
let a = r.get(n), o = i.get(n);
|
|
93
|
+
if (a && o) {
|
|
94
|
+
a.push(t), o.set(t, a.length - 1);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
r.set(n, [t]);
|
|
98
|
+
let s = /* @__PURE__ */ new WeakMap();
|
|
99
|
+
s.set(t, 0), i.set(n, s);
|
|
100
|
+
}
|
|
101
|
+
}, o = class {
|
|
102
|
+
constructor(e) {
|
|
103
|
+
this.callbacks = e;
|
|
104
|
+
}
|
|
105
|
+
create(e) {
|
|
106
|
+
let t = e;
|
|
107
|
+
return Object.defineProperties(t, {
|
|
108
|
+
filter: i((t, n) => n === void 0 ? this.callbacks.filter(e, t) : this.callbacks.filter(t, n)),
|
|
109
|
+
add: i((e) => this.callbacks.add(e)),
|
|
110
|
+
update: i((e) => this.callbacks.update(e)),
|
|
111
|
+
clearIndexes: i(() => this.callbacks.clearIndexes()),
|
|
112
|
+
data: i((e) => this.callbacks.data(e)),
|
|
113
|
+
getOriginData: i(() => this.callbacks.getOriginData()),
|
|
114
|
+
clearData: i(() => this.callbacks.clearData()),
|
|
115
|
+
resetFilterState: i(() => this.callbacks.resetFilterState())
|
|
116
|
+
}), t;
|
|
117
|
+
}
|
|
118
|
+
}, s = class e extends Error {
|
|
119
|
+
constructor(e) {
|
|
120
|
+
super(e), this.name = "FilterEngineError";
|
|
121
|
+
}
|
|
122
|
+
static missingDatasetForBuildIndex() {
|
|
123
|
+
return new e("FilterEngine: no dataset in memory. Call data() or add() before buildIndex().");
|
|
124
|
+
}
|
|
125
|
+
static missingDatasetForFilter() {
|
|
126
|
+
return new e("FilterEngine: no dataset in memory. Call data() or add() before filter().");
|
|
127
|
+
}
|
|
128
|
+
static duplicateMutableExcludeField(t) {
|
|
129
|
+
return new e(`FilterEngine: cannot use mutable exclude on field \`${t}\` because it contains duplicate values.`);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/filter/criterion.ts
|
|
134
|
+
function c(e) {
|
|
135
|
+
let t = /* @__PURE__ */ new Map();
|
|
136
|
+
for (let n = 0; n < e.length; n++) {
|
|
137
|
+
let r = e[n], i = Array.isArray(r.values), a = d(r.exclude), o = a !== null, s = i ? f(r.values, a) : null;
|
|
138
|
+
if (!i && !o) continue;
|
|
139
|
+
let c = r.field, l = t.get(c);
|
|
140
|
+
if (!l) {
|
|
141
|
+
t.set(c, {
|
|
142
|
+
field: r.field,
|
|
143
|
+
values: [],
|
|
144
|
+
exclude: [],
|
|
145
|
+
hasValues: i,
|
|
146
|
+
hasExclude: o,
|
|
147
|
+
includedValues: s ? new Set(s) : null,
|
|
148
|
+
excludedValues: a ? new Set(a) : null,
|
|
149
|
+
cacheKeySegment: ""
|
|
150
|
+
});
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (i) if (!l.hasValues) l.hasValues = !0, l.includedValues = new Set(s);
|
|
154
|
+
else for (let e of l.includedValues) s.has(e) || l.includedValues.delete(e);
|
|
155
|
+
if (o) if (!l.hasExclude) l.hasExclude = !0, l.excludedValues = new Set(a);
|
|
156
|
+
else for (let e of a) l.excludedValues.add(e);
|
|
157
|
+
}
|
|
158
|
+
let n = [...t.values()].sort((e, t) => e.field.localeCompare(t.field));
|
|
159
|
+
for (let e = 0; e < n.length; e++) p(n[e]);
|
|
160
|
+
return n;
|
|
161
|
+
}
|
|
162
|
+
function l(e, t) {
|
|
163
|
+
return !(e.hasValues && !e.includedValues.has(t) || e.hasExclude && e.excludedValues.has(t));
|
|
164
|
+
}
|
|
165
|
+
function u(e) {
|
|
166
|
+
return e.hasValues && e.values.length === 0;
|
|
167
|
+
}
|
|
168
|
+
function d(e) {
|
|
169
|
+
return !Array.isArray(e) || e.length === 0 ? null : new Set(e);
|
|
170
|
+
}
|
|
171
|
+
function f(e, t) {
|
|
172
|
+
let n = /* @__PURE__ */ new Set();
|
|
173
|
+
for (let r = 0; r < e.length; r++) {
|
|
174
|
+
let i = e[r];
|
|
175
|
+
t?.has(i) || n.add(i);
|
|
176
|
+
}
|
|
177
|
+
return n;
|
|
178
|
+
}
|
|
179
|
+
function p(e) {
|
|
180
|
+
if (e.hasValues && e.hasExclude) for (let t of e.excludedValues) e.includedValues.delete(t);
|
|
181
|
+
e.values = e.includedValues ? [...e.includedValues] : [], e.exclude = e.excludedValues ? [...e.excludedValues] : [], e.cacheKeySegment = m(e);
|
|
182
|
+
}
|
|
183
|
+
function m(e) {
|
|
184
|
+
let t = e.field;
|
|
185
|
+
return t += `|hv:${e.hasValues ? "1" : "0"}|v:`, e.hasValues && (t += h(e.values)), t += `|hx:${e.hasExclude ? "1" : "0"}|x:`, e.hasExclude && (t += h(e.exclude)), t;
|
|
186
|
+
}
|
|
187
|
+
function h(e) {
|
|
188
|
+
return e.length === 0 ? "" : e.map((e) => g(e)).sort().join(",");
|
|
189
|
+
}
|
|
190
|
+
function g(e) {
|
|
191
|
+
if (e === null) return "null:null";
|
|
192
|
+
let t = typeof e;
|
|
193
|
+
return t === "object" ? `object:${JSON.stringify(e)}` : `${t}:${String(e)}`;
|
|
194
|
+
}
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region src/filter/nested.ts
|
|
197
|
+
var _ = class {
|
|
198
|
+
constructor(e = {
|
|
199
|
+
indexes: /* @__PURE__ */ new Map(),
|
|
200
|
+
itemPositions: /* @__PURE__ */ new Map()
|
|
201
|
+
}) {
|
|
202
|
+
this.storage = e, this.registeredFields = /* @__PURE__ */ new Set(), this.fieldDescriptors = /* @__PURE__ */ new Map();
|
|
203
|
+
}
|
|
204
|
+
registerFields(e) {
|
|
205
|
+
if (e?.length) for (let t = 0; t < e.length; t++) this.registerField(e[t]);
|
|
206
|
+
}
|
|
207
|
+
hasRegisteredFields() {
|
|
208
|
+
return this.registeredFields.size > 0;
|
|
209
|
+
}
|
|
210
|
+
hasField(e) {
|
|
211
|
+
return this.registeredFields.has(e);
|
|
212
|
+
}
|
|
213
|
+
clearIndexes() {
|
|
214
|
+
this.storage.indexes.clear(), this.storage.itemPositions.clear();
|
|
215
|
+
}
|
|
216
|
+
buildIndexes(e) {
|
|
217
|
+
this.storage.indexes.clear(), this.storage.itemPositions.clear();
|
|
218
|
+
for (let t of this.registeredFields) this.buildIndex(e, t);
|
|
219
|
+
}
|
|
220
|
+
addItems(e) {
|
|
221
|
+
if (!(e.length === 0 || this.storage.indexes.size === 0)) for (let t = 0; t < e.length; t++) this.addItem(e[t]);
|
|
222
|
+
}
|
|
223
|
+
removeItem(e) {
|
|
224
|
+
for (let t of this.storage.indexes.keys()) this.removeItemFromIndex(t, e);
|
|
225
|
+
}
|
|
226
|
+
updateItem(e, t) {
|
|
227
|
+
for (let n of this.storage.indexes.keys()) this.updateItemInIndex(n, e, t);
|
|
228
|
+
}
|
|
229
|
+
filter(e, t, n) {
|
|
230
|
+
let r = this.resolveCriteria(t);
|
|
231
|
+
if (e.length === 0 || r.length === 0) return e;
|
|
232
|
+
let i = [], a = [];
|
|
233
|
+
for (let e = 0; e < r.length; e++) {
|
|
234
|
+
let t = r[e];
|
|
235
|
+
if (this.storage.indexes.has(t.field)) {
|
|
236
|
+
i.push(t);
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
a.push(t);
|
|
240
|
+
}
|
|
241
|
+
let o = e;
|
|
242
|
+
if (i.length > 0 && (o = this.filterByIndexes(i, e, n), o.length === 0)) return o;
|
|
243
|
+
for (let e = 0; e < a.length; e++) if (o = this.filterLinearly(o, a[e]), o.length === 0) return o;
|
|
244
|
+
return o;
|
|
245
|
+
}
|
|
246
|
+
resolveCriteria(e) {
|
|
247
|
+
if (e.length === 0) return [];
|
|
248
|
+
let t = e[0];
|
|
249
|
+
return "hasValues" in t && "hasExclude" in t && "includedValues" in t ? e : c(e);
|
|
250
|
+
}
|
|
251
|
+
registerField(e) {
|
|
252
|
+
let t = r(e);
|
|
253
|
+
t && (this.registeredFields.add(e), this.fieldDescriptors.set(e, t));
|
|
254
|
+
}
|
|
255
|
+
buildIndex(e, t) {
|
|
256
|
+
let n = this.fieldDescriptors.get(t);
|
|
257
|
+
if (!n) return;
|
|
258
|
+
let { collectionKey: r, nestedKey: i } = n, a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map();
|
|
259
|
+
for (let t = 0, n = e.length; t < n; t++) {
|
|
260
|
+
let n = e[t], s = n[r];
|
|
261
|
+
if (Array.isArray(s)) for (let e = 0; e < s.length; e++) {
|
|
262
|
+
let t = s[e][i];
|
|
263
|
+
if (t == null) continue;
|
|
264
|
+
let r = a.get(t);
|
|
265
|
+
if (r) {
|
|
266
|
+
r[r.length - 1] !== n && (r.push(n), o.get(t).set(n, r.length - 1));
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
a.set(t, [n]);
|
|
270
|
+
let c = /* @__PURE__ */ new WeakMap();
|
|
271
|
+
c.set(n, 0), o.set(t, c);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
this.storage.indexes.set(t, a), this.storage.itemPositions.set(t, o);
|
|
275
|
+
}
|
|
276
|
+
removeItemFromIndex(e, t) {
|
|
277
|
+
let n = this.fieldDescriptors.get(e), r = this.storage.indexes.get(e), i = this.storage.itemPositions.get(e);
|
|
278
|
+
if (!n || !r || !i) return;
|
|
279
|
+
let { collectionKey: a, nestedKey: o } = n, s = t[a];
|
|
280
|
+
if (!Array.isArray(s) || s.length === 0) return;
|
|
281
|
+
let c = /* @__PURE__ */ new Set();
|
|
282
|
+
for (let e = 0; e < s.length; e++) {
|
|
283
|
+
let t = s[e][o];
|
|
284
|
+
t != null && c.add(t);
|
|
285
|
+
}
|
|
286
|
+
for (let e of c) {
|
|
287
|
+
let n = r.get(e), a = i.get(e), o = a?.get(t);
|
|
288
|
+
if (!n || !a || o === void 0) continue;
|
|
289
|
+
let s = n.length - 1, c = n[s];
|
|
290
|
+
o !== s && (n[o] = c, a.set(c, o)), n.pop(), a.delete(t), n.length === 0 && (r.delete(e), i.delete(e));
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
addItem(e) {
|
|
294
|
+
for (let t of this.storage.indexes.keys()) this.addItemToIndex(t, e);
|
|
295
|
+
}
|
|
296
|
+
addItemToIndex(e, t) {
|
|
297
|
+
let n = this.fieldDescriptors.get(e), r = this.storage.indexes.get(e), i = this.storage.itemPositions.get(e);
|
|
298
|
+
if (!n || !r || !i) return;
|
|
299
|
+
let { collectionKey: a, nestedKey: o } = n, s = t[a];
|
|
300
|
+
if (!Array.isArray(s) || s.length === 0) return;
|
|
301
|
+
let c = /* @__PURE__ */ new Set();
|
|
302
|
+
for (let e = 0; e < s.length; e++) {
|
|
303
|
+
let t = s[e][o];
|
|
304
|
+
t != null && c.add(t);
|
|
305
|
+
}
|
|
306
|
+
for (let e of c) {
|
|
307
|
+
let n = r.get(e), a = i.get(e);
|
|
308
|
+
if (n && a) {
|
|
309
|
+
n.push(t), a.set(t, n.length - 1);
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
r.set(e, [t]);
|
|
313
|
+
let o = /* @__PURE__ */ new WeakMap();
|
|
314
|
+
o.set(t, 0), i.set(e, o);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
updateItemInIndex(e, t, n) {
|
|
318
|
+
let r = this.fieldDescriptors.get(e), i = this.storage.indexes.get(e), a = this.storage.itemPositions.get(e);
|
|
319
|
+
if (!r || !i || !a) return;
|
|
320
|
+
let o = this.collectUniqueValues(n, r), s = this.collectUniqueValues(t, r);
|
|
321
|
+
if (this.areValueSetsEqual(o, s)) {
|
|
322
|
+
for (let e of o) {
|
|
323
|
+
let r = i.get(e), o = a.get(e), s = o?.get(n);
|
|
324
|
+
!r || !o || s === void 0 || (r[s] = t, o.delete(n), o.set(t, s));
|
|
325
|
+
}
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
this.removeItemFromIndex(e, n), this.addItemToIndex(e, t);
|
|
329
|
+
}
|
|
330
|
+
collectUniqueValues(e, t) {
|
|
331
|
+
let { collectionKey: n, nestedKey: r } = t, i = e[n], a = /* @__PURE__ */ new Set();
|
|
332
|
+
if (!Array.isArray(i) || i.length === 0) return a;
|
|
333
|
+
for (let e = 0; e < i.length; e++) {
|
|
334
|
+
let t = i[e][r];
|
|
335
|
+
t != null && a.add(t);
|
|
336
|
+
}
|
|
337
|
+
return a;
|
|
338
|
+
}
|
|
339
|
+
areValueSetsEqual(e, t) {
|
|
340
|
+
if (e.size !== t.size) return !1;
|
|
341
|
+
for (let n of e) if (!t.has(n)) return !1;
|
|
342
|
+
return !0;
|
|
343
|
+
}
|
|
344
|
+
filterByIndexes(e, t, n) {
|
|
345
|
+
let r = e.filter((e) => e.hasValues), i = e.filter((e) => e.hasExclude), a = t === n ? null : new Set(t);
|
|
346
|
+
if (r.length === 0) return this.applyIndexedExclusions(t, i);
|
|
347
|
+
if (r.length === 1) {
|
|
348
|
+
let e = this.storage.indexes.get(r[0].field);
|
|
349
|
+
if (!e) return [];
|
|
350
|
+
let t = this.getItemsByValues(e, r[0].values);
|
|
351
|
+
if (t.length === 0) return [];
|
|
352
|
+
let n = [];
|
|
353
|
+
for (let e = 0; e < t.length; e++) {
|
|
354
|
+
let r = t[e];
|
|
355
|
+
a && !a.has(r) || n.push(r);
|
|
356
|
+
}
|
|
357
|
+
return this.applyIndexedExclusions(n, i);
|
|
358
|
+
}
|
|
359
|
+
let o = r.map((e) => ({
|
|
360
|
+
criterion: e,
|
|
361
|
+
size: this.estimateIndexSize(e)
|
|
362
|
+
})).sort((e, t) => e.size - t.size), s = a, c = [];
|
|
363
|
+
for (let e = 0; e < o.length; e++) {
|
|
364
|
+
let { criterion: t } = o[e], n = this.storage.indexes.get(t.field);
|
|
365
|
+
if (!n) return [];
|
|
366
|
+
let r = this.getItemsByValues(n, t.values);
|
|
367
|
+
if (r.length === 0) return [];
|
|
368
|
+
if (s === null) c = r;
|
|
369
|
+
else {
|
|
370
|
+
c = [];
|
|
371
|
+
for (let e = 0; e < r.length; e++) {
|
|
372
|
+
let t = r[e];
|
|
373
|
+
s.has(t) && c.push(t);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (c.length === 0) return [];
|
|
377
|
+
s = new Set(c);
|
|
378
|
+
}
|
|
379
|
+
return this.applyIndexedExclusions(c, i);
|
|
380
|
+
}
|
|
381
|
+
getItemsByValues(e, t) {
|
|
382
|
+
if (t.length === 1) return e.get(t[0]) ?? [];
|
|
383
|
+
let n = /* @__PURE__ */ new Set(), r = [];
|
|
384
|
+
for (let i = 0; i < t.length; i++) {
|
|
385
|
+
let a = e.get(t[i]);
|
|
386
|
+
if (a) for (let e = 0; e < a.length; e++) {
|
|
387
|
+
let t = a[e];
|
|
388
|
+
n.has(t) || (n.add(t), r.push(t));
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return r;
|
|
392
|
+
}
|
|
393
|
+
estimateIndexSize(e) {
|
|
394
|
+
let t = this.storage.indexes.get(e.field);
|
|
395
|
+
return t ? e.values.reduce((e, n) => {
|
|
396
|
+
let r = t.get(n);
|
|
397
|
+
return r ? e + r.length : e;
|
|
398
|
+
}, 0) : Infinity;
|
|
399
|
+
}
|
|
400
|
+
filterLinearly(e, t) {
|
|
401
|
+
let n = this.fieldDescriptors.get(t.field);
|
|
402
|
+
if (!n) return e;
|
|
403
|
+
let { collectionKey: r, nestedKey: i } = n, a = [];
|
|
404
|
+
for (let n = 0; n < e.length; n++) {
|
|
405
|
+
let o = e[n], s = o[r];
|
|
406
|
+
if (!Array.isArray(s)) continue;
|
|
407
|
+
let c = !t.hasValues, l = !1;
|
|
408
|
+
for (let e = 0; e < s.length; e++) {
|
|
409
|
+
let n = s[e][i];
|
|
410
|
+
if (t.hasExclude && t.excludedValues.has(n)) {
|
|
411
|
+
l = !0;
|
|
412
|
+
break;
|
|
413
|
+
}
|
|
414
|
+
t.hasValues && t.includedValues.has(n) && (c = !0);
|
|
415
|
+
}
|
|
416
|
+
!l && c && a.push(o);
|
|
417
|
+
}
|
|
418
|
+
return a;
|
|
419
|
+
}
|
|
420
|
+
applyIndexedExclusions(e, t) {
|
|
421
|
+
if (t.length === 0 || e.length === 0) return e;
|
|
422
|
+
let n = /* @__PURE__ */ new Set();
|
|
423
|
+
for (let e = 0; e < t.length; e++) {
|
|
424
|
+
let r = t[e], i = this.storage.indexes.get(r.field);
|
|
425
|
+
if (!i) continue;
|
|
426
|
+
let a = this.getItemsByValues(i, r.exclude);
|
|
427
|
+
for (let e = 0; e < a.length; e++) n.add(a[e]);
|
|
428
|
+
}
|
|
429
|
+
if (n.size === 0) return e;
|
|
430
|
+
let r = [];
|
|
431
|
+
for (let t = 0; t < e.length; t++) {
|
|
432
|
+
let i = e[t];
|
|
433
|
+
n.has(i) || r.push(i);
|
|
434
|
+
}
|
|
435
|
+
return r;
|
|
436
|
+
}
|
|
437
|
+
}, v = () => ({
|
|
438
|
+
indexedFields: /* @__PURE__ */ new Set(),
|
|
439
|
+
indexerStorage: {
|
|
440
|
+
indexes: /* @__PURE__ */ new Map(),
|
|
441
|
+
itemPositions: /* @__PURE__ */ new Map()
|
|
442
|
+
},
|
|
443
|
+
nestedStorage: {
|
|
444
|
+
indexes: /* @__PURE__ */ new Map(),
|
|
445
|
+
itemPositions: /* @__PURE__ */ new Map()
|
|
446
|
+
},
|
|
447
|
+
deferredMutationVersion: null,
|
|
448
|
+
mutableExclude: {
|
|
449
|
+
datasetPositions: /* @__PURE__ */ new Map(),
|
|
450
|
+
valueCounts: /* @__PURE__ */ new Map(),
|
|
451
|
+
duplicateValueCount: 0,
|
|
452
|
+
hasDuplicateValues: !1
|
|
453
|
+
},
|
|
454
|
+
sequentialCache: {
|
|
455
|
+
previousResult: null,
|
|
456
|
+
previousCriteria: null,
|
|
457
|
+
previousCriteriaKey: null,
|
|
458
|
+
previousBaseData: null,
|
|
459
|
+
previousResultsByCriteria: /* @__PURE__ */ new Map(),
|
|
460
|
+
previousResultSet: null
|
|
461
|
+
}
|
|
462
|
+
}), y = class {
|
|
463
|
+
constructor(t = {}) {
|
|
464
|
+
this.chainBuilder = new o({
|
|
465
|
+
filter: (e, t) => t === void 0 ? this.filter(e) : this.filter(e, t),
|
|
466
|
+
getOriginData: () => this.getOriginData(),
|
|
467
|
+
add: (e) => this.add(e),
|
|
468
|
+
update: (e) => this.update(e),
|
|
469
|
+
data: (e) => this.data(e),
|
|
470
|
+
clearIndexes: () => this.clearIndexes(),
|
|
471
|
+
clearData: () => this.clearData(),
|
|
472
|
+
resetFilterState: () => this.resetFilterState()
|
|
473
|
+
}), this.mutableExcludeField = t.mutableExcludeField ?? null, this.state = t.state ?? new e(t.data ?? [], { filterByPreviousResult: t.filterByPreviousResult ?? !1 }), t.state && t.filterByPreviousResult && this.state.setFilterByPreviousResult(!0), this.filterByPreviousResult = this.state.isFilterByPreviousResultEnabled(), this.namespace = this.state.createNamespace("filter"), this.indexer = new a(this.runtime.indexerStorage), this.nestedCollection = new _(this.runtime.nestedStorage), this.nestedCollection.registerFields(t.nestedFields), this.state.subscribe((e) => this.handleStateMutation(e)), this.rebuildMutableExcludeState();
|
|
474
|
+
let n = t.fields?.length, r = this.nestedCollection.hasRegisteredFields();
|
|
475
|
+
if (n) for (let e of t.fields) this.indexedFields.add(e);
|
|
476
|
+
this.dataset.length > 0 && (n || r) && this.rebuildConfiguredIndexes();
|
|
477
|
+
}
|
|
478
|
+
get dataset() {
|
|
479
|
+
return this.state.getOriginData();
|
|
480
|
+
}
|
|
481
|
+
get runtime() {
|
|
482
|
+
return this.state.getOrCreateScopedValue(this.namespace, "runtime", v);
|
|
483
|
+
}
|
|
484
|
+
get mutableExcludeState() {
|
|
485
|
+
return this.runtime.mutableExclude;
|
|
486
|
+
}
|
|
487
|
+
get indexedFields() {
|
|
488
|
+
return this.runtime.indexedFields;
|
|
489
|
+
}
|
|
490
|
+
get sequentialCache() {
|
|
491
|
+
return this.runtime.sequentialCache;
|
|
492
|
+
}
|
|
493
|
+
shouldDeferMutationIndexUpdates() {
|
|
494
|
+
return this.state.getScopedValue(t, n) === !0;
|
|
495
|
+
}
|
|
496
|
+
markDeferredMutationState() {
|
|
497
|
+
this.runtime.deferredMutationVersion = this.state.getMutationVersion(), this.indexer.clear(), this.nestedCollection.clearIndexes(), this.clearMutableExcludeState(), this.resetFilterState();
|
|
498
|
+
}
|
|
499
|
+
ensureRuntimeReady() {
|
|
500
|
+
this.runtime.deferredMutationVersion !== null && (this.runtime.deferredMutationVersion = null, this.rebuildMutableExcludeState(), this.dataset.length > 0 && (this.indexedFields.size > 0 || this.nestedCollection.hasRegisteredFields()) && this.rebuildConfiguredIndexes());
|
|
501
|
+
}
|
|
502
|
+
rebuildConfiguredIndexes() {
|
|
503
|
+
this.indexer.clear(), this.nestedCollection.clearIndexes();
|
|
504
|
+
for (let e of this.indexedFields) this.buildIndex(this.dataset, e);
|
|
505
|
+
this.nestedCollection.hasRegisteredFields() && this.nestedCollection.buildIndexes(this.dataset);
|
|
506
|
+
}
|
|
507
|
+
buildIndex(e, t) {
|
|
508
|
+
if (!Array.isArray(e)) {
|
|
509
|
+
if (!this.dataset.length) throw s.missingDatasetForBuildIndex();
|
|
510
|
+
return this.indexer.buildIndex(this.dataset, e), this;
|
|
511
|
+
}
|
|
512
|
+
return this.resetFilterState(), this.rebuildMutableExcludeState(), this.indexer.buildIndex(e, t), this;
|
|
513
|
+
}
|
|
514
|
+
clearIndexes() {
|
|
515
|
+
return this.indexer.clear(), this.nestedCollection.clearIndexes(), this;
|
|
516
|
+
}
|
|
517
|
+
resetFilterState() {
|
|
518
|
+
return this.sequentialCache.previousResult = null, this.sequentialCache.previousCriteria = null, this.sequentialCache.previousCriteriaKey = null, this.sequentialCache.previousBaseData = null, this.sequentialCache.previousResultsByCriteria.clear(), this.sequentialCache.previousResultSet = null, this.state.clearPreviousResult(), this;
|
|
519
|
+
}
|
|
520
|
+
clearData() {
|
|
521
|
+
return this.state.clearData(), this;
|
|
522
|
+
}
|
|
523
|
+
data(e) {
|
|
524
|
+
return this.state.data(e), this;
|
|
525
|
+
}
|
|
526
|
+
add(e) {
|
|
527
|
+
return this.state.add(e), this;
|
|
528
|
+
}
|
|
529
|
+
update(e) {
|
|
530
|
+
return this.state.update(e), this;
|
|
531
|
+
}
|
|
532
|
+
applyAddedItems(e, t) {
|
|
533
|
+
return e.length === 0 ? this : (this.updateMutableExcludeStateForAddedItems(e, t), this.resetFilterState(), this.indexer.addItems(e), this.nestedCollection.addItems(e), this);
|
|
534
|
+
}
|
|
535
|
+
getOriginData() {
|
|
536
|
+
return this.state.getOriginData();
|
|
537
|
+
}
|
|
538
|
+
filter(e, t) {
|
|
539
|
+
return t === void 0 ? this.withChain(this.rawFilter(e)) : this.withChain(this.rawFilter(e, t));
|
|
540
|
+
}
|
|
541
|
+
rawFilter(e, t) {
|
|
542
|
+
this.ensureRuntimeReady();
|
|
543
|
+
let n = t === void 0;
|
|
544
|
+
if (n && !this.dataset.length) throw s.missingDatasetForFilter();
|
|
545
|
+
let r = c(n ? e : t), i = n ? this.dataset : e;
|
|
546
|
+
if (n) {
|
|
547
|
+
let e = this.applyMutableExclude(r);
|
|
548
|
+
if (e !== void 0) return e;
|
|
549
|
+
}
|
|
550
|
+
let a = i, o = r;
|
|
551
|
+
if (this.filterByPreviousResult) {
|
|
552
|
+
let e = this.resolveWithSequentialCache(i, n, r);
|
|
553
|
+
if (e.isFromCache) return e.result;
|
|
554
|
+
a = e.sourceData, o = e.executionCriteria;
|
|
555
|
+
}
|
|
556
|
+
if (r.length === 0) return this.filterByPreviousResult && this.resetFilterState(), i;
|
|
557
|
+
for (let e = 0; e < o.length; e++) if (u(o[e])) return this.createEmptyResult(n, i, r);
|
|
558
|
+
let l = [], d = [];
|
|
559
|
+
for (let e = 0; e < o.length; e++) {
|
|
560
|
+
let t = o[e];
|
|
561
|
+
this.nestedCollection.hasField(t.field) ? l.push(t) : d.push(t);
|
|
562
|
+
}
|
|
563
|
+
if (l.length > 0 && (a = this.nestedCollection.filter(a, l, this.dataset), a.length === 0)) return this.createEmptyResult(n, i, r);
|
|
564
|
+
if (d.length === 0) return this.storePreviousResult(a, n, i, r), a;
|
|
565
|
+
let f = [], p = [];
|
|
566
|
+
for (let e = 0; e < d.length; e++) {
|
|
567
|
+
let t = d[e];
|
|
568
|
+
this.indexer.hasIndex(t.field) ? f.push(t) : p.push(t);
|
|
569
|
+
}
|
|
570
|
+
let m;
|
|
571
|
+
if (f.length > 0 && p.length === 0) return m = this.filterViaIndex(f, a), this.storePreviousResult(m, n, i, r), m;
|
|
572
|
+
if (f.length > 0 && p.length > 0) {
|
|
573
|
+
let e = this.filterViaIndex(f, a);
|
|
574
|
+
return m = this.linearFilter(e, p), this.storePreviousResult(m, n, i, r), m;
|
|
575
|
+
}
|
|
576
|
+
return m = this.linearFilter(a, d), this.storePreviousResult(m, n, i, r), m;
|
|
577
|
+
}
|
|
578
|
+
resolveWithSequentialCache(e, t, n) {
|
|
579
|
+
let { previousResult: r, previousCriteria: i, previousCriteriaKey: a, previousBaseData: o, previousResultsByCriteria: s } = this.sequentialCache;
|
|
580
|
+
if (r === null || i === null || o !== e) return {
|
|
581
|
+
isFromCache: !1,
|
|
582
|
+
sourceData: e,
|
|
583
|
+
executionCriteria: n
|
|
584
|
+
};
|
|
585
|
+
let c = this.createCriteriaCacheKey(n), l = s.get(c);
|
|
586
|
+
if (c === a) return {
|
|
587
|
+
isFromCache: !0,
|
|
588
|
+
result: r
|
|
589
|
+
};
|
|
590
|
+
if (l !== void 0) return this.storePreviousResult(l.result, t, e, n, c, l.resultSet), {
|
|
591
|
+
isFromCache: !0,
|
|
592
|
+
result: l.result
|
|
593
|
+
};
|
|
594
|
+
let u = this.hasCriteriaBacktrack(i, n) || r.length === 0 && !this.canApplySequentiallyToEmptyResult(i, n);
|
|
595
|
+
return {
|
|
596
|
+
isFromCache: !1,
|
|
597
|
+
sourceData: u ? e : r,
|
|
598
|
+
executionCriteria: u ? n : this.createSequentialExecutionCriteria(i, n)
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
withChain(e) {
|
|
602
|
+
return this.chainBuilder.create(e);
|
|
603
|
+
}
|
|
604
|
+
handleStateMutation(e) {
|
|
605
|
+
if (this.shouldDeferMutationIndexUpdates() && (e.type === "add" || e.type === "update")) {
|
|
606
|
+
this.markDeferredMutationState();
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
switch (e.type) {
|
|
610
|
+
case "add":
|
|
611
|
+
this.applyAddedItems(e.items, e.startIndex);
|
|
612
|
+
return;
|
|
613
|
+
case "update":
|
|
614
|
+
this.applyUpdatedItem(e.index, e.previousItem, e.nextItem);
|
|
615
|
+
return;
|
|
616
|
+
case "data":
|
|
617
|
+
this.runtime.deferredMutationVersion = null, this.rebuildMutableExcludeState(), this.resetFilterState(), this.rebuildConfiguredIndexes();
|
|
618
|
+
return;
|
|
619
|
+
case "clearData":
|
|
620
|
+
this.runtime.deferredMutationVersion = null, this.clearMutableExcludeState(), this.indexer.clear(), this.nestedCollection.clearIndexes(), this.resetFilterState();
|
|
621
|
+
return;
|
|
622
|
+
case "remove":
|
|
623
|
+
this.applyRemovedItem(e.field, e.value, e.removedItem, e.removedIndex, e.movedItem);
|
|
624
|
+
return;
|
|
625
|
+
case "removeMany":
|
|
626
|
+
this.applyRemovedItems(e.field, e.entries);
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
rebuildMutableExcludeState() {
|
|
631
|
+
if (this.clearMutableExcludeState(), this.mutableExcludeField !== null) for (let e = 0; e < this.dataset.length; e++) {
|
|
632
|
+
let t = this.dataset[e][this.mutableExcludeField];
|
|
633
|
+
t != null && this.registerMutableExcludeValue(t, e);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
updateMutableExcludeStateForAddedItems(e, t) {
|
|
637
|
+
if (this.mutableExcludeField !== null) for (let n = 0; n < e.length; n++) {
|
|
638
|
+
let r = e[n][this.mutableExcludeField];
|
|
639
|
+
r != null && this.registerMutableExcludeValue(r, t + n);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
applyMutableExclude(e) {
|
|
643
|
+
if (this.mutableExcludeField === null || e.length !== 1) return;
|
|
644
|
+
let t = e[0];
|
|
645
|
+
if (!(t.field !== this.mutableExcludeField || t.hasValues || !t.hasExclude)) {
|
|
646
|
+
if (this.mutableExcludeState.hasDuplicateValues) throw s.duplicateMutableExcludeField(this.mutableExcludeField);
|
|
647
|
+
return this.state.removeByFieldValues(this.mutableExcludeField, t.exclude), this.resetFilterState(), this.dataset;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
applyUpdatedItem(e, t, n) {
|
|
651
|
+
this.updateMutableExcludeStateForUpdatedItem(e, t, n), this.resetFilterState(), this.indexer.updateItem(t, n), this.nestedCollection.updateItem(n, t);
|
|
652
|
+
}
|
|
653
|
+
applyRemovedItem(e, t, n, r, i) {
|
|
654
|
+
if (e === this.mutableExcludeField && (this.unregisterMutableExcludeValue(t), this.mutableExcludeState.datasetPositions.delete(t), i !== null && this.mutableExcludeField !== null)) {
|
|
655
|
+
let e = i[this.mutableExcludeField];
|
|
656
|
+
e != null && this.mutableExcludeState.datasetPositions.set(e, r);
|
|
657
|
+
}
|
|
658
|
+
this.resetFilterState(), this.indexer.removeItem(n), this.nestedCollection.removeItem(n);
|
|
659
|
+
}
|
|
660
|
+
applyRemovedItems(e, t) {
|
|
661
|
+
for (let n = 0; n < t.length; n++) {
|
|
662
|
+
let r = t[n];
|
|
663
|
+
if (e === this.mutableExcludeField && (this.unregisterMutableExcludeValue(r.value), this.mutableExcludeState.datasetPositions.delete(r.value), r.movedItem !== null && this.mutableExcludeField !== null)) {
|
|
664
|
+
let e = r.movedItem[this.mutableExcludeField];
|
|
665
|
+
e != null && this.mutableExcludeState.datasetPositions.set(e, r.removedIndex);
|
|
666
|
+
}
|
|
667
|
+
this.indexer.removeItem(r.removedItem), this.nestedCollection.removeItem(r.removedItem);
|
|
668
|
+
}
|
|
669
|
+
this.resetFilterState();
|
|
670
|
+
}
|
|
671
|
+
updateMutableExcludeStateForUpdatedItem(e, t, n) {
|
|
672
|
+
if (this.mutableExcludeField === null) return;
|
|
673
|
+
let r = t[this.mutableExcludeField], i = n[this.mutableExcludeField];
|
|
674
|
+
if (r === i) {
|
|
675
|
+
i != null && this.mutableExcludeState.datasetPositions.set(i, e);
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
r != null && (this.unregisterMutableExcludeValue(r), this.mutableExcludeState.datasetPositions.get(r) === e && this.mutableExcludeState.datasetPositions.delete(r)), i != null && this.registerMutableExcludeValue(i, e);
|
|
679
|
+
}
|
|
680
|
+
clearMutableExcludeState() {
|
|
681
|
+
this.mutableExcludeState.datasetPositions.clear(), this.mutableExcludeState.valueCounts.clear(), this.mutableExcludeState.duplicateValueCount = 0, this.mutableExcludeState.hasDuplicateValues = !1;
|
|
682
|
+
}
|
|
683
|
+
registerMutableExcludeValue(e, t) {
|
|
684
|
+
let n = (this.mutableExcludeState.valueCounts.get(e) ?? 0) + 1;
|
|
685
|
+
n === 2 && this.mutableExcludeState.duplicateValueCount++, this.mutableExcludeState.valueCounts.set(e, n), this.mutableExcludeState.datasetPositions.set(e, t), this.mutableExcludeState.hasDuplicateValues = this.mutableExcludeState.duplicateValueCount > 0;
|
|
686
|
+
}
|
|
687
|
+
unregisterMutableExcludeValue(e) {
|
|
688
|
+
let t = this.mutableExcludeState.valueCounts.get(e);
|
|
689
|
+
t !== void 0 && (t === 2 && this.mutableExcludeState.duplicateValueCount--, t <= 1 ? this.mutableExcludeState.valueCounts.delete(e) : this.mutableExcludeState.valueCounts.set(e, t - 1), this.mutableExcludeState.hasDuplicateValues = this.mutableExcludeState.duplicateValueCount > 0);
|
|
690
|
+
}
|
|
691
|
+
linearFilter(e, t) {
|
|
692
|
+
let n = [];
|
|
693
|
+
for (let r = 0; r < e.length; r++) {
|
|
694
|
+
let i = e[r], a = !0;
|
|
695
|
+
for (let e = 0; e < t.length; e++) {
|
|
696
|
+
let n = t[e];
|
|
697
|
+
if (!l(n, i[n.field])) {
|
|
698
|
+
a = !1;
|
|
699
|
+
break;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
a && n.push(i);
|
|
703
|
+
}
|
|
704
|
+
return n;
|
|
705
|
+
}
|
|
706
|
+
filterViaIndex(e, t) {
|
|
707
|
+
let n = t !== this.dataset, r = null;
|
|
708
|
+
if (n) {
|
|
709
|
+
if (t === this.sequentialCache.previousResult && this.sequentialCache.previousResultSet !== null) r = this.sequentialCache.previousResultSet;
|
|
710
|
+
else if (r = new Set(t), t === this.sequentialCache.previousResult) {
|
|
711
|
+
this.sequentialCache.previousResultSet = r;
|
|
712
|
+
let e = this.sequentialCache.previousCriteriaKey;
|
|
713
|
+
if (e !== null) {
|
|
714
|
+
let t = this.sequentialCache.previousResultsByCriteria.get(e);
|
|
715
|
+
t && (t.resultSet = r);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
let i = [], a = [];
|
|
720
|
+
for (let t = 0; t < e.length; t++) {
|
|
721
|
+
let n = e[t];
|
|
722
|
+
n.hasValues && i.push(n), n.hasExclude && a.push(n);
|
|
723
|
+
}
|
|
724
|
+
if (i.length === 0) return this.applyIndexedExclusions(t, a);
|
|
725
|
+
if (i.length === 1) {
|
|
726
|
+
let e = i[0], t = !1;
|
|
727
|
+
for (let n = 0; n < a.length; n++) if (a[n].field !== e.field) {
|
|
728
|
+
t = !0;
|
|
729
|
+
break;
|
|
730
|
+
}
|
|
731
|
+
if (r === null && !t) return e.values.length === 1 ? this.indexer.getByValue(e.field, e.values[0]).slice() : this.indexer.getByValues(e.field, e.values);
|
|
732
|
+
let n = this.indexer.getByValues(e.field, e.values), o = [];
|
|
733
|
+
for (let e = 0; e < n.length; e++) {
|
|
734
|
+
let t = n[e];
|
|
735
|
+
r && !r.has(t) || o.push(t);
|
|
736
|
+
}
|
|
737
|
+
return this.applyIndexedExclusions(o, a);
|
|
738
|
+
}
|
|
739
|
+
let o = i[0], s = this.estimateIndexSize(o);
|
|
740
|
+
for (let e = 1; e < i.length; e++) {
|
|
741
|
+
let t = i[e], n = this.estimateIndexSize(t);
|
|
742
|
+
n < s && (o = t, s = n);
|
|
743
|
+
}
|
|
744
|
+
let c = this.indexer.getByValues(o.field, o.values);
|
|
745
|
+
if (c.length === 0) return [];
|
|
746
|
+
let u = [];
|
|
747
|
+
for (let e = 0; e < c.length; e++) {
|
|
748
|
+
let t = c[e], n = !0;
|
|
749
|
+
if (!(r && !r.has(t))) {
|
|
750
|
+
for (let e = 0; e < i.length; e++) {
|
|
751
|
+
let r = i[e];
|
|
752
|
+
if (r !== o && !l(r, t[r.field])) {
|
|
753
|
+
n = !1;
|
|
754
|
+
break;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
n && u.push(t);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return this.applyIndexedExclusions(u, a);
|
|
761
|
+
}
|
|
762
|
+
estimateIndexSize(e) {
|
|
763
|
+
let t = this.indexer.getIndexMap(e.field);
|
|
764
|
+
if (!t) return Infinity;
|
|
765
|
+
let n = 0;
|
|
766
|
+
for (let r = 0; r < e.values.length; r++) {
|
|
767
|
+
let i = t.get(e.values[r]);
|
|
768
|
+
i && (n += i.length);
|
|
769
|
+
}
|
|
770
|
+
return n;
|
|
771
|
+
}
|
|
772
|
+
applyIndexedExclusions(e, t) {
|
|
773
|
+
if (t.length === 0 || e.length === 0) return e;
|
|
774
|
+
let n = /* @__PURE__ */ new Set();
|
|
775
|
+
for (let e = 0; e < t.length; e++) {
|
|
776
|
+
let r = t[e], i = this.indexer.getIndexMap(r.field);
|
|
777
|
+
if (i) for (let e = 0; e < r.exclude.length; e++) {
|
|
778
|
+
let t = i.get(r.exclude[e]);
|
|
779
|
+
if (t) for (let e = 0; e < t.length; e++) n.add(t[e]);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
if (n.size === 0) return e;
|
|
783
|
+
let r = [];
|
|
784
|
+
for (let t = 0; t < e.length; t++) {
|
|
785
|
+
let i = e[t];
|
|
786
|
+
n.has(i) || r.push(i);
|
|
787
|
+
}
|
|
788
|
+
return r;
|
|
789
|
+
}
|
|
790
|
+
createEmptyResult(e, t, n) {
|
|
791
|
+
let r = [];
|
|
792
|
+
return this.storePreviousResult(r, e, t, n), r;
|
|
793
|
+
}
|
|
794
|
+
storePreviousResult(e, t, n, r, i = this.createCriteriaCacheKey(r), a) {
|
|
795
|
+
if (!this.filterByPreviousResult) return;
|
|
796
|
+
let o = a === void 0 ? null : a;
|
|
797
|
+
this.sequentialCache.previousResult = e, this.sequentialCache.previousCriteria = r, this.sequentialCache.previousCriteriaKey = i, this.sequentialCache.previousBaseData = t ? this.dataset : n, this.sequentialCache.previousResultSet = o, this.state.setPreviousResult(e, t ? this.dataset : n), this.sequentialCache.previousResultsByCriteria.set(i, this.createSequentialCacheEntry(e, o));
|
|
798
|
+
}
|
|
799
|
+
createCriteriaCacheKey(e) {
|
|
800
|
+
let t = "";
|
|
801
|
+
for (let n = 0; n < e.length; n++) t += e[n].cacheKeySegment, t += ";";
|
|
802
|
+
return t;
|
|
803
|
+
}
|
|
804
|
+
createSequentialCacheEntry(e, t) {
|
|
805
|
+
return {
|
|
806
|
+
result: e,
|
|
807
|
+
resultSet: t
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
createSequentialExecutionCriteria(e, t) {
|
|
811
|
+
let n = /* @__PURE__ */ new Map();
|
|
812
|
+
for (let t = 0; t < e.length; t++) {
|
|
813
|
+
let r = e[t];
|
|
814
|
+
n.set(r.field, r);
|
|
815
|
+
}
|
|
816
|
+
return t.map((e) => {
|
|
817
|
+
let t = n.get(e.field);
|
|
818
|
+
if (!t || !t.hasValues || !e.hasValues || t.hasExclude || e.hasExclude || e.includedValues.size <= t.includedValues.size) return e;
|
|
819
|
+
for (let n of t.includedValues) if (!e.includedValues.has(n)) return e;
|
|
820
|
+
let r = [];
|
|
821
|
+
for (let n of e.includedValues) t.includedValues.has(n) || r.push(n);
|
|
822
|
+
return r.length === 0 ? e : {
|
|
823
|
+
...e,
|
|
824
|
+
values: r,
|
|
825
|
+
includedValues: new Set(r)
|
|
826
|
+
};
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
hasCriteriaBacktrack(e, t) {
|
|
830
|
+
let n = this.createCriteriaStateMap(e), r = this.createCriteriaStateMap(t);
|
|
831
|
+
for (let [e, t] of n) {
|
|
832
|
+
let n = r.get(e);
|
|
833
|
+
if (!n || this.isCriterionBacktracked(t, n)) return !0;
|
|
834
|
+
}
|
|
835
|
+
return !1;
|
|
836
|
+
}
|
|
837
|
+
canApplySequentiallyToEmptyResult(e, t) {
|
|
838
|
+
let n = this.createCriteriaStateMap(e), r = this.createCriteriaStateMap(t);
|
|
839
|
+
for (let [e, t] of r) {
|
|
840
|
+
let r = n.get(e);
|
|
841
|
+
if (r) {
|
|
842
|
+
if (r.hasValues !== t.hasValues && (r.hasValues || !t.hasValues)) return !1;
|
|
843
|
+
if (r.hasValues && t.hasValues) {
|
|
844
|
+
let e = r.includedValues, n = t.includedValues;
|
|
845
|
+
if (!this.areSetsEqual(e, n) && !this.isSubset(e, n)) return !1;
|
|
846
|
+
}
|
|
847
|
+
if (r.hasExclude !== t.hasExclude && (r.hasExclude || !t.hasExclude)) return !1;
|
|
848
|
+
if (r.hasExclude && t.hasExclude) {
|
|
849
|
+
let e = r.excludedValues, n = t.excludedValues;
|
|
850
|
+
if (!this.areSetsEqual(e, n) && !this.isSubset(e, n)) return !1;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
return !0;
|
|
855
|
+
}
|
|
856
|
+
createCriteriaStateMap(e) {
|
|
857
|
+
let t = /* @__PURE__ */ new Map();
|
|
858
|
+
for (let n = 0; n < e.length; n++) {
|
|
859
|
+
let r = e[n];
|
|
860
|
+
t.set(r.field, r);
|
|
861
|
+
}
|
|
862
|
+
return t;
|
|
863
|
+
}
|
|
864
|
+
isCriterionBacktracked(e, t) {
|
|
865
|
+
if (e.hasValues && !t.hasValues) return !0;
|
|
866
|
+
if (e.hasValues && t.hasValues) {
|
|
867
|
+
let n = e.includedValues, r = t.includedValues;
|
|
868
|
+
if (!this.areSetsEqual(n, r)) return this.isSubset(r, n);
|
|
869
|
+
}
|
|
870
|
+
if (!e.hasValues && t.hasValues) return !1;
|
|
871
|
+
if (e.hasExclude && !t.hasExclude) return !0;
|
|
872
|
+
if (!e.hasExclude && t.hasExclude) return !1;
|
|
873
|
+
if (e.hasExclude && t.hasExclude) {
|
|
874
|
+
let n = e.excludedValues, r = t.excludedValues;
|
|
875
|
+
if (!this.areSetsEqual(n, r)) return this.isSubset(r, n);
|
|
876
|
+
}
|
|
877
|
+
return !1;
|
|
878
|
+
}
|
|
879
|
+
isSubset(e, t) {
|
|
880
|
+
for (let n of e) if (!t.has(n)) return !1;
|
|
881
|
+
return !0;
|
|
882
|
+
}
|
|
883
|
+
areSetsEqual(e, t) {
|
|
884
|
+
if (e === t) return !0;
|
|
885
|
+
if (!e || !t || e.size !== t.size) return !1;
|
|
886
|
+
for (let n of e) if (!t.has(n)) return !1;
|
|
887
|
+
return !0;
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
//#endregion
|
|
891
|
+
export { y as FilterEngine };
|