Recent

Author Topic: [Solved] Simple Map for built-in types  (Read 2271 times)

Hi im Pascal

  • Jr. Member
  • **
  • Posts: 53
[Solved] Simple Map for built-in types
« on: February 21, 2022, 12:32:46 am »
Hi,

What I  want to do is simply track a number (Integer, Real, Float, Currency, whatever) mapped to various short strings, maybe 10-1000 max. I'm open to any other solution ideas you may have in FPC!

I usually do o.k. with programming but I'm really struggling to find a "working" (in the sense that I understand it) "hash map", I've tried everything from GHashMap over the 3.0.4 Collections.Generics from GitHub over some other container types, but I cannot seem to find a simple substitute for
Code: [Select]
std::unordered_map
If anyone has a simple solution what the equivalent is to
Code: [Select]
std::unordered_map<std::string, int> or
Code: [Select]
std::unordered_map<std::string, double> in FPC that'd be awesome, thank you.

Here's an example:
Code: Pascal  [Select][+][-]
  1. TMapStringReal = specialize THashMap<String, Real>;
Quote
generics.dictionaries.inc(1586,37) Error: Incompatible type for arg no. 2: Got "Class Of IExtendedEqualityComparer$1$crcB76584A7", expected "TClass"

Edit: Perhaps at this point, since I'm already using a database, I'll just create another table for this and use it, ha ha...
« Last Edit: February 26, 2022, 04:47:48 pm by Hi im Pascal »

jamie

  • Hero Member
  • *****
  • Posts: 7774
Re: Simple Map for built-in types
« Reply #1 on: February 21, 2022, 01:31:54 am »
I don't know what problem you are having, code failures of the libs or just not sure what you want yourself ?

Have you looked at "TFPGMap" generics?

You should be able to use a short STRING as a key if you wish and a Variant as the data which can hold all the standard types.

But maybe you are actually truly looking for a real database ?
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 6398
  • Compiler Developer
Re: Simple Map for built-in types
« Reply #2 on: February 21, 2022, 01:43:17 pm »
I usually do o.k. with programming but I'm really struggling to find a "working" (in the sense that I understand it) "hash map", I've tried everything from GHashMap over the 3.0.4 Collections.Generics from GitHub over some other container types, but I cannot seem to find a simple substitute for
Code: [Select]
std::unordered_map

What compiler version are you using?

Here's an example:
Code: Pascal  [Select][+][-]
  1. TMapStringReal = specialize THashMap<String, Real>;
Quote
generics.dictionaries.inc(1586,37) Error: Incompatible type for arg no. 2: Got "Class Of IExtendedEqualityComparer$1$crcB76584A7", expected "TClass"

Edit: Perhaps at this point, since I'm already using a database, I'll just create another table for this and use it, ha ha...

These works in FPC 3.2.2 (I haven't included GHashMap, cause I don't use that):

Code: Pascal  [Select][+][-]
  1. program tmap;
  2.  
  3. {$mode objfpc}
  4.  
  5. uses
  6.   fgl, Generics.Collections;
  7.  
  8. type
  9.   TMap1 = specialize TFPGMap<String, Single>;
  10.   TMap2 = specialize TDictionary<String, Single>;
  11.   TMap3 = specialize THashMap<String, Single>;
  12.   TMap4 = specialize TFastHashMap<String, Single>;
  13.  
  14. begin
  15.  
  16. end.

And if you have specific issues with them, then please describe them.

Hi im Pascal

  • Jr. Member
  • **
  • Posts: 53
Re: Simple Map for built-in types
« Reply #3 on: February 26, 2022, 04:47:34 pm »
Hi all,

thank you, I guess I was having a bad day, but now I tried "TFPGMap" which didn't before and it seems to work well, thank you.
Compiler Version is 3.0.4, that's why I'd tried to download the Collections.Generics from a GitHub repo.

 

TinyPortal © 2005-2018