mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-11 04:15:48 +02:00
Use map to quickly find children in BuildTreeFromFiles
This commit is contained in:
parent
c67979abbb
commit
b18f12ca0f
1 changed files with 17 additions and 5 deletions
|
@ -10,6 +10,8 @@ import (
|
||||||
func BuildTreeFromFiles(files []*models.File) *Node[models.File] {
|
func BuildTreeFromFiles(files []*models.File) *Node[models.File] {
|
||||||
root := &Node[models.File]{}
|
root := &Node[models.File]{}
|
||||||
|
|
||||||
|
childrenMapsByNode := make(map[*Node[models.File]]map[string]*Node[models.File])
|
||||||
|
|
||||||
var curr *Node[models.File]
|
var curr *Node[models.File]
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
splitPath := split(file.Name)
|
splitPath := split(file.Name)
|
||||||
|
@ -23,11 +25,19 @@ func BuildTreeFromFiles(files []*models.File) *Node[models.File] {
|
||||||
}
|
}
|
||||||
|
|
||||||
path := join(splitPath[:i+1])
|
path := join(splitPath[:i+1])
|
||||||
for _, existingChild := range curr.Children {
|
|
||||||
if existingChild.Path == path {
|
var currNodeChildrenMap map[string]*Node[models.File]
|
||||||
curr = existingChild
|
var isCurrNodeMapped bool
|
||||||
continue outer
|
|
||||||
}
|
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]{
|
newChild := &Node[models.File]{
|
||||||
|
@ -36,6 +46,8 @@ func BuildTreeFromFiles(files []*models.File) *Node[models.File] {
|
||||||
}
|
}
|
||||||
curr.Children = append(curr.Children, newChild)
|
curr.Children = append(curr.Children, newChild)
|
||||||
|
|
||||||
|
currNodeChildrenMap[path] = newChild
|
||||||
|
|
||||||
curr = newChild
|
curr = newChild
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue