container

package
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: MIT Imports: 8 Imported by: 1

README

Read this in other languages: English, 中文.

container

bitmap_test.go

TestBitmapExists

bitmap := initTestData()
t.Log(bitmap)

if !bitmap.Exists(122) {
	t.FailNow()
}

if bitmap.Exists(123) {
	t.FailNow()
}
TestBitmapSet

bitmap := initTestData()

if bitmap.Exists(1256) {
	t.FailNow()
}

bitmap.Set(1256)

if !bitmap.Exists(1256) {
	t.FailNow()
}
TestBitmapUnionOr

bitmap := initTestData()
bitmap2 := initTestData()
bitmap2.Set(256)

bitmap3 := bitmap.Union(&bitmap2)
if !bitmap3.Exists(256) {
	t.FailNow()
}

bitmap3.Set(562)

if !bitmap3.Exists(562) {
	t.FailNow()
}

if bitmap.Exists(562) {
	t.FailNow()
}
TestBitmapBitInverse

bitmap := initTestData()

if !bitmap.Exists(66) {
	t.FailNow()
}

bitmap.Inverse()

if bitmap.Exists(66) {
	t.FailNow()
}

buff_pool_test.go

TestBuffPool

buf1 := GetPoolBuff(BUFF_128K)
ptr1 := uintptr(unsafe.Pointer(&buf1[0]))
len1 := len(buf1)
PutPoolBuff(BUFF_128K, buf1)

buf2 := GetPoolBuff(BUFF_128K)
ptr2 := uintptr(unsafe.Pointer(&buf2[0]))
len2 := len(buf2)
PutPoolBuff(BUFF_128K, buf2)

if len1 != BUFF_128K {
	t.Error("pool get BUFF_128K len failed")
}

if len1 != len2 {
	t.Error("pool get BUFF_128K len failed")
}

if ptr1 != ptr2 {
	t.Error("pool get BUFF_128K failed")
}

//4M
buf1 = GetPoolBuff(BUFF_4M)
ptr1 = uintptr(unsafe.Pointer(&buf1[0]))
len1 = len(buf1)
PutPoolBuff(BUFF_4M, buf1)

buf2 = GetPoolBuff(BUFF_4M)
ptr2 = uintptr(unsafe.Pointer(&buf2[0]))
len2 = len(buf2)
PutPoolBuff(BUFF_4M, buf2)

if len1 != BUFF_4M {
	t.Error("pool get BUFF_4M len failed")
}

if len1 != len2 {
	t.Error("pool get BUFF_4M len failed")
}

if ptr1 != ptr2 {
	t.Error("pool get BUFF_4M failed")
}

const_hash_test.go

TestConstHash


var ringchash CHashRing

var configs []CHashNode
for i := 0; i < 10; i++ {
	configs = append(configs, testConstHashNode("node"+strconv.Itoa(i)))
}

ringchash.Adds(configs)

t.Log("init:", ringchash.Debug())

if ringchash.GetByC32(100, false).Id() != "node0" {
	t.Fail()
}

if ringchash.GetByC32(134217727, false).Id() != "node0" {
	t.Fail()
}

if ringchash.GetByC32(134217728, false).Id() != "node8" {
	t.Fail()
}

var configs2 []CHashNode
for i := 0; i < 2; i++ {
	configs2 = append(configs2, testConstHashNode("node"+strconv.Itoa(10+i)))
}
ringchash.Adds(configs2)

t.Log("add 2 nodes", ringchash.Debug())

if ringchash.GetByC32(134217727, false).Id() != "node10" {
	t.Fail()
}

if ringchash.GetByC32(134217728, false).Id() != "node10" {
	t.Fail()
}

ringchash.Del("node0")
t.Log("del 1 node", ringchash.Debug())

if ringchash.GetByC32(100, false).Id() != "node10" {
	t.Fail()
}

t.Log(ringchash.GetByKey("goutils", false))

mdb_test.go

TestDataTable

if len(testDt.Rows()) != 10 {
	t.FailNow()
}

if testDt.PkString(testDt.Row("9")) != "9" {
	t.FailNow()
}

if testDt.PkInt(testDt.Row("8")) != 8 {
	t.FailNow()
}

if reflect.DeepEqual(testDt.Row("2"), []string{"2", "C2", "N2"}) {
	t.FailNow()
}

if reflect.DeepEqual(testDt.RowsBy("code", "C2")[0], []string{"2", "C2", "N2"}) {
	t.FailNow()
}

if reflect.DeepEqual(testDt.RowsByPredicate(func(dr *DataRow) bool { return dr.String("name") == "N4" })[0], []string{"4", "C4", "N4"}) {
	t.FailNow()
}

testDt.Push([]string{"2", "C2", "N3"})

if reflect.DeepEqual(testDt.RowsByIndexPredicate("code", "C2", func(dr *DataRow) bool { return dr.String("name") == "N3" })[0], []string{"2", "C2", "N2"}) {
	t.FailNow()
}
TestDataSet

if testDs.Table("testDt") != testDt {
	t.FailNow()
}

queue_test.go

TestEnqueueBack

q := InitQueue()
if !q.IsEmpty() {
	t.FailNow()
}

for i := 0; i < 25; i++ {
	deItem := q.EnqueueBack(fmt.Sprint(i))

	if i >= q.Len() {
		expectedItem := fmt.Sprint(i - q.Len())
		if deItem != expectedItem {
			t.Error(deItem)
		}
	} else {
		if deItem != "" {
			t.Error(deItem)
		}
	}
}

if q.IsEmpty() {
	t.FailNow()
}

// q.Range(func(i string) bool {
// 	t.Log("left:", i)
// 	return true
// })

TestDequeueFront

q := InitQueue()
if !q.IsEmpty() {
	t.FailNow()
}

for i := 0; i < 25; i++ {
	q.EnqueueBack(fmt.Sprint(i))
}

for i := 0; i < 5; i++ {
	deItem := q.DequeueFront()
	expectedItem := fmt.Sprint(i + 25 - 10)
	if deItem != expectedItem {
		t.Error(deItem)
	}
}

// q.Range(func(i string) bool {
// 	t.Log("left:", i)
// 	return true
// })

if q.IsEmpty() {
	t.FailNow()
}

for i := 0; i < 5; i++ {
	deItem := q.DequeueFront()
	expectedItem := fmt.Sprint(i + 25 - 10 + 5)
	if deItem != expectedItem {
		t.Error(deItem)
	}
}

// q.Range(func(i string) bool {
// 	t.Log("left:", i)
// 	return true
// })

if !q.IsEmpty() {
	t.FailNow()
}
TestEnqueueFront

q := InitQueue()
if !q.IsEmpty() {
	t.FailNow()
}

for i := 0; i < 25; i++ {
	deItem := q.EnqueueFront(fmt.Sprint(i))
	if i >= q.Len() {
		expectedItem := fmt.Sprint(i - q.Len())
		if deItem != expectedItem {
			t.Error(deItem)
		}
	} else {
		if deItem != "" {
			t.Error(deItem)
		}
	}
}

if q.IsEmpty() {
	t.FailNow()
}

// q.Range(func(i string) bool {
// 	t.Log("left:", i)
// 	return true
// })
TestDequeueBack

q := InitQueue()
if !q.IsEmpty() {
	t.FailNow()
}

for i := 0; i < 25; i++ {
	q.EnqueueFront(fmt.Sprint(i))
}

for i := 0; i < 5; i++ {
	deItem := q.DequeueBack()
	expectedItem := fmt.Sprint(i + 25 - 10)
	if deItem != expectedItem {
		t.Error(deItem)
	}
}
if q.IsEmpty() {
	t.FailNow()
}

// q.Range(func(i string) bool {
// 	t.Log("left:", i)
// 	return true
// })

for i := 0; i < 5; i++ {
	deItem := q.DequeueBack()
	expectedItem := fmt.Sprint(i + 25 - 10 + 5)
	if deItem != expectedItem {
		t.Error(deItem)
	}
}

if !q.IsEmpty() {
	t.FailNow()
}
TestQueueClear

q := InitQueue()
if q.Cap() != 10 {
	t.FailNow()
}

if !q.IsEmpty() {
	t.FailNow()
}

for i := 0; i < 25; i++ {
	q.EnqueueBack(fmt.Sprint(i))
}

// q.Range(func(i string) bool {
// 	t.Log("left:", i)
// 	return true
// })

q.Clear()

if q.Cap() != 10 {
	t.FailNow()
}

if !q.IsEmpty() {
	t.FailNow()
}

// q.Range(func(i string) bool {
// 	t.Log("left:", i)
// 	return true
// })
TestQueueFindBy

q := NewQueue[int](25)

for i := 0; i < 25; i++ {
	q.EnqueueBack(i)
}

i20 := q.FindOneBy(func(i int) bool {
	return i == 20
})

if i20 != 20 {
	t.FailNow()
}

items := q.FindBy(func(i int) bool {
	return i%3 == 0
})

for _, item := range items {
	if item%3 != 0 {
		t.Error(item)
	}
}

red_black_tree_test.go

TestReaBlackTree


type personT struct {
	name   string
	age    int
	gender bool
	score  int64
}

tree := RedBlackTree{}

for i := 0; i < 1000000; i++ {

	name := fmt.Sprintf("rongo%d", i)

	tree.Put(name, &personT{
		name:   name,
		age:    i,
		gender: true,
		score:  int64(i) + 100,
	})
}

nodeVal := tree.Get("rongo999999")

personVal, ok := nodeVal.(*personT)
if !ok {
	t.FailNow()
}

if personVal.name != "rongo999999" {
	t.FailNow()
}

if personVal.age != 999999 {
	t.FailNow()
}

if personVal.score != 1000099 {
	t.FailNow()
}

Documentation

Index

Constants

View Source
const (
	BUFF_128K = 128 * 1024
	BUFF_4M   = 4 * 1024 * 1024
)

Variables

This section is empty.

Functions

func GetPoolBuff added in v1.3.5

func GetPoolBuff(size int32) []byte

func PutPoolBuff added in v1.3.5

func PutPoolBuff(size int32, buff []byte)

Types

type Bitmap

type Bitmap []uint32

func (*Bitmap) Clone

func (b *Bitmap) Clone() *Bitmap

func (*Bitmap) Exists

func (b *Bitmap) Exists(item uint32) bool

func (*Bitmap) Grow

func (b *Bitmap) Grow(item uint32)

func (*Bitmap) Init

func (b *Bitmap) Init(maxNum uint32)

func (*Bitmap) Inverse

func (b *Bitmap) Inverse()

func (*Bitmap) MaxU

func (b *Bitmap) MaxU(x, y uint32) uint32

func (*Bitmap) Min

func (b *Bitmap) Min(x, y int) int

func (*Bitmap) Set

func (b *Bitmap) Set(item uint32) bool

func (*Bitmap) Sets

func (b *Bitmap) Sets(items []uint32)

func (*Bitmap) Union

func (b *Bitmap) Union(b2 *Bitmap) *Bitmap

type CHash

type CHash interface {
	Adds([]CHashNode)
	Del(id string)
	Get(key string) CHashNode
}

type CHashNode

type CHashNode interface {
	Id() string
	Health() bool
}

type CHashRing

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

func (*CHashRing) Adds

func (c *CHashRing) Adds(nodes []CHashNode)

func (*CHashRing) Debug

func (c *CHashRing) Debug() string

func (*CHashRing) Del

func (c *CHashRing) Del(nodeId string)

func (*CHashRing) GetByC32 added in v1.3.5

func (c *CHashRing) GetByC32(keyC32 uint32, mustHealth bool) CHashNode

func (*CHashRing) GetByKey added in v1.3.5

func (c *CHashRing) GetByKey(key string, mustHealth bool) CHashNode

type DataRow

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

func (*DataRow) Data

func (r *DataRow) Data() []string

func (*DataRow) Int64 added in v1.0.16

func (r *DataRow) Int64(fieldName string) int64

func (*DataRow) String

func (r *DataRow) String(fieldName string) string

func (*DataRow) UInt64 added in v1.0.16

func (r *DataRow) UInt64(fieldName string) uint64

type DataSet

type DataSet map[string]*DataTable

func NewDataSet added in v1.3.5

func NewDataSet() *DataSet

func (*DataSet) AddTable added in v1.3.5

func (s *DataSet) AddTable(tName string, dt *DataTable)

func (*DataSet) Table

func (s *DataSet) Table(tName string) *DataTable

type DataTable

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

func NewDataTable

func NewDataTable(cols []string, pkCol string, indexes []string, initCap int) *DataTable

func (*DataTable) Cols added in v1.0.17

func (t *DataTable) Cols() []string

meta info

func (*DataTable) Indexes added in v1.0.17

func (t *DataTable) Indexes() []string

func (*DataTable) PkCol added in v1.0.17

func (t *DataTable) PkCol() string

func (*DataTable) PkInt

func (t *DataTable) PkInt(row *DataRow) int

func (*DataTable) PkString

func (t *DataTable) PkString(row *DataRow) string

func (*DataTable) Push

func (t *DataTable) Push(row []string)

func (*DataTable) PushAll

func (t *DataTable) PushAll(rows [][]string)

func (*DataTable) Row

func (t *DataTable) Row(pk string) *DataRow

data info

func (*DataTable) Rows

func (t *DataTable) Rows() (rows []*DataRow)

func (*DataTable) RowsBy

func (t *DataTable) RowsBy(indexName, indexValue string) []*DataRow

func (*DataTable) RowsByIndexPredicate added in v1.1.32

func (t *DataTable) RowsByIndexPredicate(indexName, indexValue string, predicate func(*DataRow) bool) []*DataRow

func (*DataTable) RowsByPredicate added in v1.1.32

func (t *DataTable) RowsByPredicate(predicate func(*DataRow) bool) []*DataRow

type PoolBuffer128K added in v1.3.5

type PoolBuffer128K []byte

type PoolBuffer4M added in v1.3.5

type PoolBuffer4M []byte

type Queue added in v1.3.4

type Queue[T any] struct {
	// contains filtered or unexported fields
}

func NewQueue added in v1.3.4

func NewQueue[T any](maxSize int) *Queue[T]

func (*Queue[T]) Cap added in v1.3.5

func (q *Queue[T]) Cap() int

func (*Queue[T]) Clear added in v1.3.4

func (q *Queue[T]) Clear()

func (*Queue[T]) DequeueBack added in v1.3.4

func (q *Queue[T]) DequeueBack() (t T)

func (*Queue[T]) DequeueFront added in v1.3.4

func (q *Queue[T]) DequeueFront() (t T)

func (*Queue[T]) EnqueueBack added in v1.3.4

func (q *Queue[T]) EnqueueBack(item T) (t T)

func (*Queue[T]) EnqueueFront added in v1.3.4

func (q *Queue[T]) EnqueueFront(item T) (t T)

func (*Queue[T]) FindBy added in v1.3.4

func (q *Queue[T]) FindBy(fn func(T) bool) (data []T)

func (*Queue[T]) FindOneBy added in v1.3.4

func (q *Queue[T]) FindOneBy(fn func(T) bool) (t T)

func (*Queue[T]) IsEmpty added in v1.3.4

func (q *Queue[T]) IsEmpty() bool

func (*Queue[T]) Len added in v1.3.5

func (q *Queue[T]) Len() int

func (*Queue[T]) Range added in v1.3.4

func (q *Queue[T]) Range(fn func(t T) bool)

type RBColor added in v1.3.5

type RBColor bool
const (
	BLACK RBColor = true
	RED   RBColor = false
)

type RBNode added in v1.3.5

type RBNode struct {
	Key         string
	Value       interface{}
	Color       RBColor
	Left, Right *RBNode
}

type RedBlackTree added in v1.3.5

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

func (*RedBlackTree) Get added in v1.3.5

func (t *RedBlackTree) Get(key string) interface{}

func (*RedBlackTree) Put added in v1.3.5

func (t *RedBlackTree) Put(key string, value interface{})

Jump to

Keyboard shortcuts

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