Created
January 18, 2022 16:41
-
-
Save gkucmierz/d7c1f1c486c6eb6ab1dd0a6f8953eb71 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
// usefull object, to clip array in O(1) when we don't want to make copies of it (slice) | |
const ClipArray = arr => { | |
let low = 0; | |
let up = arr.length; | |
const length = () => up - low; | |
return { | |
clipHead: n => { | |
low += n; | |
if (low > up) low = up; | |
}, | |
clipTail: n => { | |
up -= n; | |
if (up < low) up = low; | |
}, | |
get: idx => { | |
if (idx >= length || idx < 0) return [][0]; | |
return arr[low + idx]; | |
}, | |
getClipped: () => arr.slice(low, up), | |
length | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment