private static IDictionary Combine(IDictionary d1, IDictionary d2, bool sorted)
{
// Create new dictionary
IDictionary result;
if(sorted) result = new ListDictionary(); else result = new Hashtable();
// Copy all items from d1 to result
IDictionaryEnumerator d1e = d1.GetEnumerator();
while(d1e.MoveNext()) result.Add(d1e.Key, d1e.Value);
// Go for all items in d2
IDictionaryEnumerator d2e = d2.GetEnumerator();
while(d2e.MoveNext())
{
// Check if this is another Hashtable
if(d2e.Value is IDictionary)
{
// Check if already in result
if(result.Contains(d2e.Key) && (result[d2e.Key] is IDictionary))
{
// Modify result
result[d2e.Key] = Combine((IDictionary)result[d2e.Key], (IDictionary)d2e.Value, sorted);
}
else
{
// Copy from d2
if(sorted)
{
// Sorted combine
result[d2e.Key] = Combine(new ListDictionary(), (IDictionary)d2e.Value, sorted);
}
else
{
// Unsorted combine
result[d2e.Key] = Combine(new Hashtable(), (IDictionary)d2e.Value, sorted);
}
}
}
else
{
// Check if also in d1
if(result.Contains(d2e.Key))
{
// Modify result
result[d2e.Key] = d2e.Value;
}
else
{
// Copy
result.Add(d2e.Key, d2e.Value);
}
}
}
// Return result
return result;
}