Hello guys


Hello guys,

I have ran on problem in a personal project (a full reimplementation of collection classes):
The TDictionary class implements the generic IEnumerable interface to support interation, but I am not able to implement this interface properly within the class :'(

I have tried "method resolution clause" but it doesn't work as expected, I cannot build the project because of E2003 error ("Enumerable identifier: 'IEnumerable'").

The full file is at https://gist.github.com/horaciojcfilho/42dabdea506e47c797ab.

Will be a great pleasure rely on your help :D
 Thanks in advance :D

Comments

  1. IEnumerable is a real pain. But actually you don't need to implement it - you just need to implement the methods it defines, and suddenly your type is magically enumerable. IEnumerable is actually not required at all. Just define your own interface with the same methods and implement that.

    ReplyDelete
  2. Also, why are you writing your own dictionary? I'm interested in what it provides that the RTL or Spring ones don't.  (Not that it's a bad thing - it's fun writing code like this.)

    ReplyDelete
  3. Reinventing the wheel, now even more round! ;p

    Also to answer your question: don't use System.IEnumerable/IEnumerable. The person putting it there forgot that Delphi does not have a rooted type system (yet). So inheriting a generic type from a non generic one that uses TObject is not going to work (unless the generic type has the class constraint)

    I can just invite you to contribute to the Spring4D project rather than wasting your time re-implementing things that already exist (these interfaces look fairly familiar to me - no surprise as you obviously have the same model)

    ReplyDelete
  4. Stefan Glienke Reuleaux polygons for the win!

    On the subject of S4D: one thing I miss from C++ is std::map, which is subtly different from TDictionary. It is sorted, so uses operators < and = (usually using a tree internally instead of hashing), and accessing an index that doesn't exist returns an empty / nil instance of whatever type it was defined with, (0 for integers, nil for objects, etc.)

    With XE8's Default() intrinsic, and operator overloading, is it now possible to implement something similar in Delphi, do you think?  I haven't tried even with a simple program yet but I think it may be. It's on my list of things to take a stab at sometime soon.

    ReplyDelete
  5. Default() is not new in XE8. It exists since generics were introduced. Also what you are describing I would describe as sorteddictionary which imo has a very limited use (only if you want to iterate the elements).

    ReplyDelete
  6. It can be more efficient, though, can't it? Assuming it's stored via a tree, it can be quicker to place or retrieve an item in a tree than to calculate a hash and use a hash table.

    Plus what I really like is the access semantics for non-defined elements.

    Does S4D have a sorted dictionary?

    ReplyDelete
  7. David Millington C++ STL separate these into "map" (ordered) and "unordered_map" (typenames off the top of my head). The downside with the ordered map (sorted dictionary) is that the keys must be ordered, which isn't always possible.

    ReplyDelete
  8. Thanks for the inputs, they help me a lot. I love Spring4D and took many inspiration from its source codes (Many thanks for Stefan Glienke).

    David Millington, my personal project is called Delic and it is hosted at https://github.com/horaciojcfilho/Delic/tree/nightly, it is just in begining, it aims try new APIs and implementations for collections classes.

    For example, if is better or not use spreading hash function in dictionaries (the cost associated with this use), struggling against hacker attacks in dictionaries that create too much collisions in its entries and drop the server when backed by this structure.
    Sorted dictionary using a fast self-balancing binary tree structure and, at the same time, with low memory consumption (AVL trees, Red-black tree, AA trees, B trees, B+ trees, discover which of these ones is the best one for powering the sorted dictionary is a great challenge addressed by this project).

    TSortedDictionary, TOrderedDictionary, TSortedSet, TSortedQueue are planned classes that you don't see in Spring4D (but all of them are, currently, either unimplemented or incomplete).
    Besides that, the project also aims create immutable and concurrent versions of the collection classes.

    Stefan Glienke Thanks so much for invite me, I would love to contribute to Spring4D, I think when Delic project achieve a stable version I can merge it into Spring4D :D, do you agree?

    ReplyDelete
  9. Horácio Filho Very interesting! I especially like the use of balanced trees - I'm not a fan of hash-based approaches. From an old, old project I still hope to resurrect someday: http://stackoverflow.com/questions/16384520/problems-with-promote-using-the-red-black-tree-implementation-from-the-tomes-o

    What trees do you have so far? I saw only interfaces defined in the one file I could find, no code.  I'd like to see more :)

    Asbjørn Heid The names are correct (from memory.)  Interesting you note that downside... and perhaps that's why Delphi's TDictionary goes with the approach it does.  I always found I tended to use std::map far more (far, far, far more!) than unordered_map, I think because of the speed and because writing < and = operators, where required, was usually not an especially hard task. It's kindof normal in C++ whereas in Delphi people might think it weird to be forced to write those methods, just to put something in a container.

    I would love to see one in S4D and would definitely contribute one if the need was perceived by others too.

    ReplyDelete
  10. David Millington No, it doesn't. I experimented with it but found out that the few benefits of it would not justify the work to implement it.
    Also our dictionary is still backed by the RTL one which might be changed in the future.

    Horácio Filho Do as you wish - it's your time and project. :) But I would like to point out that merging something into Spring4D will be more complicated when developed individually without compatibility in mind in the first place.

    ReplyDelete
  11. I'm working on a library of trees to plug into the Sping4D framework. It implements the IMap<>, ISet<> and IDictionary<> interfaces. in BST, RB, AVL and Splay flavours.

    ReplyDelete
  12. I hadn't really used unordered_map as its part of boost stl and not part of std stl, we mainly stick to the std stl due to not overly complex tasks or speed requirements.  Would be interesting to see benchmarks on the different methodologies

    ReplyDelete
  13. Brett Wilton The C++ project I worked on used unordered_map for some cases mainly because our keys didn't have a natural order (points in 3d space) and due to speed.

    However other places we'd stick with a regular map, often because we wanted to iterate over the elements in a sorted order in the end.

    But yes, benchmarks highlighting when each shine for types which can be both ordered and compared for equality would be nice.

    ReplyDelete
  14. David Millington Thanks a lot for support me :D You will see more as soon as possible :D o/

    ReplyDelete

Post a Comment