{"id":13411709,"url":"https://github.com/viant/ptrie","last_synced_at":"2025-04-09T14:14:46.747Z","repository":{"id":35812604,"uuid":"187653641","full_name":"viant/ptrie","owner":"viant","description":"A prefix tree implementation in go ","archived":false,"fork":false,"pushed_at":"2024-04-05T16:12:12.000Z","size":69,"stargazers_count":39,"open_issues_count":1,"forks_count":11,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-07-31T20:48:39.297Z","etag":null,"topics":["prefix-tree","trie"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/viant.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-20T14:13:05.000Z","updated_at":"2024-07-12T16:34:45.000Z","dependencies_parsed_at":"2024-04-05T17:29:46.421Z","dependency_job_id":"894559e6-e2d2-4034-ba1a-4794c34f2eb4","html_url":"https://github.com/viant/ptrie","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viant%2Fptrie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viant%2Fptrie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viant%2Fptrie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viant%2Fptrie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/viant","download_url":"https://codeload.github.com/viant/ptrie/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248054193,"owners_count":21039952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["prefix-tree","trie"],"created_at":"2024-07-30T20:01:16.046Z","updated_at":"2025-04-09T14:14:46.730Z","avatar_url":"https://github.com/viant.png","language":"Go","readme":"# Trie (Prefix tree)\n\n[![GoReportCard](https://goreportcard.com/badge/github.com/viant/ptrie)](https://goreportcard.com/report/github.com/viant/ptrie)\n[![GoDoc](https://godoc.org/github.com/viant/ptrie?status.svg)](https://godoc.org/github.com/viant/ptrie)\n\n\nThis library is compatible with Go 1.11+\n\nPlease refer to [`CHANGELOG.md`](CHANGELOG.md) if you encounter breaking changes.\n\n- [Motivation](#motivation)\n- [Introduction](#introduction)\n- [Usage](#usage)\n- [Benchmark](#benchmark)\n\n## Motivation\n\nThe goal of this project is to provide serverless prefix tree friendly implementation.\n where one function can easily building tree and publishing to some cloud storge.\nThen the second load trie to perform various operations.\n\n## Introduction\n\n\nA  trie (prefix tree) is a space-optimized tree data structure  in which each node that is merged with its parent.\nUnlike regular trees (where whole keys are from their beginning up to the point of inequality), the key at each node is compared chunk by chunk,\n\n\nPrefix tree has the following application:\n\n - text document searching\n - rule based matching\n - constructing associative arrays for string keys\n\n \nCharacter comparision complexity:\n\n* **Brute Force:** _O(d n k)_\n* **Prefix Trie**: _O(d log(k))_\n\nWhere\n- d: number of characters in document\n- n: number of keywords\n- k: average keyword length\n\n\n### Usage\n\n\n```go\n\n    trie := ptrie.New()\n    for key, value := range pairs {\n        if err = trie.Put(key, value); err != nil {\n            log.Fatal(err)\n         }\n    }\n    //...\n    has := trie.Has(key)\n    value, has := trie.Get(key)\n    //...\n    matched := trie.MatchAll(input,  func(key []byte, value interface{}) bool {\n        fmt.Printf(\"matched: key: %s, value %v\\n\", key, value)\n        return true \n    })\n    \n```\n\n1. Building\n\n\n```go\n\n    trie := ptrie.New()\n    \n    for key, value := range pairs {\n         if err = trie.Put(key, value); err != nil {\n         \tlog.Fatal(err)\n         }\n    }\n    \n    writer := new(bytes.Buffer)\n\tif err := trie.Encode(writer); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tencoded := write.Bytes()\n\t//write encode data\n\n```\n\n2. Loading\n\n```go\n\n    //V type can be any type\n    var v *V\n    \n\n    trie := ptrie.New()\n    trie.UseType(reflect.TypeOf(v))\n    if err := trie.Decode(reader); err != nil {\n    \tlog.Fatal(err)\n    }\n\n```    \n\n3. Traversing (range map)\n\n```go\n\n    trie.Walk(func(key []byte, value interface{}) bool {\n\t\tfmt.Printf(\"key: %s, value %v\\n\", key, value)\n\t\treturn true\n\t})\n\n```\n\n4. Lookup\n\n```go\n\n    has := trie.Has(key)\n    value, has := trie.Get(key)\n\n```\n\n5. MatchPrefix\n\n```go\n\n    var input []byte\n    ...\n\n    matched := trie.MatchPrefix(input,  func(key []byte, value interface{}) bool {\n        fmt.Printf(\"matched: key: %s, value %v\\n\", key, value)\n        return true \n    })\n\n```\n\n6. MatchAll\n\n```go\n\n    var input []byte\n    ...\n\n    matched := trie.MatchAll(input,  func(key []byte, value interface{}) bool {\n        fmt.Printf(\"matched: key: %s, value %v\\n\", key, value)\n        return true \n    })\n\n```\n\n#### Benchmark\n\nThe benchmark count all words that are part of the following extracts:\n\n**[Lorem Ipsum](test/lorem.txt)**\n\n1. Short: avg line size: 20, words: 13\n2. Long: avg line size: 711, words: 551\n\n\n```bash\nBenchmark_LoremBruteForceShort-8    \t  500000\t      3646 ns/op\nBenchmark_LoremTrieShort-8          \t  500000\t      2376 ns/op\nBenchmark_LoremBruteForceLong-8     \t    1000\t   1612877 ns/op\nBenchmark_LoremTrieLong-8           \t   10000\t    119990 ns/op\n```\n\n**[Hamlet](test/hamlet.txt)**\n\n1. Short: avg line size: 20, words: 49\n2. Long: avg line size: 41, words: 105\n\n```bash\nBenchmark_HamletBruteForceShort-8   \t   30000\t     44306 ns/op\nBenchmark_HamletTrieShort-8         \t  100000\t     18530 ns/op\nBenchmark_HamletBruteForceLong-8    \t   10000\t    226836 ns/op\nBenchmark_HamletTrieLong-8          \t   50000\t     39329 ns/op\n```\n\n##### Code coverage\n\n[![GoCover](https://gocover.io/github.com/viant/ptrie)](https://gocover.io/github.com/viant/ptrie)\n\n\n\u003ca name=\"License\"\u003e\u003c/a\u003e\n## License\n\nThe source code is made available under the terms of the Apache License, Version 2, as stated in the file `LICENSE`.\n\nIndividual files may be made available under their own specific license,\nall compatible with Apache License, Version 2. Please see individual files for details.\n\n\n\u003ca name=\"Credits-and-Acknowledgements\"\u003e\u003c/a\u003e\n\n##  Credits and Acknowledgements\n\n**Library Author:** Adrian Witas\n","funding_links":[],"categories":["Data Structures and Algorithms","数据结构与算法","Data Integration Frameworks","Data Structures","数据结构","Generators","Uncategorized","数据结构`go语言实现的数据结构与算法`"],"sub_categories":["Text Analysis","文本分析","Advanced Console UIs","标准 CLI"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviant%2Fptrie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fviant%2Fptrie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviant%2Fptrie/lists"}