Do C# dictionaries use hashing in the same manner as Java HashMaps? - c#

I understand that Java HashMap elements are stored in "buckets" based on the hashes of the elements' keys. Does that same hashing occur in C# dictionaries? If not, then how does lookup work?

There are several kind of Dictionarys inside C# System.Collections - namespace. They use different strategies to store theire internal data:
This one System.Collections.Specialized.HybridDictionary
uses DoubleLinkedLists until a certain size is reached, then switches to Hashsets. The "normal" System.Collections.Generics-Dictionary uses Hashsets internally all the time. There is also a Dictionary for concurrent uses - look it up yourself (Concurrent Dictionary)if you like.
So it depends on what kind of Dictionary you are using and (in above case) might change due to internal considerations of the class you are using for performance or other reasons.

Related

ImmutableSortedDictionary range enumeration by key

I was reading about C#'s ImmutableSortedDictionary in System.Collections.Immutable and thinking about how to apply it in my program. I quite like C++'s lower_bound and upper_bound (see here), and I was rather expecting to see something of the sort for range lookups. However, similar methods seem to be strangely absent from the documentation. Am I missing something? Or does MS truly provide a sorted dictionary without efficient access to the sorted ranges? That doesn't exactly seem like something one could do on an IEnumerable of the keys as say an extension method, so I'm a bit puzzled I'm not seeing something provided directly by the collection.
It is irritating that the available built-in collections are not offering a full set of features (like the SortedDictionary lacking a BinarySearch method), forcing us to search for third-party solutions (like the C5 library).
In your case instead of an ImmutableSortedDictionary you could probably use a ImmutableSortedSet, embedding the values in the keys and using an appropriate comparer. At least the API of this class contains the properties Min and Max.

Best structure for a growing collection of objects in C#?

For my Game Programming class, I am designing a class for a player character. The player can collect power-ups throughout the game, which are held onto and can be used whenever the player chooses.
I thought a dynamic array would work well, but my background is in C++ (we are using Unity in class, which uses Java & C#), and I know that memory deallocation is handled differently in C# (I know there is a garbage collector, but don't really know much about how it functions). After looking around the web a while, I couldn't find anything that seemed to fit the functionality I need (or if I did it was over my head and I didn't realize it).
If someone can list a C# structure, or structures, that would be good for storing a growing collection of objects, it would be extremely helpful.
List is probably the simplest structure you could use, it is like a dynamic array which will automatically grow as you add things to it. It is strongly typed so it will only contain objects of the same type (if you have some interface for your power-ups you can have a list of IPowerUp instances)
Start by looking at .NET's generic collections. (.NET generics are similar in concept to C++ templates.) Either List or Dictionary will likely be useful to you depending on how you need to store and retrieve your objects.
Since you'll probably have types of objects you may consider using a Dictionary of Lists where the key will be a string identifier or an enumeration of types of collected objects and the value will be a list of object instances.

Fast insert container

I need a container where insert is fast and thread-safe, because I plan to use it inside a Parallel.for_each instance.
Once in a while, I will scan said container and remove every items contained.
What's the best choice given those costraints?
Thanks
You could use a ConcurrentBag<T>. Basically the System.Collections.Concurrent namespace is worth checking. If you have unique keys, a ConcurrentDictionary<TKey, TValue> would be a great choice as it provides you a very fast access to elements given a key.
There are a bunch of concurrent collections in .NET 4.0: dictionary, queue, etc. See http://msdn.microsoft.com/en-us/library/dd997305.aspx
try ConcurrentBag - it is thread-safe and very fast since most operations are implemented lock-free... there are also ConcurrentDictionary etc. so I am not sure which features you exactly need.
Linked List. That has fast insertion, though I'm not sure if a threadsafe version exists in .NET

System.Linq.Lookup vs. Wintellect.PowerCollections.MultiDictionary

I still use Wintellect's PowerCollections library, even though it is aging and not maintained because it did a good job covering holes left in the standard MS Collections libraries. But LINQ and C# 4.0 are poised to replace PowerCollections...
I was very happy to discover System.Linq.Lookup because it should replace Wintellect.PowerCollections.MultiDictionary in my toolkit. But Lookup seems to be immutable! Is that true, can you only created a populated Lookup by calling ToLookup?
Yes, you can only create a Lookup by calling ToLookup. The immutable nature of it means that it's easy to share across threads etc, of course.
If you want a mutable version, you could always use the Edulinq implementation as a starting point. It's internally mutable, but externally immutable - and I wouldn't be surprised if the Microsoft implementation worked in a similar way.
Personally I'm rarely in a situation where I want to mutate the lookup - I would prefer to perform appropriate transformations on the input first. I would encourage you to think in this way too - I find myself wishing for better immutability support from other collections (e.g. Dictionary) more often than I wish that Lookup were mutable :)
That is correct. Lookup is immutable, you can create an instance by using the Linq ToLookup() extension method. Technically even that fact is an implementation detail since the method returns an ILookup interface which in the future might be implemented by some other concrete class.

How are elements stored in containers in .Net?

How are elements stored in containers in .Net?
For example, a C++ vector stores in sequential order, while List doesn't.
How are they implemented for .Net containers (Array, ArrayList, ...)?
Thanks.
It depends on the element. But a C++ Vector is equivalent to a C# List, and a C++ List<T> is equivalent to a C# LinkedList
The C# ArrayList is pretty much, a C# List<object>
Wikipedia lists many data structures, and I suggest you have a look there, to see how the different ones are implemented.
So:
C++ C# How
Vector ArrayList / List Array (sequential)
List LinkedList Linked List (non-sequential, i.e. linked)
It varies by container. Because the implementation is proprietary, there are no public specs mandating what the underlying data structure must be.
If you're interested in taking the time, I've used the following tools before to understand how MS implemented this class or that:
Debugging with the Microsoft .NET Framework debugging symbols.
Inspecting assemblies with Reflector.
In .net, containers (even arrays) handle all access to them. You don't use pointers to work with them (except in extremely rare cases you probably won't ever get into), so it often doesn't matter how they store info. In many cases, it's not even specified how things work behind the scenes, so the implementation can be changed to something "better" without breaking stuff that relies on those details for some stupid reason.
Last i heard, though, arrays store their entries sequentially -- with the caveat that for reference-type objects (everything that's not a struct), "entries" are the references as opposed to the objects themselves. The data could be anywhere in memory. Think of it more like an array of references than an array of objects.
ArrayLists, being based on arrays, should store their stuff the same way.

Categories

Resources