sets

Is there a "Set" data structure in .Net?

Ideally, I'm looking for a templated logical Set class. It would have all of the standard set operations such as Union, Intersection, Etc., and collapse duplicated items. I ended up creating my own set class based on the C# Dictionary<>- just using the Keys. ...

Elegant way to remove items from sequence in Python?

When I am writing code in Python, I often need to remove items from a list or other sequence type based on some criteria. I haven't found a solution that is elegant and efficient, as removing items from a list you are currently iterating through is bad. For example, you can't do this: for name in names: if name[-5:] == 'Smith': ...

How to retrieve an element from a set without removing it?

Suppose the following: >>>s = set([1, 2, 3]) How do I get a value (any value) out of s without doing s.pop()? I want to leave the item in the set until I am sure I can remove it - something I can only be sure of after an asynchronous call to another host. Quick and dirty: >>>elem = s.pop() >>>s.add(elem) But do you know of a bette...

Set operation in .NET C#

I'm working on a something related to roughset right now. The project uses alot of sets operation and manipulation. I've been using string operations as a stop gap measure for set operation. It has worked fine until we need to process some ungodly amount of data ( 500,000 records with about 40+ columns each ) through the algorithm. I ...

What is the best way to store set data in Python?

Hello Stack-Overflow, Here is my situation. I have a list of data that looks like this: [(id__1_, description, id_type), (id__2_, description, id_type), ... , (id__n_, description, id_type)) The data is loaded from multiple files that all belong to the same grouping. In each grouping there could be multiples of the same id, each comin...

Classical set operations for java.util.Collection

Is there any built-in functionality for classical set operations on the java.util.Collection class? My specific implementation would be for ArrayList, but this sounds like something that should apply for all subclasses of Collection. I'm looking for something like: ArrayList<Integer> setA ... ArrayList<Integer> setB ... ArrayList<Intege...

SQL Cursors...Any use cases you would defend?

I'll go first. I'm 100% in the set-operations camp. But what happens when the set logic on the entire desired input domain leads to a such a large retrieval that the query slows down significantly, comes to a crawl, or basically takes infinite time? That's one case where I'll use a itty-bitty cursor (or a while loop) of perhaps most d...

Updating an object within a Set

Hey, Let's say I have this type in my application: public class A { public int id; public B b; public boolean equals(Object another) { return this.id == ((A)another).id; } public int hashCode() { return 31 * id; //nice prime number } } and a Set<A> structure. Now, I have an object of type A and want to do the following: If...

Partition a list of sets by shared elements

Here's the jist of the problem: Given a list of sets, such as: [ (1,2,3), (5,2,6), (7,8,9), (6,12,13), (21,8,34), (19,20) ] Return a list of groups of the sets, such that sets that have a shared element are in the same group. [ [ (1,2,3), (5,2,6), (6,12,13) ], [ (7,8,9), (21,8,34) ], [ (19,20) ] ] Note the stickeyness - the set (6,...

C# Set collection?

Does anyone know if there is a good equivelent to Java's Set collection in C#. It's one of the few things I miss from having moved to C#. I am aware that you can have a "pretend" set using a Dictionary or Hashtable and only bothering to populate the keys. But it's not very elegant. It's wierd every other way I turn in C# somone seems...

Is there any kind of hashCode function in javascript?

Basically what I'm trying to do is create an object of unique objects, a set. I had the brilliant idea of just using a JS object with objects for the property names. Such as, set[obj] = true; This works, to a point. It works great with string and numbers, but with other objects, they all seem to "hash" to the same value and access the ...

Using set.insert( key ) as a conditional?

I am trying to use set.insert (key) as a conditional, where if the key is inserted correctly (meaning that the key does NOT already exsist in the set ) then it should go on and perform some kind of code. For example, something like: if (set.insert( key ) { // some kind of code } Is this allowed? Because the compiler is throw...

STL sorted set where the conditions of order may change

I have a C++ STL set with a custom ordering defined. The idea was that when items get added to the set, they're naturally ordered as I want them. However, what I've just realised is that the ordering predicate can change as time goes by. Presumably, the items in the set will then no longer be in order. So two questions really: Is i...

How do I fill a Delphi set?

If I have a type defined as a set of an enumerated type, it's easy to create an empty set with [], but how do I create a full set? EDIT: Yeah, the obvious solution is to use a for loop. That's also a really bad solution if there's another way. Does anyone know of a way that'll work in constant time? ...

MySQL - Select from a list of numbers those without a counterpart in the id field of a table

I have a list of numbers, say {2,4,5,6,7} I have a table, foos, with foos.ID, including say, {1,2,3,4,8,9} Id like to take my list of numbers, and find those without a counterpart in the ID field of my table. One way to achieve this would be to create a table bars, loaded with {2,4,5,6,7} in the ID field. Then, I would do SELECT bar...

Running time of set union operation

Given two sets A and B, what is the common algorithm used to find their union, and what is it's running time? My intuition: a = set((1, 2, 3)) b = set((2, 3, 5)) union = set() for el in a: union.add(el) for el in b: union.add(el) Add checks for a collision, which is O(1), and then adds the element, which is (??). This is do...

What's the best way to store a Delphi set in a dataset?

The title pretty much says it all. I'm using a TClientDataset to store an array of objects, and one of the objects has a member defined as a set of an enumerated type. As I understand it, Delphi sets are bitfields whose size can vary from 1 to 32 bytes depending on how much data they contain, and Delphi doesn't define a TSetField. What...

Representing sparse integer sets?

What is a good way to represent sparse set of integers (really C memory addresses) in a compact and fast way. I already know about the obvious things like bit-vectors and run-length encoding. but I want something much more compact than one word per set element. I need to add and remove elements and test for membership. I do not need othe...

Generating sets of integers in C#

In F#, you can generate a set of numbers, just by saying [1..100]. I want to do something similar in C#. This is what I have come up with so far: public static int[] To(this int start, int end) { var result = new List<int>(); for(int i = start; i <= end; i++) result.Add(i); return result.ToArray(); } By doing thi...

Java: Is there an easy, quick way to AND, OR, or XOR together sets?

That is, if I had two or more sets, and I wanted to return a new set containing either: All of the elements each set has in common (AND). All of the elements total of each set (OR). All of the elements unique to each set. (XOR). Is there an easy, pre-existing way to do that? Edit: That's the wrong terminology, isn't it? ...