This repository has been archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Optimized the string key. - Implemented the StringKey type that precalculate the hash. * Add a description of StringKey.
- Loading branch information
Showing
11 changed files
with
376 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
using System.Collections.Generic; | ||
using UnsafeGeneric; | ||
|
||
#if BETTER_PATCH | ||
namespace Better | ||
|
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
12 changes: 12 additions & 0 deletions
12
Assets/Plugins/BetterDictionary/EqualityComparerFactory.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,71 +1,70 @@ | ||
using System.Collections.Generic; | ||
using UnsafeGeneric; | ||
|
||
#if !BETTER_PATCH | ||
namespace Better | ||
{ | ||
#endif | ||
/// <summary> | ||
/// A generic hash set class optimized for the Unity. | ||
/// </summary> | ||
/// <typeparam name="T">The type of elements in the hash set.</typeparam> | ||
public class HashSet<T> : System.Collections.Generic.HashSet<T> | ||
{ | ||
private static readonly IEqualityComparer<T> EqualityComparer = EqualityComparerFactory.Create<T>(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:Better.HashSet`1" /> class that is empty | ||
/// and uses the default equality comparer for the set type. | ||
/// A generic hash set class optimized for the Unity. | ||
/// </summary> | ||
public HashSet() : this((IEqualityComparer<T>) null) | ||
/// <typeparam name="T">The type of elements in the hash set.</typeparam> | ||
public class HashSet<T> : System.Collections.Generic.HashSet<T> | ||
{ | ||
} | ||
private static readonly IEqualityComparer<T> EqualityComparer = EqualityComparerFactory.Create<T>(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:Better.HashSet`1" /> class that is empty | ||
/// and uses the specified equality comparer for the set type. | ||
/// </summary> | ||
/// <param name="comparer"> | ||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> implementation to use when | ||
/// comparing values in the set, or null to use the default | ||
/// <see cref="T:System.Collections.Generic.EqualityComparer`1" /> implementation for the set type. | ||
/// </param> | ||
public HashSet(IEqualityComparer<T> comparer) : base(comparer ?? EqualityComparer) | ||
{ | ||
} | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:Better.HashSet`1" /> class that is empty | ||
/// and uses the default equality comparer for the set type. | ||
/// </summary> | ||
public HashSet() : this((IEqualityComparer<T>)null) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:Better.HashSet`1" /> class that uses the | ||
/// default equality comparer for the set type, contains elements copied from the specified collection, and has | ||
/// sufficient capacity to accommodate the number of elements copied. | ||
/// </summary> | ||
/// <param name="collection">The collection whose elements are copied to the new set.</param> | ||
/// <exception cref="T:System.ArgumentNullException"> | ||
/// <paramref name="collection" /> is null. | ||
/// </exception> | ||
public HashSet(IEnumerable<T> collection) : this(collection, null) | ||
{ | ||
} | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:Better.HashSet`1" /> class that is empty | ||
/// and uses the specified equality comparer for the set type. | ||
/// </summary> | ||
/// <param name="comparer"> | ||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> implementation to use when | ||
/// comparing values in the set, or null to use the default | ||
/// <see cref="T:System.Collections.Generic.EqualityComparer`1" /> implementation for the set type. | ||
/// </param> | ||
public HashSet(IEqualityComparer<T> comparer) : base(comparer ?? EqualityComparer) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:Better.HashSet`1" /> class that uses the | ||
/// specified equality comparer for the set type, contains elements copied from the specified collection, and has | ||
/// sufficient capacity to accommodate the number of elements copied. | ||
/// </summary> | ||
/// <param name="collection">The collection whose elements are copied to the new set.</param> | ||
/// <param name="comparer"> | ||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> implementation to use when | ||
/// comparing values in the set, or null to use the default | ||
/// <see cref="T:System.Collections.Generic.EqualityComparer`1" /> implementation for the set type. | ||
/// </param> | ||
/// <exception cref="T:System.ArgumentNullException"> | ||
/// <paramref name="collection" /> is null. | ||
/// </exception> | ||
public HashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer) | ||
: base(collection, comparer ?? EqualityComparer) | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:Better.HashSet`1" /> class that uses the | ||
/// default equality comparer for the set type, contains elements copied from the specified collection, and has | ||
/// sufficient capacity to accommodate the number of elements copied. | ||
/// </summary> | ||
/// <param name="collection">The collection whose elements are copied to the new set.</param> | ||
/// <exception cref="T:System.ArgumentNullException"> | ||
/// <paramref name="collection" /> is null. | ||
/// </exception> | ||
public HashSet(IEnumerable<T> collection) : this(collection, null) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:Better.HashSet`1" /> class that uses the | ||
/// specified equality comparer for the set type, contains elements copied from the specified collection, and has | ||
/// sufficient capacity to accommodate the number of elements copied. | ||
/// </summary> | ||
/// <param name="collection">The collection whose elements are copied to the new set.</param> | ||
/// <param name="comparer"> | ||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> implementation to use when | ||
/// comparing values in the set, or null to use the default | ||
/// <see cref="T:System.Collections.Generic.EqualityComparer`1" /> implementation for the set type. | ||
/// </param> | ||
/// <exception cref="T:System.ArgumentNullException"> | ||
/// <paramref name="collection" /> is null. | ||
/// </exception> | ||
public HashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer) | ||
: base(collection, comparer ?? EqualityComparer) | ||
{ | ||
} | ||
} | ||
} | ||
#if !BETTER_PATCH | ||
} | ||
#endif |
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 UnsafeGeneric; | ||
|
||
#if !BETTER_PATCH | ||
namespace Better | ||
{ | ||
#endif | ||
/// <summary> | ||
/// A string key class optimized for the Dictionary and HashSet. | ||
/// </summary> | ||
public struct StringKey : IEquatable<StringKey> | ||
{ | ||
/// <summary> | ||
/// The string of the key. | ||
/// </summary> | ||
public readonly string Value; | ||
|
||
/// <summary> | ||
/// The hash code of the string. | ||
/// </summary> | ||
public readonly int HashCode; | ||
|
||
public StringKey(string str) | ||
{ | ||
Value = str; | ||
HashCode = StringHashCode.Calculate(str); | ||
} | ||
|
||
public bool Equals(StringKey key) | ||
{ | ||
return Value.Equals(key.Value); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj is StringKey) | ||
{ | ||
return Equals((StringKey)obj); | ||
} | ||
|
||
// exception will be thrown later for null this | ||
return this == null; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return HashCode; | ||
} | ||
|
||
public static bool operator ==(StringKey a, StringKey b) | ||
{ | ||
return a.Equals(b); | ||
} | ||
|
||
public static bool operator !=(StringKey a, StringKey b) | ||
{ | ||
return !a.Equals(b); | ||
} | ||
|
||
// StringKey key = "Foo"; | ||
public static implicit operator StringKey(string str) | ||
{ | ||
return new StringKey(str); | ||
} | ||
|
||
// StringKey key; | ||
// string str = key; | ||
public static implicit operator string(StringKey str) | ||
{ | ||
return str.Value; | ||
} | ||
} | ||
#if !BETTER_PATCH | ||
} | ||
#endif |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
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
Oops, something went wrong.