Grasshopper

algorithmic modeling for Rhino

I have used HashSets quite often in C# in order to get lists of unique values from a list like: 

Original List:

(1,1,2,3,4,5,5,5,5,6)

Set:

(1,2,3,4,5,6)

What i'm wondering is if it's possible to do something similar but with nested lists. For example, I have some panels. For each one, I have put their dimensions(lenght width) into a list and put that list into another list, so I have something like this:

[  [10,5], [10,6], [10,5],[10,7], [10,7] ]

What I want would be to extract unique pairs of information so that I would have:

[ [10,5], [10,6], [10,7] ]

Is this doable?

Views: 935

Replies to This Discussion

Easiest solution, convert your data to strings, then use use a regular hashset. Proper solution, create a new type which contains both width and length values, override the GetHashCode and Equals methods on that type (or maybe implement IComparable[T] ?)... anyway, do whatever you need to do to make hashsets understand your data.

Hi David,

It will take a little more self education to try out the "proper" solution, but the easy solution seams to do the trick for what I need. Very sneaky. Thanks for the help!

As far as I understand  David's solution, you'll end up with a hashset of strings rather than lists of integers. 

Another solution would be to use SortedList(of string, list(of integer)) .... That way you can create the string as in David's solution (the key) and check if it already exists in the SortedList (SortedList.keys.contains() as far as I remember). If it doesn't exist, then you can add this list to the SortedList (and  you never have to parse back).

Also as David mentioned - you might implement IComparable (so you can always ask a collection if Collection.Contains(your type instance)

The last solution which comes to my mind is to Overload the operators (=, <> or ==, !=)... that way you will be always able to compare 2 of your objects. Easiest IMHO, worst IMHO.

Hey,
U may want to try this:

public static class HashSetExtensions
{
public static HashSet<T> CreateSet<T>(this IEnumerable<T> toHash, Func<T, T, bool> Predicate)
{
return new HashSet<T>(toHash, new LambdaEquality<T>(Predicate));
}

public class LambdaEquality<T> : IEqualityComparer<T>
{
private readonly Func<T, T, bool> IsEqual;
public LambdaEquality(Func<T, T, bool> isEqual) { IsEqual = isEqual; }
public bool Equals(T x, T y) { return IsEqual(x, y); }
public int GetHashCode(T obj) { return -1; }
}
}

_________________________________________________________

public static void Test()
{
List<IEnumerable<int>> L = new List<IEnumerable<int>>();
L.Add(new int[] { 1, 2 });
L.Add(new int[] { 1, 6 });
L.Add(new int[] { 1, 1 });
L.Add(new int[] { 5, 6 });
L.Add(new int[] { 1, 2 });
L.Add(new int[] { 5, 6 });
L.Add(new int[] { 4, 2, 9 });
L.Add(new int[] { 1, 1 });
L.Add(new int[] { 5, 6 });

var ListSet = L.CreateSet((a, b) => a.SequenceEqual(b));

foreach (var s in ListSet)
{
foreach (var i in s)
Console.Write(i.ToString() + " ");
Console.WriteLine();
}
}



RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service