From cafd949471819c34e2acb811d9e3c69187e2f6b9 Mon Sep 17 00:00:00 2001 From: MURAOKA Taro Date: Thu, 15 Feb 2024 23:57:23 +0900 Subject: [PATCH] arrange codes --- .github/workflows/go.yml | 13 ++++++++----- Makefile | 20 +++++++++++--------- README.md | 20 ++++++++++++++++++-- dynamic.go | 28 ++++++++++++++++++++++------ go.mod | 4 ++-- go.sum | 4 ++-- trietree.go | 2 +- 7 files changed, 64 insertions(+), 27 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index d742216..73021a5 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -2,6 +2,9 @@ name: Go on: [push] +env: + GO_VERSION: '>=1.21.0' + jobs: build: @@ -13,14 +16,14 @@ jobs: os: [ ubuntu-latest, macos-latest, windows-latest ] steps: - - uses: actions/setup-go@v3 - with: - go-version: 1.x + - uses: actions/checkout@v4 - - uses: actions/checkout@v3 + - uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} - run: go test ./... - - run: go build + - run: go build ./... # based on: github.com/koron-go/_skeleton/.github/workflows/go.yml diff --git a/Makefile b/Makefile index 76740b2..b86bb9b 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,16 @@ +TEST_PACKAGE ?= ./... + .PHONY: build build: go build -gcflags '-e' ./... .PHONY: test test: - go test ./... + go test $(TEST_PACKAGE) + +.PHONY: bench +bench: + go test -bench $(TEST_PACKAGE) .PHONY: tags tags: @@ -13,23 +19,19 @@ tags: .PHONY: cover cover: mkdir -p tmp - go test -coverprofile tmp/_cover.out ./... + go test -coverprofile tmp/_cover.out $(TEST_PACKAGE) go tool cover -html tmp/_cover.out -o tmp/cover.html .PHONY: checkall -checkall: vet lint staticcheck +checkall: vet staticcheck .PHONY: vet vet: - go vet ./... - -.PHONY: lint -lint: - golint ./... + go vet $(TEST_PACKAGE) .PHONY: staticcheck staticcheck: - staticcheck ./... + staticcheck $(TEST_PACKAGE) .PHONY: clean clean: diff --git a/README.md b/README.md index a554606..c38abe9 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,26 @@ [![Actions/Go](https://github.com/koron-go/trietree/workflows/Go/badge.svg)](https://github.com/koron-go/trietree/actions?query=workflow%3AGo) [![Go Report Card](https://goreportcard.com/badge/github.com/koron-go/trietree)](https://goreportcard.com/report/github.com/koron-go/trietree) -trietree implement trie tree and Aho-Corasick algorithm. +trietree implement trie-tree and Aho-Corasick algorithm. ## How to install or update ```console -$ go get -u github.com/koron-go/trietree +$ go get github.com/koron-go/trietree@latest ``` + +## Desription + +The `trietree` package provides two trie-tree implementations. +One is `DTree` which allows you to dynamically add elements one by one. +Another is `STree`, which is static and cannot add elements to it, but can be serialized and deserialized and is compact. +`STree` can be constructed from `DTree`. +Both trie-trees implement an efficient search based on the Aho–Corasick algorithm. + +### Japanese + +`trietree` パッケージは2つのトライ木の実装を提供します。 +1つは1個ずつ動的に要素を追加できる `DTree` です。 +もう1つは静的で要素の追加はできませんが、シリアライズ・デシリアライズが可能でコンパクトな `STree` です。 +`STree` は `DTree` から構築できます。 +どちらのトライ木もエイホ–コラシック法に基づく効率の良い探索を実装しています。 diff --git a/dynamic.go b/dynamic.go index 611742f..bb66b89 100644 --- a/dynamic.go +++ b/dynamic.go @@ -14,18 +14,32 @@ type DTree struct { // DNode is a node of dynamic tree. type DNode struct { + // Label is a rune assigned this node. The label is unique among sibling + // nodes Label rune - Low *DNode - High *DNode - Child *DNode - + // EdgeID indicates the node has a corresponding key or not. EdgeID int - Level int + // Level is equals key length when EdgeID is not zero. + Level int + + // Low is sibling nodes which have smaller Label. + Low *DNode + + // High is sibling node which have greater Label. + High *DNode + + // Child is a top node of children nods. + Child *DNode + + // Failure is used as a search destination when the desired Label is not + // found in Child. This will be filled by FillFailure(). Failure *DNode } +// dig searches for a node with the desired label among its sibling nodes, or +// creates one if it does not exist. func (dn *DNode) dig(c rune) *DNode { p := dn.Child if p == nil { @@ -68,7 +82,8 @@ func (dn *DNode) Get(r rune) *DNode { return nil } -// Put puts an edige for key and emits ID for it. ID will be greater than zero. +// Put allocates an edge node for k key and emits ID for it. +// The ID will be greater than zero. func (dt *DTree) Put(k string) int { n := &dt.Root level := 0 @@ -132,6 +147,7 @@ func (dt *DTree) nextNode(curr *DNode, c rune) *DNode { type procDNode func(*DNode) +// eachSiblings applies the function fn to all sibling nodes. func (dn *DNode) eachSiblings(fn procDNode) { if dn == nil { return diff --git a/go.mod b/go.mod index 12742eb..ff8122c 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/koron-go/trietree -go 1.19 +go 1.21 -require github.com/google/go-cmp v0.5.9 +require github.com/google/go-cmp v0.6.0 diff --git a/go.sum b/go.sum index 62841cd..5a8d551 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/trietree.go b/trietree.go index 6a3fb63..d94d521 100644 --- a/trietree.go +++ b/trietree.go @@ -1,5 +1,5 @@ /* -Package trietree provides trie-tree (prefix tree) algorithm. +Package trietree provides two trie-tree (prefix tree) implementations. */ package trietree