// Copyright 2016 The CMux Authors. All rights reserved.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or// implied. See the License for the specific language governing// permissions and limitations under the License.package cmuximport ()// patriciaTree is a simple patricia tree that handles []byte instead of string// and cannot be changed after instantiation.type patriciaTree struct { root *ptNode maxDepth int// max depth of the tree.}func newPatriciaTree( ...[]byte) *patriciaTree { := 0for , := range {if < len() { = len() } }return &patriciaTree{root: newNode(),maxDepth: + 1, }}func newPatriciaTreeString( ...string) *patriciaTree { := make([][]byte, len())for , := range { [] = []byte() }returnnewPatriciaTree(...)}func ( *patriciaTree) ( io.Reader) bool { := make([]byte, .maxDepth) , := io.ReadFull(, )return .root.match([:], true)}func ( *patriciaTree) ( io.Reader) bool { := make([]byte, .maxDepth) , := io.ReadFull(, )return .root.match([:], false)}type ptNode struct { prefix []byte next map[byte]*ptNode terminal bool}func newNode( [][]byte) *ptNode {iflen() == 0 {return &ptNode{prefix: []byte{},terminal: true, } }iflen() == 1 {return &ptNode{prefix: [0],terminal: true, } } , := splitPrefix() := &ptNode{prefix: , } := make(map[byte][][]byte)for , := range {iflen() == 0 { .terminal = truecontinue } [[0]] = append([[0]], [1:]) } .next = make(map[byte]*ptNode)for , := range { .next[] = () }return}func splitPrefix( [][]byte) ( []byte, [][]byte) {iflen() == 0 || len([0]) == 0 {return , }iflen() == 1 {return [0], [][]byte{{}} }for := 0; ; ++ {varbyte := truefor , := range {iflen() <= { = falsebreak }if == 0 { = []continue }if != [] { = falsebreak } }if ! {break } = append(, ) } = make([][]byte, 0, len())for , := range { = append(, [len():]) }return , }func ( *ptNode) ( []byte, bool) bool { := len(.prefix)if > 0 {if > len() { = len() }if !bytes.Equal([:], .prefix) {returnfalse } }if .terminal && ( || len(.prefix) == len()) {returntrue }if >= len() {returnfalse } , := .next[[]]if ! {returnfalse }if == len() { = [:] } else { = [+1:] }return .(, )}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.