From 2973cf2ba4454dfd1ca3b784c74fd06d3499709f Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Thu, 29 Dec 2022 10:19:42 +0800 Subject: [PATCH 1/6] Add go.mod --- go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..efd5d38 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/yourbasic/bit + +go 1.9 From e0a2e84688e9802b86221dea196ed2c6fd5bfeba Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Thu, 29 Dec 2022 10:23:58 +0800 Subject: [PATCH 2/6] Add test coverage and benchmarking instructions --- .gitignore | 2 ++ README.md | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..43937e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.html +*.out \ No newline at end of file diff --git a/README.md b/README.md index 7e8c6b3..2e56f1c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,15 @@ Once you have [installed Go][golang-install], run this command to install the `bit` package: go get github.com/yourbasic/bit - + +### Testing + + go test -v -coverprofile coverage.out && go tool cover -html coverage.out -o coverage.html + +### Benchmarking + + go test -bench . -benchmem -cpu 1 + ### Documentation There is an online reference for the package at From 22f19d1d36bb9d3aa3216f79945f88e6d4c01ec0 Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Thu, 29 Dec 2022 10:27:06 +0800 Subject: [PATCH 3/6] go fmt --- example_test.go | 3 ++- example_union_test.go | 1 + set.go | 6 +++--- set_1_10.go | 6 +++--- set_1_9.go | 9 ++++----- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/example_test.go b/example_test.go index 6381f6f..299780e 100644 --- a/example_test.go +++ b/example_test.go @@ -2,8 +2,9 @@ package bit_test import ( "fmt" - "github.com/yourbasic/bit" "math" + + "github.com/yourbasic/bit" ) // Create, combine, compare and print bit sets. diff --git a/example_union_test.go b/example_union_test.go index 5de52f8..80743a9 100644 --- a/example_union_test.go +++ b/example_union_test.go @@ -2,6 +2,7 @@ package bit_test import ( "fmt" + "github.com/yourbasic/bit" ) diff --git a/set.go b/set.go index 0abebcb..1a539c6 100644 --- a/set.go +++ b/set.go @@ -1,15 +1,16 @@ +//go:build !go1.9 // +build !go1.9 // Package bit provides a bit array implementation. // -// Bit set +// # Bit set // // A bit set, or bit array, is an efficient set data structure // that consists of an array of 64-bit words. Because it uses // bit-level parallelism, limits memory access, and efficiently uses // the data cache, a bit set often outperforms other data structures. // -// Tutorial +// # Tutorial // // The Basics example shows how to create, combine, compare and // print bit sets. @@ -19,7 +20,6 @@ // // Union is a more advanced example demonstrating how to build // an efficient variadic Union function using the SetOr method. -// package bit import ( diff --git a/set_1_10.go b/set_1_10.go index 0dfd3c6..af3189f 100644 --- a/set_1_10.go +++ b/set_1_10.go @@ -1,15 +1,16 @@ +//go:build go1.10 // +build go1.10 // Package bit provides a bit array implementation. // -// Bit set +// # Bit set // // A bit set, or bit array, is an efficient set data structure // that consists of an array of 64-bit words. Because it uses // bit-level parallelism, limits memory access, and efficiently uses // the data cache, a bit set often outperforms other data structures. // -// Tutorial +// # Tutorial // // The Basics example shows how to create, combine, compare and // print bit sets. @@ -19,7 +20,6 @@ // // Union is a more advanced example demonstrating how to build // an efficient variadic Union function using the SetOr method. -// package bit import ( diff --git a/set_1_9.go b/set_1_9.go index 70e5f07..797c41a 100644 --- a/set_1_9.go +++ b/set_1_9.go @@ -1,16 +1,16 @@ -// +build go1.9 -// +build !go1.10 +//go:build go1.9 && !go1.10 +// +build go1.9,!go1.10 // Package bit provides a bit array implementation. // -// Bit set +// # Bit set // // A bit set, or bit array, is an efficient set data structure // that consists of an array of 64-bit words. Because it uses // bit-level parallelism, limits memory access, and efficiently uses // the data cache, a bit set often outperforms other data structures. // -// Tutorial +// # Tutorial // // The Basics example shows how to create, combine, compare and // print bit sets. @@ -20,7 +20,6 @@ // // Union is a more advanced example demonstrating how to build // an efficient variadic Union function using the SetOr method. -// package bit import ( From 278d2cc6e9ece85cbc70687a27ca40d80bbdfb2c Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Thu, 29 Dec 2022 10:27:43 +0800 Subject: [PATCH 4/6] Add test case for And() --- example_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/example_test.go b/example_test.go index 299780e..d5cc546 100644 --- a/example_test.go +++ b/example_test.go @@ -22,11 +22,16 @@ func Example_basics() { // Compute A △ B as (A ∖ B) ∪ (B ∖ A). Y := A.AndNot(B).Or(B.AndNot(A)) + // Compute A ∩ B + Z := A.And(B) + fmt.Println(Z) + // Compare the results. if X.Equal(Y) { fmt.Println(X) } - // Output: {1..49 100..149 200} + // Output: {0 50..99} + // {1..49 100..149 200} } // Create the set of all primes less than n in O(n log log n) time. From abd53df49f3009b23b9dc08475db30f408ef6b8e Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Thu, 29 Dec 2022 11:05:50 +0800 Subject: [PATCH 5/6] Add test case for do(n) == true in common case for Visit() --- set_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/set_test.go b/set_test.go index 755909b..475eb88 100644 --- a/set_test.go +++ b/set_test.go @@ -299,11 +299,11 @@ func TestVisit(t *testing.T) { t.Errorf("%v.Visit(func(n int) { s.DeleteRange(0, n+1); s += Itoa(n) }) -> s=%q; want %q", s, res, x.res) } } - s := New(1, 2) + s := New(1, 2, 3) count := 0 aborted := s.Visit(func(n int) (skip bool) { count++ - if n == 1 { + if n == 2 { skip = true return } @@ -312,7 +312,7 @@ func TestVisit(t *testing.T) { if aborted == false { t.Errorf("Visit returned false when aborted.") } - if count > 1 { + if count > 2 { t.Errorf("Visit didn't abort.") } count = 0 @@ -323,7 +323,7 @@ func TestVisit(t *testing.T) { if aborted == true { t.Errorf("Visit returned true when not aborted.") } - if count != 2 { + if count != 3 { t.Errorf("Visit aborted.") } } From a45621ded79f4d6165618d7ec9c94ed33134e5fc Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Thu, 29 Dec 2022 11:16:34 +0800 Subject: [PATCH 6/6] Rearrange Example_basics() output --- example_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/example_test.go b/example_test.go index d5cc546..88b69b5 100644 --- a/example_test.go +++ b/example_test.go @@ -22,16 +22,16 @@ func Example_basics() { // Compute A △ B as (A ∖ B) ∪ (B ∖ A). Y := A.AndNot(B).Or(B.AndNot(A)) - // Compute A ∩ B - Z := A.And(B) - fmt.Println(Z) - // Compare the results. if X.Equal(Y) { fmt.Println(X) } - // Output: {0 50..99} - // {1..49 100..149 200} + + // Compute A ∩ B + Z := A.And(B) + fmt.Println(Z) + // Output: {1..49 100..149 200} + // {0 50..99} } // Create the set of all primes less than n in O(n log log n) time.