Changeset 6340 for branches/histogram/HeuristicLab.Common
- Timestamp:
- 05/30/11 17:31:53 (13 years ago)
- Location:
- branches/histogram
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/histogram
- Property svn:mergeinfo changed
/trunk/sources merged: 6201,6205,6207-6209,6223,6233-6241,6250,6252,6254-6256,6259,6262,6265,6291,6298,6302
- Property svn:mergeinfo changed
-
branches/histogram/HeuristicLab.Common/3.3/Cloner.cs
r5445 r6340 41 41 /// <param name="item">The object which should be cloned.</param> 42 42 /// <returns>A clone of the given object.</returns> 43 public IDeepCloneable Clone(IDeepCloneable obj) {44 if (obj == null) return null;45 IDeepCloneable clone;46 if (mapping.TryGetValue(obj, out clone))47 return clone;48 else49 return obj.Clone(this);50 }51 /// <summary>52 /// Creates a deep clone of a given deeply cloneable object.53 /// </summary>54 /// <param name="item">The object which should be cloned.</param>55 /// <returns>A clone of the given object.</returns>56 43 public T Clone<T>(T obj) where T : class, IDeepCloneable { 57 44 if (obj == null) return null; … … 70 57 mapping.Add(item, clone); 71 58 } 59 60 /// <summary> 61 /// Checks if a clone is already registered for a given deeply cloneable item. 62 /// </summary> 63 /// <param name="item">The original object.</param> 64 /// <returns>True if a clone is already registered for the given item; false otherwise</returns> 65 public bool ClonedObjectRegistered(IDeepCloneable item) { 66 return mapping.ContainsKey(item); 67 } 68 72 69 } 73 70 } -
branches/histogram/HeuristicLab.Common/3.3/ObjectExtensions.cs
r6195 r6340 23 23 using System.Collections; 24 24 using System.Collections.Generic; 25 using System.Collections.Specialized; 25 26 using System.Reflection; 26 27 using System.Threading; … … 36 37 /// Types not collected: 37 38 /// * System.Delegate 38 /// * System. EventHandler (+ System.EventHandler<T>)39 /// * System.Reflection.Pointer 39 40 /// * Primitives (Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, Single) 40 41 /// * string, decimal 41 /// * Arrays of primitives (+ string, decimal)42 /// Types of which the fields are not further collected:43 /// * System.Type44 /// * System.Threading.ThreadLocal<T>42 /// * Arrays of types not collected 43 /// 44 /// Dictionaries and HashSets are treated specially, because it is cheaper to iterate over their keys and values 45 /// compared to traverse their internal data structures. 45 46 /// </summary> 46 47 private static void CollectObjectGraphObjects(this object obj, HashSet<object> objects) { 47 48 if (obj == null || objects.Contains(obj)) return; 48 if (obj is Delegate || obj is EventHandler) return;49 49 Type type = obj.GetType(); 50 if (type.IsSubclassOfRawGeneric(typeof(EventHandler<>))) return; 51 if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal)) return; 52 if (type.IsArray) { 53 Type elementType = type.GetElementType(); 54 if (elementType.IsPrimitive || elementType == typeof(string) || elementType == typeof(decimal)) return; 55 } 50 if (ExcludeType(type)) return; 51 if (type.HasElementType && ExcludeType(type.GetElementType())) return; 56 52 57 53 objects.Add(obj); 58 54 59 if (typeof(Type).IsInstanceOfType(obj)) return; // avoid infinite recursion 60 if (type.IsSubclassOfRawGeneric(typeof(ThreadLocal<>))) return; // avoid stack overflow when the field `ConcurrentStack<int> s_availableIndices` grows large 61 62 // performance critical to handle dictionaries in a special way 63 var dictionary = obj as IDictionary; 64 if (dictionary != null) { 65 foreach (object value in dictionary.Keys) { 55 if (type.IsSubclassOfRawGeneric(typeof(ThreadLocal<>))) return; // avoid stack overflow when the field `ConcurrentStack<int> s_availableIndices` too grows large 56 57 // performance critical to handle dictionaries, hashsets and hashtables in a special way 58 if (type.IsSubclassOfRawGeneric(typeof(Dictionary<,>)) || 59 type.IsSubclassOfRawGeneric(typeof(SortedDictionary<,>)) || 60 type.IsSubclassOfRawGeneric(typeof(SortedList<,>)) || 61 obj is SortedList || 62 obj is OrderedDictionary || 63 obj is ListDictionary || 64 obj is Hashtable) { 65 var dictionary = obj as IDictionary; 66 foreach (object value in dictionary.Keys) 66 67 CollectObjectGraphObjects(value, objects); 67 } 68 foreach (object value in dictionary.Values) { 68 foreach (object value in dictionary.Values) 69 69 CollectObjectGraphObjects(value, objects); 70 }71 70 return; 72 } 73 74 if (type.IsArray) { 75 var array = obj as Array; 76 foreach (object value in array) { 71 } else if (type.IsArray || type.IsSubclassOfRawGeneric(typeof(HashSet<>))) { 72 var enumerable = obj as IEnumerable; 73 foreach (var value in enumerable) 77 74 CollectObjectGraphObjects(value, objects); 78 }79 75 return; 80 76 } … … 82 78 foreach (FieldInfo f in type.GetAllFields()) { 83 79 f.GetValue(obj).CollectObjectGraphObjects(objects); 84 } 80 } 81 } 82 83 private static bool ExcludeType(Type type) { 84 return type.IsPrimitive || 85 type == typeof(string) || 86 type == typeof(decimal) || 87 typeof(Delegate).IsAssignableFrom(type) || 88 typeof(Pointer).IsAssignableFrom(type); 85 89 } 86 90 }
Note: See TracChangeset
for help on using the changeset viewer.