Fuzzy NSDictionary - use nearest key - objective-c

I have a list of around 4000 numbers e.g: {10, 20, 30, 40, 50,...}
Each number is a key in an NSDictionary, so I can fetch the object associated with a number, e.g.
[NSDictionary objectForKey:[NSNumber numberWithInt:20];
However if the key is not in the dictionary, I'd like to find the nearest key (assuming there is a meaningful relationship between values, in my example, 10>20>30 etc).
So e.g.
[NSDictionary objectForKey:[NSNumberWithInt:19]] would return the value for key:20.
Or is there another data structure that would be more appropriate for doing this? I'd thought of using a sorted NSArray, where the key would be the array index, then if object was null keep incrementing the array pointer until the object is found, however this would result in a sparsly populated array with 999,999 elements :)
Thanks

Essentially you need to keep a sorted list (NSMutableArray) of keys. To find a key use the indexOfObject:inSortedRange:options:usingComparator: method of NSArray passing in NSBinarySearchingInsertionIndex as the options which will perform a binary search giving you an index even if it doesnt find the exact element. You'll have to fetch both keys yourself and compare them.

Assuming your list of numbers is in ascending order you could do a binary search in the array.
So when looking for the key x, you would start at index array.length/2, compare the key at that position with x and continue with the left part if it was greater than x or with the right part if it was less than x. Continue until you've found the closest key to x.
Thats very fast (in your case about log(4000) ~ 12 array lookups) and does not need additional storage.

Related

Using arc4rand() generate a ordered list

Using int rand=((arc4random()%4)+1);
how would you make a sorted list based on the numbers generated which have to be clicked in order .Example if 4 2,3,1 was generated you have to click images based on those numbers ?
You could keep those integers [x,y,z] in a mutable array. and if you want the user to click x then y then z (indexing ascending order) then you could check the object at index 0 and see if it matches your image (I'm assuming you can map an integer to your image somehow). If it does, remove the object at index 0 from the list, if it doesn't, tell the user they have failed.
When the list is empty you know they have succeeded. you should check this after ever time your remove index 0.

Is there a way to find only part of the key value in a dictionary object?

There a dictionary object that gets loaded with the following key values:
119
189a
189b
189c
197
201a
201b
In most situations, life is good and all the individual key values are needed/unique. But in certain situations, the keys with letters behind them (ie...189a, 189b, 189c) all mean the same thing (ie...189). So I need a way to see if a key value exists (like the containskey method) for only the first part of the key and then return true.
Any ideas on how to accomplish this?
Since you only sometimes need ignore the suffixed letter, for the greatest efficiency, I would recommend using an additional HashSet(T) to store the numeric portion. When you add/remove elements from your dictionary, also add/remove the numeric from the HashSet(T). HashSet(T).Contains method is O(1), so checking to see if an element exists will be quick.
Something like this?
dictionary.Keys.Any(Function(key) key.StartsWith("189"))
or you can use a Regex for more find-grained control:
dictionary.Keys.Any(Function(key) Regex.IsMatch(key, "^189[^\d]?")

Core Data creating NSPredicate for NSNumber using something akin to BEGINSWITH

I am trying to retrieve results from search bar text in Core Data on a property that is an NSNumber.
In particular, getting objects from the store whose id number "BEGINSWITH" the search text.
As an example, if the user enters the number "1" in the search box, my fetched request returns objects including 1, 12, 14, 19, 100, 149, 1324, etc.
Unfortunately, I am implementing sorting on the table as well, so when sorting by this id number, if represented as a string in the data model, sorting becomes: 1,10,100,2,20,200 instead of 1,2,10,20,100,200.
How can I go about getting the best of both worlds here such that I can search in a string-sort-of-way but sort in a number-sort-of-way?
Do I have to create two properties in the data model, a string and a number representation of the same data?
Any help would be much appreciated.
Rgds,
Felipe
I suggest you have two fields, the actual identifier that is a number and then a searchIdentifier which is a string. Search on the string, sort on the number.
Though it is not mentioned anywhere in the apple documentation, Like works with nsnumber as well.
For example,
[NSPredicate predicateWithFormat:#"numberField like '%#*'", searchTerm]
works, where numberField is a core data attribute of type NSNumber. This fetches all records where numberfield value begins with searchTerm.

How to avoid multiple Iterations to search and assign one of the possible object property to a variable ?

How can we avoid multiple iteration to search for an object's property and if found then assign it to a variable else search for another key ?
eq we have Video Class with one of the field as videoType which can have values as hq(high-quality),normal(normal), def(default)..etc and so on.
From an array containing multiple video objects, how can we search and return a particular object in an order that if the array contains object with property hq then first return it,else search for normal and proceed so on. if a set of n keys are to be tested in the key set (hq,normal,def,....) then do we always need to iterate the entire array "n" times unless the key is found.
Can this be done is single iteration ? Do we need to first sort the original array in the order of occurrence of the keys in desired key set. I hope my problem statement is clear.
One possible solution for this would be to create separate NSMutableArrays for each videoType. Then, as you iterate once over your array of video objects, you check its array type and add the video to the correct array.
After you finished iterating, you create the final mutable array by concatenating the other array with addObjectsFromArray.
If you have a lot or variable list of video types, you can create the separate mutable arrays as values in an NSDictionary, where the keys are the video types. This way, you can get the target array with one step, by fetching it from the dictionary.

How to view, find and remove key value pairs in CFMutable Dictionary

How do i find and remove the key value pairs of a particular key using CFMutableDictionaryRef.
I have added a value using CFMutableDictionary but i need to know how to search ,view and delete a keyvalue pair.
The value i have created is a structure pointer and key is an integer value.
Beata,
The CFMutableDictionaryRef documentation shown Here will guide you.
In the order of your question:
For finding an element, see CFDictionaryGetValue
For removing an element, see CFDictionaryRemoveValue
Note that the CFDictionary types are a 'toll-free-bridge' with NSDictionary.
Frank