Skip to content

Commit

Permalink
Use map to quickly find childrens in BuildTreeFromFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
parthokunda committed Sep 17, 2024
1 parent c67979a commit 861bbf4
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions pkg/gui/filetree/build_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
func BuildTreeFromFiles(files []*models.File) *Node[models.File] {
root := &Node[models.File]{}

childrenMapsByNode := make(map[*Node[models.File]]map[string]*Node[models.File])

var curr *Node[models.File]
for _, file := range files {
splitPath := split(file.Name)
Expand All @@ -23,19 +25,28 @@ func BuildTreeFromFiles(files []*models.File) *Node[models.File] {
}

path := join(splitPath[:i+1])
for _, existingChild := range curr.Children {
if existingChild.Path == path {
curr = existingChild
continue outer
}

var childrenMap map[string]*Node[models.File]
var ok bool
if childrenMap, ok = childrenMapsByNode[curr]; !ok {
childrenMap = make(map[string]*Node[models.File])
childrenMapsByNode[curr] = childrenMap
}
child, isFound := childrenMap[path]
if isFound {
curr = child
continue outer
}

newChild := &Node[models.File]{
Path: path,
File: setFile,
}

curr.Children = append(curr.Children, newChild)

childrenMap[path] = newChild

curr = newChild
}
}
Expand Down

0 comments on commit 861bbf4

Please sign in to comment.