Explorar o código

Avoid boxing in Skin dictionary.

closes #156
NathanSweet %!s(int64=12) %!d(string=hai) anos
pai
achega
e5aca584a2
Modificáronse 1 ficheiros con 15 adicións e 1 borrados
  1. 15 1
      spine-csharp/src/Skin.cs

+ 15 - 1
spine-csharp/src/Skin.cs

@@ -38,9 +38,10 @@ namespace Spine {
 	/// <summary>Stores attachments by slot index and attachment name.</summary>
 	public class Skin {
 		internal String name;
+		private Dictionary<KeyValuePair<int, String>, Attachment> attachments =
+			new Dictionary<KeyValuePair<int, String>, Attachment>(AttachmentComparer.Instance);
 
 		public String Name { get { return name; } }
-		private Dictionary<KeyValuePair<int, String>, Attachment> attachments = new Dictionary<KeyValuePair<int, String>, Attachment>();
 
 		public Skin (String name) {
 			if (name == null) throw new ArgumentNullException("name cannot be null.");
@@ -86,5 +87,18 @@ namespace Spine {
 				}
 			}
 		}
+
+		// Avoids boxing in the dictionary.
+		private class AttachmentComparer : IEqualityComparer<KeyValuePair<int, String>> {
+			internal static readonly AttachmentComparer Instance = new AttachmentComparer();
+
+			bool IEqualityComparer<KeyValuePair<int, string>>.Equals (KeyValuePair<int, string> o1, KeyValuePair<int, string> o2) {
+				return o1.Key == o2.Key && o1.Value == o2.Value;
+			}
+
+			int IEqualityComparer<KeyValuePair<int, string>>.GetHashCode (KeyValuePair<int, string> o) {
+				return o.Key;
+			}
+		}
 	}
 }