What is the C# equivalent of the stl set?
You could use an HashSet
The
HashSet<T>
class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
The capacity of a HashSet<T>
object is the number of elements that the object can hold. A HashSet<T>
object's capacity automatically increases as elements are added to the object.
If you require sorted set, use
SortedDictionary<T,U>
. This is implemented using a binary search tree. Admittedly, you will be using 64-bits per entry because you are storing a key-value pair underneath. You can write a wrapper around it like this:class Set<T> : SortedDictionary<T, bool> { public void Add(T item) { this.Add(item, true); } }
If you don't require a sorted set, use
HashSet<T>
.Otherwise, check out C5 Generic Collection Library. In particular
TreeSet<T>
. It is a red-black tree and only stores the values.
Try RedBlackTree.NET. It's in VB but I think it can be easily converted to C#.
And I believe some of the collection type actually uses a red-black tree internally. So you might want to decompile the framework itself and look around for some clues.
I don't think a binary tree can be replaced by a HashSet. Their performance characteristics are different, roughly:
HashSet - O(1) lookup (n) search
Binary search tree - O(log n) lookup O(log n) search
If you want to store the values and later perform a search, you will want to be using a binary tree instead of a HashSet.