github.com/andy2046/gopie@v0.7.0/pkg/skiplist/types.go (about)

     1  package skiplist
     2  
     3  import (
     4  	"math/rand"
     5  	"sync"
     6  )
     7  
     8  type (
     9  	elementNode struct {
    10  		forward []*Element
    11  	}
    12  
    13  	// Element represents an element in the list.
    14  	Element struct {
    15  		elementNode
    16  		key   string
    17  		value int64
    18  	}
    19  
    20  	// SkipList represents the list.
    21  	SkipList struct {
    22  		elementNode
    23  		randSource       rand.Source
    24  		searchNodesCache []*elementNode
    25  		probability      float64
    26  		probTable        []float64
    27  		maxLevel         int
    28  		length           int
    29  		mutex            sync.RWMutex
    30  	}
    31  )
    32  
    33  // Key returns the given Element key.
    34  func (e *Element) Key() string {
    35  	return e.key
    36  }
    37  
    38  // Value returns the given Element value.
    39  func (e *Element) Value() int64 {
    40  	return e.value
    41  }
    42  
    43  // Next returns the adjacent next Element if existed or nil otherwise.
    44  func (e *Element) Next() *Element {
    45  	return e.forward[0]
    46  }
    47  
    48  // NextLevel returns the adjacent next Element at provided level if existed or nil otherwise.
    49  func (e *Element) NextLevel(level int) *Element {
    50  	if level >= len(e.forward) || level < 0 {
    51  		return nil
    52  	}
    53  
    54  	return e.forward[level]
    55  }