Documentation
¶
Overview ¶
package skiplist implements a thread-safe, generic skiplist. A skiplist is a probabilistic data structure that allows for fast search, insertion, and deletion of elements in a sorted sequence. It provides O(log n) performance on average for these operations.
Index ¶
- Constants
- type Arena
- type ArenaOption
- type Comparator
- type INode
- type Iterator
- func (it *Iterator[K, V]) Clone() *Iterator[K, V]
- func (it *Iterator[K, V]) First() bool
- func (it *Iterator[K, V]) Key() K
- func (it *Iterator[K, V]) Last() bool
- func (it *Iterator[K, V]) Next() bool
- func (it *Iterator[K, V]) Prev() bool
- func (it *Iterator[K, V]) Reset()
- func (it *Iterator[K, V]) Seek(key K) bool
- func (it *Iterator[K, V]) SeekToFirst() bool
- func (it *Iterator[K, V]) SeekToLast() bool
- func (it *Iterator[K, V]) Value() V
- type IteratorOption
- type Option
- type SkipList
- func (sl *SkipList[K, V]) Clear()
- func (sl *SkipList[K, V]) CountRange(start, end K) int
- func (sl *SkipList[K, V]) Delete(key K) bool
- func (sl *SkipList[K, V]) GetByRank(rank int) (INode[K, V], bool)
- func (sl *SkipList[K, V]) Insert(key K, value V) INode[K, V]
- func (sl *SkipList[K, V]) Len() int
- func (sl *SkipList[K, V]) Max() (INode[K, V], bool)
- func (sl *SkipList[K, V]) Min() (INode[K, V], bool)
- func (sl *SkipList[K, V]) NewIterator(opts ...IteratorOption[K, V]) *Iterator[K, V]
- func (sl *SkipList[K, V]) PopMax() (INode[K, V], bool)
- func (sl *SkipList[K, V]) PopMin() (INode[K, V], bool)
- func (sl *SkipList[K, V]) Predecessor(key K) (INode[K, V], bool)
- func (sl *SkipList[K, V]) Range(f func(key K, value V) bool)
- func (sl *SkipList[K, V]) RangeQuery(start, end K, f func(key K, value V) bool)
- func (sl *SkipList[K, V]) RangeWithIterator(f func(it *Iterator[K, V]))
- func (sl *SkipList[K, V]) Rank(key K) int
- func (sl *SkipList[K, V]) Search(key K) (INode[K, V], bool)
- func (sl *SkipList[K, V]) Seek(key K) (INode[K, V], bool)
- func (sl *SkipList[K, V]) Successor(key K) (INode[K, V], bool)
Constants ¶
const ( // MaxLevel is the maximum number of levels a skiplist can have. // A value of 32 is sufficient for approximately 2^32 (4 billion) items. // MaxLevel คือจำนวนชั้นสูงสุดที่ skiplist สามารถมีได้ // ค่า 32 เพียงพอสำหรับข้อมูลประมาณ 2^32 (4 พันล้าน) รายการ MaxLevel = 32 // P is the probability used to determine the level of a new node. // A value of 0.25 is commonly used and provides good performance. // P คือค่าความน่าจะเป็นในการเพิ่มชั้นของโหนดใหม่ // ค่า 0.25 เป็นค่าที่นิยมใช้และให้ประสิทธิภาพที่ดี P = 0.25 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Arena ¶ added in v1.2.0
type Arena struct {
// contains filtered or unexported fields
}
The Arena itself is NOT thread-safe; synchronization must be handled by the caller. ตัว Arena เองไม่ thread-safe; ผู้เรียกต้องจัดการ synchronization เอง
func NewArena ¶ added in v1.2.0
func NewArena(initialSize int, opts ...ArenaOption) *Arena
NewArena creates a new, potentially growable Arena.
func (*Arena) Alloc ¶ added in v1.2.0
Alloc จัดสรรหน่วยความจำตามขนาด (size) และ alignment ที่ต้องการ คืนค่า pointer ไปยังหน่วยความจำที่จัดสรร หรือ nil หาก Arena เต็ม. This implementation is NOT thread-safe. The caller must provide synchronization. การทำงานนี้ไม่ thread-safe ผู้เรียกต้องจัดการ synchronization เอง
type ArenaOption ¶ added in v1.2.1
type ArenaOption func(*Arena)
ArenaOption configures an Arena.
func WithGrowthBytes ¶ added in v1.2.1
func WithGrowthBytes(bytes int) ArenaOption
WithGrowthBytes sets the arena to grow by a fixed number of bytes.
func WithGrowthFactor ¶ added in v1.2.1
func WithGrowthFactor(factor float64) ArenaOption
WithGrowthFactor sets the arena to grow by a factor of the previous chunk's size. For example, a factor of 2.0 means each new chunk will be twice as large as the last.
func WithGrowthThreshold ¶ added in v1.2.1
func WithGrowthThreshold(threshold float64) ArenaOption
WithGrowthThreshold sets a threshold (e.g., 0.9 for 90%) for proactive growth. If an allocation would cause the current chunk's usage to exceed this threshold, a new chunk is allocated preemptively. This helps avoid leaving small, unusable fragments at the end of chunks. A threshold of 0 disables this feature.
type Comparator ¶
Comparator is a function that compares two keys. It should return:
- a negative value if a < b
- zero if a == b
- a positive value if a > b
Comparator คือฟังก์ชันสำหรับเปรียบเทียบ key สองตัว ควรคืนค่า:
< 0 ถ้า a น้อยกว่า b 0 ถ้า a เท่ากับ b > 0 ถ้า a มากกว่า b
type Iterator ¶
Iterator provides a way to iterate over the elements of a SkipList. The typical use is:
it := sl.NewIterator()
for it.Next() {
key := it.Key()
value := it.Value()
// ...
}
Iterator คือโครงสร้างที่ใช้สำหรับวนลูปผ่านรายการใน Skiplist รูปแบบการใช้งานทั่วไป:
it := sl.NewIterator()
for it.Next() {
key := it.Key()
value := it.Value()
// ...
}
func (*Iterator[K, V]) Clone ¶ added in v1.2.0
Clone creates an independent copy of the iterator at its current position. The new iterator can be moved independently of the original. Clone สร้างสำเนาของ Iterator ณ ตำแหน่งปัจจุบัน Iterator ที่สร้างขึ้นใหม่จะทำงานเป็นอิสระจากตัวต้นฉบับ
func (*Iterator[K, V]) First ¶ added in v1.2.0
First moves the iterator to the first element in the skiplist. This is the element with the smallest key, regardless of the iterator's direction. It returns true if a first element exists, otherwise it returns false. After a call to First(), Key() and Value() will return the data of the first element. First เลื่อน Iterator ไปยังรายการแรกใน Skiplist (รายการที่มี key น้อยที่สุด) โดยไม่สนใจทิศทางของ iterator คืนค่า true หากมีรายการแรกอยู่, มิฉะนั้นคืนค่า false หลังจากเรียก First(), Key() และ Value() จะคืนค่าของรายการแรก
func (*Iterator[K, V]) Key ¶
func (it *Iterator[K, V]) Key() K
Key returns the key of the element at the current iterator position. It should only be called after a call to Next() has returned true. Key คืนค่า key ของรายการปัจจุบันที่ Iterator ชี้อยู่ ควรเรียกใช้หลังจากที่ Next(), First(), Last(), หรือ Seek() ที่สำเร็จ คืนค่า true เท่านั้น การเรียกใช้บน iterator ที่ไม่ถูกต้อง (เช่น สิ้นสุดไปแล้ว) จะทำให้เกิด panic
func (*Iterator[K, V]) Last ¶ added in v1.2.0
Last moves the iterator to the last element in the skiplist. This is the element with the largest key, regardless of the iterator's direction. It returns true if a last element exists, otherwise it returns false. After a call to Last(), Key() and Value() will return the data of the last element. Last เลื่อน Iterator ไปยังรายการสุดท้ายใน Skiplist (รายการที่มี key มากที่สุด) โดยไม่สนใจทิศทางของ iterator คืนค่า true หากมีรายการสุดท้ายอยู่, มิฉะนั้นคืนค่า false หลังจากเรียก Last(), Key() และ Value() จะคืนค่าของรายการสุดท้าย
func (*Iterator[K, V]) Next ¶
Next moves the iterator to the next element and returns true if the move was successful. If the iterator was created with WithReverse, "next" means the previous element in the list. It returns false if there are no more elements. Next เลื่อน Iterator ไปยังรายการถัดไป และคืนค่า true หากสำเร็จ หาก Iterator ถูกสร้างด้วย WithReverse, "ถัดไป" จะหมายถึงรายการ "ก่อนหน้า" คืนค่า false หากไม่มีรายการเหลือแล้ว
func (*Iterator[K, V]) Prev ¶ added in v1.2.0
Prev moves the iterator to the previous element and returns true if the move was successful. If the iterator was created with WithReverse, "previous" means the next element in the list. It returns false if there are no more elements in that direction. Prev เลื่อน Iterator ไปยังรายการก่อนหน้า และคืนค่า true หากสำเร็จ หาก Iterator ถูกสร้างด้วย WithReverse, "ก่อนหน้า" จะหมายถึงรายการ "ถัดไป" คืนค่า false หากไม่มีรายการเหลือแล้วในทิศทางนั้น
func (*Iterator[K, V]) Reset ¶ added in v1.2.0
func (it *Iterator[K, V]) Reset()
Reset moves the iterator back to its initial state, before the first element. A subsequent call to Next() is required to advance to the first element. This method respects the iterator's direction (normal or reverse). Reset เลื่อน Iterator กลับไปยังสถานะเริ่มต้น (ก่อนรายการแรก) โดยจะเคารพทิศทางของ iterator (ปกติหรือย้อนกลับ) ต้องเรียก Next() อีกครั้งเพื่อเลื่อนไปยังรายการแรก
func (*Iterator[K, V]) Seek ¶
Seek moves the iterator to the first element with a key greater than or equal to the given key. This behavior is consistent regardless of the iterator's direction (normal or reverse). It returns true if such an element is found, otherwise it returns false and the iterator is positioned at the end. After a successful seek, Key() and Value() will return the data of the found element. Seek เลื่อน Iterator ไปยังรายการแรกที่มี key เท่ากับหรือมากกว่า key ที่กำหนด พฤติกรรมนี้จะเหมือนกันเสมอ ไม่ว่า iterator จะเป็นแบบปกติหรือแบบย้อนกลับ (reverse) คืนค่า true หากพบรายการดังกล่าว, มิฉะนั้นคืนค่า false และ Iterator จะชี้ไปที่ท้ายสุด หลังจาก seek สำเร็จ, Key() และ Value() จะคืนค่าของรายการที่พบ
func (*Iterator[K, V]) SeekToFirst ¶ added in v1.2.1
SeekToFirst positions the iterator just before the first element. For a forward iterator, this is before the smallest key. For a reverse iterator, this is before the largest key. A subsequent call to Next() will advance the iterator to the first element. It returns true if the list is not empty, indicating that a subsequent call to Next() will succeed. SeekToFirst เลื่อน Iterator ไปยังตำแหน่งก่อนหน้าของรายการแรกในลำดับการวนลูป สำหรับ iterator ไปข้างหน้า: คือก่อน key ที่น้อยที่สุด สำหรับ iterator ย้อนกลับ: คือก่อน key ที่มากที่สุด คืนค่า true หาก list ไม่ว่างเปล่า เพื่อบ่งชี้ว่าการเรียก Next() ครั้งถัดไปจะสำเร็จ
func (*Iterator[K, V]) SeekToLast ¶ added in v1.2.1
SeekToLast positions the iterator just before the last element in the skiplist (the one with the largest key). This behavior is consistent regardless of the iterator's direction (normal or reverse). A subsequent call to Next() will advance the iterator to the last element. It returns true if the list is not empty.
SeekToLast เลื่อน Iterator ไปยังตำแหน่งก่อนหน้าของรายการสุดท้ายใน Skiplist (รายการที่มี key สูงสุด) พฤติกรรมนี้จะเหมือนกันเสมอ ไม่ว่า iterator จะเป็นแบบปกติหรือแบบย้อนกลับ (reverse) การเรียก Next() หลังจากนี้จะเลื่อนไปยังรายการสุดท้าย
func (*Iterator[K, V]) Value ¶
func (it *Iterator[K, V]) Value() V
Value returns the value of the element at the current iterator position. It should only be called after a call to Next() has returned true. Value คืนค่า value ของรายการปัจจุบันที่ Iterator ชี้อยู่ ควรเรียกใช้หลังจากที่ Next(), First(), Last(), หรือ Seek() ที่สำเร็จ คืนค่า true เท่านั้น การเรียกใช้บน iterator ที่ไม่ถูกต้อง (เช่น สิ้นสุดไปแล้ว) จะทำให้เกิด panic
type IteratorOption ¶ added in v1.2.1
IteratorOption configures an Iterator. IteratorOption คือฟังก์ชันสำหรับกำหนดค่าของ Iterator
func WithReverse ¶ added in v1.2.1
func WithReverse[K any, V any]() IteratorOption[K, V]
WithReverse creates an iterator that iterates from the last element to the first. The standard `for it.Next() { ... }` loop will work in reverse.
type Option ¶ added in v1.2.0
Option is a function that configures a SkipList. Option คือฟังก์ชันสำหรับกำหนดค่าของ SkipList
func WithArena ¶ added in v1.2.0
WithArena configures the SkipList to use a memory arena of a given size in bytes. This sets the initial size of the arena. The arena can grow automatically if needed. WithArena กำหนดให้ SkipList ใช้ memory arena ตามขนาดที่ระบุ (เป็น byte) เป็นการกำหนดขนาดเริ่มต้น และ arena สามารถขยายขนาดได้เองเมื่อจำเป็น
func WithArenaGrowthBytes ¶ added in v1.2.1
WithArenaGrowthBytes configures the arena to grow by a fixed number of bytes. This option is only effective when used with WithArena.
func WithArenaGrowthFactor ¶ added in v1.2.1
WithArenaGrowthFactor configures the arena to grow by a factor of the previous chunk's size. For example, a factor of 2.0 means each new chunk will be twice as large as the last. This option is only effective when used with WithArena.
func WithArenaGrowthThreshold ¶ added in v1.2.1
WithArenaGrowthThreshold configures the arena's proactive growth threshold (e.g., 0.9 for 90%). If an allocation would cause a chunk's usage to exceed this threshold, the arena will grow preemptively. This option is only effective when used with WithArena.
type SkipList ¶
SkipList represents a thread-safe skiplist data structure. The zero value for a SkipList is not ready to use; one of the New functions must be called. SkipList คือโครงสร้างหลักของ skiplist ค่า zero value ของ SkipList จะยังไม่พร้อมใช้งาน, ต้องสร้างผ่านฟังก์ชัน New... เท่านั้น
func New ¶
New creates a new skiplist for key types that implement cmp.Ordered (e.g., int, string). It uses cmp.Compare as the default comparator. New สร้าง skiplist ใหม่สำหรับ key type ที่รองรับ `cmp.Ordered` (เช่น int, string) โดยจะใช้ `cmp.Compare` เป็นฟังก์ชันเปรียบเทียบโดยอัตโนมัติ
func NewWithComparator ¶
func NewWithComparator[K any, V any](compare Comparator[K], opts ...Option[K, V]) *SkipList[K, V]
NewWithComparator creates a new skiplist with a custom comparator function. This is suitable for key types that do not implement cmp.Ordered or require special sorting logic. The comparator function must not be nil. NewWithComparator สร้าง skiplist ใหม่พร้อมกับฟังก์ชันเปรียบเทียบที่กำหนดเอง เหมาะสำหรับ key type ที่ไม่รองรับ `cmp.Ordered` หรือต้องการการเรียงลำดับแบบพิเศษ
func (*SkipList[K, V]) Clear ¶ added in v1.2.0
func (sl *SkipList[K, V]) Clear()
Clear removes all items from the skiplist, resetting it to an empty state. It also replaces the internal node pool, allowing the garbage collector to reclaim memory from the old nodes. This is useful to free up memory after the skiplist is no longer needed, or before reusing it for a different dataset.
Clear ลบรายการทั้งหมดออกจาก skiplist และรีเซ็ตให้อยู่ในสถานะว่างเปล่า และยังทำการแทนที่ node pool ภายใน, ทำให้ garbage collector สามารถคืนหน่วยความจำ จากโหนดเก่าได้. มีประโยชน์ในการคืนหน่วยความจำหลังจากที่ skiplist ไม่ได้ใช้งานแล้ว หรือก่อนที่จะนำไปใช้กับข้อมูลชุดใหม่
func (*SkipList[K, V]) CountRange ¶
CountRange นับจำนวนรายการที่ key อยู่ระหว่าง start และ end (รวมทั้งสองค่า) CountRange counts the number of items where the key is between start and end (inclusive).
func (*SkipList[K, V]) Delete ¶
Delete ลบ key-value ออกจาก skiplist Delete removes a key-value pair from the skiplist. It returns true if the key was found and removed, otherwise false. คืนค่า true หากลบสำเร็จ, false หากไม่พบ key
func (*SkipList[K, V]) GetByRank ¶ added in v1.2.1
GetByRank returns the node at the given 0-based rank. If the rank is out of bounds (rank < 0 or rank >= sl.Len()), it returns nil and false. The complexity is O(log n). GetByRank คืนค่าโหนด ณ อันดับที่กำหนด (0-based) หากอันดับอยู่นอกขอบเขต (น้อยกว่า 0 หรือมากกว่าหรือเท่ากับ Len()) จะคืนค่า nil และ false มีความซับซ้อน O(log n)
func (*SkipList[K, V]) Insert ¶
Insert เพิ่ม key-value คู่ใหม่เข้าไปใน skiplist Insert adds a new key-value pair to the skiplist. If the key already exists, its value is updated, and the old node is returned. If the key is new, a new node is inserted and nil is returned. หาก key มีอยู่แล้ว จะทำการอัปเดต value และคืนค่าโหนดเก่า หากเป็น key ใหม่ จะเพิ่มโหนดใหม่และคืนค่า nil
func (*SkipList[K, V]) Len ¶
Len คืนค่าจำนวนรายการทั้งหมดใน skiplist Len returns the total number of items in the skiplist.
func (*SkipList[K, V]) Max ¶
Max คืนค่า key-value คู่สุดท้าย (มากที่สุด) ใน skiplist Max returns the last (largest) key-value pair in the skiplist. It returns the nil for Node and false if the skiplist is empty. จะคืนค่า nil และ false หาก skiplist ว่างเปล่า
func (*SkipList[K, V]) Min ¶
Min คืนค่า key-value คู่แรก (น้อยที่สุด) ใน skiplist Min returns the first (smallest) key-value pair in the skiplist. It returns the node and true if the list is not empty, otherwise it returns nil and false. จะคืนค่าโหนดและ true หาก list ไม่ว่าง, มิฉะนั้นคืนค่า nil และ false
func (*SkipList[K, V]) NewIterator ¶
func (sl *SkipList[K, V]) NewIterator(opts ...IteratorOption[K, V]) *Iterator[K, V]
NewIterator creates a new iterator. By default, it's positioned before the first element and iterates forwards. Options can be provided to change this behavior. A call to Next() is required to advance to the first (or last, if reversed) element. NewIterator สร้างและคืนค่า Iterator ใหม่ โดยปกติจะชี้ไปยังตำแหน่งก่อนรายการแรก และวนลูปไปข้างหน้า สามารถใช้ options เพื่อเปลี่ยนพฤติกรรมนี้ได้ ต้องเรียก Next() เพื่อเลื่อนไปยังรายการแรก (หรือรายการสุดท้ายหากเป็นแบบย้อนกลับ)
func (*SkipList[K, V]) PopMax ¶
PopMax ดึง key-value คู่ที่มี key มากที่สุดออกจาก skiplist และลบโหนดนั้นออก PopMax removes and returns the largest key-value pair from the skiplist. It returns a node containing the popped data and true if an item was popped, otherwise it returns nil and false. คืนค่าโหนดที่เก็บข้อมูลที่ถูกดึงออกและ true หากมีรายการ, มิฉะนั้นคืนค่า nil และ false
func (*SkipList[K, V]) PopMin ¶
PopMin ดึง key-value คู่ที่มี key น้อยที่สุดออกจาก skiplist และลบโหนดนั้นออก PopMin removes and returns the smallest key-value pair from the skiplist. It returns a node containing the popped data and true if an item was popped, otherwise it returns nil and false. คืนค่าโหนดที่เก็บข้อมูลที่ถูกดึงออกและ true หากมีรายการ, มิฉะนั้นคืนค่า nil และ false
func (*SkipList[K, V]) Predecessor ¶
Predecessor ค้นหา key-value คู่ของโหนดที่อยู่ก่อนหน้า (predecessor) ของ key ที่กำหนด Predecessor finds the key-value pair of the node that precedes the given key. The predecessor is the node with the largest key that is smaller than the target key. It returns the node and true if found, otherwise it returns nil and false. Predecessor คือโหนดที่มี key มากที่สุดซึ่งน้อยกว่า key ที่กำหนด คืนค่าโหนดและ true หากพบ, มิฉะนั้นคืนค่า nil และ false
func (*SkipList[K, V]) Range ¶
Range วนลูปไปตามรายการทั้งหมดใน skiplist ตามลำดับ key Range iterates over all items in the skiplist in ascending key order. The iteration stops if the provided function f returns false. และเรียกใช้ฟังก์ชัน f สำหรับแต่ละคู่ key-value การวนลูปจะหยุดลงหากฟังก์ชัน f คืนค่า false
func (*SkipList[K, V]) RangeQuery ¶
RangeQuery วนลูปไปตามรายการที่ key อยู่ระหว่าง start และ end (รวมทั้งสองค่า) RangeQuery iterates over items where the key is between start and end (inclusive). The iteration stops if the provided function f returns false. และเรียกใช้ฟังก์ชัน f สำหรับแต่ละคู่ key-value การวนลูปจะหยุดลงหากฟังก์ชัน f คืนค่า false
func (*SkipList[K, V]) RangeWithIterator ¶ added in v1.1.0
RangeWithIterator provides a locked iterator to a callback function. This is more efficient than creating a new iterator and manually locking, as it acquires a single read lock for the entire duration of the callback's execution. The iterator provided to the callback is only valid within the scope of that callback.
RangeWithIterator ให้ Iterator ที่ถูก lock ไปยัง callback function ซึ่งมีประสิทธิภาพสูงกว่าการสร้าง Iterator แล้ว lock ด้วยตนเอง เพราะจะทำการ RLock เพียงครั้งเดียวตลอดการทำงานของ callback Iterator ที่ได้มาจะสามารถใช้งานได้ภายใน callback เท่านั้น
func (*SkipList[K, V]) Rank ¶ added in v1.2.1
Rank returns the 0-based rank of the given key. The rank is the number of elements with keys strictly smaller than the given key. If the key is not in the list, it returns the rank it would have if it were inserted. The complexity is O(log n). Rank คืนค่าอันดับ (0-based) ของ key ที่กำหนด อันดับคือจำนวนรายการที่มี key น้อยกว่า key ที่ระบุ หากไม่พบ key จะคืนค่าอันดับที่ควรจะเป็นหากมีการเพิ่ม key นั้นเข้าไป มีความซับซ้อน O(log n)
func (*SkipList[K, V]) Search ¶
Search ค้นหา value จาก key ที่กำหนด Search searches for a value by its key. It returns the node and true if the key is found, otherwise it returns nil and false. คืนค่าโหนดและ true หากพบ, มิฉะนั้นคืนค่า nil และ false
func (*SkipList[K, V]) Seek ¶ added in v1.2.0
Seek finds the first node with a key greater than or equal to the given key. It returns the node and true if such a node is found, otherwise it returns nil and false. Seek ค้นหาโหนดแรกที่มี key เท่ากับหรือมากกว่า key ที่กำหนด คืนค่าโหนดและ true หากพบ, มิฉะนั้นคืนค่า nil และ false
func (*SkipList[K, V]) Successor ¶
Successor ค้นหา key-value คู่ของโหนดที่อยู่ถัดไป (successor) ของ key ที่กำหนด Successor finds the key-value pair of the node that succeeds the given key. The successor is the node with the smallest key that is larger than the given key. It returns the node and true if found, otherwise it returns nil and false. Successor คือโหนดที่มี key น้อยที่สุดที่มากกว่า key ที่กำหนด คืนค่าโหนดและ true หากพบ, มิฉะนั้นคืนค่า nil และ false