folder_stack.go 990 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package UI
  2. import (
  3. "sync"
  4. )
  5. type FolderStack struct {
  6. lock *sync.Mutex
  7. head *element
  8. Size int
  9. RootPath string
  10. }
  11. func (stk *FolderStack) Push(data interface{}) {
  12. stk.lock.Lock()
  13. element := new(element)
  14. element.data = data
  15. temp := stk.head
  16. element.next = temp
  17. stk.head = element
  18. stk.Size++
  19. stk.lock.Unlock()
  20. }
  21. func (stk *FolderStack) Pop() interface{} {
  22. if stk.head == nil {
  23. return nil
  24. }
  25. stk.lock.Lock()
  26. r := stk.head.data
  27. stk.head = stk.head.next
  28. stk.Size--
  29. stk.lock.Unlock()
  30. return r
  31. }
  32. func (stk *FolderStack) SetRootPath(path string) {
  33. stk.RootPath = path
  34. }
  35. func (stk *FolderStack) Length() int {
  36. return stk.Size
  37. }
  38. func (stk *FolderStack) Last() string {
  39. idx := stk.Length() - 1
  40. if idx < 0 {
  41. return stk.RootPath
  42. } else {
  43. return stk.head.data.(string)
  44. }
  45. }
  46. func (stk *FolderStack) Clear() {
  47. for stk.Length() > 0 {
  48. stk.Pop()
  49. }
  50. }
  51. func NewFolderStack() *FolderStack {
  52. stk := new(FolderStack)
  53. stk.lock = &sync.Mutex{}
  54. return stk
  55. }