You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

var.go 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package dag
  2. import (
  3. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  4. "gitlink.org.cn/cloudream/common/utils/lo2"
  5. )
  6. type Var interface {
  7. GetVarID() exec.VarID
  8. }
  9. type StreamVar struct {
  10. VarID exec.VarID
  11. Src Node
  12. Dst DstList
  13. }
  14. func (v *StreamVar) GetVarID() exec.VarID {
  15. return v.VarID
  16. }
  17. func (v *StreamVar) IndexAtSrc() int {
  18. return v.Src.OutputStreams().IndexOf(v)
  19. }
  20. func (v *StreamVar) To(to Node, slotIdx int) {
  21. v.Dst.Add(to)
  22. to.InputStreams().Slots.Set(slotIdx, v)
  23. }
  24. func (v *StreamVar) ToSlot(slot StreamInputSlot) {
  25. v.Dst.Add(slot.Node)
  26. slot.Node.InputStreams().Slots.Set(slot.Index, v)
  27. }
  28. func (v *StreamVar) NotTo(node Node) {
  29. v.Dst.Remove(node)
  30. node.InputStreams().Slots.Clear(v)
  31. }
  32. func (v *StreamVar) ClearAllDst() {
  33. for _, n := range v.Dst {
  34. n.InputStreams().Slots.Clear(v)
  35. }
  36. v.Dst = nil
  37. }
  38. type ValueVar struct {
  39. VarID exec.VarID
  40. Src Node
  41. Dst DstList
  42. }
  43. func (v *ValueVar) GetVarID() exec.VarID {
  44. return v.VarID
  45. }
  46. func (v *ValueVar) IndexAtSrc() int {
  47. return v.Src.InputValues().IndexOf(v)
  48. }
  49. func (v *ValueVar) To(to Node, slotIdx int) {
  50. v.Dst.Add(to)
  51. to.InputValues().Slots.Set(slotIdx, v)
  52. }
  53. func (v *ValueVar) NotTo(node Node) {
  54. v.Dst.Remove(node)
  55. node.InputValues().Slots.Clear(v)
  56. }
  57. func (v *ValueVar) ClearAllDst() {
  58. for _, n := range v.Dst {
  59. n.InputValues().Slots.Clear(v)
  60. }
  61. v.Dst = nil
  62. }
  63. type DstList []Node
  64. func (s *DstList) Len() int {
  65. return len(*s)
  66. }
  67. func (s *DstList) Get(idx int) Node {
  68. return (*s)[idx]
  69. }
  70. func (s *DstList) Add(n Node) int {
  71. (*s) = append((*s), n)
  72. return len(*s) - 1
  73. }
  74. func (s *DstList) Remove(n Node) {
  75. for i, e := range *s {
  76. if e == n {
  77. (*s) = lo2.RemoveAt((*s), i)
  78. return
  79. }
  80. }
  81. }
  82. func (s *DstList) RemoveAt(idx int) {
  83. lo2.RemoveAt((*s), idx)
  84. }
  85. func (s *DstList) Resize(size int) {
  86. if s.Len() < size {
  87. (*s) = append((*s), make([]Node, size-s.Len())...)
  88. } else if s.Len() > size {
  89. (*s) = (*s)[:size]
  90. }
  91. }
  92. func (s *DstList) RawArray() []Node {
  93. return *s
  94. }