Last active
November 22, 2020 01:13
-
-
Save tpmccallum/48ef2f600c4364fa88c61fab13208e9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isSubset(haystack, needle) { | |
return new Promise(function(resolve, reject) { | |
let n = haystack.length; | |
let m = needle.length; | |
for (let i = 0, j = 0; i < n; i++) { | |
if (haystack[i] === needle[j++]) { | |
if (j >= m) resolve("Present"); | |
} else j = 0 | |
} | |
resolve("Absent"); | |
}); | |
} | |
var buffer_1 = new ArrayBuffer(50000); | |
var buffer_2 = new ArrayBuffer(1000000); | |
var needle = new Uint8Array(buffer_1); | |
var haystack = new Uint8Array(buffer_2); | |
needle.fill(111); | |
haystack.fill(222); | |
if (needle_length < 1 || needle_length > haystack_length) { | |
alert("Needle length must be greater than zero and less than haystack length"); | |
} else { | |
isSubset(haystack, needle) | |
.then(function(result) { | |
console.log("Result: " + result); | |
}) | |
.catch(function() { | |
console.log("Error"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by this Tweet https://twitter.com/MaxGraey/status/1329915996342259720