Advertisement
Guest User

Untitled

a guest
Oct 16th, 2011
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1.     class FastComparer : IEqualityComparer<Object>
  2.     {
  3.         bool IEqualityComparer<object>.Equals(object x, object y)
  4.         {
  5.             return ReferenceEquals(x, y);
  6.         }
  7.  
  8.         int IEqualityComparer<object>.GetHashCode(object obj)
  9.         {
  10.             return obj.GetHashCode();
  11.         }
  12.     }
  13.  
  14.     public class ObjectIDGeneratorXPlatform
  15.     {
  16.         private Dictionary<object, long> mObjects;
  17.         private long mCounter = 1;
  18.  
  19.         public ObjectIDGeneratorXPlatform()
  20.         {
  21.             mObjects = new Dictionary<object, long>(4096, new FastComparer());
  22.         }
  23.  
  24.         public long GetId(Object obj, out bool firstTime)
  25.         {
  26.             long id;
  27.             if (mObjects.TryGetValue(obj, out id))
  28.             {
  29.                 firstTime = false;
  30.                 return id;
  31.             }
  32.             else
  33.             {
  34.                 firstTime = true;
  35.                 id = mCounter;
  36.                 ++mCounter;
  37.                 mObjects.Add(obj, id);
  38.                 return id;
  39.             }
  40.         }
  41.     }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement