From 1150cd3e33efb4ac82bd5684db64e1aeb1f8f646 Mon Sep 17 00:00:00 2001 From: CHEN Feng Date: Sun, 7 Aug 2022 12:46:10 +0800 Subject: [PATCH] add credits for skiplist (#48) --- README.md | 26 +++++++++++++------------- README_zh.md | 26 +++++++++++++------------- skiplist.go | 10 ++++++++++ 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 6e72da6..bd0e419 100644 --- a/README.md +++ b/README.md @@ -1059,7 +1059,7 @@ type Signed interface { } ``` -## type [SkipList]() +## type [SkipList]() SkipList is a probabilistic data structure that seem likely to supplant balanced trees as the implementation method of choice for many applications. Skip list algorithms have the same asymptotic expected time bounds as balanced trees and are simpler, faster and use less space. @@ -1071,7 +1071,7 @@ type SkipList[K any, V any] struct { } ``` -### func [NewSkipList]() +### func [NewSkipList]() ```go func NewSkipList[K Ordered, V any]() *SkipList[K, V] @@ -1079,7 +1079,7 @@ func NewSkipList[K Ordered, V any]() *SkipList[K, V] NewSkipList creates a new Skiplist. -### func [NewSkipListFromMap]() +### func [NewSkipListFromMap]() ```go func NewSkipListFromMap[K Ordered, V any](m map[K]V) *SkipList[K, V] @@ -1087,7 +1087,7 @@ func NewSkipListFromMap[K Ordered, V any](m map[K]V) *SkipList[K, V] NewSkipListFromMap create a new Skiplist from a map. -### func [NewSkipListFunc]() +### func [NewSkipListFunc]() ```go func NewSkipListFunc[K any, V any](keyCmp CompareFn[K]) *SkipList[K, V] @@ -1095,13 +1095,13 @@ func NewSkipListFunc[K any, V any](keyCmp CompareFn[K]) *SkipList[K, V] NewSkipListFunc creates a new Skiplist with specified compare function keyCmp. -### func \(\*SkipList\[K, V\]\) [Clear]() +### func \(\*SkipList\[K, V\]\) [Clear]() ```go func (sl *SkipList[K, V]) Clear() ``` -### func \(\*SkipList\[K, V\]\) [Find]() +### func \(\*SkipList\[K, V\]\) [Find]() ```go func (sl *SkipList[K, V]) Find(key K) *V @@ -1109,25 +1109,25 @@ func (sl *SkipList[K, V]) Find(key K) *V Find returns the value associated with the passed key if the key is in the skiplist, otherwise returns nil. -### func \(\*SkipList\[K, V\]\) [ForEach]() +### func \(\*SkipList\[K, V\]\) [ForEach]() ```go func (sl *SkipList[K, V]) ForEach(op func(K, *V)) ``` -### func \(\*SkipList\[K, V\]\) [ForEachIf]() +### func \(\*SkipList\[K, V\]\) [ForEachIf]() ```go func (sl *SkipList[K, V]) ForEachIf(op func(K, *V) bool) ``` -### func \(\*SkipList\[K, V\]\) [Has]() +### func \(\*SkipList\[K, V\]\) [Has]() ```go func (sl *SkipList[K, V]) Has(key K) bool ``` -### func \(\*SkipList\[K, V\]\) [Insert]() +### func \(\*SkipList\[K, V\]\) [Insert]() ```go func (sl *SkipList[K, V]) Insert(key K, value V) @@ -1135,19 +1135,19 @@ func (sl *SkipList[K, V]) Insert(key K, value V) Insert inserts a key\-value pair into the skiplist. If the key is already in the skip list, it's value will be updated. -### func \(\*SkipList\[K, V\]\) [IsEmpty]() +### func \(\*SkipList\[K, V\]\) [IsEmpty]() ```go func (sl *SkipList[K, V]) IsEmpty() bool ``` -### func \(\*SkipList\[K, V\]\) [Len]() +### func \(\*SkipList\[K, V\]\) [Len]() ```go func (sl *SkipList[K, V]) Len() int ``` -### func \(\*SkipList\[K, V\]\) [Remove]() +### func \(\*SkipList\[K, V\]\) [Remove]() ```go func (sl *SkipList[K, V]) Remove(key K) bool diff --git a/README_zh.md b/README_zh.md index 6a01282..697f606 100644 --- a/README_zh.md +++ b/README_zh.md @@ -1055,7 +1055,7 @@ type Signed interface { } ``` -## type [SkipList]() +## type [SkipList]() SkipList is a probabilistic data structure that seem likely to supplant balanced trees as the implementation method of choice for many applications. Skip list algorithms have the same asymptotic expected time bounds as balanced trees and are simpler, faster and use less space. @@ -1067,7 +1067,7 @@ type SkipList[K any, V any] struct { } ``` -### func [NewSkipList]() +### func [NewSkipList]() ```go func NewSkipList[K Ordered, V any]() *SkipList[K, V] @@ -1075,7 +1075,7 @@ func NewSkipList[K Ordered, V any]() *SkipList[K, V] NewSkipList creates a new Skiplist. -### func [NewSkipListFromMap]() +### func [NewSkipListFromMap]() ```go func NewSkipListFromMap[K Ordered, V any](m map[K]V) *SkipList[K, V] @@ -1083,7 +1083,7 @@ func NewSkipListFromMap[K Ordered, V any](m map[K]V) *SkipList[K, V] NewSkipListFromMap create a new Skiplist from a map. -### func [NewSkipListFunc]() +### func [NewSkipListFunc]() ```go func NewSkipListFunc[K any, V any](keyCmp CompareFn[K]) *SkipList[K, V] @@ -1091,13 +1091,13 @@ func NewSkipListFunc[K any, V any](keyCmp CompareFn[K]) *SkipList[K, V] NewSkipListFunc creates a new Skiplist with specified compare function keyCmp. -### func \(\*SkipList\[K, V\]\) [Clear]() +### func \(\*SkipList\[K, V\]\) [Clear]() ```go func (sl *SkipList[K, V]) Clear() ``` -### func \(\*SkipList\[K, V\]\) [Find]() +### func \(\*SkipList\[K, V\]\) [Find]() ```go func (sl *SkipList[K, V]) Find(key K) *V @@ -1105,25 +1105,25 @@ func (sl *SkipList[K, V]) Find(key K) *V Find returns the value associated with the passed key if the key is in the skiplist, otherwise returns nil. -### func \(\*SkipList\[K, V\]\) [ForEach]() +### func \(\*SkipList\[K, V\]\) [ForEach]() ```go func (sl *SkipList[K, V]) ForEach(op func(K, *V)) ``` -### func \(\*SkipList\[K, V\]\) [ForEachIf]() +### func \(\*SkipList\[K, V\]\) [ForEachIf]() ```go func (sl *SkipList[K, V]) ForEachIf(op func(K, *V) bool) ``` -### func \(\*SkipList\[K, V\]\) [Has]() +### func \(\*SkipList\[K, V\]\) [Has]() ```go func (sl *SkipList[K, V]) Has(key K) bool ``` -### func \(\*SkipList\[K, V\]\) [Insert]() +### func \(\*SkipList\[K, V\]\) [Insert]() ```go func (sl *SkipList[K, V]) Insert(key K, value V) @@ -1131,19 +1131,19 @@ func (sl *SkipList[K, V]) Insert(key K, value V) Insert inserts a key\-value pair into the skiplist. If the key is already in the skip list, it's value will be updated. -### func \(\*SkipList\[K, V\]\) [IsEmpty]() +### func \(\*SkipList\[K, V\]\) [IsEmpty]() ```go func (sl *SkipList[K, V]) IsEmpty() bool ``` -### func \(\*SkipList\[K, V\]\) [Len]() +### func \(\*SkipList\[K, V\]\) [Len]() ```go func (sl *SkipList[K, V]) Len() int ``` -### func \(\*SkipList\[K, V\]\) [Remove]() +### func \(\*SkipList\[K, V\]\) [Remove]() ```go func (sl *SkipList[K, V]) Remove(key K) bool diff --git a/skiplist.go b/skiplist.go index cdfc38a..b035f3d 100644 --- a/skiplist.go +++ b/skiplist.go @@ -1,3 +1,13 @@ +// This implementation is based on https://github.com/liyue201/gostl/tree/master/ds/skiplist +// (many thanks), added many optimizations, such as: +// +// - adaptive level +// - lesser search for prevs when key already exists. +// - reduce memory allocations +// - richer interface. +// +// etc. + package stl4go import (