Created
April 19, 2020 17:12
-
-
Save d-asensio/64cebc874aab4908824a17556f524b76 to your computer and use it in GitHub Desktop.
This file contains 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
class SkipListOptimizer { | |
private nItemsInList: number | |
constructor (nItemsInList: number) { | |
this.nItemsInList = nItemsInList; | |
} | |
public optimalLevels () { | |
const { floor, pow, log10 } = Math; | |
return floor( | |
pow(2, log10(this.nItemsInList)) | |
); | |
} | |
public optimalDispersionForLevel(level: number) { | |
const { pow } = Math; | |
return pow(2, level - 1); | |
} | |
public isPositionFillingLevel (itemPosition: number, level: number) { | |
const optimalLevels = this.optimalLevels(); | |
const levelDispersionRatio = this.optimalDispersionForLevel(level); | |
return ( | |
level <= optimalLevels && | |
itemPosition % levelDispersionRatio === 0 | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment