Coder Social home page Coder Social logo

porfirion / trie Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 5.0 60 KB

Efficient generic radix tree (trie) implementation in go

Home Page: https://pkg.go.dev/github.com/porfirion/trie

License: MIT License

Go 100.00%
trie prefix prefix-tree prefix-tree-library radix-tree radix-trie efficient golang patricia-tree zero-dependency

trie's People

Contributors

porfirion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

trie's Issues

Adopt 1.18 generic feature.

Hi @porfirion

I would like to submit a PR to make this library generic-able. By turning most part of the library into generic type/function and make a alias for backward-compatibility.

For example:

// Turn Trie into TrieGen with type parameter
type TrieGen[T any] struct {
	Prefix   []byte
	Value    *T // Here we use *T, not  T, for optional value
	Children *[256]*TrieGen[T]
}
// Other methods on TrieGen are modified too, with input/output type T, not *T

// Provide existing interface Trie as a type alias, ValueType will be removed directly
type Trie = TrieGen[interface{}] 

This interface provide easier, less error-prone interface for most types, since user don't need to 1. type assert and 2. dereference pointer.

func SomeJob() {
	var routes *TrieGen[string] = BuildFromMap[string](map[string]string{
		"/user/info": "some-user-info",
	})
	content, _, ok := routes.SearchPrefixInString("/user")
	if ok {
		fmt.Println(content)
	}
}

Problem

However, the Trie.Value now has incompatible type chage, from interface{} to *interface{}.

Since Value is a public field for Trie, there may be some existing user who interact with the trie content,
not using public methods from the trie. For these users, the proposed change can broken their code when they upgrade this library.

Do you think this is a good change for this library? If so, I would like to submit a PR for this change. :D
(Actually I already complete the required modification and fix corresponding test cases in my local repo.)

Hope for your kind response.
And thanks for this great library, I learned a lot from it. :D


If we do not care about compatibility, we can just provide:

type Trie[T any] struct {
	Prefix   []byte
	Value    *T
	Children *[256]*Trie[T]
}

Related Information

There is already a fork doing similar change, but the public method of that fork return values in *T, which I don't think a good design decision.

See: https://github.com/MrMelon54/trie

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.