getRank method receives a SkipList pointer and two parameters: an integer 'score' and a string 'key'. It then calculates the rank of an element in the SkipList. The rank is determined based on two conditions: - the score of the next node is less than the provided score - or, the score of the next no
(score int, key string)
| 404 | // Return: |
| 405 | // Returns the rank of the element in the SkipList if it's found, otherwise returns 0. |
| 406 | func (sl *SkipList) getRank(score int, key string) int { |
| 407 | var rank int |
| 408 | h := sl.head // Start at the head node of the SkipList |
| 409 | |
| 410 | // For loop starts from the top level and goes down to the level 0 |
| 411 | for i := sl.level; i >= 0; i-- { |
| 412 | // While loop advances the 'h' pointer as long as the next node exists and the conditions are fulfilled |
| 413 | for h.level[i].next != nil && |
| 414 | (h.level[i].next.value.score < score || |
| 415 | (h.level[i].next.value.score == score && |
| 416 | h.level[i].next.value.member <= key)) { |
| 417 | |
| 418 | // Increase the rank by the span of the current level |
| 419 | rank += h.level[i].span |
| 420 | // Move to the next node |
| 421 | h = h.level[i].next |
| 422 | } |
| 423 | // If the key of the current node is equal to the provided key, return the rank |
| 424 | if h.value.member == key { |
| 425 | return rank |
| 426 | } |
| 427 | } |
| 428 | // If the element is not found in the SkipList, return 0 |
| 429 | return 0 |
| 430 | } |
| 431 | |
| 432 | // getNodeByRank is a method of the SkipList type that is used to retrieve a node based on its rank within the list. |
| 433 | // The method takes as argument an integer rank and returns a pointer to the SkipListNode at the specified rank, |