Skip to content

Instantly share code, notes, and snippets.

@d-asensio
Created April 19, 2020 17:12
Show Gist options
  • Save d-asensio/64cebc874aab4908824a17556f524b76 to your computer and use it in GitHub Desktop.
Save d-asensio/64cebc874aab4908824a17556f524b76 to your computer and use it in GitHub Desktop.
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