Skip to content

Instantly share code, notes, and snippets.

@uzzu
Last active December 20, 2015 14:49
Show Gist options
  • Select an option

  • Save uzzu/6150155 to your computer and use it in GitHub Desktop.

Select an option

Save uzzu/6150155 to your computer and use it in GitHub Desktop.
Tuple implementation for unity3d (before .NET 4.0 environment)
using System;
using System.Collections;
using System.Text;
namespace System
{
/// <summary>
/// Tuple.
/// </summary>
public static class Tuple
{
#region Factory
/// <summary>
/// Create the specified item1.
/// </summary>
/// <param name="item1">Item1.</param>
/// <typeparam name="T1">The 1st type parameter.</typeparam>
public static Tuple<T1> Create<T1>(T1 item1)
{
return new Tuple<T1>(item1);
}
/// <summary>
/// Create the specified item1 and item2.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <typeparam name="T1">The 1st type parameter.</typeparam>
/// <typeparam name="T2">The 2nd type parameter.</typeparam>
public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
{
return new Tuple<T1, T2>(item1, item2);
}
/// <summary>
/// Create the specified item1, item2 and item3.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
/// <typeparam name="T1">The 1st type parameter.</typeparam>
/// <typeparam name="T2">The 2nd type parameter.</typeparam>
/// <typeparam name="T3">The 3rd type parameter.</typeparam>
public static Tuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3)
{
return new Tuple<T1, T2, T3>(item1, item2, item3);
}
/// <summary>
/// Create the specified item1, item2, item3 and item4.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
/// <param name="item4">Item4.</param>
/// <typeparam name="T1">The 1st type parameter.</typeparam>
/// <typeparam name="T2">The 2nd type parameter.</typeparam>
/// <typeparam name="T3">The 3rd type parameter.</typeparam>
/// <typeparam name="T4">The 4th type parameter.</typeparam>
public static Tuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(T1 item1, T2 item2, T3 item3, T4 item4)
{
return new Tuple<T1, T2, T3, T4>(item1, item2, item3, item4);
}
/// <summary>
/// Create the specified item1, item2, item3, item4 and item5.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
/// <param name="item4">Item4.</param>
/// <param name="item5">Item5.</param>
/// <typeparam name="T1">The 1st type parameter.</typeparam>
/// <typeparam name="T2">The 2nd type parameter.</typeparam>
/// <typeparam name="T3">The 3rd type parameter.</typeparam>
/// <typeparam name="T4">The 4th type parameter.</typeparam>
/// <typeparam name="T5">The 5th type parameter.</typeparam>
public static Tuple<T1, T2, T3, T4, T5> Create<T1, T2, T3, T4, T5>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
{
return new Tuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5);
}
/// <summary>
/// Create the specified item1, item2, item3, item4, item5 and item6.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
/// <param name="item4">Item4.</param>
/// <param name="item5">Item5.</param>
/// <param name="item6">Item6.</param>
/// <typeparam name="T1">The 1st type parameter.</typeparam>
/// <typeparam name="T2">The 2nd type parameter.</typeparam>
/// <typeparam name="T3">The 3rd type parameter.</typeparam>
/// <typeparam name="T4">The 4th type parameter.</typeparam>
/// <typeparam name="T5">The 5th type parameter.</typeparam>
/// <typeparam name="T6">The 6th type parameter.</typeparam>
public static Tuple<T1, T2, T3, T4, T5, T6> Create<T1, T2, T3, T4, T5, T6>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
{
return new Tuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6);
}
/// <summary>
/// Create the specified item1, item2, item3, item4, item5, item6 and item7.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
/// <param name="item4">Item4.</param>
/// <param name="item5">Item5.</param>
/// <param name="item6">Item6.</param>
/// <param name="item7">Item7.</param>
/// <typeparam name="T1">The 1st type parameter.</typeparam>
/// <typeparam name="T2">The 2nd type parameter.</typeparam>
/// <typeparam name="T3">The 3rd type parameter.</typeparam>
/// <typeparam name="T4">The 4th type parameter.</typeparam>
/// <typeparam name="T5">The 5th type parameter.</typeparam>
/// <typeparam name="T6">The 6th type parameter.</typeparam>
/// <typeparam name="T7">The 7th type parameter.</typeparam>
public static Tuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
{
return new Tuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7);
}
/// <summary>
/// Create the specified item1, item2, item3, item4, item5, item6, item7 and rest.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
/// <param name="item4">Item4.</param>
/// <param name="item5">Item5.</param>
/// <param name="item6">Item6.</param>
/// <param name="item7">Item7.</param>
/// <param name="rest">Rest.</param>
/// <typeparam name="T1">The 1st type parameter.</typeparam>
/// <typeparam name="T2">The 2nd type parameter.</typeparam>
/// <typeparam name="T3">The 3rd type parameter.</typeparam>
/// <typeparam name="T4">The 4th type parameter.</typeparam>
/// <typeparam name="T5">The 5th type parameter.</typeparam>
/// <typeparam name="T6">The 6th type parameter.</typeparam>
/// <typeparam name="T7">The 7th type parameter.</typeparam>
/// <typeparam name="TRest">The 8th type parameter must be Tuple.</typeparam>
public static Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> Create<T1, T2, T3, T4, T5, T6, T7, TRest>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest)
{
return new Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>(item1, item2, item3, item4, item5, item6, item7, rest);
}
#endregion
#region Unitilities
internal static int CombineHashCodes(int h1, int h2)
{
return (((h1 << 5) + h1) ^ h2);
}
internal static int CombineHashCodes(int h1, int h2, int h3)
{
return CombineHashCodes(CombineHashCodes(h1, h2), h3);
}
internal static int CombineHashCodes(int h1, int h2, int h3, int h4)
{
return CombineHashCodes(CombineHashCodes(h1, h2), CombineHashCodes(h3, h4));
}
internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5)
{
return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), h5);
}
internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6)
{
return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6));
}
internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7)
{
return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6, h7));
}
internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8)
{
return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), CombineHashCodes(h5, h6, h7, h8));
}
#endregion
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace System.Collections.Generic
{
interface IStructuralEquatable
{
bool Equals(Object that, IEqualityComparer comparer);
int GetHashCode(IEqualityComparer comparer);
}
interface IStructuralComparable
{
int CompareTo(object that, IComparer comparer);
}
}
namespace System
{
interface ITuple
{
int Size { get; }
string ToString();
}
}
namespace System
{
[Serializable]
public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
{
/// <summary>
/// Gets the item1.
/// </summary>
/// <value>The item1.</value>
public T1 Item1 { get; private set; }
int ITuple.Size { get { return 1; } }
/// <summary>
/// Initializes a new instance of the <see cref="System.Tuple`1"/> class.
/// </summary>
/// <param name="item1">Item1.</param>
public Tuple(T1 item1)
{
Item1 = item1;
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Tuple`1"/>.
/// </summary>
/// <param name="that">The <see cref="System.Object"/> to compare with the current <see cref="System.Tuple`1"/>.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
/// <see cref="System.Tuple`1"/>; otherwise, <c>false</c>.</returns>
public override bool Equals(object that)
{
return ((IStructuralEquatable) this).Equals(that, EqualityComparer<object>.Default);
}
/// <summary>
/// Serves as a hash function for a <see cref="System.Tuple`1"/> object.
/// </summary>
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
/// hash table.</returns>
public override int GetHashCode()
{
return ((IStructuralEquatable) this).GetHashCode(EqualityComparer<object>.Default);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="System.Tuple`1"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="System.Tuple`1"/>.</returns>
public override string ToString()
{
return "(" + ((ITuple) this).ToString() + ")";
}
bool IStructuralEquatable.Equals(object that, IEqualityComparer comparer)
{
if (that == null)
{
return false;
}
var thatTuple = that as Tuple<T1>;
if (thatTuple == null)
{
return false;
}
return comparer.Equals(Item1, thatTuple.Item1);
}
int IComparable.CompareTo(object that)
{
return ((IStructuralComparable) this).CompareTo(that, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo(object that, IComparer comparer)
{
if (that == null)
{
return 1;
}
var thatTuple = that as Tuple<T1>;
if (thatTuple == null)
{
return 1;
}
return comparer.Compare(Item1, thatTuple.Item1);
}
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
{
return comparer.GetHashCode(Item1);
}
string ITuple.ToString()
{
return string.Format("{0}", Item1);
}
}
[Serializable]
/// <summary>
/// Tuple.
/// </summary>
public class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
{
/// <summary>
/// Gets the item1.
/// </summary>
/// <value>The item1.</value>
public T1 Item1 { get; private set; }
/// <summary>
/// Gets the item2.
/// </summary>
/// <value>The item2.</value>
public T2 Item2 { get; private set; }
int ITuple.Size { get { return 2; } }
/// <summary>
/// Initializes a new instance of the <see cref="System.Tuple`2"/> class.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
public Tuple(T1 item1, T2 item2)
{
Item1 = item1;
Item2 = item2;
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Tuple`2"/>.
/// </summary>
/// <param name="that">The <see cref="System.Object"/> to compare with the current <see cref="System.Tuple`2"/>.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
/// <see cref="System.Tuple`2"/>; otherwise, <c>false</c>.</returns>
public override bool Equals(object that)
{
return ((IStructuralEquatable) this).Equals(that, EqualityComparer<object>.Default);
}
/// <summary>
/// Serves as a hash function for a <see cref="System.Tuple`2"/> object.
/// </summary>
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
/// hash table.</returns>
public override int GetHashCode()
{
return ((IStructuralEquatable) this).GetHashCode(EqualityComparer<object>.Default);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="System.Tuple`2"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="System.Tuple`2"/>.</returns>
public override string ToString()
{
return "(" + ((ITuple) this).ToString() + ")";
}
bool IStructuralEquatable.Equals(object that, IEqualityComparer comparer)
{
if (that == null)
{
return false;
}
var thatTuple = that as Tuple<T1, T2>;
if (thatTuple == null)
{
return false;
}
return comparer.Equals(Item1, thatTuple.Item1)
&& comparer.Equals(Item2, thatTuple.Item2);
}
int IComparable.CompareTo(object that)
{
return ((IStructuralComparable) this).CompareTo(that, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo(object that, IComparer comparer)
{
if (that == null)
{
return 1;
}
var thatTuple = that as Tuple<T1, T2>;
if (thatTuple == null)
{
return 1;
}
var c = 0;
c = comparer.Compare(Item1, thatTuple.Item1);
if (c != 0)
{
return c;
}
return comparer.Compare(Item2, thatTuple.Item2);
}
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
{
return Tuple.CombineHashCodes(
comparer.GetHashCode(Item1),
comparer.GetHashCode(Item2)
);
}
string ITuple.ToString()
{
return string.Format("{0}, {1}", Item1, Item2);
}
}
[Serializable]
public class Tuple<T1, T2, T3> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
{
/// <summary>
/// Gets the item1.
/// </summary>
/// <value>The item1.</value>
public T1 Item1 { get; private set; }
/// <summary>
/// Gets the item2.
/// </summary>
/// <value>The item2.</value>
public T2 Item2 { get; private set; }
/// <summary>
/// Gets the item3.
/// </summary>
/// <value>The item3.</value>
public T3 Item3 { get; private set; }
int ITuple.Size { get { return 3; } }
/// <summary>
/// Initializes a new instance of the <see cref="System.Tuple`3"/> class.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
public Tuple(T1 item1, T2 item2, T3 item3)
{
Item1 = item1;
Item2 = item2;
Item3 = item3;
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Tuple`3"/>.
/// </summary>
/// <param name="that">The <see cref="System.Object"/> to compare with the current <see cref="System.Tuple`3"/>.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
/// <see cref="System.Tuple`3"/>; otherwise, <c>false</c>.</returns>
public override bool Equals(object that)
{
return ((IStructuralEquatable) this).Equals(that, EqualityComparer<object>.Default);
}
/// <summary>
/// Serves as a hash function for a <see cref="System.Tuple`3"/> object.
/// </summary>
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
/// hash table.</returns>
public override int GetHashCode()
{
return ((IStructuralEquatable) this).GetHashCode(EqualityComparer<object>.Default);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="System.Tuple`3"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="System.Tuple`3"/>.</returns>
public override string ToString()
{
return "(" + ((ITuple) this).ToString() + ")";
}
bool IStructuralEquatable.Equals(object that, IEqualityComparer comparer)
{
if (that == null)
{
return false;
}
var thatTuple = that as Tuple<T1, T2, T3>;
if (thatTuple == null)
{
return false;
}
return comparer.Equals(Item1, thatTuple.Item1)
&& comparer.Equals(Item2, thatTuple.Item2)
&& comparer.Equals(Item3, thatTuple.Item3);
}
int IComparable.CompareTo(object that)
{
return ((IStructuralComparable) this).CompareTo(that, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo(object that, IComparer comparer)
{
if (that == null)
{
return 1;
}
var thatTuple = that as Tuple<T1, T2, T3>;
if (thatTuple == null)
{
return 1;
}
var c = 0;
c = comparer.Compare(Item1, thatTuple.Item1);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item2, thatTuple.Item2);
if (c != 0)
{
return c;
}
return comparer.Compare(Item3, thatTuple.Item3);
}
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
{
return Tuple.CombineHashCodes(
comparer.GetHashCode(Item1),
comparer.GetHashCode(Item2),
comparer.GetHashCode(Item3)
);
}
string ITuple.ToString()
{
return string.Format("{0}, {1}, {2}", Item1, Item2, Item3);
}
}
[Serializable]
public class Tuple<T1, T2, T3, T4> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
{
/// <summary>
/// Gets the item1.
/// </summary>
/// <value>The item1.</value>
public T1 Item1 { get; private set; }
/// <summary>
/// Gets the item2.
/// </summary>
/// <value>The item2.</value>
public T2 Item2 { get; private set; }
/// <summary>
/// Gets the item3.
/// </summary>
/// <value>The item3.</value>
public T3 Item3 { get; private set; }
/// <summary>
/// Gets the item4.
/// </summary>
/// <value>The item4.</value>
public T4 Item4 { get; private set; }
int ITuple.Size { get { return 4; } }
/// <summary>
/// Initializes a new instance of the <see cref="System.Tuple`4"/> class.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
/// <param name="item4">Item4.</param>
public Tuple(T1 item1, T2 item2, T3 item3, T4 item4)
{
Item1 = item1;
Item2 = item2;
Item3 = item3;
Item4 = item4;
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Tuple`4"/>.
/// </summary>
/// <param name="that">The <see cref="System.Object"/> to compare with the current <see cref="System.Tuple`4"/>.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
/// <see cref="System.Tuple`4"/>; otherwise, <c>false</c>.</returns>
public override bool Equals(object that)
{
return ((IStructuralEquatable) this).Equals(that, EqualityComparer<object>.Default);
}
/// <summary>
/// Serves as a hash function for a <see cref="System.Tuple`4"/> object.
/// </summary>
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
/// hash table.</returns>
public override int GetHashCode()
{
return ((IStructuralEquatable) this).GetHashCode(EqualityComparer<object>.Default);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="System.Tuple`4"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="System.Tuple`4"/>.</returns>
public override string ToString()
{
return "(" + ((ITuple) this).ToString() + ")";
}
bool IStructuralEquatable.Equals(object that, IEqualityComparer comparer)
{
if (that == null)
{
return false;
}
var thatTuple = that as Tuple<T1, T2, T3, T4>;
if (thatTuple == null)
{
return false;
}
return comparer.Equals(Item1, thatTuple.Item1)
&& comparer.Equals(Item2, thatTuple.Item2)
&& comparer.Equals(Item3, thatTuple.Item3)
&& comparer.Equals(Item4, thatTuple.Item4);
}
int IComparable.CompareTo(object that)
{
return ((IStructuralComparable) this).CompareTo(that, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo(object that, IComparer comparer)
{
if (that == null)
{
return 1;
}
var thatTuple = that as Tuple<T1, T2, T3, T4>;
if (thatTuple == null)
{
return 1;
}
var c = 0;
c = comparer.Compare(Item1, thatTuple.Item1);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item2, thatTuple.Item2);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item3, thatTuple.Item3);
if (c != 0)
{
return c;
}
return comparer.Compare(Item4, thatTuple.Item4);
}
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
{
return Tuple.CombineHashCodes(
comparer.GetHashCode(Item1),
comparer.GetHashCode(Item2),
comparer.GetHashCode(Item3),
comparer.GetHashCode(Item4)
);
}
string ITuple.ToString()
{
return string.Format("{0}, {1}, {2}, {3}", Item1, Item2, Item3, Item4);
}
}
[Serializable]
public class Tuple<T1, T2, T3, T4, T5> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
{
/// <summary>
/// Gets the item1.
/// </summary>
/// <value>The item1.</value>
public T1 Item1 { get; private set; }
/// <summary>
/// Gets the item2.
/// </summary>
/// <value>The item2.</value>
public T2 Item2 { get; private set; }
/// <summary>
/// Gets the item3.
/// </summary>
/// <value>The item3.</value>
public T3 Item3 { get; private set; }
/// <summary>
/// Gets the item4.
/// </summary>
/// <value>The item4.</value>
public T4 Item4 { get; private set; }
/// <summary>
/// Gets the item5.
/// </summary>
/// <value>The item5.</value>
public T5 Item5 { get; private set; }
int ITuple.Size { get { return 5; } }
/// <summary>
/// Initializes a new instance of the <see cref="System.Tuple`5"/> class.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
/// <param name="item4">Item4.</param>
/// <param name="item5">Item5.</param>
public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
{
Item1 = item1;
Item2 = item2;
Item3 = item3;
Item4 = item4;
Item5 = item5;
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Tuple`5"/>.
/// </summary>
/// <param name="that">The <see cref="System.Object"/> to compare with the current <see cref="System.Tuple`5"/>.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
/// <see cref="System.Tuple`5"/>; otherwise, <c>false</c>.</returns>
public override bool Equals(object that)
{
return ((IStructuralEquatable) this).Equals(that, EqualityComparer<object>.Default);
}
/// <summary>
/// Serves as a hash function for a <see cref="System.Tuple`5"/> object.
/// </summary>
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
/// hash table.</returns>
public override int GetHashCode()
{
return ((IStructuralEquatable) this).GetHashCode(EqualityComparer<object>.Default);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="System.Tuple`5"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="System.Tuple`5"/>.</returns>
public override string ToString()
{
return "(" + ((ITuple) this).ToString() + ")";
}
bool IStructuralEquatable.Equals(object that, IEqualityComparer comparer)
{
if (that == null)
{
return false;
}
var thatTuple = that as Tuple<T1, T2, T3, T4, T5>;
if (thatTuple == null)
{
return false;
}
return comparer.Equals(Item1, thatTuple.Item1)
&& comparer.Equals(Item2, thatTuple.Item2)
&& comparer.Equals(Item3, thatTuple.Item3)
&& comparer.Equals(Item4, thatTuple.Item4)
&& comparer.Equals(Item5, thatTuple.Item5);
}
int IComparable.CompareTo(object that)
{
return ((IStructuralComparable) this).CompareTo(that, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo(object that, IComparer comparer)
{
if (that == null)
{
return 1;
}
var thatTuple = that as Tuple<T1, T2, T3, T4, T5>;
if (thatTuple == null)
{
return 1;
}
var c = 0;
c = comparer.Compare(Item1, thatTuple.Item1);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item2, thatTuple.Item2);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item3, thatTuple.Item3);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item4, thatTuple.Item4);
if (c != 0)
{
return c;
}
return comparer.Compare(Item5, thatTuple.Item5);
}
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
{
return Tuple.CombineHashCodes(
comparer.GetHashCode(Item1),
comparer.GetHashCode(Item2),
comparer.GetHashCode(Item3),
comparer.GetHashCode(Item4),
comparer.GetHashCode(Item5)
);
}
string ITuple.ToString()
{
return string.Format("{0}, {1}, {2}, {3}, {4}", Item1, Item2, Item3, Item4, Item5);
}
}
[Serializable]
public class Tuple<T1, T2, T3, T4, T5, T6> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
{
/// <summary>
/// Gets the item1.
/// </summary>
/// <value>The item1.</value>
public T1 Item1 { get; private set; }
/// <summary>
/// Gets the item2.
/// </summary>
/// <value>The item2.</value>
public T2 Item2 { get; private set; }
/// <summary>
/// Gets the item3.
/// </summary>
/// <value>The item3.</value>
public T3 Item3 { get; private set; }
/// <summary>
/// Gets the item4.
/// </summary>
/// <value>The item4.</value>
public T4 Item4 { get; private set; }
/// <summary>
/// Gets the item5.
/// </summary>
/// <value>The item5.</value>
public T5 Item5 { get; private set; }
/// <summary>
/// Gets the item6.
/// </summary>
/// <value>The item6.</value>
public T6 Item6 { get; private set; }
int ITuple.Size { get { return 6; } }
/// <summary>
/// Initializes a new instance of the <see cref="System.Tuple`6"/> class.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
/// <param name="item4">Item4.</param>
/// <param name="item5">Item5.</param>
/// <param name="item6">Item6.</param>
public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
{
Item1 = item1;
Item2 = item2;
Item3 = item3;
Item4 = item4;
Item5 = item5;
Item6 = item6;
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Tuple`6"/>.
/// </summary>
/// <param name="that">The <see cref="System.Object"/> to compare with the current <see cref="System.Tuple`6"/>.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
/// <see cref="System.Tuple`6"/>; otherwise, <c>false</c>.</returns>
public override bool Equals(object that)
{
return ((IStructuralEquatable) this).Equals(that, EqualityComparer<object>.Default);
}
/// <summary>
/// Serves as a hash function for a <see cref="System.Tuple`6"/> object.
/// </summary>
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
/// hash table.</returns>
public override int GetHashCode()
{
return ((IStructuralEquatable) this).GetHashCode(EqualityComparer<object>.Default);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="System.Tuple`6"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="System.Tuple`6"/>.</returns>
public override string ToString()
{
return "(" + ((ITuple) this).ToString() + ")";
}
bool IStructuralEquatable.Equals(object that, IEqualityComparer comparer)
{
if (that == null)
{
return false;
}
var thatTuple = that as Tuple<T1, T2, T3, T4, T5, T6>;
if (thatTuple == null)
{
return false;
}
return comparer.Equals(Item1, thatTuple.Item1)
&& comparer.Equals(Item2, thatTuple.Item2)
&& comparer.Equals(Item3, thatTuple.Item3)
&& comparer.Equals(Item4, thatTuple.Item4)
&& comparer.Equals(Item5, thatTuple.Item5)
&& comparer.Equals(Item6, thatTuple.Item6);
}
int IComparable.CompareTo(object that)
{
return ((IStructuralComparable) this).CompareTo(that, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo(object that, IComparer comparer)
{
if (that == null)
{
return 1;
}
var thatTuple = that as Tuple<T1, T2, T3, T4, T5, T6>;
if (thatTuple == null)
{
return 1;
}
var c = 0;
c = comparer.Compare(Item1, thatTuple.Item1);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item2, thatTuple.Item2);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item3, thatTuple.Item3);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item4, thatTuple.Item4);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item5, thatTuple.Item5);
if (c != 0)
{
return c;
}
return comparer.Compare(Item6, thatTuple.Item6);
}
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
{
return Tuple.CombineHashCodes(
comparer.GetHashCode(Item1),
comparer.GetHashCode(Item2),
comparer.GetHashCode(Item3),
comparer.GetHashCode(Item4),
comparer.GetHashCode(Item5),
comparer.GetHashCode(Item6)
);
}
string ITuple.ToString()
{
return string.Format("{0}, {1}, {2}, {3}, {4}, {5}", Item1, Item2, Item3, Item4, Item5, Item6);
}
}
[Serializable]
public class Tuple<T1, T2, T3, T4, T5, T6, T7> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
{
/// <summary>
/// Gets the item1.
/// </summary>
/// <value>The item1.</value>
public T1 Item1 { get; private set; }
/// <summary>
/// Gets the item2.
/// </summary>
/// <value>The item2.</value>
public T2 Item2 { get; private set; }
/// <summary>
/// Gets the item3.
/// </summary>
/// <value>The item3.</value>
public T3 Item3 { get; private set; }
/// <summary>
/// Gets the item4.
/// </summary>
/// <value>The item4.</value>
public T4 Item4 { get; private set; }
/// <summary>
/// Gets the item5.
/// </summary>
/// <value>The item5.</value>
public T5 Item5 { get; private set; }
/// <summary>
/// Gets the item6.
/// </summary>
/// <value>The item6.</value>
public T6 Item6 { get; private set; }
/// <summary>
/// Gets the item7.
/// </summary>
/// <value>The item7.</value>
public T7 Item7 { get; private set; }
int ITuple.Size { get { return 7; } }
/// <summary>
/// Initializes a new instance of the <see cref="System.Tuple`7"/> class.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
/// <param name="item4">Item4.</param>
/// <param name="item5">Item5.</param>
/// <param name="item6">Item6.</param>
/// <param name="item7">Item7.</param>
public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
{
Item1 = item1;
Item2 = item2;
Item3 = item3;
Item4 = item4;
Item5 = item5;
Item6 = item6;
Item7 = item7;
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Tuple`7"/>.
/// </summary>
/// <param name="that">The <see cref="System.Object"/> to compare with the current <see cref="System.Tuple`7"/>.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
/// <see cref="System.Tuple`7"/>; otherwise, <c>false</c>.</returns>
public override bool Equals(object that)
{
return ((IStructuralEquatable) this).Equals(that, EqualityComparer<object>.Default);
}
/// <summary>
/// Serves as a hash function for a <see cref="System.Tuple`7"/> object.
/// </summary>
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
/// hash table.</returns>
public override int GetHashCode()
{
return ((IStructuralEquatable) this).GetHashCode(EqualityComparer<object>.Default);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="System.Tuple`7"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="System.Tuple`7"/>.</returns>
public override string ToString()
{
return "(" + ((ITuple) this).ToString() + ")";
}
bool IStructuralEquatable.Equals(object that, IEqualityComparer comparer)
{
if (that == null)
{
return false;
}
var thatTuple = that as Tuple<T1, T2, T3, T4, T5, T6, T7>;
if (thatTuple == null)
{
return false;
}
return comparer.Equals(Item1, thatTuple.Item1)
&& comparer.Equals(Item2, thatTuple.Item2)
&& comparer.Equals(Item3, thatTuple.Item3)
&& comparer.Equals(Item4, thatTuple.Item4)
&& comparer.Equals(Item5, thatTuple.Item5)
&& comparer.Equals(Item6, thatTuple.Item6)
&& comparer.Equals(Item7, thatTuple.Item7);
}
int IComparable.CompareTo(object that)
{
return ((IStructuralComparable) this).CompareTo(that, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo(object that, IComparer comparer)
{
if (that == null)
{
return 1;
}
var thatTuple = that as Tuple<T1, T2, T3, T4, T5, T6, T7>;
if (thatTuple == null)
{
return 1;
}
var c = 0;
c = comparer.Compare(Item1, thatTuple.Item1);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item2, thatTuple.Item2);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item3, thatTuple.Item3);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item4, thatTuple.Item4);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item5, thatTuple.Item5);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item6, thatTuple.Item6);
if (c != 0)
{
return c;
}
return comparer.Compare(Item7, thatTuple.Item7);
}
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
{
return Tuple.CombineHashCodes(
comparer.GetHashCode(Item1),
comparer.GetHashCode(Item2),
comparer.GetHashCode(Item3),
comparer.GetHashCode(Item4),
comparer.GetHashCode(Item5),
comparer.GetHashCode(Item6),
comparer.GetHashCode(Item7)
);
}
string ITuple.ToString()
{
return string.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}", Item1, Item2, Item3, Item4, Item5, Item6, Item7);
}
}
[Serializable]
public class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
{
/// <summary>
/// Gets the item1.
/// </summary>
/// <value>The item1.</value>
public T1 Item1 { get; private set; }
/// <summary>
/// Gets the item2.
/// </summary>
/// <value>The item2.</value>
public T2 Item2 { get; private set; }
/// <summary>
/// Gets the item3.
/// </summary>
/// <value>The item3.</value>
public T3 Item3 { get; private set; }
/// <summary>
/// Gets the item4.
/// </summary>
/// <value>The item4.</value>
public T4 Item4 { get; private set; }
/// <summary>
/// Gets the item5.
/// </summary>
/// <value>The item5.</value>
public T5 Item5 { get; private set; }
/// <summary>
/// Gets the item6.
/// </summary>
/// <value>The item6.</value>
public T6 Item6 { get; private set; }
/// <summary>
/// Gets the item7.
/// </summary>
/// <value>The item7.</value>
public T7 Item7 { get; private set; }
/// <summary>
/// Gets the rest.
/// </summary>
/// <value>The rest.</value>
public TRest Rest { get; private set; }
int ITuple.Size { get { return 7 + ((ITuple) Rest).Size; } }
/// <summary>
/// Initializes a new instance of the <see cref="System.Tuple`8"/> class.
/// </summary>
/// <param name="item1">Item1.</param>
/// <param name="item2">Item2.</param>
/// <param name="item3">Item3.</param>
/// <param name="item4">Item4.</param>
/// <param name="item5">Item5.</param>
/// <param name="item6">Item6.</param>
/// <param name="item7">Item7.</param>
/// <param name="rest">Rest.</param>
public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest)
{
if (!(rest is ITuple))
{
throw new ArgumentException("Tuple`8 last argument not Tuple");
}
Item1 = item1;
Item2 = item2;
Item3 = item3;
Item4 = item4;
Item5 = item5;
Item6 = item6;
Item7 = item7;
Rest = rest;
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Tuple`8"/>.
/// </summary>
/// <param name="that">The <see cref="System.Object"/> to compare with the current <see cref="System.Tuple`8"/>.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
/// <see cref="System.Tuple`8"/>; otherwise, <c>false</c>.</returns>
public override bool Equals(object that)
{
return ((IStructuralEquatable) this).Equals(that, EqualityComparer<object>.Default);
}
/// <summary>
/// Serves as a hash function for a <see cref="System.Tuple`8"/> object.
/// </summary>
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
/// hash table.</returns>
public override int GetHashCode()
{
return ((IStructuralEquatable) this).GetHashCode(EqualityComparer<object>.Default);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="System.Tuple`8"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="System.Tuple`8"/>.</returns>
public override string ToString()
{
return "(" + ((ITuple) this).ToString() + ")";
}
bool IStructuralEquatable.Equals(object that, IEqualityComparer comparer)
{
if (that == null)
{
return false;
}
var thatTuple = that as Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>;
if (thatTuple == null)
{
return false;
}
return comparer.Equals(Item1, thatTuple.Item1)
&& comparer.Equals(Item2, thatTuple.Item2)
&& comparer.Equals(Item3, thatTuple.Item3)
&& comparer.Equals(Item4, thatTuple.Item4)
&& comparer.Equals(Item5, thatTuple.Item5)
&& comparer.Equals(Item6, thatTuple.Item6)
&& comparer.Equals(Item7, thatTuple.Item7)
&& comparer.Equals(Rest, thatTuple.Rest);
}
int IComparable.CompareTo(object that)
{
return ((IStructuralComparable) this).CompareTo(that, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo(object that, IComparer comparer)
{
if (that == null)
{
return 1;
}
var thatTuple = that as Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>;
if (thatTuple == null)
{
return 1;
}
var c = 0;
c = comparer.Compare(Item1, thatTuple.Item1);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item2, thatTuple.Item2);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item3, thatTuple.Item3);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item4, thatTuple.Item4);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item5, thatTuple.Item5);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item6, thatTuple.Item6);
if (c != 0)
{
return c;
}
c = comparer.Compare(Item7, thatTuple.Item7);
if (c != 0)
{
return c;
}
return comparer.Compare(Rest, thatTuple.Rest);
}
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
{
return Tuple.CombineHashCodes(
comparer.GetHashCode(Item1),
comparer.GetHashCode(Item2),
comparer.GetHashCode(Item3),
comparer.GetHashCode(Item4),
comparer.GetHashCode(Item5),
comparer.GetHashCode(Item6),
comparer.GetHashCode(Item7),
((IStructuralEquatable) Rest).GetHashCode(comparer)
);
}
string ITuple.ToString()
{
return string.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}", Item1, Item2, Item3, Item4, Item5, Item6, Item7, ((ITuple) Rest).ToString());
}
}
}
using NUnit.Framework;
using System.Collections;
using HashTable = System.Collections.Hashtable;
[TestFixture]
public class TupleTest
{
[Test]
public void TestCreate1Tuple()
{
var actual = Tuple.Create<int>(1);
Assert.AreEqual(1, actual.Item1);
}
[Test]
public void TestCreate2Tuple()
{
var actual = Tuple.Create<int, string>(1, "hogehoge");
Assert.AreEqual(1, actual.Item1);
Assert.AreEqual("hogehoge", actual.Item2);
}
[Test]
public void TestCreate3Tuple()
{
var actual = Tuple.Create<bool, int, float>(false, -155, 255.34f);
Assert.AreEqual(false, actual.Item1);
Assert.AreEqual(-155, actual.Item2);
Assert.AreEqual(255.34f, actual.Item3);
}
[Test]
public void TestCreate4Tuple()
{
var hash = new Hashtable();
var actual = Tuple.Create<string, HashTable, double, long>("hogehoge", hash, -3.14159d, 10000L);
Assert.AreEqual("hogehoge", actual.Item1);
Assert.AreEqual(hash, actual.Item2);
Assert.AreEqual(-3.14159d, actual.Item3);
Assert.AreEqual(10000L, actual.Item4);
}
[Test]
public void TestCreate5Tuple()
{
var actual = Tuple.Create<int, int, int, int, int>(1, 2, 3, 4, 5);
Assert.AreEqual(1, actual.Item1);
Assert.AreEqual(2, actual.Item2);
Assert.AreEqual(3, actual.Item3);
Assert.AreEqual(4, actual.Item4);
Assert.AreEqual(5, actual.Item5);
}
[Test]
public void TestCreate6Tuple()
{
var actual = Tuple.Create<string, string, string, string, string, string>("a", "b", "c", "d", "e", "f");
Assert.AreEqual("a", actual.Item1);
Assert.AreEqual("b", actual.Item2);
Assert.AreEqual("c", actual.Item3);
Assert.AreEqual("d", actual.Item4);
Assert.AreEqual("e", actual.Item5);
Assert.AreEqual("f", actual.Item6);
}
[Test]
public void Create7Tuple()
{
var actual = Tuple.Create<string, string, string, string, string, string, bool>("a", "b", "c", "d", "e", "f", false);
Assert.AreEqual("a", actual.Item1);
Assert.AreEqual("b", actual.Item2);
Assert.AreEqual("c", actual.Item3);
Assert.AreEqual("d", actual.Item4);
Assert.AreEqual("e", actual.Item5);
Assert.AreEqual("f", actual.Item6);
Assert.AreEqual(false, actual.Item7);
}
[Test]
public void Create8Tuple()
{
var rest = Tuple.Create<object>(new object());
var actual = Tuple.Create<int, uint, float, double, long, bool, string, Tuple<object>>(int.MinValue, uint.MaxValue, float.MinValue, double.MaxValue, long.MaxValue, false, "piyopiyo", rest);
Assert.AreEqual(int.MinValue, actual.Item1);
Assert.AreEqual(uint.MaxValue, actual.Item2);
Assert.AreEqual(float.MinValue, actual.Item3);
Assert.AreEqual(double.MaxValue, actual.Item4);
Assert.AreEqual(long.MaxValue, actual.Item5);
Assert.AreEqual(false, actual.Item6);
Assert.AreEqual("piyopiyo", actual.Item7);
Assert.AreEqual(rest, actual.Rest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment