diff --git a/README.md b/README.md index e2022ae..c290484 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,33 @@ go 1.18+泛型的基础工具集合 ## app 一些可以直接使用的模块工具,base/strengthen上的实现 +- [bcache](app/bcache/README.md) +- [bconcurrent](app/bconcurrent/README.md) -## structure +## [structure](structure/README.md) 一些数据结构的实现,base上的实现 +- [lists](structure/lists/README.md) + +- [maps](structure/maps/README.md) + +- [queues](structure/queues/README.md) + +- [sets](structure/sets/README.md) + +- [stacks](structure/stacks/README.md) + +- [trees](structure/trees/README.md) ## base 基础组件 +- [banytostring](base/banytostring/README.md) +- [bcomparator](base/bcomparator/README.md) +- [bmap](base/bmap/README.md) +- [bmath](base/bmath/README.md) +- [bpoint](base/bpoint/README.md) +- [breflect](base/breflect/README.md) +- [bslice](base/bslice/README.md) +- [bternaryexpr](base/bternaryexpr/README.md) +- [bpoint](base/bpoint/README.md) diff --git a/base/anytostring/README.md b/base/banytostring/README.md similarity index 100% rename from base/anytostring/README.md rename to base/banytostring/README.md diff --git a/base/anytostring/anytostring.go b/base/banytostring/anytostring.go similarity index 100% rename from base/anytostring/anytostring.go rename to base/banytostring/anytostring.go diff --git a/base/anytostring/example_test.go b/base/banytostring/example_test.go similarity index 100% rename from base/anytostring/example_test.go rename to base/banytostring/example_test.go diff --git a/structure/lists/README.md b/structure/lists/README.md new file mode 100644 index 0000000..ed2c0f3 --- /dev/null +++ b/structure/lists/README.md @@ -0,0 +1,21 @@ +# lists + +## API +- Get 通过index获取对应元素,如果索引越界返回错误 +- Remove 删除对应下标元素 +- Add 在list末尾添加若干元素 +- Contains 列表中是否包含指定元素 +- Sort 对列表进行排序 +- Swap 交换对应下标的两个元素 +- Insert 执行下标位置后插入元素 +- Set 根据对应下标设置值 +- Empty 判断是否为空 +- Size 获取列表长度 +- Clear 清空列表 +- Values 获取列表中的所有元素 +- String 返回列表的字符串表示 + +## Realize +- arraylist +- doublylinkedlist +- singlylinkedlist \ No newline at end of file diff --git a/structure/maps/README.md b/structure/maps/README.md new file mode 100644 index 0000000..c0404c4 --- /dev/null +++ b/structure/maps/README.md @@ -0,0 +1,29 @@ +# maps + +## API +### Map +- Put 设置键值对 +- Get 获取对应键的值 +- Remove 删除对应key的值 +- Keys 获取所有key +- Empty 判断是否为空 +- Size 获取map长度 +- Clear 清空列表 +- Values 获取列表中的所有元素 +- String 返回列表的字符串表示 + +## Realize +- hashbidimap +- hashmap +- linkedhashmap +- skipmap +- treemapbidimap +- treemap + +### Bidimap +- Map 继承map +- GetKey 获取值对应的key + +## Realize +- hashbidimap +- treemapbidimap \ No newline at end of file diff --git a/structure/maps/hashmap/hashmap.go b/structure/maps/hashmap/hashmap.go index 2e9f5de..51ace00 100644 --- a/structure/maps/hashmap/hashmap.go +++ b/structure/maps/hashmap/hashmap.go @@ -11,7 +11,7 @@ import ( "encoding/json" "fmt" - banytostring "github.com/songzhibin97/go-baseutils/base/anytostring" + banytostring "github.com/songzhibin97/go-baseutils/base/banytostring" "github.com/songzhibin97/go-baseutils/structure/maps" ) diff --git a/structure/maps/treebidimap/treebidimap.go b/structure/maps/treebidimap/treebidimap.go index 0e98f5d..23ab10c 100644 --- a/structure/maps/treebidimap/treebidimap.go +++ b/structure/maps/treebidimap/treebidimap.go @@ -18,7 +18,7 @@ import ( "fmt" "strings" - banytostring "github.com/songzhibin97/go-baseutils/base/anytostring" + banytostring "github.com/songzhibin97/go-baseutils/base/banytostring" "github.com/songzhibin97/go-baseutils/base/bcomparator" "github.com/songzhibin97/go-baseutils/structure/maps" "github.com/songzhibin97/go-baseutils/structure/trees/redblacktree" diff --git a/structure/queues/README.md b/structure/queues/README.md new file mode 100644 index 0000000..d6ebb48 --- /dev/null +++ b/structure/queues/README.md @@ -0,0 +1,18 @@ +# queues + +## API +- Enqueue 入队 +- Dequeue 出队 +- Peek 查看队头 +- Empty 判断是否为空 +- Size 获取map长度 +- Clear 清空列表 +- Values 获取列表中的所有元素 +- String 返回列表的字符串表示 + +## Realize +- arrayqueue +- circularbuffer +- linkedlistqueue +- [lscq](structure/queues/lscq/README.md)(只实现Enqueue、Dequeue,由于泛型问题,不推荐直接使用) +- priorityqueue \ No newline at end of file diff --git a/structure/queues/lscq/README.md b/structure/queues/lscq/README.md index d06bcab..ab969de 100644 --- a/structure/queues/lscq/README.md +++ b/structure/queues/lscq/README.md @@ -10,98 +10,4 @@ The ideas behind the LSCQ are [A Scalable, Portable, and Memory-Efficient Lock-Free FIFO Queue](https://arxiv.org/abs/1908.04511) and [Fast Concurrent Queues for x86 Processors](https://www.cs.tau.ac.il/~mad/publications/ppopp2013-x86queues.pdf). -## Benchmark - -- Go version: go1.16.2 linux/amd64 -- OS: ubuntu 18.04 -- CPU: AMD 3700x(8C16T), running at 3.6 GHZ (disable CPU turbo boost) -- MEMORY: 16G x 2 DDR4 memory, running at 3200 MHZ - -### CPU=100 - -```bash -go test -bench=. -cpu=100 -run=NOTEST -benchtime=1000000x -``` - -![benchmarkcpu100](https://raw.githubusercontent.com/zhangyunhao116/public-data/master/lscq-benchmark-cpu100.png) - -``` -Default/EnqueueOnly/LSCQ-100 38.9ns ±14% -Default/EnqueueOnly/linkedQ-100 209ns ± 3% -Default/EnqueueOnly/msqueue-100 379ns ± 2% -Default/DequeueOnlyEmpty/LSCQ-100 10.0ns ±31% -Default/DequeueOnlyEmpty/linkedQ-100 79.2ns ± 4% -Default/DequeueOnlyEmpty/msqueue-100 7.59ns ±44% -Default/Pair/LSCQ-100 58.7ns ± 7% -Default/Pair/linkedQ-100 324ns ± 5% -Default/Pair/msqueue-100 393ns ± 2% -Default/50Enqueue50Dequeue/LSCQ-100 34.9ns ± 8% -Default/50Enqueue50Dequeue/linkedQ-100 183ns ± 7% -Default/50Enqueue50Dequeue/msqueue-100 191ns ± 3% -Default/30Enqueue70Dequeue/LSCQ-100 78.5ns ± 4% -Default/30Enqueue70Dequeue/linkedQ-100 148ns ± 8% -Default/30Enqueue70Dequeue/msqueue-100 136ns ± 4% -Default/70Enqueue30Dequeue/LSCQ-100 36.2ns ±13% -Default/70Enqueue30Dequeue/linkedQ-100 195ns ± 4% -Default/70Enqueue30Dequeue/msqueue-100 267ns ± 2% -``` - -### CPU=16 - -```bash -go test -bench=. -cpu=16 -run=NOTEST -benchtime=1000000x -``` - -![benchmarkcpu16](https://raw.githubusercontent.com/zhangyunhao116/public-data/master/lscq-benchmark-cpu16.png) - -``` -Default/EnqueueOnly/LSCQ-16 33.7ns ± 5% -Default/EnqueueOnly/linkedQ-16 177ns ± 2% -Default/EnqueueOnly/msqueue-16 370ns ± 1% -Default/DequeueOnlyEmpty/LSCQ-16 3.27ns ±47% -Default/DequeueOnlyEmpty/linkedQ-16 91.1ns ± 2% -Default/DequeueOnlyEmpty/msqueue-16 3.23ns ±46% -Default/Pair/LSCQ-16 56.1ns ± 3% -Default/Pair/linkedQ-16 290ns ± 1% -Default/Pair/msqueue-16 367ns ± 1% -Default/50Enqueue50Dequeue/LSCQ-16 31.8ns ± 3% -Default/50Enqueue50Dequeue/linkedQ-16 157ns ± 8% -Default/50Enqueue50Dequeue/msqueue-16 188ns ± 4% -Default/30Enqueue70Dequeue/LSCQ-16 73.8ns ± 2% -Default/30Enqueue70Dequeue/linkedQ-16 149ns ± 5% -Default/30Enqueue70Dequeue/msqueue-16 123ns ± 2% -Default/70Enqueue30Dequeue/LSCQ-16 28.8ns ± 4% -Default/70Enqueue30Dequeue/linkedQ-16 176ns ± 3% -Default/70Enqueue30Dequeue/msqueue-16 261ns ± 2% -``` - -### CPU=1 - -```bash -go test -bench=. -cpu=1 -run=NOTEST -benchtime=1000000x -``` - -![benchmarkcpu1](https://raw.githubusercontent.com/zhangyunhao116/public-data/master/lscq-benchmark-cpu1.png) - -``` -name time/op -Default/EnqueueOnly/LSCQ 17.3ns ± 1% -Default/EnqueueOnly/linkedQ 59.9ns ± 6% -Default/EnqueueOnly/msqueue 67.1ns ± 2% -Default/DequeueOnlyEmpty/LSCQ 4.77ns ± 1% -Default/DequeueOnlyEmpty/linkedQ 11.3ns ± 2% -Default/DequeueOnlyEmpty/msqueue 3.14ns ± 1% -Default/Pair/LSCQ 36.7ns ± 0% -Default/Pair/linkedQ 56.2ns ± 6% -Default/Pair/msqueue 60.2ns ± 2% -Default/50Enqueue50Dequeue/LSCQ 23.1ns ± 2% -Default/50Enqueue50Dequeue/linkedQ 34.1ns ± 3% -Default/50Enqueue50Dequeue/msqueue 40.8ns ± 9% -Default/30Enqueue70Dequeue/LSCQ 26.5ns ± 2% -Default/30Enqueue70Dequeue/linkedQ 27.0ns ±28% -Default/30Enqueue70Dequeue/msqueue 26.7ns ± 7% -Default/70Enqueue30Dequeue/LSCQ 25.2ns ± 5% -Default/70Enqueue30Dequeue/linkedQ 47.3ns ± 5% -Default/70Enqueue30Dequeue/msqueue 55.2ns ± 8% -``` diff --git a/structure/sets/README.md b/structure/sets/README.md new file mode 100644 index 0000000..19158f5 --- /dev/null +++ b/structure/sets/README.md @@ -0,0 +1,14 @@ +# sets + +## API +- Empty 判断是否为空 +- Size 获取map长度 +- Clear 清空列表 +- Values 获取列表中的所有元素 +- String 返回列表的字符串表示 + +## Realize +- avltree +- binaryheap +- btree +- eadblacktree \ No newline at end of file diff --git a/structure/sets/skipset/README.md b/structure/sets/skipset/README.md index a03758c..899db4f 100644 --- a/structure/sets/skipset/README.md +++ b/structure/sets/skipset/README.md @@ -28,88 +28,3 @@ In these situations, `sync.Map` is better - Only one goroutine access the set for most of the time, such as insert a batch of elements and then use only `Contains` (use built-in map is even better). - -## Benchmark - -Go version: go1.16.2 linux/amd64 - -CPU: AMD 3700x(8C16T), running at 3.6GHz - -OS: ubuntu 18.04 - -MEMORY: 16G x 2 (3200MHz) - -![benchmark](https://raw.githubusercontent.com/zhangyunhao116/public-data/master/skipset-benchmark.png) - -```shell -$ go test -run=NOTEST -bench=. -benchtime=100000x -benchmem -count=20 -timeout=60m > x.txt -$ benchstat x.txt -``` - -``` -name time/op -Int64/Add/skipset-16 86.6ns ±11% -Int64/Add/sync.Map-16 674ns ± 6% -Int64/Contains50Hits/skipset-16 9.85ns ±12% -Int64/Contains50Hits/sync.Map-16 14.7ns ±30% -Int64/30Add70Contains/skipset-16 38.8ns ±18% -Int64/30Add70Contains/sync.Map-16 586ns ± 5% -Int64/1Remove9Add90Contains/skipset-16 24.9ns ±17% -Int64/1Remove9Add90Contains/sync.Map-16 493ns ± 5% -Int64/1Range9Remove90Add900Contains/skipset-16 25.9ns ±16% -Int64/1Range9Remove90Add900Contains/sync.Map-16 1.00µs ±12% -String/Add/skipset-16 130ns ±14% -String/Add/sync.Map-16 878ns ± 4% -String/Contains50Hits/skipset-16 18.3ns ± 9% -String/Contains50Hits/sync.Map-16 19.2ns ±18% -String/30Add70Contains/skipset-16 61.0ns ±15% -String/30Add70Contains/sync.Map-16 756ns ± 7% -String/1Remove9Add90Contains/skipset-16 31.3ns ±13% -String/1Remove9Add90Contains/sync.Map-16 614ns ± 6% -String/1Range9Remove90Add900Contains/skipset-16 36.2ns ±18% -String/1Range9Remove90Add900Contains/sync.Map-16 1.20µs ±17% - -name alloc/op -Int64/Add/skipset-16 65.0B ± 0% -Int64/Add/sync.Map-16 128B ± 1% -Int64/Contains50Hits/skipset-16 0.00B -Int64/Contains50Hits/sync.Map-16 0.00B -Int64/30Add70Contains/skipset-16 19.0B ± 0% -Int64/30Add70Contains/sync.Map-16 77.7B ±16% -Int64/1Remove9Add90Contains/skipset-16 5.00B ± 0% -Int64/1Remove9Add90Contains/sync.Map-16 57.5B ± 4% -Int64/1Range9Remove90Add900Contains/skipset-16 5.00B ± 0% -Int64/1Range9Remove90Add900Contains/sync.Map-16 255B ±22% -String/Add/skipset-16 97.0B ± 0% -String/Add/sync.Map-16 152B ± 0% -String/Contains50Hits/skipset-16 15.0B ± 0% -String/Contains50Hits/sync.Map-16 15.0B ± 0% -String/30Add70Contains/skipset-16 40.0B ± 0% -String/30Add70Contains/sync.Map-16 98.2B ±11% -String/1Remove9Add90Contains/skipset-16 23.0B ± 0% -String/1Remove9Add90Contains/sync.Map-16 73.9B ± 4% -String/1Range9Remove90Add900Contains/skipset-16 23.0B ± 0% -String/1Range9Remove90Add900Contains/sync.Map-16 261B ±18% - -name allocs/op -Int64/Add/skipset-16 1.00 ± 0% -Int64/Add/sync.Map-16 4.00 ± 0% -Int64/Contains50Hits/skipset-16 0.00 -Int64/Contains50Hits/sync.Map-16 0.00 -Int64/30Add70Contains/skipset-16 0.00 -Int64/30Add70Contains/sync.Map-16 1.00 ± 0% -Int64/1Remove9Add90Contains/skipset-16 0.00 -Int64/1Remove9Add90Contains/sync.Map-16 0.00 -Int64/1Range9Remove90Add900Contains/skipset-16 0.00 -Int64/1Range9Remove90Add900Contains/sync.Map-16 0.00 -String/Add/skipset-16 2.00 ± 0% -String/Add/sync.Map-16 5.00 ± 0% -String/Contains50Hits/skipset-16 1.00 ± 0% -String/Contains50Hits/sync.Map-16 1.00 ± 0% -String/30Add70Contains/skipset-16 1.00 ± 0% -String/30Add70Contains/sync.Map-16 2.00 ± 0% -String/1Remove9Add90Contains/skipset-16 1.00 ± 0% -String/1Remove9Add90Contains/sync.Map-16 1.00 ± 0% -String/1Range9Remove90Add900Contains/skipset-16 1.00 ± 0% -String/1Range9Remove90Add900Contains/sync.Map-16 1.00 ± 0% -``` \ No newline at end of file diff --git a/structure/stacks/README.md b/structure/stacks/README.md new file mode 100644 index 0000000..c595ca8 --- /dev/null +++ b/structure/stacks/README.md @@ -0,0 +1,15 @@ +# stacks + +## API +- Push 入栈 +- Pop 出栈 +- Peek 查看栈顶 +- Empty 判断是否为空 +- Size 获取map长度 +- Clear 清空列表 +- Values 获取列表中的所有元素 +- String 返回列表的字符串表示 + +## Realize +- arraystack +- linkedliststack diff --git a/structure/trees/README.md b/structure/trees/README.md new file mode 100644 index 0000000..2f4be3e --- /dev/null +++ b/structure/trees/README.md @@ -0,0 +1,15 @@ +# trees + +## API +- Push 入栈 +- Pop 出栈 +- Peek 查看栈顶 +- Empty 判断是否为空 +- Size 获取map长度 +- Clear 清空列表 +- Values 获取列表中的所有元素 +- String 返回列表的字符串表示 + +## Realize +- arraystack +- linkedliststack