Skip to content

Instantly share code, notes, and snippets.

@sillyfellow
Last active July 22, 2018 18:14
Show Gist options
  • Save sillyfellow/40624d3905e243617d443490114c6eb8 to your computer and use it in GitHub Desktop.
Save sillyfellow/40624d3905e243617d443490114c6eb8 to your computer and use it in GitHub Desktop.
Pseudocode, heavily influenced by Golang
func RankReposition(newPosition int, siblings List) {
// No siblings, we take the middle spot
if len(siblings) == 0 {
return midWay // 2^63?
}
// Want to be the first one? Take the lowest rank and stand before it
if newPosition == 0 {
result = siblings.first.rank - windowSize
if result < 0 {
return -1 // we need respreading
}
return result
}
// To be the last one, take the last rank, and stand after it.
if newPosition >= len(siblings) {
result = siblings.last.rank + windowSize
if result > maxSize {
return -1 // we need respreading
}
return result
}
// To be at position i, stay in the middle of i-1 and i.
availableWindowSize = siblings[newPosition].rank - siblings[newPosition-1].rank
if availableWindowSize < 2 {
return -1 // we need respreading
}
return siblings[newPosition].rank + (availableWindowSize / 2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment