| 1 | # asciiset |
| 2 | |
| 3 | [](https://pkg.go.dev/github.com/elliotwutingfeng/asciiset) |
| 4 | [](https://goreportcard.com/report/github.com/elliotwutingfeng/asciiset) |
| 5 | [](https://coveralls.io/github/elliotwutingfeng/asciiset?branch=main) |
| 6 | |
| 7 | [](LICENSE) |
| 8 | |
| 9 | ## Summary |
| 10 | |
| 11 | **asciiset** is an [ASCII](https://simple.wikipedia.org/wiki/ASCII) character bitset. |
| 12 | |
| 13 | Bitsets are fast and memory-efficient data structures for storing and retrieving information using bitwise operations. |
| 14 | |
| 15 | **asciiset** is an extension of the **asciiSet** data structure from the Go Standard library [source code](https://cs.opensource.google/go/go/+/master:src/bytes/bytes.go). |
| 16 | |
| 17 | Possible applications include checking strings for prohibited ASCII characters, and counting unique ASCII characters in a string. |
| 18 | |
| 19 | Spot any bugs? Report them [here](https://github.com/elliotwutingfeng/asciiset/issues). |
| 20 | |
| 21 |  |
| 22 | |
| 23 | ## Installation |
| 24 | |
| 25 | ```bash |
| 26 | go get github.com/elliotwutingfeng/asciiset |
| 27 | ``` |
| 28 | |
| 29 | ## Testing |
| 30 | |
| 31 | ```bash |
| 32 | make tests |
| 33 | |
| 34 | # Alternatively, run tests without race detection |
| 35 | # Useful for systems that do not support the -race flag like windows/386 |
| 36 | # See https://tip.golang.org/src/cmd/dist/test.go |
| 37 | make tests_without_race |
| 38 | ``` |
| 39 | |
| 40 | ## Benchmarks |
| 41 | |
| 42 | ```bash |
| 43 | make bench |
| 44 | ``` |
| 45 | |
| 46 | ### Results |
| 47 | |
| 48 | ```text |
| 49 | CPU: AMD Ryzen 7 5800X |
| 50 | Time in nanoseconds (ns) | Lower is better |
| 51 | |
| 52 | ASCIISet |
| 53 | |
| 54 | Add() ▏ 891 🟦🟦🟦 11x faster |
| 55 | |
| 56 | Contains() ▏ 580 🟦🟦 28x faster |
| 57 | |
| 58 | Remove() ▏ 1570 🟦🟦🟦🟦 1.5x faster |
| 59 | |
| 60 | Size() ▏ 313 🟦 equivalent |
| 61 | |
| 62 | Visit() ▏ 1421 🟦🟦🟦🟦 3.5x faster |
| 63 | |
| 64 | map[byte]struct{} |
| 65 | |
| 66 | Add() ▏ 9850 🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥 |
| 67 | |
| 68 | Contains() ▏16605 🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥 |
| 69 | |
| 70 | Remove() ▏ 2510 🟥🟥🟥🟥🟥🟥 |
| 71 | |
| 72 | Size() ▏ 318 🟥 |
| 73 | |
| 74 | Visit() ▏ 5085 🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥 |
| 75 | ``` |
| 76 | |
| 77 | ```bash |
| 78 | go test -bench . -benchmem -cpu 1 |
| 79 | goos: linux |
| 80 | goarch: amd64 |
| 81 | pkg: github.com/elliotwutingfeng/asciiset |
| 82 | cpu: AMD Ryzen 7 5800X 8-Core Processor |
| 83 | BenchmarkASCIISet/ASCIISet_Add() 1340958 891.8 ns/op 0 B/op 0 allocs/op |
| 84 | BenchmarkASCIISet/ASCIISet_Contains() 2058140 580.9 ns/op 0 B/op 0 allocs/op |
| 85 | BenchmarkASCIISet/ASCIISet_Remove() 762636 1570 ns/op 0 B/op 0 allocs/op |
| 86 | BenchmarkASCIISet/ASCIISet_Size() 3808866 313.2 ns/op 0 B/op 0 allocs/op |
| 87 | BenchmarkASCIISet/ASCIISet_Visit() 840808 1421 ns/op 0 B/op 0 allocs/op |
| 88 | BenchmarkMapSet/map_Add 122043 9850 ns/op 0 B/op 0 allocs/op |
| 89 | BenchmarkMapSet/map_Contains 72583 16605 ns/op 0 B/op 0 allocs/op |
| 90 | BenchmarkMapSet/map_Remove 451785 2510 ns/op 0 B/op 0 allocs/op |
| 91 | BenchmarkMapSet/map_Size 3789381 318.3 ns/op 0 B/op 0 allocs/op |
| 92 | BenchmarkMapSet/map_Visit 235515 5085 ns/op 0 B/op 0 allocs/op |
| 93 | PASS |
| 94 | ok github.com/elliotwutingfeng/asciiset 14.438s |
| 95 | ``` |