2021-10-03 05:02:56 +08:00
|
|
|
package skiplist
|
|
|
|
|
|
|
|
import "bytes"
|
|
|
|
|
|
|
|
func compareElement(a *SkipListElement, key []byte) int {
|
2021-10-03 16:07:35 +08:00
|
|
|
if len(a.Key) == 0 {
|
2021-10-03 05:02:56 +08:00
|
|
|
return -1
|
|
|
|
}
|
2021-10-03 16:07:35 +08:00
|
|
|
return bytes.Compare(a.Key, key)
|
2021-10-03 05:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *SkipListElement) Reference() *SkipListElementReference {
|
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &SkipListElementReference{
|
|
|
|
ElementPointer: node.Id,
|
2021-10-03 16:07:35 +08:00
|
|
|
Key: node.Key,
|
2021-10-03 05:02:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-03 17:19:21 +08:00
|
|
|
|
2021-10-07 09:18:24 +08:00
|
|
|
func (t *SkipList) SaveElement(element *SkipListElement) error {
|
2021-10-03 17:19:21 +08:00
|
|
|
if element == nil {
|
|
|
|
return nil
|
2021-10-03 05:02:56 +08:00
|
|
|
}
|
2021-10-07 09:18:24 +08:00
|
|
|
return t.ListStore.SaveElement(element.Id, element)
|
2021-10-03 05:02:56 +08:00
|
|
|
}
|
|
|
|
|
2021-10-07 09:18:24 +08:00
|
|
|
func (t *SkipList) DeleteElement(element *SkipListElement) error {
|
2021-10-03 17:19:21 +08:00
|
|
|
if element == nil {
|
|
|
|
return nil
|
2021-10-03 05:02:56 +08:00
|
|
|
}
|
2021-10-07 09:18:24 +08:00
|
|
|
return t.ListStore.DeleteElement(element.Id)
|
2021-10-03 05:02:56 +08:00
|
|
|
}
|
|
|
|
|
2021-10-07 09:18:24 +08:00
|
|
|
func (t *SkipList) LoadElement(ref *SkipListElementReference) (*SkipListElement, error) {
|
2021-10-04 17:30:44 +08:00
|
|
|
if ref.IsNil() {
|
2021-10-03 17:19:21 +08:00
|
|
|
return nil, nil
|
2021-10-03 05:02:56 +08:00
|
|
|
}
|
2021-10-07 09:18:24 +08:00
|
|
|
return t.ListStore.LoadElement(ref.ElementPointer)
|
2021-10-03 05:02:56 +08:00
|
|
|
}
|
2021-10-04 17:30:44 +08:00
|
|
|
|
|
|
|
func (ref *SkipListElementReference) IsNil() bool {
|
|
|
|
if ref == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(ref.Key) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|