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