Documentation ¶
Index ¶
- Constants
- func HeapSort(data sort.Interface)
- func InsertionSort(data sort.Interface)
- type BitSet
- func (bs *BitSet) Clear(i int) bool
- func (bs *BitSet) ClearAll()
- func (bs *BitSet) Count() int
- func (bs *BitSet) Flip(i int) bool
- func (bs BitSet) FormattedString(width int) string
- func (bs BitSet) HashCode() string
- func (bs *BitSet) Set(i int) bool
- func (bs *BitSet) Size() int
- func (bs BitSet) String() string
- func (bs *BitSet) Test(i int) bool
- type Comparable
- type Consistent
- type HashTrie
- func (t *HashTrie) AddWord(word string)
- func (t *HashTrie) Contains(word string) bool
- func (t *HashTrie) ExactMatch(word string) bool
- func (t *HashTrie) Filter(word string) string
- func (t *HashTrie) Remove(word string) bool
- func (t *HashTrie) Reset()
- func (t *HashTrie) String() string
- func (t *HashTrie) WordsCount() int
- type LRUCache
- func (c *LRUCache) Cap() int
- func (c *LRUCache) Exist(key string) bool
- func (c *LRUCache) Get(key string) (interface{}, bool)
- func (c *LRUCache) GetOldest() (key string, value interface{}, ok bool)
- func (c *LRUCache) Keys() []string
- func (c *LRUCache) Len() int
- func (c *LRUCache) Peek(key string) (interface{}, bool)
- func (c *LRUCache) Put(key string, value interface{}) bool
- func (c *LRUCache) Remove(key string) bool
- func (c *LRUCache) RemoveOldest() (string, interface{}, bool)
- func (c *LRUCache) Reset()
- type LRUEntry
- type Queue
- type SortedSet
- func (s *SortedSet) Add(ele Comparable, score int64) bool
- func (s *SortedSet) Count(min, max int64) int
- func (s *SortedSet) GetRange(start, end int, reverse bool) []Comparable
- func (s *SortedSet) GetRangeByScore(min, max int64, reverse bool) []Comparable
- func (s *SortedSet) GetRank(ele Comparable, reverse bool) int
- func (s *SortedSet) GetScore(ele Comparable) int64
- func (s *SortedSet) Len() int
- func (s *SortedSet) Remove(ele Comparable) bool
- func (s *SortedSet) RemoveRangeByRank(start, end int) int
- func (s *SortedSet) RemoveRangeByScore(min, max int64) int
- type U32Array
- type ZSkipList
- func (zsl *ZSkipList) Delete(score int64, ele Comparable) *ZSkipListNode
- func (zsl *ZSkipList) DeleteRangeByRank(start, end int, dict map[Comparable]int64) int
- func (zsl *ZSkipList) DeleteRangeByScore(min, max int64, dict map[Comparable]int64) int
- func (zsl *ZSkipList) Dump(w io.Writer)
- func (zsl *ZSkipList) FirstInRange(min, max int64) *ZSkipListNode
- func (zsl *ZSkipList) GetElementByRank(rank int) *ZSkipListNode
- func (zsl *ZSkipList) GetRank(score int64, ele Comparable) int
- func (zsl *ZSkipList) HeadNode() *ZSkipListNode
- func (zsl *ZSkipList) Height() int
- func (zsl *ZSkipList) Insert(score int64, ele Comparable) *ZSkipListNode
- func (zsl *ZSkipList) IsInRange(min, max int64) bool
- func (zsl *ZSkipList) LastInRange(min, max int64) *ZSkipListNode
- func (zsl *ZSkipList) Len() int
- func (zsl ZSkipList) String() string
- func (zsl *ZSkipList) TailNode() *ZSkipListNode
- type ZSkipListNode
Examples ¶
Constants ¶
const ( ZSKIPLIST_MAXLEVEL = 12 // Should be enough ZSKIPLIST_P = 0.25 // Skiplist P = 1/4 )
const (
ReplicaCount = 20 // 虚拟节点数量
)
const WildCardStar = '*' // '\u002A'
通配符
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BitSet ¶
type BitSet struct {
// contains filtered or unexported fields
}
定长bitset
func BitSetFrom ¶
func (BitSet) FormattedString ¶
type Comparable ¶
type Comparable interface { // a.CompareTo(b) < 0 表明a < b // a.CompareTo(b) > 0 表明a > b // a.CompareTo(b) == 0 表明a == b // // 内部实现要符合结合律: // (a.compareTo(b) > 0 && b.compareTo(c) > 0) implies a.compareTo(c) > 0 CompareTo(Comparable) int }
丐版java.lang.Comparable https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/Comparable.java
type Consistent ¶
type Consistent struct {
// contains filtered or unexported fields
}
一致性hash
func NewConsistent ¶
func NewConsistent() *Consistent
func (*Consistent) RemoveNode ¶
func (c *Consistent) RemoveNode(node string)
type HashTrie ¶
type HashTrie struct {
// contains filtered or unexported fields
}
哈希表实现的字典树
func NewHashTrie ¶
func NewHashTrie() *HashTrie
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRU缓存 https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)
func NewLRUCache ¶
func (*LRUCache) RemoveOldest ¶
删除最老的的key-value,并返回
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue represents an unbounded, dynamically growing FIFO queue. The zero value for queue is an empty queue ready to use. see https://github.com/golang/go/issues/27935
func (*Queue) Front ¶
Front returns the first element of queue q or nil if the queue is empty. The second, bool result indicates whether a valid value was returned;
if the queue is empty, false will be returned.
The complexity is O(1).
type SortedSet ¶
type SortedSet struct {
// contains filtered or unexported fields
}
跳表实现的有序字典
func NewSortedSet ¶
func NewSortedSet() *SortedSet
func (*SortedSet) GetRange ¶
func (s *SortedSet) GetRange(start, end int, reverse bool) []Comparable
返回排名在[start, end]之间的所有元素
func (*SortedSet) GetRangeByScore ¶
func (s *SortedSet) GetRangeByScore(min, max int64, reverse bool) []Comparable
获取score在[min, max]之间的所有元素
func (*SortedSet) GetRank ¶
func (s *SortedSet) GetRank(ele Comparable, reverse bool) int
返回元素的排名,排名从0开始,如果元素不在zset里,返回-1
func (*SortedSet) RemoveRangeByRank ¶
删除排名在[start, end]之间的元素,排名从1开始
func (*SortedSet) RemoveRangeByScore ¶
删除score区间[min, max]的元素
type ZSkipList ¶
type ZSkipList struct {
// contains filtered or unexported fields
}
带索引的排序链表
Example ¶
var playerMap = make(map[int64]*testPlayer) var zset = NewSortedSet() //简单的测试角色数据 var p1 = &testPlayer{Uid: 1001, Level: 12, Point: 2012} var p2 = &testPlayer{Uid: 1002, Level: 13, Point: 2015} var p3 = &testPlayer{Uid: 1003, Level: 14, Point: 2014} var p4 = &testPlayer{Uid: 1004, Level: 11, Point: 2014} var p5 = &testPlayer{Uid: 1005, Level: 14, Point: 2011} playerMap[p1.Uid] = p1 playerMap[p2.Uid] = p2 playerMap[p3.Uid] = p3 playerMap[p4.Uid] = p4 playerMap[p5.Uid] = p5 //插入角色数据到zskiplist for _, v := range playerMap { zset.Add(v, v.Point) } //打印调试信息 // fmt.Printf("%v\n", zset.zsl) //获取角色的排行信息 var rank = zset.GetRank(p1, false) // in ascend order var myRank = zset.Len() - rank + 1 // get descend rank fmt.Printf("rank of %d: %d\n", p1.Uid, myRank) //根据排行获取角色信息 //var node = zset.GetRank(rank) //var player = playerMap[node.Obj.Uuid()] //fmt.Printf("rank at %d is: %s\n", rank, player.name) // ////遍历整个zskiplist //zsl.Walk(true, func(rank int, v RankInterface) bool { // fmt.Printf("rank %d: %v", rank, v) // return true //}) // ////从zset中删除p1 //if !zset.Remove(p1) { // // error handling //} // //p1.score += 10 //if zset.Insert(p1.score, p1) == nil { // // error handling //}
Output:
func NewZSkipList ¶
func NewZSkipList() *ZSkipList
func (*ZSkipList) Delete ¶
func (zsl *ZSkipList) Delete(score int64, ele Comparable) *ZSkipListNode
删除对应score的节点
func (*ZSkipList) DeleteRangeByRank ¶
func (zsl *ZSkipList) DeleteRangeByRank(start, end int, dict map[Comparable]int64) int
删除排名在[start-end]之间的节点,排名从1开始
func (*ZSkipList) DeleteRangeByScore ¶
func (zsl *ZSkipList) DeleteRangeByScore(min, max int64, dict map[Comparable]int64) int
删除score在[min-max]之间的节点
func (*ZSkipList) FirstInRange ¶
func (zsl *ZSkipList) FirstInRange(min, max int64) *ZSkipListNode
Find the first node that is contained in the specified range. Returns NULL when no element is contained in the range.
func (*ZSkipList) GetElementByRank ¶
func (zsl *ZSkipList) GetElementByRank(rank int) *ZSkipListNode
根据排名获得节点,排名从1开始
func (*ZSkipList) GetRank ¶
func (zsl *ZSkipList) GetRank(score int64, ele Comparable) int
获取score所在的排名,排名从1开始
func (*ZSkipList) Insert ¶
func (zsl *ZSkipList) Insert(score int64, ele Comparable) *ZSkipListNode
插入一个不存在的节点
func (*ZSkipList) LastInRange ¶
func (zsl *ZSkipList) LastInRange(min, max int64) *ZSkipListNode
Find the last node that is contained in the specified range. Returns NULL when no element is contained in the range.
type ZSkipListNode ¶
type ZSkipListNode struct { Ele Comparable Score int64 // contains filtered or unexported fields }
list node
func (*ZSkipListNode) Before ¶
func (n *ZSkipListNode) Before() *ZSkipListNode
func (*ZSkipListNode) Next ¶
func (n *ZSkipListNode) Next() *ZSkipListNode
Next return next forward pointer