Returns a random level for the new skiplist node we are going to create. * The return value of this function is between 1 and ZSKIPLIST_MAXLEVEL * (both inclusive), with a powerlaw-alike distribution where higher * levels are less likely to be returned. */
| 120 | * (both inclusive), with a powerlaw-alike distribution where higher |
| 121 | * levels are less likely to be returned. */ |
| 122 | int zslRandomLevel(void) { |
| 123 | int level = 1; |
| 124 | while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF)) |
| 125 | level += 1; |
| 126 | return (level<ZSKIPLIST_MAXLEVEL) ? level : ZSKIPLIST_MAXLEVEL; |
| 127 | } |
| 128 | |
| 129 | /* Insert a new node in the skiplist. Assumes the element does not already |
| 130 | * exist (up to the caller to enforce that). The skiplist takes ownership |