Tuples
Tuples are not entirely trivial. There is variance in the features that are needed in different situations.
In this package there are tuples that come from the permutations of the following features.
Value-Typed and Reference-Typed
There are value-typed struct versions and reference-typed class versions of each tuple. Reference-typed tuples have suffix Object, for example "PairObject".
// Stack allocated
var pair_stack = new Pair<int, int>(1, 2);
// Heap allocated
var pair_heap = new PairObject<int, int>(1, 2);
Mutability
Tuples that are mutable have suffix Mutable. Immutable tuples don't have a suffix. Immutable tuples cache their hashcode, and mutable calculate as needed.
// Immutable
var pair_immutable = new Pair<int, int>(1, 2);
int hash_is_cached = pair_immutable.GetHashCode();
// Mutable
var pair_mutable = new PairMutable<int, int>(2, 3);
pair_mutable.a = 4;
pair_mutable.b = 6;
int hash_is_calculated = pair_mutable.GetHashCode();
Set Tuples
The elements of set tuples are interchangeable, the order of elements doesn't matter in hash-equals comparisons. That means that PairSet(1, 2) is equal to PairSet(2, 1). The name suffix is "Set", for example "PairSet".
// Create pairs
var pair1 = new PairSet<int>(1, 2);
var pair2 = new PairSet<int>(2, 1);
// Get equality comparer singleton instance
IEqualityComparer<PairSet<int>> equalsComparer = PairSet<int>.EqualityComparer.Default;
// Compare
bool areEqual = equalsComparer.Equals(pair1, pair2);
EqualityComparer
Each tuple class comes with an EqualityComparer implementation. There is a singleton instance TupleType.EqualityComparer.Default.
// Create pairs
var pair1 = new Pair<int, int>(1, 2);
var pair2 = new Pair<int, int>(2, 3);
// Get equality comparer singleton instance
IEqualityComparer<Pair<int, int>> equalsComparer = Pair<int, int>.EqualityComparer.Default;
// Compare
bool areEqual = equalsComparer.Equals(pair1, pair2);
Tuple types implement IEquatable<TupleType> any way, so they comparable as is. This makes value typed tuples comparable without boxing.
// Compare
bool areEqual = pair1.Equals(pair2);
Comparer
Each non-Set tuple class comes with a Comparer implementation. There is a singleton instance TupleType.Comparer.Default.
// Create pairs
var pair1 = new Pair<int, int>(1, 2);
var pair2 = new Pair<int, int>(2, 3);
// Get equality comparer singleton instance
IComparer<Pair<int, int>> comparer = Pair<int, int>.Comparer.Default;
// Compare
int c = comparer.Compare(pair1, pair2);
Tuple types implement IComparable<TupleType> any way, so they comparable as is. This makes value typed tuples comparable without boxing.
// Compare
int c = pair1.CompareTo(pair2);
Argument Count
There is a separate tuple type for cases with different number of arguments. The stem of the name is based on the count.
- Container
- Pair
- Triple
- Quad
- Pento
- Sextim
- Septim
- Octet
// One argument
var container = new ContainerMutableObject<string>("Hello");
container.a = "New Hello";
// Two arguments
var pair = new PairMutable<int, int>(1, 2);
// Three arguments
var triple = new TripleSet<int>(5, 6, 7);
// Four arguments
var quad = new QuadObject<byte, byte, byte, byte>(1, 2, 3, 4);
Classes
Name | Type | Hashcode | Mutability | Elements Ordered |
---|---|---|---|---|
Container<A> | struct | cached | immutable | are ordered |
ContainerObject<A> | class | cached | immutable | are ordered |
ContainerMutable<A> | struct | not cached | mutable | are ordered |
ContainerMutableObject<A> | class | not cached | mutable | are ordered |
Pair<A, B> | struct | cached | immutable | are ordered |
PairObject<A, B> | class | cached | immutable | are ordered |
PairMutable<A, B> | struct | not cached | mutable | are ordered |
PairMutableObject<A, B> | class | not cached | mutable | are ordered |
PairSet<T> | struct | cached | immutable | are interchangeable |
PairSetObject<T> | class | cached | immutable | are interchangeable |
PairSetMutable<T> | struct | not cached | mutable | are interchangeable |
PairSetMutableObject<T> | class | not cached | mutable | are interchangeable |
Triple<A, B, C> | struct | cached | immutable | are ordered |
TripleObject<A, B, C> | class | cached | immutable | are ordered |
TripleMutable<A, B, C> | struct | not cached | mutable | are ordered |
TripleMutableObject<A, B, C> | class | not cached | mutable | are ordered |
TripleSet<T> | struct | cached | immutable | are interchangeable |
TripleSetObject<T> | class | cached | immutable | are interchangeable |
TripleSetMutable<T> | struct | not cached | mutable | are interchangeable |
TripleSetMutableObject<T> | class | not cached | mutable | are interchangeable |
Quad<A, B, C, D> | struct | cached | immutable | are ordered |
QuadObject<A, B, C, D> | class | cached | immutable | are ordered |
QuadMutable<A, B, C, D> | struct | not cached | mutable | are ordered |
QuadMutableObject<A, B, C, D> | class | not cached | mutable | are ordered |
QuadSet<T> | struct | cached | immutable | are interchangeable |
QuadSetObject<T> | class | cached | immutable | are interchangeable |
QuadSetMutable<T> | struct | not cached | mutable | are interchangeable |
QuadSetMutableObject<T> | class | not cached | mutable | are interchangeable |
Pento<A, B, C, D, E> | struct | cached | immutable | are ordered |
PentoObject<A, B, C, D, E> | class | cached | immutable | are ordered |
PentoMutable<A, B, C, D, E> | struct | not cached | mutable | are ordered |
PentoMutableObject<A, B, C, D, E> | class | not cached | mutable | are ordered |
Sextim<A, B, C, D, E, F> | struct | cached | immutable | are ordered |
SextimObject<A, B, C, D, E, F> | class | cached | immutable | are ordered |
SextimMutable<A, B, C, D, E, F> | struct | not cached | mutable | are ordered |
SextimMutableObject<A, B, C, D, E, F> | class | not cached | mutable | are ordered |
Septim<A, B, C, D, E, F, G> | struct | cached | immutable | are ordered |
SeptimObject<A, B, C, D, E, F, G> | class | cached | immutable | are ordered |
SeptimMutable<A, B, C, D, E, F, G> | struct | not cached | mutable | are ordered |
SeptimMutableObject<A, B, C, D, E, F, G> | class | not cached | mutable | are ordered |
Octet<A, B, C, D, E, F, G, H> | struct | cached | immutable | are ordered |
OctetObject<A, B, C, D, E, F, G, H> | class | cached | immutable | are ordered |
OctetMutable<A, B, C, D, E, F, G, H> | struct | not cached | mutable | are ordered |
OctetMutableObject<A, B, C, D, E, F, G, H> | class | not cached | mutable | are ordered |