2

I am in trouble trying to create an indefinite hashed map, as I want as a key specific objects that inherits from an abstract class, so the Key_Type is the parent class-wide, but I do not know what to do with the Hash that the container requires, as the Hash_Type is a modular type. How can I deal with the hash of a class-wide key?

Albatros23
  • 297
  • 2
  • 14

1 Answers1

3

First thing that comes to mind is to add a "Hash" primitive function to the Key_Type abstract class, to be implemented in each concrete derived key type using the components of that concrete type, and then to make the Hash function for the map call this primitive Hash function with redispatch according to the actual type of the key.

Niklas Holsti
  • 1,907
  • 3
  • 9
  • Well, all the descendants of the abstract parent are common, they do not extend the parent record, but I do not know how is this problem usually solved in this language. My first idea was to store the Key_Type required by the indefinite hashed map as String, with the String hash provided by Ada, and to store the external_tag provided by the descendants, but I was wondering if there would be a better option as I prefer to use my own hash and avoiding the use of the String type – Albatros23 May 13 '21 at 12:54
  • 1
    The hash function has to hash the key. @Albatros23, if the key is a string, why wouldn’t you want to use the provided hash function? – Simon Wright May 13 '21 at 19:18
  • @SimonWright I don't want to use the provided hash function for strings due to the explanation of the `Intrinsic` subprograms, used in the `System.String_Hash` function, provided by GNAT on next link: https://docs.adacore.com/gnat_rm-docs/html/gnat_rm/gnat_rm/intrinsic_subprograms.html `Note that any use of this feature is potentially non-portable, since the Ada standard does not require Ada compilers to implement this feature` – Albatros23 May 14 '21 at 07:11
  • 1
    You could use the Pearson string hash function (https://github.com/jrcarter/PragmARC/blob/Ada-12/pragmarc-hash_fast_variable_length.ads). This hashes to a byte; if that's not enough values, you could divide your string into pieces, hash each piece separately, and combine the hashes into a larger value. – Jeffrey R. Carter May 14 '21 at 08:48
  • 1
    Why not use the standard function Ada.Strings.Hash? That should be entirely portable to all Ada compilers. – Niklas Holsti May 14 '21 at 13:08
  • 1
    @NiklasHolsti two comments above you can see the answer that I gave to Simon, It's due to the `Intrinsic` subprogram feature used on the hash function provided by ada. I prefer to not rely on things that doesn't hear clear to me as I'am new to the language :P – Albatros23 May 14 '21 at 19:29
  • @Albatros23: If I understand correctly, GNAT's use of an `Intrinsic` subprogram in this context is an implementation detail; another implementation may have different performance, but it should compile correctly. – trashgod May 14 '21 at 20:03
  • See [ARM A.4.9](http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-4-9.html). – Simon Wright May 15 '21 at 07:34
  • @trashgod as it may have a different permormance, it is not interesting to me as I want it to behave always on the same way – Albatros23 May 26 '21 at 07:46