I am looking at System.Collections.Specialized.NameValueCollection and it takes an IEqualityComparer, which is good news if someone like me wanted to sort the items in the collection by, say, something like the alphabetical order of the keys.
But, on a closer look in Reflector, I don't see the NVC class actually using the IEqualityComparer anywhere. Is my observation correct? As in, I don't see any use for an IEqualityComparer in an enumerable entity other than to re-arrange its constituent items on some cardinal order. And I do not see a method on NVC that looks like it might want to do something of that sort (Sort, Arrange, OrderBy, Aggregate, etc.).
NameValueCollection uses a Hashtable internally to store the values. The Hashtable uses the IEqualityComparer to hash and compare keys for equality.
Also note that IEqualityComparer doesn't do any ordering, it only compares for equality (as the name implies) so it's not useful if you want to sort values/keys.
Related
I have a Matlab struct:
a(1).x=54.23; a(1).y=2.3; a(1).col=32.221; a(1).id=1;
a(2).x=5.23; a(2).y=3.3; a(2).col=2.221; a(2).id=2;
... and so on. Now i want to access the struct in a having id 73. I can think of doing a for loop but the thing is i have to access elements of array a several times like this based on id. Wat is the fastest data structure available for this purpose? Python like dictionary may work but i am not sure ow to implement it. Pointing out some code examples would be very helpful.
Try this:
id=[a.id];
a(id==73)
It's not as efficient as a dictionary, but if it's fast enough for your purposes it's not worth looking further.
The a.id part evaluates to a comma-separated list of id values, which are concatenated in an array that you can then use for lookup.
Is there a way through hashes or bitwise operators or another algorithm to avoid using database when simply checking for previously appeared string or value?
Assuming, there is no way to store whole history of the strings appeared before, only little information can be stored.
You may be interested in Bloom filters. They don't let you authoritatively say, "yes, this value is in the set of interest", but they do let you say "yes, this value is probably in the set" vs. "no, this value definitely is not in the set". For many situations, that's enough to be useful.
The way it works is:
You create an array of Boolean values (i.e. of bits). The larger you can afford to make this array, the better.
You create a bunch of different hash functions that each take an input string and map it to one element of the array. You want these hash functions to be independent, so that even if one hash function maps two strings to the same element, a different hash function will most likely map them to different elements.
To record that a string is in the set, you apply each of your hash functions to it in turn — giving you a set of elements in the array — and you set all of the mapped-to elements to TRUE.
To check if a string is (probably) is in the set, you do the same thing, except that now you just check the mapped-to elements to see if they are TRUE. If all of them are TRUE, then the string is probably in the set; otherwise, it definitely isn't.
If you're interested in this approach, see https://en.wikipedia.org/wiki/Bloom_filter for detailed analysis that can help you tune the filter appropriately (choosing the right array-size and number of hash functions) to get useful probabilities.
How can I write a NSPredicate that will check if all the elements in an NSArray are the same? I need this for a more complex predicate that I am using to filter NSTableView.
NSPredicate can't tell you if all the items are the same, but you could use it to filter an array for objects that are not equal to object[0]. This wouldn't be particularly performant since it would have to check the entire contents of the array.
You could instead use - (void)enumerateObjectsWithOptions: then bailing out when you find a different object via *stop = YES.
Consider looking at using NSSet, since that is designed to store a collection of unique objects (albeit unordered).
Without knowing more detail it's impossible to say what is the best solution for you.
In a C99+SDL game, I have an array that contains sound effects (SDL_mixer chunk data and some extra flags and filename string) and is referenced by index such as "sounds[2].data".
I'd like to be able to call sounds by filename, but I don't want to strcmp all the array until a match is found. This way as I add more sounds, or change the order, or allow for player-defined sound mods, they can still be called with a common identifier (such as "SHOT01" or "EXPL04").
What would be the fastest approach for this? I heard about hashing, which would result in something similar to lua's string indexes (such as table["field"]) but I don't know anything about the topic, and seems fairly complicated.
Just in case it matters, I plan to have filenames or labels be from 6 to 8 all caps filenames (such as "SHOT01.wav").
So to summarize, where can I learn about hashing short strings like that, or what would be the fastest way to keep track of something like sound effects so they can be called using arbitrary labels or identifiers?
I think in your case you can probably just keep all the sounds in a sorted data structure and use a fast search algorithm to find matches. Something like a binary search is very simple implement and it gives good performance.
However, if you are interested in hash tables and hashing, the basics of it all are pretty simple. There is no place like Wikipedia to get the basics down and you can then tailor your searches better on Google to find more in depth articles.
The basics are you start out with a fixed size array and store everything in there. To figure out where to store something you take the key (in your case the sound name) and you perform some operation on it such that it gives you an exact location where the value can be found. So the simplest case for string hashing is just adding up all the letters in the string as integer values then take the value and use modulus to give you an index in your array.
position = SUM(string letters) % [array size]
Of course naturally multiple strings will have same sum and thus give you the same position. This is called a collision, and collisions can be handled in many ways. The simplest way is to have an array of lists rather than array of values, and simply append to the list every there there is a collision. When searching for a value, simply iterate the lists and find the value you need.
Ideally a good hashing algorithm will have few collisions and quick hashing algorithm thus providing huge performance boost.
I hope this helps :)
You are right, when it comes to mapping objects with a set of string keys, hash tables are often the way to go.
I think this article on wikipedia is a good starting point to understand hash table mechanism: http://en.wikipedia.org/wiki/Hash_table
I have an array which is dynamic, and key and values are displaying in order if they are set. I want to display keys of this array in my own order, What kind of condition must I write in this case?
Take a look at the Core Utility Libraries, namely the sort() method of the Set class. Check for more info the cookbook.