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 acc0331
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 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,30 @@ 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 currNodeChildrenMap map[string]*Node[models.File]
var isCurrNodeMapped bool

if currNodeChildrenMap, isCurrNodeMapped = childrenMapsByNode[curr]; !isCurrNodeMapped {
currNodeChildrenMap = make(map[string]*Node[models.File])
childrenMapsByNode[curr] = currNodeChildrenMap
}

child, doesCurrNodeHaveChildAlready := currNodeChildrenMap[path]
if doesCurrNodeHaveChildAlready {
curr = child
continue outer
}

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

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

currNodeChildrenMap[path] = newChild

curr = newChild
}
}
Expand Down

0 comments on commit acc0331

Please sign in to comment.