Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing pair of ints on the list

Tags:

How can I store pairs of integers in a List? I know I could make a class for them like:

class Pair   {     int i1,i2; } 

But if I do that I'm not able to use the Contains function to check if a given pair is in the list. How can I do that so I can easily store integers in a List and check if a pair of integers already exists? I cannot use table because it is not known how many pairs there will be.

EDIT:
Forgot to add: In my program pairs (x, y) and (y, x) are to be treated as equals.

EDIT:
(x,y) and (y,x) are equals while checking if Point is in the list, but x and y can not be swapped because x and y represent a connection between two points (integer is id of point, and no I can't use any reference etc...). When I'm checking if List contains a connection it is not important if it is (x,y) or (y,x) but later I would need that information.

like image 750
Miko Kronn Avatar asked Dec 04 '10 16:12

Miko Kronn


People also ask

How to make list of pair in Python?

Practical Data Science using PythonInitialize the lists with elements. Iterate over the lists and append the pair into a list if the corresponding elements from the lists are not same. Print the result.

Can we store pair in stack in Java?

Can we store pair in stack? You can store any type of data in Stack such as int, string, pairs, char, etc.

Are lists passed by value in C#?

Your List is an object created on heap. The variable myList is a reference to that object. In C# you never pass objects, you pass their references by value.


2 Answers

If you're using .NET 4.0, you could use the Tuple class as in

var tuple = new Tuple<int, int>(17, 42); var otherTuple = Tuple.Create(17, 42); 

and

var list = new List<Tuple<int, int>>(); 

Note that if you go the route of using Tuple<int, int> then you will need to create a custom implementation of IEqualityComparer<Tuple<TFirst, TSecond>> to reflect your equality rules that (x, y) be considered equal to (y, x). You will then have to pass an instance of this comparer to List<T>.Contains(T, IEqualityComparer<T>) (here T is Tuple<int, int> for you).

class TupleAsUnorderedPairComparer : IEqualityComparer<Tuple<TFirst, TSecond>> {     public bool Equals(Tuple<TFirst, TSecond> x, Tuple<TFirst, TSecond> y) {         if(Object.ReferenceEquals(x, y)) {             return true;         }         if(x == null || y == null) {             return false;         }         return x.Item1 == y.Item1 && x.Item2 == y.Item2 ||                x.Item1 == y.Item2 && x.Item2 == y.Item1;     }      public int GetHashCode(Tuple<TFirst, TSecond> x) {         if(x == null) {             return 0;         }         return x.Item1.GetHashCode() ^ x.Item2.GetHashCode();     } } 

Otherwise, if you can't or don't want to use Tuple then you will need to implement an IEqualityComparer<Pair> for your Pair class or override Object.Equals and Object.GetHashCode.

class Pair {     public int First { get; private set; }     public int Second { get; private set; }     public Pair(int first, int second) {         this.First = first;         this.Second = second;     }      public override bool Equals(object obj) {         if(Object.ReferenceEquals(this, obj)) {             return true;         }         Pair instance = obj as Pair;         if(instance == null) {             return false;         }         return this.First == instance.First && this.Second == instance.Second ||                this.First == instance.Second && this.Second == instance.First;     }      public override int GetHashCode() {         return this.First.GetHashCode() ^ this.Second.GetHashCode();     } } 

and

class PairEqualityComparer : IEqualityComparer<Pair> {     // details elided  } 

If you use

list.Contains(pair); 

then it will use Equals and GetHashCode but if you use

list.Contains(pair, new PairEqualityComparer); 

then it will use PairEqualityComparer.Equals and PairEqualityComparer.GetHashCode. Note that these could be different than your implementations of Object.Equals and Object.GetHashCode.

Finally, if testing for containment is something that you'll be doing often then a List is not your best bet; you should use a class designed for that purpose like a HashSet.

like image 60
jason Avatar answered Oct 04 '22 04:10

jason


The class is your best bet. If you're dead set on using the Contains method, you'll have to implement the IComparable interface in your Pair class. This will allow you to establish what "equality" means for this pair of integers.

The simplest way would be to create the class as you have and then create and extension method on the List<T> object.

public static bool ContainsIntegers(this List<Pair> targetList, Pair comparer) {     foreach(Pair pair in targetList)     {         if(pair.i1 == comparer.i1 && pair.i2 == comparer.i2) return true;     }     return false; } 
like image 24
Joel Etherton Avatar answered Oct 04 '22 02:10

Joel Etherton