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

FastAction.cs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace TMPro
  5. {
  6. /// <summary>
  7. /// Alternative Action delegate with increased performance when adding or removing delegates.
  8. /// </summary>
  9. public class FastAction
  10. {
  11. LinkedList<System.Action> delegates = new LinkedList<System.Action>();
  12. Dictionary<System.Action, LinkedListNode<System.Action>> lookup = new Dictionary<System.Action, LinkedListNode<System.Action>>();
  13. public void Add(System.Action rhs)
  14. {
  15. if (lookup.ContainsKey(rhs)) return;
  16. lookup[rhs] = delegates.AddLast(rhs);
  17. }
  18. public void Remove(System.Action rhs)
  19. {
  20. LinkedListNode<System.Action> node;
  21. if (lookup.TryGetValue(rhs, out node))
  22. {
  23. lookup.Remove(rhs);
  24. delegates.Remove(node);
  25. }
  26. }
  27. public void Call()
  28. {
  29. var node = delegates.First;
  30. while (node != null)
  31. {
  32. node.Value();
  33. node = node.Next;
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// Alternative Action delegate with increased performance when adding or removing delegates.
  39. /// </summary>
  40. /// <typeparam name="A">The parameter of the method that this delegate encapsulates.</typeparam>
  41. public class FastAction<A>
  42. {
  43. LinkedList<System.Action<A>> delegates = new LinkedList<System.Action<A>>();
  44. Dictionary<System.Action<A>, LinkedListNode<System.Action<A>>> lookup = new Dictionary<System.Action<A>, LinkedListNode<System.Action<A>>>();
  45. public void Add(System.Action<A> rhs)
  46. {
  47. if (lookup.ContainsKey(rhs)) return;
  48. lookup[rhs] = delegates.AddLast(rhs);
  49. }
  50. public void Remove(System.Action<A> rhs)
  51. {
  52. LinkedListNode<System.Action<A>> node;
  53. if (lookup.TryGetValue(rhs, out node))
  54. {
  55. lookup.Remove(rhs);
  56. delegates.Remove(node);
  57. }
  58. }
  59. public void Call(A a)
  60. {
  61. var node = delegates.First;
  62. while (node != null)
  63. {
  64. node.Value(a);
  65. node = node.Next;
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Alternative Action delegate with increased performance when adding or removing delegates.
  71. /// </summary>
  72. /// <typeparam name="A">The first parameter of the method that this delegate encapsulates.</typeparam>
  73. /// <typeparam name="B">The second parameter of the method that this delegate encapsulates.</typeparam>
  74. public class FastAction<A, B>
  75. {
  76. LinkedList<System.Action<A, B>> delegates = new LinkedList<System.Action<A, B>>();
  77. Dictionary<System.Action<A, B>, LinkedListNode<System.Action<A, B>>> lookup = new Dictionary<System.Action<A, B>, LinkedListNode<System.Action<A, B>>>();
  78. public void Add(System.Action<A, B> rhs)
  79. {
  80. if (lookup.ContainsKey(rhs)) return;
  81. lookup[rhs] = delegates.AddLast(rhs);
  82. }
  83. public void Remove(System.Action<A, B> rhs)
  84. {
  85. LinkedListNode<System.Action<A, B>> node;
  86. if (lookup.TryGetValue(rhs, out node))
  87. {
  88. lookup.Remove(rhs);
  89. delegates.Remove(node);
  90. }
  91. }
  92. public void Call(A a, B b)
  93. {
  94. var node = delegates.First;
  95. while (node != null)
  96. {
  97. node.Value(a, b);
  98. node = node.Next;
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// Alternative Action delegate with increased performance when adding or removing delegates.
  104. /// </summary>
  105. /// <typeparam name="A">The first parameter of the method that this delegate encapsulates.</typeparam>
  106. /// <typeparam name="B">The second parameter of the method that this delegate encapsulates.</typeparam>
  107. /// <typeparam name="C">The third parameter of the method that this delegate encapsulates.</typeparam>
  108. public class FastAction<A, B, C>
  109. {
  110. LinkedList<System.Action<A, B, C>> delegates = new LinkedList<System.Action<A, B, C>>();
  111. 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>>>();
  112. public void Add(System.Action<A, B, C> rhs)
  113. {
  114. if (lookup.ContainsKey(rhs)) return;
  115. lookup[rhs] = delegates.AddLast(rhs);
  116. }
  117. public void Remove(System.Action<A, B, C> rhs)
  118. {
  119. LinkedListNode<System.Action<A, B, C>> node;
  120. if (lookup.TryGetValue(rhs, out node))
  121. {
  122. lookup.Remove(rhs);
  123. delegates.Remove(node);
  124. }
  125. }
  126. public void Call(A a, B b, C c)
  127. {
  128. var node = delegates.First;
  129. while (node != null)
  130. {
  131. node.Value(a, b, c);
  132. node = node.Next;
  133. }
  134. }
  135. }
  136. }