structx

package module
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2023 License: MIT Imports: 12 Imported by: 0

README

structx

Data structures and algorithms implemented using generics.

Currently, structx provides the following types of data structures to support generic types:

  • List
  • MapSyncMap
  • LSet (ListSet)
  • SkiplistZSet (SortedSet)
  • Pool
  • Cache
  • BitMap
BitMap

bitmap implement backed by a slice of []uint64, and is nice wrappered.

usage

bm := structx.NewBitMap(1,2,3)
bm.Add(4) // [1,2,3,4]
bm.Add(1) // [1,2,3.4]
bm.Remove(4) // [1,2,3]
bm.Contains(2) // true

bm.Min() // 1
bm.Max() // 3
bm.Len() // 3

bm1 := structx.NewBitMap(3,4,5)
bm.Union(bm1, true) // [1,2,3,4,5] OR operation and set inplaced

Benchmark

Benchmarks below were run on a pre-allocated bitmap of 100,000,000 elements.

goos: linux
goarch: amd64
pkg: github.com/xgzlucario/structx/test
cpu: AMD Ryzen 7 5800H with Radeon Graphics         
BenchmarkBmAdd-16                  	787935627	         1.515 ns/op	       0 B/op	       0 allocs/op
BenchmarkBmContains-16             	1000000000	         0.3916 ns/op	       0 B/op	       0 allocs/op
BenchmarkBmRemove-16               	1000000000	         0.7613 ns/op	       0 B/op	       0 allocs/op
BenchmarkBmMax-16                  	1000000000	         1.169 ns/op	       0 B/op	       0 allocs/op
BenchmarkBmMin-16                  	1000000000	         0.8804 ns/op	       0 B/op	       0 allocs/op
BenchmarkBmUnion-16                	 2249113	       512.9 ns/op	    2080 B/op	       2 allocs/op
BenchmarkBmUnionInplace-16         	 9901345	       118.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkBmIntersect-16            	 3246958	       370.4 ns/op	    1312 B/op	       2 allocs/op
BenchmarkBmIntersectInplace-16     	11000289	       110.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkBmDifference-16           	 2107741	       606.5 ns/op	    2080 B/op	       2 allocs/op
BenchmarkBmDifferenceInplace-16    	 9769774	       121.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkBmMarshal-16              	      66	  16559485 ns/op	39497262 B/op	       7 allocs/op
PASS
ok  	github.com/xgzlucario/structx/test	32.252s
List

List is a data structure wrapping basic type slice. Compare to basic slice type, List is sequential, sortable, and nice wrappered.

usage
ls := structx.NewList(1,2,3)
ls.RPush(4) // [1,2,3,4]
ls.LPop() // 1 [2,3,4]
ls.Reverse() // [4,3,2]

ls.Index(1) // 3
ls.Find(4) // 0

ls.RShift() // [2,4,3]
ls.Top(1) // [4,2,3]

ls.Sort(func(i, j int) bool {
	return i<j
}) // [2,3,4]
LSet

LSet uses Map + List as the storage structure. LSet is Inherited from List, where the elements are sequential and have good iterative performance, as well as richer api. When the data volume is small only list is used.

usage
s := structx.NewLSet(1,2,3,4,1) // [1,2,3,4]

s.Add(5) // [1,2,4,5]
s.Remove(3) // [1,2,4]

s.Reverse() // [5,4,2,1]
s.Top(2) // [2,5,4,1]
s.Rpop() // [5,4,1]

s.Range(func(k int) bool {...})

s1 := structx.NewLSet(1,2,3) // [1,2,3]

union := s.Union(s1) // [0,1,2,3]
intersect := s.Intersect(s1) // [1,2]
diff := s.Difference(s1) // [0,3]
Benchmark

Compare with mapset deckarep/golang-set.

goos: linux
goarch: amd64
pkg: github.com/xgzlucario/structx/test
cpu: AMD Ryzen 7 5800H with Radeon Graphics  
Benchmark_MapSetRange-16          130693	     8991 ns/op	        0 B/op	      0 allocs/op
Benchmark_LSetRange-16            821851	     1415 ns/op	        0 B/op	      0 allocs/op
Benchmark_MapSetRemove-16      318151948	    3.758 ns/op	        0 B/op	      0 allocs/op
Benchmark_LSetRemove-16        364006822	    3.303 ns/op	        0 B/op	      0 allocs/op
Benchmark_MapSetAdd-16         	   21847	    55064 ns/op	    47871 B/op	     68 allocs/op
Benchmark_LSetAdd-16               17355	    68348 ns/op	    73055 B/op	     78 allocs/op
Benchmark_MapSetUnion-16           12676	    94480 ns/op	    47874 B/op	     68 allocs/op
Benchmark_LSetUnion-16             31516	    38181 ns/op	    30181 B/op	     10 allocs/op
Benchmark_MapSetIntersect-16       14566	    82046 ns/op	    47878 B/op	     68 allocs/op
Benchmark_LSetIntersect-16         37855	    31650 ns/op	    30181 B/op	     10 allocs/op
Benchmark_MapSetDiff-16            30876	    38927 ns/op	     8059 B/op	   1002 allocs/op
Benchmark_LSetDiff-16          	   92643	    12866 ns/op	      153 B/op	      4 allocs/op

Documentation

Index

Constants

View Source
const (
	NoTTL int64 = math.MaxInt64
)

Variables

View Source
var (
	// duration of expired keys evictions
	GCDuration = time.Minute

	// duration of update current timestamp
	TickerDuration = time.Millisecond

	// default expiry time
	DefaultTTL = time.Minute * 10
)

Functions

This section is empty.

Types

type BitMap added in v0.2.6

type BitMap struct {
	// contains filtered or unexported fields
}

func NewBitMap added in v0.2.6

func NewBitMap(nums ...uint32) *BitMap

NewBitMap return bitmap object.

func (*BitMap) Add added in v0.3.0

func (bm *BitMap) Add(num uint32) bool

Add If you use numbers that increment from 0, the bitmap performance will be very good.

func (*BitMap) AddRange added in v0.4.2

func (bm *BitMap) AddRange(start uint32, end uint32) *BitMap

AddRange

func (*BitMap) Contains added in v0.4.1

func (bm *BitMap) Contains(num uint32) bool

Contains

func (*BitMap) Copy added in v0.4.1

func (bm *BitMap) Copy() *BitMap

Copy

func (*BitMap) Difference added in v0.4.2

func (bm *BitMap) Difference(t *BitMap, inplace ...bool) *BitMap

Difference

func (*BitMap) Equal added in v0.6.0

func (bm *BitMap) Equal(t *BitMap) bool

Equal

func (*BitMap) Intersect added in v0.4.1

func (bm *BitMap) Intersect(target *BitMap, inplace ...bool) *BitMap

Intersect

func (*BitMap) Len added in v0.2.6

func (bm *BitMap) Len() int

Len

func (*BitMap) MarshalJSON added in v0.5.0

func (bm *BitMap) MarshalJSON() ([]byte, error)

func (*BitMap) Max added in v0.4.1

func (bm *BitMap) Max() int

Max

func (*BitMap) Min added in v0.4.1

func (bm *BitMap) Min() int

Min

func (*BitMap) Range added in v0.4.1

func (bm *BitMap) Range(f func(uint32) bool)

Range: Not recommended for poor performance

func (*BitMap) Remove added in v0.2.6

func (bm *BitMap) Remove(num uint32) bool

Remove

func (*BitMap) RevRange added in v0.4.1

func (bm *BitMap) RevRange(f func(uint32) bool)

RevRange: Not recommended for poor performance

func (*BitMap) Union added in v0.4.1

func (bm *BitMap) Union(t *BitMap, inplace ...bool) *BitMap

Union

func (*BitMap) UnmarshalJSON added in v0.5.0

func (bm *BitMap) UnmarshalJSON(src []byte) error

type Bloom added in v0.6.0

type Bloom struct {
	*bloom.BloomFilter
}

func NewBloom added in v0.6.0

func NewBloom() *Bloom

type Cache added in v0.1.7

type Cache[K string, V any] struct {
	// contains filtered or unexported fields
}

func NewCache added in v0.1.7

func NewCache[V any]() *Cache[string, V]

NewCache

func (*Cache[K, V]) Clear added in v0.1.7

func (c *Cache[K, V]) Clear()

Clear

func (*Cache[K, V]) Get added in v0.3.0

func (c *Cache[K, V]) Get(key K) (v V, ok bool)

Get

func (*Cache[K, V]) Keys added in v0.4.1

func (c *Cache[K, V]) Keys() []K

Keys

func (*Cache[K, V]) Len added in v0.1.7

func (c *Cache[K, V]) Len() int

Len

func (*Cache[K, V]) MSet added in v0.3.0

func (c *Cache[K, V]) MSet(values map[K]V, ttl ...time.Duration)

MSet

func (*Cache[K, V]) MarshalJSON added in v0.5.0

func (c *Cache[K, V]) MarshalJSON() ([]byte, error)

func (*Cache[K, V]) OnExpired added in v0.2.5

func (c *Cache[K, V]) OnExpired(f cmap.RemoveCb[K, *cacheItem[V]]) *Cache[K, V]

OnExpired

func (*Cache[K, V]) Print added in v0.2.0

func (c *Cache[K, V]) Print()

Print

func (*Cache[K, V]) Range added in v0.1.7

func (c *Cache[K, V]) Range(f func(key K, value V) bool)

Range

func (*Cache[K, V]) RangeWithTTL added in v0.1.7

func (c *Cache[K, V]) RangeWithTTL(f func(key K, value V, ttl int64) bool)

RangeWithTTL

func (*Cache[K, V]) Remove added in v0.4.1

func (c *Cache[K, V]) Remove(key K)

Remove

func (*Cache[K, V]) Set added in v0.1.7

func (c *Cache[K, V]) Set(key K, value V, ttl ...time.Duration)

Set

func (*Cache[K, V]) SetTTL added in v0.1.7

func (c *Cache[K, V]) SetTTL(key K, ttl time.Duration) bool

SetTTL

func (*Cache[K, V]) UnmarshalJSON added in v0.5.0

func (c *Cache[K, V]) UnmarshalJSON(src []byte) error

type LSet added in v0.1.4

type LSet[T comparable] struct {
	*List[T]
	// contains filtered or unexported fields
}

LSet (ListSet): map + list structure LSet has richer api and faster Intersect, Union, Range operations than mapset

func NewLSet added in v0.1.4

func NewLSet[T comparable](values ...T) *LSet[T]

NewLSet: Create a new LSet

func (*LSet[T]) Add added in v0.1.4

func (s *LSet[T]) Add(key T) bool

Add

func (LSet) Bottom added in v0.1.7

func (s LSet) Bottom(i int)

Bottom: move value to the bottom

func (LSet) Capacity added in v0.6.0

func (s LSet) Capacity() int

func (*LSet[T]) Copy added in v0.1.4

func (s *LSet[T]) Copy() *LSet[T]

Copy

func (*LSet[T]) Difference added in v0.1.5

func (s *LSet[T]) Difference(t *LSet[T]) *LSet[T]

Difference

func (*LSet[T]) Equal added in v0.2.1

func (s *LSet[T]) Equal(t *LSet[T]) bool

Equal: Compare members between two lsets is equal

func (*LSet[T]) Exist added in v0.1.4

func (s *LSet[T]) Exist(key T) bool

Exist

func (LSet) Find added in v0.2.1

func (s LSet) Find(elem T) int

Find: return the index of element

func (LSet) Index added in v0.2.1

func (s LSet) Index(i int) T

Index: return the element of index

func (*LSet[T]) Intersect added in v0.1.4

func (s *LSet[T]) Intersect(t *LSet[T]) *LSet[T]

Intersect

func (*LSet[T]) IsSubSet added in v0.2.1

func (s *LSet[T]) IsSubSet(t *LSet[T]) bool

IsSubSet

func (*LSet[T]) LPop added in v0.1.7

func (s *LSet[T]) LPop() (key T, ok bool)

LPop: Pop a elem from left

func (LSet) LShift added in v0.2.1

func (s LSet) LShift()

LShift: Shift all elements of the array left exp: [1, 2, 3] => [2, 3, 1]

func (LSet) Len added in v0.1.5

func (s LSet) Len() int

func (LSet) Print added in v0.1.5

func (s LSet) Print()

Print

func (*LSet[T]) RPop added in v0.1.7

func (s *LSet[T]) RPop() (key T, ok bool)

RPop: Pop a elem from right

func (LSet) RShift added in v0.2.1

func (s LSet) RShift()

RShift: Shift all elements of the array right exp: [1, 2, 3] => [3, 1, 2]

func (*LSet[T]) RandomPop added in v0.1.7

func (s *LSet[T]) RandomPop() (key T, ok bool)

RandomPop: Pop a random elem

func (LSet) Range added in v0.1.4

func (s LSet) Range(f func(T) bool)

Range

func (*LSet[T]) Remove added in v0.1.4

func (s *LSet[T]) Remove(key T) bool

Remove

func (LSet) Reverse added in v0.1.4

func (s LSet) Reverse()

Reverse

func (LSet) Swap added in v0.2.1

func (s LSet) Swap(i, j int)

func (LSet) Top added in v0.1.7

func (s LSet) Top(i int)

Top: move value to the top

func (*LSet[T]) Union added in v0.1.4

func (s *LSet[T]) Union(t *LSet[T]) *LSet[T]

Union: Return the union of two sets

func (*LSet[T]) UnmarshalJSON added in v0.2.2

func (s *LSet[T]) UnmarshalJSON(src []byte) error

func (LSet) Values added in v0.1.4

func (s LSet) Values() []T

Values

type List

type List[T comparable] struct {
	// contains filtered or unexported fields
}

func NewList

func NewList[T comparable](values ...T) *List[T]

NewList: return new List

func (List) Bottom added in v0.2.0

func (s List) Bottom(i int)

Bottom: move value to the bottom

func (List) Capacity added in v0.6.0

func (s List) Capacity() int

func (*List[T]) Clip added in v0.6.0

func (s *List[T]) Clip()

Clip removes unused capacity from the slice.

func (*List[T]) Compact added in v0.6.0

func (s *List[T]) Compact()

Compact replaces consecutive runs of equal elements with a single copy.

func (List) Copy added in v0.2.0

func (s List) Copy() array[T]

Copy

func (*List[T]) Filter added in v0.6.0

func (ls *List[T]) Filter(f func(T) bool) *List[T]

Filter

func (List) Find added in v0.2.0

func (s List) Find(elem T) int

Find: return the index of element

func (List) Index added in v0.2.0

func (s List) Index(i int) T

Index: return the element of index

func (*List[T]) Insert added in v0.1.7

func (ls *List[T]) Insert(i int, values ...T)

Insert

func (*List[T]) IsSorted added in v0.2.0

func (ls *List[T]) IsSorted(f func(T, T) bool) bool

IsSorted: Input param is Order function

func (*List[T]) LPop

func (ls *List[T]) LPop() (val T, ok bool)

LPop

func (*List[T]) LPush

func (ls *List[T]) LPush(values ...T)

LPush

func (List) LShift added in v0.2.0

func (s List) LShift()

LShift: Shift all elements of the array left exp: [1, 2, 3] => [2, 3, 1]

func (List) Len added in v0.2.0

func (s List) Len() int

func (*List[T]) MarshalJSON added in v0.2.1

func (s *List[T]) MarshalJSON() ([]byte, error)

func (*List[T]) Max added in v0.2.0

func (ls *List[T]) Max(less func(T, T) bool) T

Max: Input param is Less function

func (*List[T]) Mean added in v0.2.2

func (ls *List[T]) Mean(f func(T) float64) float64

Mean

func (*List[T]) Min added in v0.2.0

func (ls *List[T]) Min(less func(T, T) bool) T

Min: Input param is Less function

func (List) Print added in v0.2.1

func (s List) Print()

Print

func (*List[T]) RPop

func (ls *List[T]) RPop() (val T, ok bool)

RPop

func (*List[T]) RPush

func (ls *List[T]) RPush(values ...T)

RPush

func (List) RShift added in v0.2.0

func (s List) RShift()

RShift: Shift all elements of the array right exp: [1, 2, 3] => [3, 1, 2]

func (List) Range added in v0.2.0

func (s List) Range(f func(T) bool)

Range

func (*List[T]) RemoveFirst added in v0.5.0

func (ls *List[T]) RemoveFirst(elem T) bool

RemoveFirst

func (*List[T]) RemoveIndex added in v0.6.0

func (ls *List[T]) RemoveIndex(i int) bool

RemoveIndex

func (List) Reverse added in v0.2.0

func (s List) Reverse()

Reverse

func (*List[T]) Sort added in v0.2.0

func (ls *List[T]) Sort(f func(T, T) bool) *List[T]

Sort: Input param is Order function

func (*List[T]) Sum added in v0.2.2

func (ls *List[T]) Sum(f func(T) float64) float64

Sum

func (List) Swap added in v0.2.0

func (s List) Swap(i, j int)

func (List) Top added in v0.2.0

func (s List) Top(i int)

Top: move value to the top

func (*List[T]) UnmarshalJSON added in v0.2.2

func (s *List[T]) UnmarshalJSON(src []byte) error

func (List) Values added in v0.2.1

func (s List) Values() []T

Values

type Map

type Map[K comparable, V any] map[K]V

func NewMap added in v0.1.1

func NewMap[K comparable, V any]() Map[K, V]

NewMap

func (Map[K, V]) Clear added in v0.5.0

func (m Map[K, V]) Clear()

Clear

func (Map[K, V]) Copy added in v0.5.0

func (m Map[K, V]) Copy() Map[K, V]

Copy

func (Map[K, V]) Delete added in v0.1.3

func (m Map[K, V]) Delete(key K) error

Delete

func (Map[K, V]) Exist added in v0.2.1

func (m Map[K, V]) Exist(key K) bool

Exist

func (Map[K, V]) Get added in v0.2.1

func (m Map[K, V]) Get(key K) (V, bool)

Get

func (Map[K, V]) Keys added in v0.5.0

func (m Map[K, V]) Keys() []K

Keys

func (Map[K, V]) Len added in v0.1.3

func (m Map[K, V]) Len() int

Len

func (Map[K, V]) MarshalJSON added in v0.6.0

func (m Map[K, V]) MarshalJSON() ([]byte, error)

func (Map[K, V]) Range added in v0.1.3

func (m Map[K, V]) Range(f func(K, V) bool)

Range

func (Map[K, V]) Set added in v0.2.1

func (m Map[K, V]) Set(key K, value V)

Set

func (Map[K, V]) UnmarshalJSON added in v0.6.0

func (m Map[K, V]) UnmarshalJSON(src []byte) error

func (Map[K, V]) Values added in v0.2.1

func (m Map[K, V]) Values() []V

Values

type Pool added in v0.1.1

type Pool struct {
	*pool.Pool
}

Pool

func NewPool added in v0.1.1

func NewPool() *Pool

NewPool

type Skiplist added in v0.1.7

type Skiplist[K, V base.Value] struct {
	// contains filtered or unexported fields
}

func NewSkipList added in v0.1.7

func NewSkipList[K, V base.Value]() *Skiplist[K, V]

NewSkipList

func (*Skiplist[K, V]) Add added in v0.1.7

func (s *Skiplist[K, V]) Add(key K, value V) *skiplistNode[K, V]

Add

func (*Skiplist[K, V]) Delete added in v0.1.7

func (s *Skiplist[K, V]) Delete(key K, value V) bool

Delete

func (*Skiplist[K, V]) Find added in v0.1.7

func (s *Skiplist[K, V]) Find(value V) bool

Find

func (*Skiplist[K, V]) GetByRank added in v0.1.7

func (s *Skiplist[K, V]) GetByRank(rank int) (k K, v V, err error)

GetByRank: Get the element by rank

func (*Skiplist[K, V]) GetScoreWithRank added in v0.1.7

func (s *Skiplist[K, V]) GetScoreWithRank(key K) (v V, rank int, err error)

GetScoreWithRank: Get the score and rank by key

func (*Skiplist[K, V]) Len added in v0.1.7

func (s *Skiplist[K, V]) Len() int

func (*Skiplist[K, V]) Print added in v0.1.7

func (s *Skiplist[K, V]) Print()

Print

func (*Skiplist[K, V]) Range added in v0.1.7

func (s *Skiplist[K, V]) Range(start, end int, f func(K, V) bool)

Range

func (*Skiplist[K, V]) RangeByScores added in v0.1.7

func (s *Skiplist[K, V]) RangeByScores(min, max V, f func(K, V) bool)

RangeByScores

type SyncMap

type SyncMap[K comparable, V any] struct {
	cmap.ConcurrentMap[K, V]
}

SynMap: use ConcurrentMap

func NewSyncMap added in v0.1.1

func NewSyncMap[V any]() *SyncMap[string, V]

NewSyncMap

func NewSyncMapStringer added in v0.4.0

func NewSyncMapStringer[K cmap.Stringer, V any]() *SyncMap[K, V]

NewSyncMapStringer

func (*SyncMap[K, V]) MarshalJSON added in v0.5.0

func (m *SyncMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON

func (*SyncMap[K, V]) Print added in v0.2.1

func (m *SyncMap[K, V]) Print()

Print

func (*SyncMap[K, V]) UnmarshalJSON added in v0.5.0

func (m *SyncMap[K, V]) UnmarshalJSON(src []byte) error

UnmarshalJSON

type Trie added in v0.3.0

type Trie[T any] struct {
	*trie.Trie[T]
}

func NewTrie added in v0.3.0

func NewTrie[T any]() *Trie[T]

NewTrie

func (*Trie[T]) MarshalJSON added in v0.6.1

func (t *Trie[T]) MarshalJSON() ([]byte, error)

func (*Trie[T]) UnmarshalJSON added in v0.6.1

func (t *Trie[T]) UnmarshalJSON(src []byte) error

func (*Trie[T]) Walk added in v0.6.1

func (t *Trie[T]) Walk(f func(key string, value T) bool)

Walk traverses the entire tree.

func (*Trie[T]) WalkPath added in v0.6.1

func (t *Trie[T]) WalkPath(prefix string, f func(key string, value T) bool)

WalkPath traverses the tree based on prefixes.

type ZSet added in v0.1.3

type ZSet[K, V base.Value] struct {
	// contains filtered or unexported fields
}

func NewZSet added in v0.1.7

func NewZSet[K, V base.Value]() *ZSet[K, V]

NewZSet

func (*ZSet[K, V]) Copy added in v0.1.7

func (z *ZSet[K, V]) Copy() *ZSet[K, V]

Copy

func (*ZSet[K, V]) Delete added in v0.1.3

func (z *ZSet[K, V]) Delete(keys ...K) error

Delete: delete keys

func (*ZSet[K, V]) GetByRank added in v0.1.7

func (z *ZSet[K, V]) GetByRank(rank int) (k K, v V, err error)

GetByRank: get value by rank

func (*ZSet[K, V]) GetScore added in v0.1.3

func (z *ZSet[K, V]) GetScore(key K) (v V, err error)

GetScore

func (*ZSet[K, V]) GetScoreWithRank added in v0.1.7

func (z *ZSet[K, V]) GetScoreWithRank(key K) (V, int, error)

GetScoreWithRank

func (*ZSet[K, V]) Incr added in v0.1.7

func (z *ZSet[K, V]) Incr(key K, value V) V

Incr: Increment value by key

func (*ZSet[K, V]) Len added in v0.1.3

func (z *ZSet[K, V]) Len() int

func (*ZSet[K, V]) Print added in v0.1.7

func (z *ZSet[K, V]) Print()

Print

func (*ZSet[K, V]) Range added in v0.1.3

func (z *ZSet[K, V]) Range(start, end int, f func(K, V) bool)

Range

func (*ZSet[K, V]) RangeByScores added in v0.1.7

func (z *ZSet[K, V]) RangeByScores(min, max V, f func(K, V) bool)

RangeByScores

func (*ZSet[K, V]) Set added in v0.1.3

func (z *ZSet[K, V]) Set(key K, value V)

Set: set key and value

func (*ZSet[K, V]) Union added in v0.1.7

func (z *ZSet[K, V]) Union(target *ZSet[K, V])

Union

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL