Dictionary (from Python) component for Delphi? - delphi

Is there similar component for Delphi like dictionary of Python?
I'm now using TStringList to map string/object pairs, but I'd like more general approach and maybe more powerful (TStringList has binary search when it is sorted).
Solutions for pre-D2009 are also welcome.

There is in Delphi 2009. A whole new containers unit has been added, and a Dictionary class is one of the classes available.
Couple this with Generics, and you have a very powerful set of classes.

There is a THashedStringList class (subclassed from TStringList) "hidden" in IniFiles.pas that can significantly speed up searching in a string list based dictionary.

I haven't tried it myself, but how about this Delphi Collections package? (Thank you Google).

If you are looking for some Hashtable implementations take a look at Hashtable implementations

I have allways used DeCAL. It includes both sequences, maps and hashtables.

Related

Are there Arrays and Objects in Dart?

Most of what I know is Javascript. I believe "Lists" are the closest thing Dart has to Arrays, but are they technically the same thing? Is it wrong to think of them as Arrays? Similarly, would Maps be considered Objects? I realize there are likely differences, but would it be "wrong" to make that comparison?
JavaScript Array objects are similar to Dart growable List objects. Both can be added to and accessed by index. Dart lists can only be accessed by index, where JavaScript lists are full JavaScript objects (see below), it just has a magic length property that is only updated when using integer-named properties. In practice, use a Dart List where you would have used a JavaScript Array.
JavaScript Objects are a combination of multiple things. They are mappings from string names to values - that part is best simulated by a Dart Map<String, Object>. They also allow prototype based inheritance, and if you are using that, you probably want to define a Dart class instead. As a data structure, use a Dart Map where you would have used a JavaScript {} object.
Everything is an object in Dart.
EVERYTHING
Everything in Dart is an Object - a List, a Map, even an int:
https://www.dartlang.org/guides/language/language-tour#important-concepts
Dart has Lists which are ordered sequences of objects. There is no class or interface called Array, though Lists act extremely similar to Arrays you might have encountered in other programming languages.
Everything in Dart is an Object.
List is pretty close to JS Array.
Map is somewhat related to Object in JS, but not quite.
A map is just to store keys and values together, but in Dart you can have non-string keys. There are also no prototypes or similar for maps in Dart.

F# equivalent of SortedDictionary from C#

new to F#
i need to store a bunch of lists of objects according to a float number where the collection of lists are sorted according to the float number. I know in C# i would use
SortedDictionary<float, List<obj>>
as the implementation is a red black tree, allowing for log(n) insert and search. But whats the best way to attack the situation in F#. I attempted to use SortedDitionary but i can't refer to SortedDictionary[int] to find the value so it renders it as useless essentially (i could be doing it wrong).
thanks for the help
The syntax is
sorteddictionary.[int]
then it works as you would expect
The first thig to do is read Okasaki's book Purely Functional Data Structures
It has ML implementations that may help you
You can use sorteddictionary.[int] as John Palmer already said but it may be worth pointing out that the F# standard library includes a purely functional sorted dictionary collection called Map.

ActionScript: Library implementing a 'set' datatype?

Has anyone implemented a Set class in ActionScript? Specifically, a Set which is similar to Python's set implementation (unordered, unique, O(1) membership, etc).
I'd like to be able to iterate over it using for each, perform operations like union and intersection and get a list of all the contained items… Which would all be possible to implement using Dictionary and Proxy, but I'd rather not reimplement it if someone's already done the heavy lifting.
This looks like a decent enough implementation.
Link to Collection class
Way overkill, but polygonal_ds is a very optimized set of data structures you can use from AS3.

Delphi non visual TTree implementation

I'm looking for a non visual persistent tree (TStringTree) implementation. If someone known any good implentation of it, please let me know.
Thanks.
You'll find a flexible, non-visual tree structure in the DI Containers library (commercial). However, as others have noted above, it's really quite easy to roll your own, adding only the functionality that you need.
You can do with just two base objects: TNode and a TNodeList (e.g. a TObjectList descendant). At minimum, TNode needs just three members: your string data, a reference to its parent node (nil if the node is root), and a TNodeList, which is a list of its child nodes. What remains is the (somewhat tedious) implementation of the various attendant methods such as Add(), Delete(), IndexOf(), MoveTo(), GetFirstChild(), GetNext() etc. The basic tree should be less than a one-nighter.
What kind of tree? B-tree? Splay tree? Red-black tree? These are all common types of tree algorithms.
You might want to look at Julian Bucknall's book, Tomes of Delphi: Data Structures and Algorithms. It has all sorts of tree implementations with full Delphi source; you could easily adapt any of them to work with strings.
And of course there is still the funky DECAL (previously Rosetta), an attempt at creating a kind of STL using interfaces and variants.
http://sourceforge.net/projects/decal/
More for the people that flexibility over speed though. The base tree structure is Red-Black iirc.
Why not simply use an XML DOM document?
It may be overkill for a truly trivial string-tree, but would not be too burdensome to use for that purpose and has the benefit of being capable of accommodating just about any extension to a string-tree (for storing additional data with each string in the tree - as attributes etc) should the need arise.
In my experience often what starts out as a trivial need can quickly grow beyond the initial or anticipated requirement. :)
If you are concerned about the "overhead" of the COM based XML implementation and the VCL wrapper around it, you might look into TNativeXML
You could just use a tStringList and add objects which are other tStringLists... crude, but it works if your data can be represented as only string data.
Child := tStringlist.create;
ParentList.AddObject('Child',Child);
Of course a better solution would be to create your own objects which contain a tobjectlist of objects.

TStringList vs. TList<string>

what is the difference in using a standard
type
sl: TStringList
compared to using a generic TList
type
sl: TList<string>
?
As far as I can see, both behave exactly the same.
Is it just another way of doing the same thing?
Are there situations where one would be better than the other?
Thanks!
TStringList is a descendant of TStrings.
TStringList knows how to sort itself alphabetically.
TStringList has an Objects property.
TStringList doesn't make your code incompatible with all previous versions of Delphi.
TStringList can be used as a published property. (A bug prevents generic classes from being published, for now.)
TStringList has been around a long time in Delphi before generics were around. Therefore, it has built up a handful of useful features that a generic list of strings would not have.
The generics version is just creating a new type that is identical to TList that works on the type of String. (.Add(), .Insert(), .Remove(), .Clear(), etc.)
TStringList has the basic TList type methods and other methods custom to working with strings, such as .SaveToFile() and .LoadFromFile()
If you want backwards compatibility, then TStringList is definitely the way to go.
If you want enhanced functionality for working with a list of Strings, then TStringList is the way to go.
If you have some basic coding fundamentals that you want to work with a list of any type, then perhaps you need to look away from TStringList.
As TStringList is a descendant of TStrings it is compatible with the Lines property of TMemo, Items of TListbox and TComboBox and other VCL components.
So can use
cbList.Items := StringList; // internally calls TStrings.Assign
I'd probably say if you want backwards compatibility use TStringList, and if you want forward compatibility (perhaps the option to change that list of strings to say list of Int64s in the future) then go for TList.
From memory point of view TStringList memory usage is increased with the size of TObject pointer added to each item. TList memory usage is increased with the size of pointer added to each item. If is needed just an array of strings without searching, replacing, sorting or associative operations, a dynamic array (array of string) should be just enough. This lacks of a good memory management of TStringList or TList, but in theory should use less memory.
The TStringlist is one very versatile class of Delphi. I used (and abused ;-) ) its Objects property many times. It's very interesting to quickly translate a delimited string to a control like a TMemo and similar ones (TListBox, TComboBox, just to list a few).
I just don't like much TList, as TStringList satisfied my needs without needing of treating pointers (as Tlist is a list of Pointer values).
EDIT: I confused the TList(list of pointers) with TList (generic list of strings). Sorry for that. My point stands: TStringList is just a lot more than a simple list of strings.
For most purposes that TStringList has been abused in the past, TObjectDictionary is better - it's faster and doesn't need sorting.
If you need a TStrings object (generally for UI stuff, since the VCL doesn't use generics much even for XE5) use TStringList - the required casting from TObject is annoying but not a showstopper.
TStringList has been used for far too long and has many advantages, all mentioned by Rob Kennedy.
The only real disadvantage of using it as a pair of a string and an object is the necessity of casting object to the actual type expected and stored in this list (when reading) and as far as I know Embarcadero did not provide Delphi 2009 and up VCL libraries with generic version of TStringList.
To overcome this limitation I implemented such list for internal use and for almost 3 years it serves it's purpose so I decided to share it today: https://github.com/t00/deltoo#tgenericstringlist
One important note - it changes the default property from Strings to Objects as in most cases when object is stored in a list it is also the mostly accessed property of it.

Resources