ImmutableSortedDictionary range enumeration by key - c#

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.

Related

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

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.

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.

Loop through T model properties. Building a search

I'm about to build a search page on a website and the search is going to be extensive with a couple of models and with each model having a lot of properties.
Is there a way to do this in a generic way or use reflector as I have seen in some posts? I need some pointers or tips on how to aproach this. Highly appreciate it.
You can use reflection to get the information you need. If you have a type T you can use
typeof(T).GetProperties()
to get all public properties. Same is possible for fields, methods, ... If you need more meta data to generate your search, you can use attributes to annotate the properties (or fields, methods, ...) That's the way I would get started. Further details depend on your exact use case.
Can you give more details?
What is the purpose of your search? Give me the 30 second version so I can understand where you are going with this.
Are you planning on using RegEx and word stemming?
What kinds of values count as matches?
I assume you only want to search properties on the objects/models. Right?
Do want to see every property or only some of them?
What kinds of data is stored in the properties? (string, byte[], enum, etc)
Brainstorming Ideas:
What about searching one the DB server-side instead of in your hydrated objects? It might be faster (at run-time) to leverage your DB than load all of the objects into memory then reflect upon them.
You could also write a method that supports your search within the context of the model itself. You pass in the search rule set as an expression then find the match. If you have some kind of a collection container, the search could be run at that level against all of the objects in the collection.
If you want some reflection code, I wrote something that shows a lot of info about an object via reflection. I wrote this a long while ago (Dec 2009). I'm not sure if it does what you want. Take a look. If it works for you, use it! (Link)

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.

Unique set of strings in C#

I have a list of strings, I need to be able to simply probe if a new string is in the table or not. When the list is large, testing a simple list directly is pretty inefficient... so typically I use a Dictionary to get constant lookup speeds, although I don't actually care about the value. This seems like a misuse of a dictionary, so I'm wondering what other approaches I could take.
Is there a better way to do hit testing that I am unaware of?
You should use a HashSet<string>, which is specifically designed for this purpose.
A HashSet is better suited than a Dictionary, for this purpose.

Categories

Resources