Skip to content

Commit

Permalink
Use map to quickly find children in BuildTreeFromFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
parthokunda authored and stefanhaller committed Sep 18, 2024
1 parent c67979a commit b18f12c
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 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,11 +25,19 @@ 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]{
Expand All @@ -36,6 +46,8 @@ func BuildTreeFromFiles(files []*models.File) *Node[models.File] {
}
curr.Children = append(curr.Children, newChild)

currNodeChildrenMap[path] = newChild

curr = newChild
}
}
Expand Down

0 comments on commit b18f12c

Please sign in to comment.