Skip to content

Commit

Permalink
added: CreateConflictsKValueEntry and the overload for AddOrUpdate wi…
Browse files Browse the repository at this point in the history
…th Type with inlining the object initializer
  • Loading branch information
m.volkau committed Mar 19, 2020
1 parent 8cdb5c1 commit ed0bbb3
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions src/ImTools/ImTools.Experimental.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ private ImMapTree<V> BalanceNewLeftTree(ImMapTree<V> newLeftTree)
new ImMapTree<V>(Entry, leftRightTree.Right, Right, rightHeight + 1),
leftLeftHeight + 2);

// Saving the old code to explaining what's happening in the new one
//return new ImMapTree<V>(leftRightTree.Entry,
// new ImMapTree<V>(newLeftTree.Entry, leftLeftHeight, newLeftTree.Left, leftRightTree.Left),
// new ImMapTree<V>(Entry, leftRightTree.Right, rightHeight, Right));
Expand Down Expand Up @@ -1432,30 +1433,31 @@ private static ImMap<KValue<K>> UpdateEntryOrAddOrUpdateConflict<K>(ImMap<KValue
return map.UpdateEntryUnsafe(conflictsEntry);
}

/// <summary>Efficiently creates the new entry</summary>
/// <summary>Creates the new entry</summary>
[MethodImpl((MethodImplOptions)256)]
private static ImMapEntry<KValue<K>> CreateKValueEntry<K>(int hash, K key, object value)
{
var newEntry = new ImMapEntry<KValue<K>>(hash);
newEntry.Value.Key = key;
newEntry.Value.Value = value;
return newEntry;
}
private static ImMapEntry<KValue<K>> CreateKValueEntry<K>(int hash, K key, object value) =>
new ImMapEntry<KValue<K>>(hash) { Value = { Key = key, Value = value }};

/// <summary>Efficiently creates the new entry</summary>
/// <summary>Creates the new entry with the conflicts - the Key for the new entry is not set, but the value contains the actual conflict entries array</summary>
[MethodImpl((MethodImplOptions)256)]
public static ImMapEntry<KValue<K>> CreateKValueEntry<K>(int hash, K key)
{
var newEntry = new ImMapEntry<KValue<K>>(hash);
newEntry.Value.Key = key;
return newEntry;
}
private static ImMapEntry<KValue<K>> CreateConflictsKValueEntry<K>(int hash, ImMapEntry<KValue<K>>[] conflicts) =>
new ImMapEntry<KValue<K>>(hash) { Value = { Value = conflicts }};

/// <summary>Creates the new entry</summary>
[MethodImpl((MethodImplOptions)256)]
public static ImMapEntry<KValue<K>> CreateKValueEntry<K>(int hash, K key) =>
new ImMapEntry<KValue<K>>(hash) { Value = { Key = key }};

/// <summary>Uses the user provided hash and adds or updates the tree with passed key-value. Returns a new tree.</summary>
[MethodImpl((MethodImplOptions)256)]
public static ImMap<KValue<K>> AddOrUpdate<K>(this ImMap<KValue<K>> map, int hash, K key, object value) =>
map.AddOrUpdate(hash, CreateKValueEntry(hash, key, value));

/// <summary>Adds or updates the tree with passed Type key and the value. Returns a new tree.</summary>
[MethodImpl((MethodImplOptions)256)]
public static ImMap<KValue<Type>> AddOrUpdate(this ImMap<KValue<Type>> map, Type key, object value) =>
map.AddOrUpdate(RuntimeHelpers.GetHashCode(key), key, value);

/// <summary>Adds or updates the tree with passed key-value. Returns a new tree.</summary>
[MethodImpl((MethodImplOptions)256)]
public static ImMap<KValue<K>> AddOrUpdate<K>(this ImMap<KValue<K>> map, K key, object value) =>
Expand Down Expand Up @@ -1508,7 +1510,7 @@ private static ImMap<KValue<K>> UpdateEntryOrAddOrUpdateConflict<K>(ImMap<KValue
newConflicts = new[] { oldEntry, newEntry };
}

return map.UpdateEntryUnsafe(CreateKValueEntry(hash, default(K), newConflicts));
return map.UpdateEntryUnsafe(CreateConflictsKValueEntry(hash, newConflicts));
}

/// <summary>Adds the new entry or keeps the current map if entry key is already present</summary>
Expand Down Expand Up @@ -1560,7 +1562,7 @@ private static ImMap<KValue<K>> AddOrKeepConflict<K>(ImMap<KValue<K>> map, int h
newConflicts = new[] { oldEntry, CreateKValueEntry(hash, key, value) };
}

return map.UpdateEntryUnsafe(CreateKValueEntry(hash, default(K), newConflicts));
return map.UpdateEntryUnsafe(CreateConflictsKValueEntry(hash, newConflicts));
}

/// <summary>Updates the map with the new value if key is found, otherwise returns the same unchanged map.</summary>
Expand Down Expand Up @@ -1603,7 +1605,7 @@ private static ImMap<KValue<K>> UpdateEntryOrReturnSelf<K>(ImMap<KValue<K>> map,
return map;
}

return map.UpdateEntryUnsafe(CreateKValueEntry(hash, default(K), newConflicts));
return map.UpdateEntryUnsafe(CreateConflictsKValueEntry(hash, newConflicts));
}

/// <summary>Updates the map with the default value if the key is found, otherwise returns the same unchanged map.</summary>
Expand Down

0 comments on commit ed0bbb3

Please sign in to comment.