Exemplo n.º 1
0
 internal override void AntiAlias(IDeepCopier deepCopier)
 {
     if (!((Result is Exception) || (Result is UnitType)))
     {
         Result = deepCopier.DeepCopy(Result);
     }
 }
Exemplo n.º 2
0
        public ReadOnlyMemory <T> DeepCopy(ReadOnlyMemory <T> input, CopyContext context)
        {
            if (input.IsEmpty)
            {
                return(input);
            }

            var inputSpan = input.Span;
            var result    = new T[inputSpan.Length];

            // Note that there is a possibility for unbounded recursion if the underlying object in the input is
            // able to take part in a cyclic reference. If we could get that object then we could prevent that cycle.
            // It is also possible that an IMemoryOwner<T> is the backing object, in which case this will not work.
            if (MemoryMarshal.TryGetArray(input, out var segment))
            {
                context.RecordCopy(segment.Array, result);
            }

            for (var i = 0; i < inputSpan.Length; i++)
            {
                result[i] = _elementCopier.DeepCopy(inputSpan[i], context);
            }

            return(result);
        }
    /// <inheritdoc/>
    public TField DeepCopy(TField input, CopyContext context)
    {
        var surrogate = _converter.ConvertToSurrogate(in input);
        var copy      = _surrogateCopier.DeepCopy(surrogate, context);
        var result    = _converter.ConvertFromSurrogate(in copy);

        return(result);
    }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public T?DeepCopy(T?input, CopyContext context)
        {
            if (!input.HasValue)
            {
                return(input);
            }

            return(new T?(_copier.DeepCopy(input.Value, context)));
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        public SortedDictionary <TKey, TValue> DeepCopy(SortedDictionary <TKey, TValue> input, CopyContext context)
        {
            if (context.TryGetCopy <SortedDictionary <TKey, TValue> >(input, out var result))
            {
                return(result);
            }

            if (input.GetType() != typeof(SortedDictionary <TKey, TValue>))
            {
                return(context.Copy(input));
            }

            result = new SortedDictionary <TKey, TValue>(input.Comparer);
            context.RecordCopy(input, result);
            foreach (var pair in input)
            {
                result[_keyCopier.DeepCopy(pair.Key, context)] = _valueCopier.DeepCopy(pair.Value, context);
            }

            return(result);
        }
Exemplo n.º 6
0
        /// <inheritdoc/>
        public List <T> DeepCopy(List <T> input, CopyContext context)
        {
            if (context.TryGetCopy <List <T> >(input, out var result))
            {
                return(result);
            }

            if (input.GetType() != typeof(List <T>))
            {
                return(context.DeepCopy(input));
            }

            result = new List <T>(input.Count);
            context.RecordCopy(input, result);
            foreach (var item in input)
            {
                result.Add(_copier.DeepCopy(item, context));
            }

            return(result);
        }
Exemplo n.º 7
0
        /// <inheritdoc />
        public SortedSet <T> DeepCopy(SortedSet <T> input, CopyContext context)
        {
            if (context.TryGetCopy <SortedSet <T> >(input, out var result))
            {
                return(result);
            }

            if (input.GetType() != typeof(SortedSet <T>))
            {
                return(context.Copy(input));
            }

            result = new SortedSet <T>(input.Comparer);
            context.RecordCopy(input, result);
            foreach (var element in input)
            {
                result.Add(_elementCopier.DeepCopy(element, context));
            }

            return(result);
        }
        public ConcurrentDictionary <TKey, TValue> DeepCopy(ConcurrentDictionary <TKey, TValue> input, CopyContext context)
        {
            if (context.TryGetCopy <ConcurrentDictionary <TKey, TValue> >(input, out var result))
            {
                return(result);
            }

            if (input.GetType() != typeof(ConcurrentDictionary <TKey, TValue>))
            {
                return(context.Copy(input));
            }

            // Note that this cannot propagate the input's key comparer, since it is not exposed from ConcurrentDictionary.
            result = new ConcurrentDictionary <TKey, TValue>();
            context.RecordCopy(input, result);
            foreach (var pair in input)
            {
                result[_keyCopier.DeepCopy(pair.Key, context)] = _valueCopier.DeepCopy(pair.Value, context);
            }

            return(result);
        }
Exemplo n.º 9
0
        /// <inheritdoc/>
        public ConcurrentQueue <T> DeepCopy(ConcurrentQueue <T> input, CopyContext context)
        {
            if (context.TryGetCopy <ConcurrentQueue <T> >(input, out var result))
            {
                return(result);
            }

            if (input.GetType() != typeof(ConcurrentQueue <T>))
            {
                return(context.Copy(input));
            }

            // Note that this cannot propagate the input's key comparer, since it is not exposed from ConcurrentDictionary.
            result = new ConcurrentQueue <T>();
            context.RecordCopy(input, result);
            foreach (var item in input)
            {
                result.Enqueue(_copier.DeepCopy(item, context));
            }

            return(result);
        }
Exemplo n.º 10
0
    /// <inheritdoc/>
    public TField DeepCopy(TField input, CopyContext context)
    {
        if (context.TryGetCopy <TField>(input, out var result))
        {
            return(result);
        }

        var surrogate = _converter.ConvertToSurrogate(in input);
        var copy      = _surrogateCopier.DeepCopy(surrogate, context);

        result = _converter.ConvertFromSurrogate(in copy);

        context.RecordCopy(input, result);
        return(result);
    }
Exemplo n.º 11
0
        public T[] DeepCopy(T[] input, CopyContext context)
        {
            if (context.TryGetCopy <T[]>(input, out var result))
            {
                return(result);
            }

            result = new T[input.Length];
            context.RecordCopy(input, result);
            for (var i = 0; i < input.Length; i++)
            {
                result[i] = _elementCopier.DeepCopy(input[i], context);
            }

            return(result);
        }
Exemplo n.º 12
0
        public Tuple <T> DeepCopy(Tuple <T> input, CopyContext context)
        {
            if (context.TryGetCopy(input, out Tuple <T> result))
            {
                return(result);
            }

            if (input.GetType() != typeof(Tuple <T>))
            {
                return(context.Copy(input));
            }

            result = new Tuple <T>(_copier.DeepCopy(input.Item1, context));
            context.RecordCopy(input, result);
            return(result);
        }
Exemplo n.º 13
0
        public ArraySegment <T> DeepCopy(ArraySegment <T> input, CopyContext context)
        {
            if (input.Array is null)
            {
                return(input);
            }

            var inputSpan = input.AsSpan();
            var result    = new T[inputSpan.Length];

            context.RecordCopy(input.Array, result);
            for (var i = 0; i < inputSpan.Length; i++)
            {
                result[i] = _elementCopier.DeepCopy(inputSpan[i], context);
            }

            return(new ArraySegment <T>(result));
        }
Exemplo n.º 14
0
        public Memory <T> DeepCopy(Memory <T> input, CopyContext context)
        {
            if (input.IsEmpty)
            {
                return(input);
            }

            // Note that there is a possibility for infinite recursion here if the underlying object in the input is
            // able to take part in a cyclic reference. If we could get that object then we could prevent that cycle.
            var inputSpan = input.Span;
            var result    = new T[inputSpan.Length];

            for (var i = 0; i < inputSpan.Length; i++)
            {
                result[i] = _elementCopier.DeepCopy(inputSpan[i], context);
            }

            return(result);
        }
Exemplo n.º 15
0
        public ReadOnlyDictionary <TKey, TValue> DeepCopy(ReadOnlyDictionary <TKey, TValue> input, CopyContext context)
        {
            if (context.TryGetCopy <ReadOnlyDictionary <TKey, TValue> >(input, out var result))
            {
                return(result);
            }

            if (input.GetType() != typeof(ReadOnlyDictionary <TKey, TValue>))
            {
                return(context.Copy(input));
            }

            var temp = new Dictionary <TKey, TValue>(input.Count);

            foreach (var pair in input)
            {
                temp[_keyCopier.DeepCopy(pair.Key, context)] = _valueCopier.DeepCopy(pair.Value, context);
            }

            result = new ReadOnlyDictionary <TKey, TValue>(temp);
            context.RecordCopy(input, result);
            return(result);
        }
Exemplo n.º 16
0
        /// <inheritdoc/>
        object IDeepCopier <object> .DeepCopy(object original, CopyContext context)
        {
            if (context.TryGetCopy <Array>(original, out var result))
            {
                return(result);
            }

            var type          = original.GetType();
            var originalArray = (Array)original;
            var elementType   = type.GetElementType();

            if (ShallowCopyableTypes.Contains(elementType))
            {
                return(originalArray.Clone());
            }

            // We assume that all arrays have lower bound 0. In .NET 4.0, it's hard to create an array with a non-zero lower bound.
            var rank    = originalArray.Rank;
            var lengths = new int[rank];

            for (var i = 0; i < rank; i++)
            {
                lengths[i] = originalArray.GetLength(i);
            }

            result = Array.CreateInstance(elementType, lengths);
            context.RecordCopy(original, result);

            if (rank == 1)
            {
                for (var i = 0; i < lengths[0]; i++)
                {
                    result.SetValue(_elementCopier.DeepCopy(originalArray.GetValue(i), context), i);
                }
            }
            else if (rank == 2)
            {
                for (var i = 0; i < lengths[0]; i++)
                {
                    for (var j = 0; j < lengths[1]; j++)
                    {
                        result.SetValue(_elementCopier.DeepCopy(originalArray.GetValue(i, j), context), i, j);
                    }
                }
            }
            else
            {
                var index = new int[rank];
                var sizes = new int[rank];
                sizes[rank - 1] = 1;
                for (var k = rank - 2; k >= 0; k--)
                {
                    sizes[k] = sizes[k + 1] * lengths[k + 1];
                }

                for (var i = 0; i < originalArray.Length; i++)
                {
                    int k = i;
                    for (int n = 0; n < rank; n++)
                    {
                        int offset = k / sizes[n];
                        k       -= offset * sizes[n];
                        index[n] = offset;
                    }

                    result.SetValue(_elementCopier.DeepCopy(originalArray.GetValue(index), context), index);
                }
            }

            return(result);
        }