UNPKG

@froalacharts/datatable

Version:

In memory tabular data storage

1 lines 10.3 kB
"use strict";exports.__esModule=true;exports.binaryInsertionSort=binaryInsertionSort;exports.mergeSort=mergeSort;exports["default"]=void 0;function binaryInsertionSort(arr,_comparer,_lo,_hi,_start){var lo=_lo||0,hi=_hi||arr.length,start=_start||lo+1,comparer=_comparer;if(!comparer){comparer=function comparer(a,b){return a-b}}if(lo===start){++start}for(;start<hi;++start){var l=lo,r=start,pivot=arr[r];do{var p=l+(r-l>>1);if(comparer(pivot,arr[p])<0){r=p}else{l=p+1}}while(l<r);for(var _p=start;_p>l;--_p){arr[_p]=arr[_p-1]}arr[l]=pivot}}function mergeSort(arr,_comparer){var comparer=_comparer,middle,leftSection,rightSection;if(arr.constructor!==Array){throw new TypeError("Merge sort can only be applied on Array.")}if(arr.length===1){return arr}if(!comparer){comparer=function comparer(a,b){return a-b}}middle=Math.floor(arr.length/2);leftSection=arr.slice(0,middle);rightSection=arr.slice(middle);return merge(mergeSort(leftSection,comparer),mergeSort(rightSection,comparer),comparer)}function merge(leftSection,rightSection,comparer){var result=[],leftSecIndex=0,rightSecIndex=0;while(leftSecIndex<leftSection.length&&rightSecIndex<rightSection.length){if(comparer(leftSection[leftSecIndex],rightSection[rightSecIndex])<=0){result.push(leftSection[leftSecIndex]);leftSecIndex++}else{result.push(rightSection[rightSecIndex]);rightSecIndex++}}return result.concat(leftSection.slice(leftSecIndex)).concat(rightSection.slice(rightSecIndex))}var MINRUN_LENGTH=64,MIN_GALLOP_LENGTH=7,DEFAULT_TMP_STORAGE_LEN=256,MAX_STACK_SIZE=40;function reverseSlice(arr,_lo,_hi){var hi=_hi,lo=_lo;--hi;while(lo<hi){var temp=arr[lo];arr[lo++]=arr[hi];arr[hi--]=temp}}function countRun(arr,comparer,lo,hi){var endIndex=lo+1;if(endIndex===hi){return 1}if(comparer(arr[endIndex++],arr[lo])<0){while(endIndex<hi&&comparer(arr[endIndex],arr[endIndex-1])<0){endIndex++}reverseSlice(arr,lo,endIndex)}else{while(endIndex<hi&&comparer(arr[endIndex],arr[endIndex-1])>=0){endIndex++}}return endIndex-lo}function mergeComputeMinrun(_n){var r=0,n=_n;while(n>=MINRUN_LENGTH){r|=n&1;n>>=1}return n+r}function gallopLeft(key,arr,start,length,hint,comparer){var lastOffset=0,maxOffset=0,offset=1,tmp,m;if(comparer(key,arr[start+hint])>0){maxOffset=length-hint;while(offset<maxOffset&&comparer(key,arr[start+hint+offset])>0){lastOffset=offset;offset=(offset<<1)+1;if(offset<=0){offset=maxOffset}}if(offset>maxOffset){offset=maxOffset}lastOffset+=hint;offset+=hint}else{maxOffset=hint+1;while(offset<maxOffset&&comparer(key,arr[start+hint-offset])<=0){lastOffset=offset;offset=(offset<<1)+1;if(offset<=0){offset=maxOffset}}if(offset>maxOffset){offset=maxOffset}tmp=lastOffset;lastOffset=hint-offset;offset=hint-tmp}lastOffset++;while(lastOffset<offset){m=lastOffset+(offset-lastOffset>>>1);if(comparer(key,arr[start+m])>0){lastOffset=m+1}else{offset=m}}return offset}function gallopRight(value,arr,start,length,hint,comparer){var lastOffset=0,maxOffset=0,offset=1,tmp,m;if(comparer(value,arr[start+hint])<0){maxOffset=hint+1;while(offset<maxOffset&&comparer(value,arr[start+hint-offset])<0){lastOffset=offset;offset=(offset<<1)+1;if(offset<=0){offset=maxOffset}}if(offset>maxOffset){offset=maxOffset}tmp=lastOffset;lastOffset=hint-offset;offset=hint-tmp}else{maxOffset=length-hint;while(offset<maxOffset&&comparer(value,arr[start+hint+offset])>=0){lastOffset=offset;offset=(offset<<1)+1;if(offset<=0){offset=maxOffset}}if(offset>maxOffset){offset=maxOffset}lastOffset+=hint;offset+=hint}lastOffset++;while(lastOffset<offset){m=lastOffset+(offset-lastOffset>>>1);if(comparer(value,arr[start+m])<0){offset=m}else{lastOffset=m+1}}return offset}var MergeState=function(){function MergeState(arr,comparer){this.arr=arr;this.comparer=comparer;this.length=arr.length;this.tmpStorageLen=this.length<2*DEFAULT_TMP_STORAGE_LEN?this.length>>>1:DEFAULT_TMP_STORAGE_LEN;this.tmp=new Array(this.tmpStorageLen);this.runBaseArr=new Array(MAX_STACK_SIZE);this.runLenArr=new Array(MAX_STACK_SIZE);this.minGallop=MIN_GALLOP_LENGTH;this.stackSize=0}var _proto=MergeState.prototype;_proto.pushRun=function pushRun(runStart,runLength){this.runBaseArr[this.stackSize]=runStart;this.runLenArr[this.stackSize]=runLength;this.stackSize++};_proto.mergeCollapse=function mergeCollapse(){var n;while(this.stackSize>1){n=this.stackSize-2;if(n>=1&&this.runLenArr[n-1]<=this.runLenArr[n]+this.runLenArr[n+1]||n>=2&&this.runLenArr[n-2]<=this.runLenArr[n]+this.runLenArr[n-1]){if(this.runLenArr[n-1]<this.runLenArr[n+1]){n--}}else if(this.runLenArr[n]>this.runLenArr[n+1]){break}this.mergeAt(n)}};_proto.mergeForceCollapse=function mergeForceCollapse(){var n;while(this.stackSize>1){n=this.stackSize-2;if(n>0&&this.runLenArr[n-1]<this.runLenArr[n+1]){n--}this.mergeAt(n)}};_proto.mergeAt=function mergeAt(i){var comparer=this.comparer,arr=this.arr,start1=this.runBaseArr[i],length1=this.runLenArr[i],start2=this.runBaseArr[i+1],length2=this.runLenArr[i+1],k;this.runLenArr[i]=length1+length2;if(i===this.stackSize-3){this.runBaseArr[i+1]=this.runBaseArr[i+2];this.runLenArr[i+1]=this.runLenArr[i+2]}this.stackSize--;k=gallopRight(arr[start2],arr,start1,length1,0,comparer);start1+=k;length1-=k;if(length1===0){return}length2=gallopLeft(arr[start1+length1-1],arr,start2,length2,length2-1,comparer);if(length2===0){return}if(length1<=length2){this.mergeLo(start1,length1,start2,length2)}else{this.mergeHi(start1,length1,start2,length2)}};_proto.mergeLo=function mergeLo(start1,_length1,start2,_length2){var i=0,cursor1=0,cursor2=start2,dest=start1,minGallop,count1,count2,exit,length1=_length1,length2=_length2;for(i=0;i<length1;i++){this.tmp[i]=this.arr[start1+i]}this.arr[dest++]=this.arr[cursor2++];if(--length2===0){for(i=0;i<length1;i++){this.arr[dest+i]=this.tmp[cursor1+i]}return}if(length1===1){for(i=0;i<length2;i++){this.arr[dest+i]=this.arr[cursor2+i]}this.arr[dest+length2]=this.tmp[cursor1];return}minGallop=this.minGallop;while(true){count1=0;count2=0;exit=false;do{if(this.comparer(this.arr[cursor2],this.tmp[cursor1])<0){this.arr[dest++]=this.arr[cursor2++];count2++;count1=0;if(--length2===0){exit=true;break}}else{this.arr[dest++]=this.tmp[cursor1++];count1++;count2=0;if(--length1===1){exit=true;break}}}while((count1|count2)<minGallop);if(exit){break}do{count1=gallopRight(this.arr[cursor2],this.tmp,cursor1,length1,0,this.comparer);if(count1!==0){for(i=0;i<count1;i++){this.arr[dest+i]=this.tmp[cursor1+i]}dest+=count1;cursor1+=count1;length1-=count1;if(length1<=1){exit=true;break}}this.arr[dest++]=this.arr[cursor2++];if(--length2===0){exit=true;break}count2=gallopLeft(this.tmp[cursor1],this.arr,cursor2,length2,0,this.comparer);if(count2!==0){for(i=0;i<count2;i++){this.arr[dest+i]=this.arr[cursor2+i]}dest+=count2;cursor2+=count2;length2-=count2;if(length2===0){exit=true;break}}this.arr[dest++]=this.tmp[cursor1++];if(--length1===1){exit=true;break}minGallop--}while(count1>=MIN_GALLOP_LENGTH||count2>=MIN_GALLOP_LENGTH);if(exit){break}if(minGallop<0){minGallop=0}minGallop+=2}this.minGallop=minGallop;if(minGallop<1){this.minGallop=1}if(length1===1){for(i=0;i<length2;i++){this.arr[dest+i]=this.arr[cursor2+i]}this.arr[dest+length2]=this.tmp[cursor1]}else{for(i=0;i<length1;i++){this.arr[dest+i]=this.tmp[cursor1+i]}}};_proto.mergeHi=function mergeHi(start1,_length1,start2,_length2){var i=0,length1=_length1,length2=_length2,cursor1=start1+length1-1,cursor2=length2-1,dest=start2+length2-1,customCursor=0,customDest=0,minGallop,count1,count2,exit;for(i=0;i<length2;i++){this.tmp[i]=this.arr[start2+i]}this.arr[dest--]=this.arr[cursor1--];if(--length1===0){customCursor=dest-(length2-1);for(i=0;i<length2;i++){this.arr[customCursor+i]=this.tmp[i]}return}if(length2===1){dest-=length1;cursor1-=length1;customDest=dest+1;customCursor=cursor1+1;for(i=length1-1;i>=0;i--){this.arr[customDest+i]=this.arr[customCursor+i]}this.arr[dest]=this.tmp[cursor2];return}minGallop=this.minGallop;while(true){count1=0;count2=0;exit=false;do{if(this.comparer(this.tmp[cursor2],this.arr[cursor1])<0){this.arr[dest--]=this.arr[cursor1--];count1++;count2=0;if(--length1===0){exit=true;break}}else{this.arr[dest--]=this.tmp[cursor2--];count2++;count1=0;if(--length2===1){exit=true;break}}}while((count1|count2)<minGallop);if(exit){break}do{count1=length1-gallopRight(this.tmp[cursor2],this.arr,start1,length1,length1-1,this.comparer);if(count1!==0){dest-=count1;cursor1-=count1;length1-=count1;customDest=dest+1;customCursor=cursor1+1;for(i=count1-1;i>=0;i--){this.arr[customDest+i]=this.arr[customCursor+i]}if(length1===0){exit=true;break}}this.arr[dest--]=this.tmp[cursor2--];if(--length2===1){exit=true;break}count2=length2-gallopLeft(this.arr[cursor1],this.tmp,0,length2,length2-1,this.comparer);if(count2!==0){dest-=count2;cursor2-=count2;length2-=count2;customDest=dest+1;customCursor=cursor2+1;for(i=0;i<count2;i++){this.arr[customDest+i]=this.tmp[customCursor+i]}if(length2<=1){exit=true;break}}this.arr[dest--]=this.arr[cursor1--];if(--length1===0){exit=true;break}minGallop--}while(count1>=MIN_GALLOP_LENGTH||count2>=MIN_GALLOP_LENGTH);if(exit){break}if(minGallop<0){minGallop=0}minGallop+=2}this.minGallop=minGallop;if(minGallop<1){this.minGallop=1}if(length2===1){dest-=length1;cursor1-=length1;customDest=dest+1;customCursor=cursor1+1;for(i=length1-1;i>=0;i--){this.arr[customDest+i]=this.arr[customCursor+i]}this.arr[dest]=this.tmp[cursor2]}else{customCursor=dest-(length2-1);for(i=0;i<length2;i++){this.arr[customCursor+i]=this.tmp[i]}}};return MergeState}();function timSort(arr,_comparer,_lo,_hi){var lo=_lo,hi=_hi,comparer=_comparer,nremaining,n,minrun,mergeState,force;if(arr.constructor!==Array){throw new TypeError("Sorting can only be applied on arrays.")}lo=lo||0;hi=hi||arr.length;if(!comparer){comparer=function comparer(a,b){return a-b}}nremaining=hi-lo;if(nremaining<2){return}if(nremaining<MINRUN_LENGTH){n=countRun(arr,comparer,lo,hi);binaryInsertionSort(arr,comparer,lo,hi,lo+n);return}minrun=mergeComputeMinrun(nremaining);mergeState=new MergeState(arr,comparer);do{n=countRun(arr,comparer,lo,lo+nremaining);if(n<minrun){force=nremaining<=minrun?nremaining:minrun;binaryInsertionSort(arr,comparer,lo,lo+force,lo+n);n=force}mergeState.pushRun(lo,n);mergeState.mergeCollapse();nremaining-=n;lo+=n}while(nremaining!==0);mergeState.mergeForceCollapse()}var _default=timSort;exports["default"]=_default;