Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class FastAction {
  6.    
  7.     LinkedList<System.Action> delegates = new LinkedList<System.Action>();
  8.    
  9.     Dictionary<System.Action,LinkedListNode<System.Action>> lookup = new Dictionary<System.Action, LinkedListNode<System.Action>>();
  10.    
  11.     public void Add (System.Action rhs) {
  12.         if (lookup.ContainsKey(rhs)) return;
  13.        
  14.         lookup[rhs] = delegates.AddLast (rhs);
  15.     }
  16.    
  17.     public void Remove (System.Action rhs) {
  18.         LinkedListNode<System.Action> node;
  19.         if (lookup.TryGetValue (rhs, out node)) {
  20.             lookup.Remove (rhs);
  21.             delegates.Remove (node);
  22.         }
  23.     }
  24.    
  25.     public void Call () {
  26.         var node = delegates.First;
  27.         while (node != null) {
  28.             node.Value ();
  29.             node = node.Next;
  30.         }
  31.     }
  32. }
  33.  
  34. public class FastAction<A> {
  35.    
  36.     LinkedList<System.Action<A>> delegates = new LinkedList<System.Action<A>>();
  37.    
  38.     Dictionary<System.Action<A>,LinkedListNode<System.Action<A>>> lookup = new Dictionary<System.Action<A>, LinkedListNode<System.Action<A>>>();
  39.    
  40.     public void Add (System.Action<A> rhs) {
  41.         if (lookup.ContainsKey(rhs)) return;
  42.        
  43.         lookup[rhs] = delegates.AddLast (rhs);
  44.     }
  45.    
  46.     public void Remove (System.Action<A> rhs) {
  47.         LinkedListNode<System.Action<A>> node;
  48.         if (lookup.TryGetValue (rhs, out node)) {
  49.             lookup.Remove (rhs);
  50.             delegates.Remove (node);
  51.         }
  52.     }
  53.    
  54.     public void Call (A a) {
  55.         var node = delegates.First;
  56.         while (node != null) {
  57.             node.Value (a);
  58.             node = node.Next;
  59.         }
  60.     }
  61. }
  62.  
  63. public class FastAction<A,B> {
  64.  
  65.     LinkedList<System.Action<A,B>> delegates = new LinkedList<System.Action<A,B>>();
  66.  
  67.     Dictionary<System.Action<A,B>,LinkedListNode<System.Action<A,B>>> lookup = new Dictionary<System.Action<A,B>, LinkedListNode<System.Action<A,B>>>();
  68.  
  69.     public void Add (System.Action<A,B> rhs) {
  70.         if (lookup.ContainsKey(rhs)) return;
  71.  
  72.         lookup[rhs] = delegates.AddLast (rhs);
  73.     }
  74.  
  75.     public void Remove (System.Action<A,B> rhs) {
  76.         LinkedListNode<System.Action<A,B>> node;
  77.         if (lookup.TryGetValue (rhs, out node)) {
  78.             lookup.Remove (rhs);
  79.             delegates.Remove (node);
  80.         }
  81.     }
  82.  
  83.     public void Call (A a, B b) {
  84.         var node = delegates.First;
  85.         while (node != null) {
  86.             node.Value (a,b);
  87.             node = node.Next;
  88.         }
  89.     }
  90. }
  91.  
  92. public class FastAction<A,B,C> {
  93.    
  94.     LinkedList<System.Action<A,B,C>> delegates = new LinkedList<System.Action<A,B,C>>();
  95.    
  96.     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>>>();
  97.    
  98.     public void Add (System.Action<A,B,C> rhs) {
  99.         if (lookup.ContainsKey(rhs)) return;
  100.        
  101.         lookup[rhs] = delegates.AddLast (rhs);
  102.     }
  103.    
  104.     public void Remove (System.Action<A,B,C> rhs) {
  105.         LinkedListNode<System.Action<A,B,C>> node;
  106.         if (lookup.TryGetValue (rhs, out node)) {
  107.             lookup.Remove (rhs);
  108.             delegates.Remove (node);
  109.         }
  110.     }
  111.    
  112.     public void Call (A a, B b, C c) {
  113.         var node = delegates.First;
  114.         while (node != null) {
  115.             node.Value (a,b,c);
  116.             node = node.Next;
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement