/*
Copyright (c) 2007, Marc Clifton
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of MyXaml nor the names of its contributors may be
used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Clifton.Collections.Generic
{
public static class Clone
{
public static List<T> ShallowCloneList<T>(List<T> list)
{
List<T> newList = new List<T>();
foreach (T t in list)
{
newList.Add(t);
}
return newList;
}
public static Dictionary<K, V> ShallowCloneDictionary<K, V>(Dictionary<K, V> dict)
{
Dictionary<K, V> newDict = null;
if (dict != null)
{
newDict = new Dictionary<K, V>();
foreach (KeyValuePair<K, V> kvp in dict)
{
newDict[kvp.Key] = kvp.Value;
}
}
return newDict;
}
public static List<T> CloneList<T>(List<T> list)
{
List<T> newList = null;
if (list != null)
{
// If the key and value are value types, clone without serialization.
if (typeof(T).IsValueType || typeof(T) == typeof(string))
{
newList = new List<T>();
newList.AddRange(list);
}
else
{
// Clone by serializing to a memory stream, then deserializing.
// Don't use this method if you've got a large objects, as the
// BinaryFormatter produces bloat, bloat, and more bloat.
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, list);
ms.Position = 0;
newList = (List<T>)bf.Deserialize(ms);
}
}
return newList;
}
/// <summary>
/// Does a deep copy of a dictionary, optimizing for value types. If
/// the key or value is not a value type (excluding strings), this method
/// relies on the type's serialization capabilities. If the type is not
/// serializable, this method will fail.
/// </summary>
public static Dictionary<K, V> CloneDictionary<K, V>(Dictionary<K, V> dict)
{
Dictionary<K, V> newDict=null;
// The clone method is immune to the source dictionary being null.
if (dict != null)
{
// If the key and value are value types, clone without serialization.
if (((typeof(K).IsValueType || typeof(K) == typeof(string)) &&
(typeof(V).IsValueType) || typeof(V) == typeof(string)))
{
newDict = new Dictionary<K, V>();
// Clone by copying the value types.
foreach (KeyValuePair<K, V> kvp in dict)
{
newDict[kvp.Key] = kvp.Value;
}
}
else
{
// Clone by serializing to a memory stream, then deserializing.
// Don't use this method if you've got a large objects, as the
// BinaryFormatter produces bloat, bloat, and more bloat.
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, dict);
ms.Position = 0;
newDict = (Dictionary<K, V>)bf.Deserialize(ms);
}
}
return newDict;
}
}
}