This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
csharp: Use value type wrapper containers
- Loading branch information
Showing
10 changed files
with
194 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
|
||
using System; | ||
|
||
namespace Serde | ||
{ | ||
public sealed class Unit | ||
public sealed class Unit : IEquatable<Unit> | ||
{ | ||
public Unit() { } | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj == null || GetType() != obj.GetType()) | ||
return false; | ||
return true; | ||
} | ||
public override bool Equals(object obj) => obj is Unit unit; | ||
|
||
public bool Equals(Unit other) => other != null; | ||
|
||
public override int GetHashCode() => 7; | ||
public override int GetHashCode() => 793253941; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Serde | ||
{ | ||
/// <summary> | ||
/// Immutable wrapper class around T[]. Implements value semantics for | ||
/// <see cref="object.Equals(object)"/> and <see cref="object.GetHashCode"/>. | ||
/// </summary> | ||
public class ValueArray<T> : IEquatable<ValueArray<T>>, IReadOnlyList<T>, IStructuralEquatable | ||
where T: IEquatable<T> | ||
{ | ||
private readonly T[] array; | ||
private int? hashCode; | ||
|
||
public int Count => array.Length; | ||
|
||
public T this[int index] => array[index]; | ||
|
||
public ValueArray(T[] data) { | ||
array = data ?? throw new ArgumentNullException(nameof(data)); | ||
hashCode = null; | ||
} | ||
|
||
public T[] ToArray() => array.ToArray(); | ||
|
||
public static implicit operator ReadOnlySpan<T>(ValueArray<T> bytes) => bytes.array; | ||
|
||
public ReadOnlySpan<T> AsReadOnlySpan() => array; | ||
|
||
public override bool Equals(object obj) => obj is ValueArray<T> bytes && Equals(bytes); | ||
|
||
public bool Equals(ValueArray<T> other) { | ||
if (other == null) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
if (Count != other.Count) return false; | ||
for (int i = 0; i < Count; i++) | ||
if (!array[i].Equals(other[i])) return false; | ||
return true; | ||
} | ||
|
||
public static bool operator ==(ValueArray<T> left, ValueArray<T> right) => Equals(left, right); | ||
|
||
public static bool operator !=(ValueArray<T> left, ValueArray<T> right) => !Equals(left, right); | ||
|
||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>)array).GetEnumerator(); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => array.GetEnumerator(); | ||
|
||
public override int GetHashCode() | ||
{ | ||
unchecked | ||
{ | ||
if (hashCode.HasValue) return hashCode.Value; | ||
int code = 1849862467; | ||
foreach (T elem in array) | ||
code = code * 31 + elem.GetHashCode(); | ||
hashCode = code; | ||
return code; | ||
} | ||
} | ||
|
||
public bool Equals(object other, IEqualityComparer comparer) | ||
{ | ||
return ((IStructuralEquatable)array).Equals(other, comparer); | ||
} | ||
|
||
public int GetHashCode(IEqualityComparer comparer) | ||
{ | ||
return ((IStructuralEquatable)array).GetHashCode(comparer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace Serde | ||
{ | ||
/// <summary> | ||
/// Immutable wrapper class around <see cref="Dictionary<K, V>"/>. Implements value semantics for | ||
/// <see cref="object.Equals(object)"/> and <see cref="object.GetHashCode"/>. | ||
/// </summary> | ||
public class ValueDictionary<K, V> : IEquatable<ValueDictionary<K, V>>, IReadOnlyDictionary<K, V> | ||
where K: IEquatable<K> | ||
where V: IEquatable<V> | ||
{ | ||
private readonly Dictionary<K, V> dict; | ||
private int? hashCode; | ||
|
||
public int Count => dict.Count; | ||
|
||
public IEnumerable<K> Keys => dict.Keys; | ||
|
||
public IEnumerable<V> Values => dict.Values; | ||
|
||
public V this[K key] => dict[key]; | ||
|
||
public ValueDictionary(Dictionary<K, V> dictionary) { | ||
dict = dictionary ?? throw new ArgumentNullException(nameof(dictionary)); | ||
hashCode = null; | ||
} | ||
|
||
public bool ContainsKey(K key) => dict.ContainsKey(key); | ||
|
||
public bool TryGetValue(K key, out V value) => dict.TryGetValue(key, out value); | ||
|
||
IEnumerator<KeyValuePair<K, V>> IEnumerable<KeyValuePair<K, V>>.GetEnumerator() => dict.GetEnumerator(); | ||
|
||
public IEnumerator GetEnumerator() => ((IEnumerable)dict).GetEnumerator(); | ||
|
||
public override bool Equals(object obj) => obj is ValueDictionary<K, V> bytes && Equals(bytes); | ||
|
||
public bool Equals(ValueDictionary<K, V> other) { | ||
if (other == null) return false; | ||
if (Count != other.Count) return false; | ||
foreach (var key in Keys) | ||
{ | ||
if (!other.ContainsKey(key)) return false; | ||
if (!dict[key].Equals(other[key])) return false; | ||
} | ||
return true; | ||
} | ||
|
||
public static bool operator ==(ValueDictionary<K, V> left, ValueDictionary<K, V> right) => Equals(left, right); | ||
|
||
public static bool operator !=(ValueDictionary<K, V> left, ValueDictionary<K, V> right) => !Equals(left, right); | ||
|
||
|
||
public override int GetHashCode() | ||
{ | ||
unchecked | ||
{ | ||
if (hashCode.HasValue) return hashCode.Value; | ||
int code = 45053; | ||
foreach (var pair in dict) | ||
{ | ||
code = code * 31 + pair.Key.GetHashCode(); | ||
code = code * 31 + pair.Value.GetHashCode(); | ||
} | ||
hashCode = code; | ||
return code; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.