Last active
November 22, 2020 03:00
-
-
Save tpmccallum/71ae0d99d84e2d63b44b8813fdab899a to your computer and use it in GitHub Desktop.
MaxGraey_search_bytes_solution_II
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 = needle.length, | |
m = haystack.length, | |
d = m - n; | |
outer: for (let i = 0; i <= d; ++i) { | |
for (let j = 0; j < n; ++j) { | |
if (needle[j] !== haystack[i + j]) { | |
continue outer | |
} | |
} | |
resolve("Present"); | |
} | |
resolve("Absent") | |
}); | |
} | |
var buffer_1 = new ArrayBuffer(14500); | |
var buffer_2 = new ArrayBuffer(1000000); | |
var needle = new Uint8Array(buffer_1); | |
var haystack = new Uint8Array(buffer_2); | |
needle.fill(111); | |
haystack.fill(111); | |
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/1330149771047276547