FastAction.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace TMPro
  5. {
  6. public class FastAction
  7. {
  8. LinkedList<System.Action> delegates = new LinkedList<System.Action>();
  9. Dictionary<System.Action, LinkedListNode<System.Action>> lookup = new Dictionary<System.Action, LinkedListNode<System.Action>>();
  10. public void Add(System.Action rhs)
  11. {
  12. if (lookup.ContainsKey(rhs)) return;
  13. lookup[rhs] = delegates.AddLast(rhs);
  14. }
  15. public void Remove(System.Action rhs)
  16. {
  17. if (lookup.TryGetValue(rhs, out LinkedListNode<System.Action> node))
  18. {
  19. lookup.Remove(rhs);
  20. delegates.Remove(node);
  21. }
  22. }
  23. public void Call()
  24. {
  25. var node = delegates.First;
  26. while (node != null)
  27. {
  28. node.Value();
  29. node = node.Next;
  30. }
  31. }
  32. }
  33. public class FastAction<A>
  34. {
  35. LinkedList<System.Action<A>> delegates = new LinkedList<System.Action<A>>();
  36. Dictionary<System.Action<A>, LinkedListNode<System.Action<A>>> lookup = new Dictionary<System.Action<A>, LinkedListNode<System.Action<A>>>();
  37. public void Add(System.Action<A> rhs)
  38. {
  39. if (lookup.ContainsKey(rhs)) return;
  40. lookup[rhs] = delegates.AddLast(rhs);
  41. }
  42. public void Remove(System.Action<A> rhs)
  43. {
  44. if (lookup.TryGetValue(rhs, out LinkedListNode<System.Action<A>> node))
  45. {
  46. lookup.Remove(rhs);
  47. delegates.Remove(node);
  48. }
  49. }
  50. public void Call(A a)
  51. {
  52. var node = delegates.First;
  53. while (node != null)
  54. {
  55. node.Value(a);
  56. node = node.Next;
  57. }
  58. }
  59. }
  60. public class FastAction<A, B>
  61. {
  62. LinkedList<System.Action<A, B>> delegates = new LinkedList<System.Action<A, B>>();
  63. Dictionary<System.Action<A, B>, LinkedListNode<System.Action<A, B>>> lookup = new Dictionary<System.Action<A, B>, LinkedListNode<System.Action<A, B>>>();
  64. public void Add(System.Action<A, B> rhs)
  65. {
  66. if (lookup.ContainsKey(rhs)) return;
  67. lookup[rhs] = delegates.AddLast(rhs);
  68. }
  69. public void Remove(System.Action<A, B> rhs)
  70. {
  71. if (lookup.TryGetValue(rhs, out LinkedListNode<System.Action<A, B>> node))
  72. {
  73. lookup.Remove(rhs);
  74. delegates.Remove(node);
  75. }
  76. }
  77. public void Call(A a, B b)
  78. {
  79. var node = delegates.First;
  80. while (node != null)
  81. {
  82. node.Value(a, b);
  83. node = node.Next;
  84. }
  85. }
  86. }
  87. public class FastAction<A, B, C>
  88. {
  89. LinkedList<System.Action<A, B, C>> delegates = new LinkedList<System.Action<A, B, C>>();
  90. Dictionary<System.Action<A, B, C>, LinkedListNode<System.Action<A, B, C>>> lookup = new Dictionary<System.Action<A, B, C>, LinkedListNode<System.Action<A, B, C>>>();
  91. public void Add(System.Action<A, B, C> rhs)
  92. {
  93. if (lookup.ContainsKey(rhs)) return;
  94. lookup[rhs] = delegates.AddLast(rhs);
  95. }
  96. public void Remove(System.Action<A, B, C> rhs)
  97. {
  98. if (lookup.TryGetValue(rhs, out LinkedListNode<System.Action<A, B, C>> node))
  99. {
  100. lookup.Remove(rhs);
  101. delegates.Remove(node);
  102. }
  103. }
  104. public void Call(A a, B b, C c)
  105. {
  106. var node = delegates.First;
  107. while (node != null)
  108. {
  109. node.Value(a, b, c);
  110. node = node.Next;
  111. }
  112. }
  113. }
  114. }