Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/30/11 17:31:53 (13 years ago)
Author:
abeham
Message:

#1465

  • updated branch from trunk
Location:
branches/histogram
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/histogram

  • branches/histogram/HeuristicLab.Common/3.3/Cloner.cs

    r5445 r6340  
    4141    /// <param name="item">The object which should be cloned.</param>
    4242    /// <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       else
    49         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>
    5643    public T Clone<T>(T obj) where T : class, IDeepCloneable {
    5744      if (obj == null) return null;
     
    7057      mapping.Add(item, clone);
    7158    }
     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
    7269  }
    7370}
  • branches/histogram/HeuristicLab.Common/3.3/ObjectExtensions.cs

    r6195 r6340  
    2323using System.Collections;
    2424using System.Collections.Generic;
     25using System.Collections.Specialized;
    2526using System.Reflection;
    2627using System.Threading;
     
    3637    /// Types not collected:
    3738    ///   * System.Delegate
    38     ///   * System.EventHandler (+ System.EventHandler<T>)
     39    ///   * System.Reflection.Pointer
    3940    ///   * Primitives (Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, Single)
    4041    ///   * string, decimal
    41     ///   * Arrays of primitives (+ string, decimal)
    42     /// Types of which the fields are not further collected:
    43     ///   * System.Type
    44     ///   * 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.
    4546    /// </summary>
    4647    private static void CollectObjectGraphObjects(this object obj, HashSet<object> objects) {
    4748      if (obj == null || objects.Contains(obj)) return;
    48       if (obj is Delegate || obj is EventHandler) return;
    4949      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;
    5652
    5753      objects.Add(obj);
    5854
    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)
    6667          CollectObjectGraphObjects(value, objects);
    67         }
    68         foreach (object value in dictionary.Values) {
     68        foreach (object value in dictionary.Values)
    6969          CollectObjectGraphObjects(value, objects);
    70         }
    7170        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)
    7774          CollectObjectGraphObjects(value, objects);
    78         }
    7975        return;
    8076      }
     
    8278      foreach (FieldInfo f in type.GetAllFields()) {
    8379        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);
    8589    }
    8690  }
Note: See TracChangeset for help on using the changeset viewer.