-
Notifications
You must be signed in to change notification settings - Fork 11
feat(internal/config): implement trie for longest common prefix #429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MeteorsLiu
wants to merge
14
commits into
goplus:main
Choose a base branch
from
MeteorsLiu:trie
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+647
−0
Open
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ba356a3
feat: implement trie for longest common prefix
MeteorsLiu 9cac662
test: add more abs tests
MeteorsLiu e25406b
chore: remove println
MeteorsLiu 1c1c16f
test: fix test
MeteorsLiu 3cf470f
Merge branch 'main' of https://github.com/goplus/llcppg into trie
MeteorsLiu bc22861
merge
MeteorsLiu c8484de
chore: fix namespace
MeteorsLiu e6de029
fix: change contains logic
MeteorsLiu 3776c37
fix: contains logic
MeteorsLiu 690b3f8
test: add more tests
MeteorsLiu 8f81a2b
chore: rename Contains
MeteorsLiu 1102c98
test: remove duplicated test
MeteorsLiu 3780339
feat: use DFS to scan the longest common prefix
MeteorsLiu c0263ed
chore: rename IsSubset
MeteorsLiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| package header | ||
|
|
||
| import ( | ||
| "iter" | ||
| "os" | ||
| "path/filepath" | ||
| "slices" | ||
| "strings" | ||
| ) | ||
|
|
||
| type Segmenter func(s string) iter.Seq[string] | ||
|
|
||
| type TrieNode struct { | ||
| isLeaf bool // Indicates if this node represents the end of a word | ||
| linkCount int // Number of children nodes | ||
| children map[string]*TrieNode // Map of child nodes by segment | ||
| } | ||
|
|
||
| // Creates a new TrieNode with empty children map | ||
| func NewTrieNode() *TrieNode { | ||
| return &TrieNode{children: make(map[string]*TrieNode)} | ||
| } | ||
|
|
||
| type Trie struct { | ||
| root *TrieNode // Root node of the trie | ||
| segmenter Segmenter // Function to split strings into segments | ||
| } | ||
| type Options func(*Trie) // Function type for configuring Trie options | ||
|
|
||
| func skipEmpty(s []string) []string { | ||
| for len(s) > 0 && s[0] == "" { | ||
| s = s[1:] | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| func splitPathAbsSafe(path string) (paths []string) { | ||
| originalPath := filepath.Clean(path) | ||
|
|
||
| sep := string(os.PathSeparator) | ||
|
|
||
| // keep absolute path info | ||
| if filepath.IsAbs(originalPath) { | ||
| i := strings.Index(originalPath[1:], sep) | ||
| if i > 0 { | ||
| // bound edge: if i is greater than zero, which means there's second separator | ||
| // for example, /usr/, i: 3, with first separator what we just skipped, i: 4 | ||
| paths = append(paths, originalPath[0:i+1]) | ||
| paths = append(paths, skipEmpty(strings.Split(originalPath[i+1:], sep))...) | ||
| } else { | ||
| // start with / but no other / is found, like /usr | ||
| paths = append(paths, originalPath) | ||
| } | ||
| } | ||
|
|
||
| if len(paths) == 0 { | ||
| paths = skipEmpty(strings.Split(originalPath, sep)) | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| // Returns an option to configure path segmenter | ||
| // Splits strings by OS path separator and yields each segment | ||
| func WithPathSegmenter() Options { | ||
| return func(t *Trie) { | ||
| t.segmenter = func(s string) iter.Seq[string] { | ||
| return func(yield func(string) bool) { | ||
| for _, path := range splitPathAbsSafe(s) { | ||
| if path != "" && !yield(path) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Returns an option to configure reverse path segmenter | ||
| // Splits and reverses strings by OS path separator | ||
| func WithReversePathSegmenter() Options { | ||
| return func(t *Trie) { | ||
| t.segmenter = func(s string) iter.Seq[string] { | ||
| return func(yield func(string) bool) { | ||
| paths := splitPathAbsSafe(s) | ||
|
|
||
| slices.Reverse(paths) | ||
|
|
||
| for _, path := range paths { | ||
| if path != "" && !yield(path) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Creates a new Trie with default path segmenter | ||
| // Applies all provided options to configure the Trie | ||
| func NewTrie(opts ...Options) *Trie { | ||
| t := &Trie{root: NewTrieNode()} | ||
|
|
||
| WithPathSegmenter()(t) | ||
|
|
||
| for _, o := range opts { | ||
| o(t) | ||
| } | ||
|
|
||
| return t | ||
| } | ||
|
|
||
| // Inserts a string into the trie | ||
| // Creates nodes for each segment in the string | ||
| func (t *Trie) Insert(s string) { | ||
| if s == "" { | ||
| return | ||
| } | ||
| node := t.root | ||
|
|
||
| for segment := range t.segmenter(s) { | ||
| child, ok := node.children[segment] | ||
| if !ok { | ||
| child = NewTrieNode() | ||
| node.children[segment] = child | ||
| node.linkCount++ | ||
| } | ||
| node = child | ||
| } | ||
| node.isLeaf = true | ||
| } | ||
|
|
||
| // Searches for a prefix in the trie | ||
| // Returns the node at the end of the prefix or nil if not found | ||
| func (t *Trie) searchPrefix(s string) *TrieNode { | ||
| if s == "" { | ||
| return nil | ||
| } | ||
| node := t.root | ||
|
|
||
| for segment := range t.segmenter(s) { | ||
| child, ok := node.children[segment] | ||
| if !ok { | ||
| return nil | ||
| } | ||
| node = child | ||
| } | ||
|
|
||
| return node | ||
| } | ||
|
|
||
| // Finds the longest common prefix of the given string | ||
| // Returns the longest prefix that exists in the trie | ||
| // | ||
| // Implement Source: https://leetcode.com/problems/longest-common-prefix/solutions/127449/longest-common-prefix | ||
| func (t *Trie) LongestPrefix(s string) string { | ||
| var prefix []string | ||
|
|
||
| node := t.root | ||
|
|
||
| for segment := range t.segmenter(s) { | ||
| child := node.children[segment] | ||
|
|
||
| isLongestPrefix := child != nil && node.linkCount == 1 && !node.isLeaf | ||
|
|
||
| if !isLongestPrefix { | ||
| break | ||
| } | ||
|
|
||
| prefix = append(prefix, segment) | ||
| node = child | ||
| } | ||
|
|
||
| return filepath.Join(prefix...) | ||
| } | ||
|
|
||
| // IsSubsetOf checks the given s is the subset of trie tree | ||
| func (t *Trie) IsSubsetOf(s string) bool { | ||
| if s == "" { | ||
| return false | ||
| } | ||
| node := t.root | ||
|
|
||
| for segment := range t.segmenter(s) { | ||
| child, ok := node.children[segment] | ||
| if !ok { | ||
| // if the current node is end, but there's something unmatched, we still consider it valid. | ||
| // for example, | ||
| // input: /c/b/a, tree: /c/b, valid | ||
| // input: /c/b/a, tree: /c/b/c, invalid | ||
| // input: /c/b, tree: /c/b/c, valid | ||
| return node.isLeaf | ||
| } | ||
| node = child | ||
| } | ||
|
|
||
| return node != nil | ||
| } | ||
|
|
||
| // Checks if the trie contains the exact string | ||
| // Returns true if the string exists in the trie | ||
| func (t *Trie) Search(s string) bool { | ||
| node := t.searchPrefix(s) | ||
| return node != nil && node.isLeaf | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.