randomLevel is a function that generates a probabilistic level for a node in a SkipList data structure. The goal is to diversify the level distribution and contribute to achieving an ideal skiplist performance. Function has no parameters. The process starts with two initial variables: - 'level' whic
()
| 133 | // If 'level' is greater, 'SKIPLIST_MAX_LEVEL' is returned, otherwise the calculated 'level' value is returned. |
| 134 | // The function returns an integer which will be the level of new node in skiplist. |
| 135 | func randomLevel() int { |
| 136 | // Initialize level to 1 |
| 137 | level := 1 |
| 138 | r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 139 | // Calculate the threshold for level. It's derived from the probability constant of the skip list. |
| 140 | thresh := int(math.Round(SKIPLIST_PROB * 0xFFF)) |
| 141 | |
| 142 | // While a randomly generated number is less than this threshold, increment the level. |
| 143 | for int(r.Int31()&0xFFF) < thresh { |
| 144 | level++ |
| 145 | } |
| 146 | |
| 147 | // Check if the level is more than the maximum allowed level for the skip list |
| 148 | // If it is, return the maximum level. Otherwise, return the generated level. |
| 149 | if level > SKIPLIST_MAX_LEVEL { |
| 150 | return SKIPLIST_MAX_LEVEL |
| 151 | } else { |
| 152 | return level |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // NewZSetStructure Returns a new ZSetStructure |
| 157 | func NewZSetStructure(options config.Options) (*ZSetStructure, error) { |
no outgoing calls