I use some TDictionary to improve searches speed. But I don't want it to clear my objects when I free it. Like TObjectList with property OwnsObjects=False, is it possible ?
Thanks
TDictionary<K,V> does not own its members. TObjectDictionary<K,V> can optionally own keys, values, both or neither. This ownership is determined by the arguments you pass to the constructor.
If you are using TDictionary<K,V> and think that it is destroying its members, then you are mistaken.
Related
I have some questions about the ideas proposed in this video.
The speaker shows an array that holds values and pointers, and he also shows a separate "free" linked list, that is updated whenever an item is added/removed.
Why are these used? Doesn't using an array / limiting yourself to a set of free nodes defeat the purpose of a linked list?
Isn't one of the perk of using a linked list the ability to traverse fragmented data?
Why use these free nodes, when you can dynamically allocate storage?
The proposed structure, to me, doesn't seem dynamic at all, and is in fact a convoluted and inefficient array.
The approach you mention makes sense in certain use cases. For example if the common case is that the array is 90% full and most of the time is spent iterating over it, you can very quickly loop over an array and just skip the few empty items. This can be much, much faster than "pointer chasing" which plain linked lists use, because the CPU's hardware prefetcher can predict which memory you will need in advance.
And compared with a plain array and no free list, it has the advantage of O(1) allocation of an element into an empty slot.
how to check if an array is sorted?
I am sorting using sort descriptors. Is there any API to check if an array is already in sorted order in Swift/Objective-C.
Thanks
i think there is no frame work, simply iterate truth the array, and check if the current element greater or equal (or less or equal, or which kind of sorting you look for) is. This is the easiest way. Look please at this Question Solution
As far as I know, there isn't a built in way to check if an array is already sort descriptors. The best way to check is to iterate through the array and check if each element should come before the element precedes it (using whatever definition of "should come before" you want for your sort). If you're sorting custom objects, you can write some sort of compareTo method that compares two objects of your class, which will make it convenient to check using the method I described.
Assuming I have a hash value of some NSObject during runtime.
Is there a way to find a pointer to that object using just hash value?
I don't want to store pointers to objects and their hashes as keys. I imagine that iOS already doas that.
There is no way, not even an unreliable way, to do this.
Many objects have hashes in ways that makes it impossible to reference it. You will have duplicates because of this. One example, as #Martin said, is NSArrays. NSArrays' hashes are just the number of elements in the array.
I need a delphi key/value collection that will allow me to iterate over the collection in the same order the key/value pairs were inserted/added.
TList<T> guarantees order but TDictionary<T1, T2> does not.
I guess I could always define a TList<TPair<Key, Value>> but it would be more cumbersome to work with.
Is there a built-in collection type that would meet my requirements or would wrapping TList<TPair<Key, Value>> be my best option? Or perhaps it would be better to have a TList<Key> and a TDictionary<Key, Value> and iterate through the list.
If your key type is string and your value type is some descendant of TObject, use a TStringList. Store your values in the Objects array property.
SL.AddObject('foo', obj1);
SL.Add('bar');
i := SL.IndexOf('bar');
SL.Objects[i] := obj2;
Set the OwnsObjects property if you need to.
the DeHL collections library contains a lot of "Ordered Dictionary"-like classes. The ordered ones use trees (which have order) instead of hash maps which are unordered.
I believe the TSortedDistinctMultiMap might be what you need, if you want to enforce uniqueness, and if you don't want to enforce Key value uniqueness, then there are other choices (without Distinct in the class name) that will be close to what you need.
Update 2017: The DeHL library is no longer maintained.
The Spring4D library provides dictionaries that are ordered. At the time of writing these are only available on the develop branch.
Is there a way to compare two objects that are generic? I'm supposed to find the largest object in a linked list. My first guess was to use the Object's class compareTo method, but I couldn't get that to work. Thanks
I assume that the language is Java. (since you mentioned Object and compareTo)
I suggest you to have a have a look into Comparator and Comparable interfaces.