change dir stack implementation

This commit is contained in:
sentriz
2019-06-28 14:26:23 +01:00
parent fb99ecc84e
commit 8850ab55f5

View File

@@ -7,34 +7,54 @@ import (
"github.com/sentriz/gonic/model" "github.com/sentriz/gonic/model"
) )
type folderStack []*model.Album type folderItem struct {
value *model.Album
func (s *folderStack) Push(v *model.Album) { next *folderItem
*s = append(*s, v)
} }
func (s *folderStack) Pop() *model.Album { type folderStack struct {
l := len(*s) top *folderItem
if l == 0 { len uint
}
func (s *folderStack) push(v *model.Album) {
s.top = &folderItem{
value: v,
next: s.top,
}
s.len++
}
func (s *folderStack) pop() *model.Album {
if s.len == 0 {
return nil return nil
} }
r := (*s)[l-1] v := s.top.value
*s = (*s)[:l-1] s.top = s.top.next
return r s.len--
return v
} }
func (s *folderStack) Peek() *model.Album { func (s *folderStack) peek() *model.Album {
l := len(*s) if s.len == 0 {
if l == 0 {
return nil return nil
} }
return (*s)[l-1] return s.top.value
} }
func (s *folderStack) String() string { func (s *folderStack) peekID() int {
paths := make([]string, len(*s)) if s.len == 0 {
for i, folder := range *s { return 0
paths[i] = folder.LeftPath
} }
return fmt.Sprintf("[%s]", strings.Join(paths, " ")) return s.top.value.ID
}
func (s *folderStack) string() string {
var str strings.Builder
str.WriteString("[")
for i := s.top; i.next != nil; i = i.next {
str.WriteString(fmt.Sprintf("%d, ", i.value.ID))
}
str.WriteString("root]")
return str.String()
} }