page_stack.go 867 B

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