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

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

Related

Why are Binary Search Trees (BST) created using a node data structure instead of arrays?

Most examples of BSTs I have seen are of the form
Class Node {
Node left;
Node right;
Key key;
Value value;
}
But BSTs looks like a specific form of Binary Heaps with a extra constraint , namely the left child value should be less than parent value which should be less than right node value.
Binary heaps are easily implemented using arrays. Why not create BSTs using arrays making sure that this extra rule is maintained ? What are the disadvantages of doing so ?
The answer is simple: You can't just dynamically change the size of an array.
The size of an array can't just be changed afterwards. If you would use an array you'd have to enlarge it or shrink it depending on what is added or removed which would cause unnecessary overhead since you would have to copy the content from the old array to the new array whenever you do that.
Using nodes that use references or pointers allow you to just (re)assign left and right to a new element accordingly whenever you insert or set it to null (or something similar) if you remove an element which gives you a much more dynamic structure.

VB.NET How to access object (deserialized from JSON)

If someone would be so kind as to help me get the highlighted line in to a variable, I would be very grateful.
I tried the following:
Dim Fromid = FBrequest.data(0)(3)("from")(2)
but it said the key was not present in the array.
Image is at imageshack.us
/img33/1491/wtfjsondeserialized.png
I would suggest doing a foreach to see what keys are present in your array. Example:
For Each entry As String In FBrequest.data(0)(3)
Console.WriteLine(entry)
Assuming that the "from" is the key that is missing. Additionally, I would also suggestion setting a breakpoint on this line and adding a watch statement. Inspect your FBRequest.data variable to see what the contents are.

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]?")

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.

Fuzzy NSDictionary - use nearest key

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.