@onesy/algorithms
Version:
7 lines (6 loc) • 7.95 kB
JavaScript
/** @license Algorithms v1.0.0
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Algorithms={})}(this,(function(e){"use strict";const t={};const r={};const n={};const o={};var a="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};const c={},i="undefined"!=typeof window&&void 0!==window.document,u=!(void 0===a||"undefined"==typeof module||!module.exports);function s(e,t){var r;const n={...c,...arguments.length>2&&void 0!==arguments[2]?arguments[2]:{}},{variant:o}=n,a=t&&"object"==typeof t&&Object.getPrototypeOf(t);switch(e){case"string":return"string"==typeof t;case"number":return"number"==typeof t&&!Number.isNaN(t);case"boolean":return"boolean"==typeof t;case"array":return Array.isArray(t);case"object":return"object"==typeof t&&!!t&&t.constructor===Object;case"object-like":return"object"==typeof t&&(null===t||t.constructor!==Object);case"class":return("object"==typeof t||"function"==typeof t)&&(/class/gi.test(String(t))||/class/gi.test(String(null==t?void 0:t.constructor)));case"function":return!!(t&&t instanceof Function);case"async":return!(!s("function",t)||!(i?"AsyncFunction"===t.constructor.name:t()instanceof Promise));case"map":return!(a!==Map.prototype);case"weakmap":return!(a!==WeakMap.prototype);case"set":return!(a!==Set.prototype);case"weakset":return!(a!==WeakSet.prototype);case"promise":return!(a!==Promise.prototype);case"int8array":return!(a!==Int8Array.prototype);case"uint8array":return!(a!==Uint8Array.prototype);case"uint8clampedarray":return!(a!==Uint8ClampedArray.prototype);case"int16array":return!(a!==Int16Array.prototype);case"uint16array":return!(a!==Uint16Array.prototype);case"int32array":return!(a!==Int32Array.prototype);case"uint32array":return!(a!==Uint32Array.prototype);case"float32array":return!(a!==Float32Array.prototype);case"float64array":return!(a!==Float64Array.prototype);case"bigint64array":return!(a!==BigInt64Array.prototype);case"biguint64array":return!(a!==BigUint64Array.prototype);case"typedarray":return s("int8array",t)||s("uint8array",t)||s("uint8clampedarray",t)||s("int16array",t)||s("uint16array",t)||s("int32array",t)||s("uint32array",t)||s("float32array",t)||s("float64array",t)||s("bigint64array",t)||s("biguint64array",t);case"dataview":return!(a!==DataView.prototype);case"arraybuffer":return!(a!==ArrayBuffer.prototype);case"sharedarraybuffer":return"undefined"!=typeof SharedArrayBuffer&&!(a!==SharedArrayBuffer.prototype);case"symbol":return!("symbol"!=typeof t);case"error":return!!(t&&t instanceof Error);case"date":return!!(t&&t instanceof Date);case"regexp":return!!(t&&t instanceof RegExp);case"arguments":return!(!t||"[object Arguments]"!==t.toString());case"null":return null===t;case"undefined":return void 0===t;case"blob":return i&&t instanceof Blob;case"buffer":return!(!u||"function"!=typeof(null==t||null===(r=t.constructor)||void 0===r?void 0:r.isBuffer)||!t.constructor.isBuffer(t));case"element":if(t)switch(o){case void 0:case"html":case"element":return i&&("object"==typeof HTMLElement?t instanceof HTMLElement:t&&"object"==typeof t&&null!==t&&1===t.nodeType&&"string"==typeof t.nodeName);case"node":return i&&("object"==typeof Node?t instanceof Node:t&&"object"==typeof t&&null!==t&&"number"==typeof t.nodeType&&"string"==typeof t.nodeName);case"react":return t.elementType||t.hasOwnProperty("$$typeof");default:return!1}return!1;case"simple":return s("string",t,n)||s("number",t,n)||s("boolean",t,n)||s("undefined",t,n)||s("null",t,n);case"not-array-object":return!s("array",t,n)&&!s("object",t,n);default:return!1}}function f(e,t){let r=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];const n=[];let o=0,a=0;for(;o<e.length&&a<t.length;)(r?e[o]<t[a]:e[o]>t[a])?(n.push(e[o]),o++):(n.push(t[a]),a++);for(;o<e.length;)n.push(e[o]),o++;for(;a<t.length;)n.push(t[a]),a++;return n}function l(e){return Math.max(...e.map((e=>String(e).length)))}function y(e,t){let r=String(e);const n=r.length-1-t;return r=n>=0?r[n]:void 0,["+","-"].includes(r)?-1:r?+r:0}function p(e){let t=0,r=1;const n=e.length,o=new Array(n).fill(0);for(;r<n;)e[t]===e[r]?(o[r]=t+1,t++,r++):0!==t?t=0:r++;return o}e.binarySearch=function(e,t){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};r?.sort&&e.sort(s("function",r?.sortMethod)?r.sortMethod:(e,t)=>e-t);let n=0,o=e.length-1;for(;n<=o;){const r=n+Math.floor((o-n)/2);if(e[r]===t)return r;t<e[r]?o=r-1:n=r+1}return-1},e.binarySearchRecursive=function e(t,r,n,o){let a=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{},c=n,i=o;if(void 0===n&&void 0===o&&(c=0,i=t.length-1,a?.sort&&t.sort(s("function",a?.sortMethod)?a.sortMethod:(e,t)=>e-t)),i<=c&&t[c]!==r)return-1;const u=c+Math.floor((i-c)/2);return t[u]===r?u:r<t[u]?e(t,r,c,u-1,a):e(t,r,u+1,i,a)},e.bubbleSort=function(e){let t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];const r=e.length;let n=!0;for(;n;){n=!1;for(let o=0;o<r;o++)if(t?e[o]>e[o+1]:e[o]<e[o+1]){const t=e[o];e[o]=e[o+1],e[o+1]=t,n=!0}}return e},e.factorial=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{cache:!0};if(t?.cache&&void 0!==r[e])return r[e];if(e<3)return e;const n=[0,1,2];for(let t=3;t<=e;t++)n[t]=t*n[t-1];const o=n[n.length-1];return t?.cache&&void 0===r[e]&&(r[e]=o),o},e.factorialRecursive=function e(r){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{cache:!0};if(n?.cache&&void 0!==t[r])return t[r];if(r<3)return n?.cache&&void 0===t[r]&&(t[r]=r),r;const o=r*e(r-1,n);return n?.cache&&void 0===t[r]&&(t[r]=o),o},e.fibonacci=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{cache:!0};if(t?.cache&&void 0!==o[e])return o[e];if(e<2)return e;const r=[0,1];for(let t=2;t<=e;t++)r[t]=r[t-1]+r[t-2];const n=r[r.length-1];return t?.cache&&void 0===o[e]&&(o[e]=n),n},e.fibonacciRecursive=function e(t){let r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{cache:!0};if(r?.cache&&void 0!==n[t])return n[t];if(t<2)return r?.cache&&void 0===n[t]&&(n[t]=t),t;const o=e(t-1,r)+e(t-2,r);return r?.cache&&void 0===n[t]&&(n[t]=o),o},e.getDigit=y,e.insertionSort=function(e){let t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];const r=e.length;for(let n=1;n<r;n++){const r=e[n];let o=n-1;for(;o>=0&&(t?e[o]>r:e[o]<r);)e[o+1]=e[o],o--;e[o+1]=r}return e},e.kmp=function(e,t){const r=e.length,n=t.length,o=p(t);let a=0,c=0;for(;a<r;)if(e[a]===t[c]?(a++,c++):c>0?c=o[c-1]:a++,c===n)return!0;return!1},e.longestPrefixSuffix=p,e.maxDigits=l,e.mergeSort=function e(t){let r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(t.length<=1)return t;const n=t.length,o=Math.floor(n/2);return f(e(t.slice(0,o),r),e(t.slice(o),r),r)},e.mergeSortResolve=f,e.naiveSearch=function(e,t){if(t.length>e.length)return!1;for(let r=0;r<e.length;r++)for(let n=0;n<t.length&&e[r+n]===t[n];n++)if(n===t.length-1)return!0;return!1},e.quickSort=function e(t){let r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];const n=t.length;if(n<=1)return t;const o=t[n-1];let a=0;const c=[],i=[];for(;a<n-1;)(r?t[a]<o:t[a]>o)?c.push(t[a]):i.push(t[a]),a++;return[...e(c,r),o,...e(i,r)]},e.radixSort=function(e){let t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],r=e;const n=l(r);for(let e=0;e<n;e++){const n=Array.from({length:11},(()=>[]));for(let o=0;o<r.length;o++){const a=y(r[o],e);n[t?a+1:10-(a+1)].push(r[o])}r=n.flatMap((e=>e))}return r},e.selectionSort=function(e){let t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];const r=e.length;for(let n=0;n<r;n++){let o=n;for(let a=n+1;a<r;a++)(t?e[a]<e[o]:e[a]>e[o])&&(o=a);if(o!==n){const t=e[n];e[n]=e[o],e[o]=t}}return e},Object.defineProperty(e,"__esModule",{value:!0})}));