util

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes2String

func Bytes2String(b []byte) string

Bytes2String converts byte slice to string.

func CIDRRange

func CIDRRange(cidr string) (start uint32, end uint32, err error)

CIDRRange a cidr to long start ip and end.

func Contains

func Contains(s []string, e string) bool

Contains return true if element in array

func FNVHash

func FNVHash(key string) uint32

FNVHash returns the FNV FNVHash of what.

func Hash

func Hash(qname string) uint32

Hash calculate string hashcode

func LongIPv4

func LongIPv4(ip string) uint32

LongIPv4 convert string to uint32

func NextPow2

func NextPow2(a int) int

NextPow2 find small x for 2^x > a.

func RReverseQname

func RReverseQname(s string) string

RReverseQname a ReverseQname

func RandStringBytesMaskImprSrc

func RandStringBytesMaskImprSrc(n int) []byte

RandStringBytesMaskImprSrc return a random []byte with length n

func ReverseQname

func ReverseQname(s string) string

ReverseQname a qname

func ReverseQnames

func ReverseQnames(qname []string) string

ReverseQnames reverse names and make to fqdn

func Shuffle

func Shuffle(src []string) []string

Shuffle string slice

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes converts string to byte slice.

Types

type CMap

type CMap struct {
	sync.Mutex
	// contains filtered or unexported fields
}

CMap implements a concurrent map.

func New

func New(size int) *CMap

New returns a new cache. When cache full, it will evict randomly.

func NewCache

func NewCache(size int, evict bool) *CMap

NewCache returns a new cache. When cache full, it will evict?.

func NewSmallMap

func NewSmallMap(size int) *CMap

NewSmallMap return a small cache with permenent.

func (*CMap) Add

func (c *CMap) Add(key string, el interface{}) int

Add adds a new element to the cache. If the element already exists it is overwritten.

func (*CMap) Clear

func (c *CMap) Clear()

Clear all datas

func (*CMap) Get

func (c *CMap) Get(key string) (interface{}, bool)

Get looks up element index under key.

func (*CMap) LRU

func (c *CMap) LRU(lru bool)

LRU set lru function enable?

func (*CMap) Len

func (c *CMap) Len() int

Len returns the number of elements in the cache.

func (*CMap) Remove

func (c *CMap) Remove(key string)

Remove removes the element indexed with key.

type MaxQueueUint64

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

MaxQueueUint64 return max value at O(2)

func NewMaxQueueUint64

func NewMaxQueueUint64(size int) *MaxQueueUint64

NewMaxQueueUint64 return a new MaxQueueUint64

func (*MaxQueueUint64) Dequeue

func (q *MaxQueueUint64) Dequeue() uint64

Dequeue one, return 0 if empty.

func (*MaxQueueUint64) Enqueue

func (q *MaxQueueUint64) Enqueue(val uint64)

Enqueue a new value, if full, Dequeue one, and then Enqueue

func (*MaxQueueUint64) Max

func (q *MaxQueueUint64) Max() uint64

Max return max value in queue, return 0 if empty.

type MaxStackUint64

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

MaxStackUint64 return max value at O(1)

func (*MaxStackUint64) Empty

func (s *MaxStackUint64) Empty() bool

Empty return true if stack empty

func (*MaxStackUint64) Max

func (s *MaxStackUint64) Max() uint64

Max return max value in stack, if stack empty return 0.

func (*MaxStackUint64) Pop

func (s *MaxStackUint64) Pop() uint64

Pop a value from stack, if stack empty, return 0

func (*MaxStackUint64) Push

func (s *MaxStackUint64) Push(val uint64) bool

Push push a value to stack, refresh max value. Return true if success.

type Shard

type Shard struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Shard is a cache with random eviction.

func NewShard

func NewShard(size int, evict bool) *Shard

NewShard returns a new shard with size.

func (*Shard) Add

func (s *Shard) Add(key string, el interface{}, lru bool) (evict int)

Add adds element indexed by key into the cache. Any existing element is overwritten

func (*Shard) Clear

func (s *Shard) Clear()

Clear all datas

func (*Shard) Evict

func (s *Shard) Evict()

Evict removes a random 3 element from the cache.

func (*Shard) Get

func (s *Shard) Get(key string, lru bool) (interface{}, bool)

Get looks up the element indexed under key.

func (*Shard) Len

func (s *Shard) Len() int

Len returns the current length of the cache.

func (*Shard) Remove

func (s *Shard) Remove(key string, lru bool)

Remove removes the element indexed by key from the cache.

Jump to

Keyboard shortcuts

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