Skip to content

Instantly share code, notes, and snippets.

@KumoKairo
Created January 23, 2018 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KumoKairo/fe7cb6a24135946ba34d7d3e15e10f19 to your computer and use it in GitHub Desktop.
Save KumoKairo/fe7cb6a24135946ba34d7d3e15e10f19 to your computer and use it in GitHub Desktop.
// Code from TextMeshPro
using System;
using System.Collections.Generic;
namespace TMPro
{
public class FastAction<A>
{
private LinkedList<Action<A>> delegates = new LinkedList<Action<A>>();
private Dictionary<Action<A>, LinkedListNode<Action<A>>> lookup = new Dictionary<Action<A>, LinkedListNode<Action<A>>>();
public void Add(Action<A> rhs)
{
if (this.lookup.ContainsKey(rhs))
return;
this.lookup[rhs] = this.delegates.AddLast(rhs);
}
public void Remove(Action<A> rhs)
{
LinkedListNode<Action<A>> node;
if (!this.lookup.TryGetValue(rhs, out node))
return;
this.lookup.Remove(rhs);
this.delegates.Remove(node);
}
public void Call(A a)
{
for (LinkedListNode<Action<A>> linkedListNode = this.delegates.First; linkedListNode != null; linkedListNode = linkedListNode.Next)
linkedListNode.Value(a);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment