InsertNode is a method on the FZSet structure. It inserts a new node or updates an existing node in the skip list and the dictionary. It takes three parameters: score (an integer), key (a string), and value (of any interface type). If key already exists in the dictionary and the score equals the ex
(score int, member string, value interface{})
| 943 | // to the dictionary, increments the size of the dictionary by 1, and also adds |
| 944 | // the node to the skip list. |
| 945 | func (fzs *FZSet) InsertNode(score int, member string, value interface{}) error { |
| 946 | // Instantiate dictionary if it's not already |
| 947 | if fzs.dict == nil { |
| 948 | fzs.dict = make(map[string]*ZSetValue) |
| 949 | } |
| 950 | if fzs.skipList == nil { |
| 951 | fzs.skipList = newSkipList() |
| 952 | } |
| 953 | |
| 954 | // Check if key exists in dictionary |
| 955 | if v, ok := fzs.dict[member]; ok { |
| 956 | if v.score != score { |
| 957 | // Update value and score as the score remains the same |
| 958 | fzs.skipList.delete(score, member) |
| 959 | fzs.dict[member] = fzs.skipList.insert(score, member, value) |
| 960 | } else { |
| 961 | // Ranking isn't altered, only update value |
| 962 | v.value = value |
| 963 | } |
| 964 | } else { // Key doesn't exist, create new key |
| 965 | fzs.dict[member] = fzs.skipList.insert(score, member, value) |
| 966 | fzs.size++ // Increase size count by 1 |
| 967 | // Node is also added to the skip list |
| 968 | } |
| 969 | |
| 970 | // Returns nil as no specific error condition is checked in this function |
| 971 | return nil |
| 972 | } |
| 973 | func (zs *ZSetStructure) adjustMinMax(zSet *FZSet, min int, max int) (adjustedMin int, adjustedMax int, err error) { |
| 974 | if min > max { |
| 975 | return min, max, ErrInvalidArgs |