Skip to content

Commit

Permalink
arrange codes
Browse files Browse the repository at this point in the history
  • Loading branch information
koron committed Feb 15, 2024
1 parent ac4c876 commit cafd949
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 27 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Go

on: [push]

env:
GO_VERSION: '>=1.21.0'

jobs:

build:
Expand All @@ -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
20 changes: 11 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` から構築できます。
どちらのトライ木もエイホ–コラシック法に基づく効率の良い探索を実装しています。
28 changes: 22 additions & 6 deletions dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
2 changes: 1 addition & 1 deletion trietree.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package trietree provides trie-tree (prefix tree) algorithm.
Package trietree provides two trie-tree (prefix tree) implementations.
*/
package trietree

Expand Down

0 comments on commit cafd949

Please sign in to comment.