Recent

Author Topic: Equivalent of python dictionary with tuples in fpc  (Read 7161 times)

soerensen3

  • Full Member
  • ***
  • Posts: 213
Equivalent of python dictionary with tuples in fpc
« on: October 28, 2017, 01:40:22 am »
Dear all,

I'm looking for an equivalent of a python dictionary with tuples as keys in FPC.
In Python the following is possible:
Code: Python  [Select][+][-]
  1. dict = {(key1, key2):value1}
The keys and values are all objects (as in instances of a class). I don't need to store the actual values as keys, a hash would be sufficient but I don't know how to generate one hash of multiple objects. The number of keys can vary.

I will try to briefly explain why I need this functionality. In my game engine I have materials, which can be used by my mesh class. Each material will compile one shader at the moment, which is generated at runtime with only the code that is needed. Because the same material can be used for let's say a cube and a character (which can be deformed by an armature), I would actually need two shaders for each combination. Similar to blender I use modifiers for that. So the cube object has no modifiers while the character has an armature modifier. There are also other modifiers. For a dictionary to find the right shader of a material I would do this in python:
Code: Python  [Select][+][-]
  1. Material1_dict = {
  2.     (None):Shader1, #for all meshes with no modifiers
  3.     (armature_mod):Shader2, #for all meshes that use the specific armature modifier
  4.     (armature_mod, some_other_modifier): Shader3} #for all meshes that use the armature modifier and some other modifier
I don't need to do the same in FPC so you are very welcome to point out a better solution. Maybe I think to complicated. I would like to point out that I would like to keep the amount of shaders as low as possible because changing the shader is expensive.

Please ask if you don't understand my explanations. Thanks in advance for any suggestions.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: Equivalent of python dictionary with tuples in fpc
« Reply #1 on: October 28, 2017, 11:05:29 am »
I don't understand all the notational details of Python, but Freepascal trunk has a TDictionary generic class.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Equivalent of python dictionary with tuples in fpc
« Reply #2 on: October 28, 2017, 11:17:07 am »
I don't understand all the notational details of Python, but Freepascal trunk has a TDictionary generic class.
Correct, but more than one. A TMap is the same.
But anyway the best one is in packages/rtl-generics/src and another equivalent in the fgl package.

( <some grumpyness... I explained that many times already... >:D >)

Ok, as a side note (ational detail), I also have problems with white space and indentation as a language feature.... 8-) O:-)
« Last Edit: October 28, 2017, 11:23:26 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

soerensen3

  • Full Member
  • ***
  • Posts: 213
Re: Equivalent of python dictionary with tuples in fpc
« Reply #3 on: October 29, 2017, 01:08:09 am »
I knew the fgl unit but it turned out I just didn't understand that there is always only one key attribute in a dictionary, even in python. The equivalent to a python tuple I used in the example should (in this case) be an array. So If I use a dynamic array for a key it should solve the problem. However I couldn't figure out how.

I started like this:
Code: Pascal  [Select][+][-]
  1. ...
  2. uses
  3.   Classes, SysUtils, fgl;
  4.  
  5. type
  6.   TObjArray = array of TObject;
  7.   TObjDictionary = specialize TFPGMap < TObjArray, Integer >;  
  8.  
It complains:
Quote
Error: Operator is not overloaded: "TObjArray" > "TObjArray"
Error: Operator is not overloaded: "TObjArray" < "TObjArray"
So far so good so I tried to overload the operators.
In Delphi Mode I have no idea at all how to do operator overloading so I used ObjFPC.

Code: Pascal  [Select][+][-]
  1. ...
  2. uses
  3.   Classes, SysUtils, fgl;
  4.  
  5. type
  6.   TObjArray = array of TObject;
  7.  
  8.   operator < ( a, b: TObjArray ): Boolean;
  9.   operator > ( a, b: TObjArray ): Boolean;
  10.  
  11. type
  12.   TObjDictionary = specialize TFPGMap < TObjArray, Integer >;  
  13.  
  14. // I ommitted the function bodies here cause the problem is not there
  15.  

It compiles to the declaration of TObjDictionary and again gives me the same errors.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: Equivalent of python dictionary with tuples in fpc
« Reply #4 on: October 29, 2017, 01:52:07 pm »
If you want more value as a key, group them in a record.

It works with most collection types that allow to specify a comparator function (or interface)

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: Equivalent of python dictionary with tuples in fpc
« Reply #5 on: November 06, 2017, 09:42:03 am »
I don't understand all the notational details of Python, but Freepascal trunk has a TDictionary generic class.
Correct, but more than one. A TMap is the same.

Definitely not true. T*Map is simple map while TDictionary is hash map.
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Equivalent of python dictionary with tuples in fpc
« Reply #6 on: December 10, 2017, 04:36:39 pm »
I don't understand all the notational details of Python, but Freepascal trunk has a TDictionary generic class.
Correct, but more than one. A TMap is the same.

Definitely not true. T*Map is simple map while TDictionary is hash map.

Hmmm, Maciej: that is not true. In computer science a map is equivalent to a dictionary and the other way around. It means the same thing.
What you are referring to is implementation detail - given FPC l;library options - and opaque to the user.

What IS true is that the dictionary in rtl-generics -written by you - is superior in performance to the maps in FGL.
As of today (3.0.4) your rtl-generics are NOT in a stable release yet. So one should use the TFPGMap from FGL instead.
« Last Edit: December 10, 2017, 04:41:00 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018