• Hash function for C string (C++14)

    From Marcel Mueller@21:1/5 to All on Sun Oct 13 13:43:56 2024
    Hi!

    Is there anything in C++14 that can be used to hash C like strings.
    In C++17 I could use std::hash<std::string_view>.

    I need to use a quite large number of strings from C APIs as keys in unordered_set or _map. It is essential that the hash function does no
    temporary allocation. Otherwise I could use std::hash<std::string>.

    Marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Muttley@DastartdlyHQ.org@21:1/5 to All on Sun Oct 13 13:57:26 2024
    On Sun, 13 Oct 2024 13:43:56 +0200
    Marcel Mueller <news.5.maazl@spamgourmet.org> boring babbled:
    Hi!

    Is there anything in C++14 that can be used to hash C like strings.
    In C++17 I could use std::hash<std::string_view>.

    std::hash appeared in C++11 IIRC so not sure what the problem is.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Sam@21:1/5 to Muttley@DastartdlyHQ.org on Sun Oct 13 12:36:27 2024
    Muttley@DastartdlyHQ.org writes:

    On Sun, 13 Oct 2024 13:43:56 +0200
    Marcel Mueller <news.5.maazl@spamgourmet.org> boring babbled:
    Hi!

    Is there anything in C++14 that can be used to hash C like strings.
    In C++17 I could use std::hash<std::string_view>.

    std::hash appeared in C++11 IIRC so not sure what the problem is.

    The problem is finding what to hash. std::hash is not overloaded for a C-
    style string represented by a lonely char pointer. In C++17 one can
    construct a std::string_view, and there is an available std::hash overload
    for that.

    I was digging through gcc's header files and came across how it implements std::hash for various things. There's nothing in C++14 for this, but it shouldn't be rocket science to define a custom specialization of std::hash
    that uses the underlying hash function on top of a C-style string.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marcel Mueller@21:1/5 to All on Mon Oct 14 00:12:17 2024
    Am 13.10.24 um 18:36 schrieb Sam:
    I was digging through gcc's header files and came across how it
    implements std::hash for various things. There's nothing in C++14 for
    this,

    It seems so.

    but it shouldn't be rocket science to define a custom
    specialization of std::hash that uses the underlying hash function on
    top of a C-style string.

    Well, I hoped that there are optimized, platform specific implementations.


    Marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Sam@21:1/5 to Marcel Mueller on Sun Oct 13 20:13:28 2024
    Marcel Mueller writes:

    Am 13.10.24 um 18:36 schrieb Sam:
    I was digging through gcc's header files and came across how it implements >> std::hash for various things. There's nothing in C++14 for this,

    It seems so.

    but it shouldn't be rocket science to define a custom specialization of
    std::hash that uses the underlying hash function on top of a C-style string.

    Well, I hoped that there are optimized, platform specific implementations.

    Working from memory: the various specializations of std::hash all boiled
    down to calling the same opaque function that computes a hash on a given
    memory blob.

    ,,, I just spent a few minutes digging it up, and the declaration is in bits/hash_bytes.hl, std::_Hash_bytes, and std::_Fnv_hash_bytes. Of course,
    this is the current gcc, but I'm pretty sure older versions worked the same way.


    Any "plaform specific implementations" will have to be encapsulated/buried
    in there, I saw no platform specific implementations in the plain headers.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Muttley@DastartdlyHQ.org@21:1/5 to All on Mon Oct 14 08:21:02 2024
    On Sun, 13 Oct 2024 12:36:27 -0400
    Sam <sam@email-scan.com> boring babbled:
    Muttley@DastartdlyHQ.org writes:

    On Sun, 13 Oct 2024 13:43:56 +0200
    Marcel Mueller <news.5.maazl@spamgourmet.org> boring babbled:
    Hi!

    Is there anything in C++14 that can be used to hash C like strings.
    In C++17 I could use std::hash<std::string_view>.

    std::hash appeared in C++11 IIRC so not sure what the problem is.

    The problem is finding what to hash. std::hash is not overloaded for a C- >style string represented by a lonely char pointer. In C++17 one can
    construct a std::string_view, and there is an available std::hash overload >for that.

    I was digging through gcc's header files and came across how it implements >std::hash for various things. There's nothing in C++14 for this, but it >shouldn't be rocket science to define a custom specialization of std::hash >that uses the underlying hash function on top of a C-style string.

    #include <stdio.h>
    #include <unordered_map>

    using namespace std;

    int main()
    {
    auto hashobj = hash<const char *>();
    printf("String1: %lu\n",hashobj("hello"));
    printf("String2: %lu\n",hashobj("world"));

    return 0;
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Muttley@DastartdlyHQ.org@21:1/5 to All on Mon Oct 14 11:14:33 2024
    On Mon, 14 Oct 2024 10:26:13 +0200
    Bonita Montero <Bonita.Montero@gmail.com> boring babbled:
    Am 14.10.2024 um 10:21 schrieb Muttley@DastartdlyHQ.org:

    #include <stdio.h>
    #include <unordered_map>

    using namespace std;

    int main()
    {
    auto hashobj = hash<const char *>();
    printf("String1: %lu\n",hashobj("hello"));
    printf("String2: %lu\n",hashobj("world"));

    return 0;
    }



    If you hash some strings with that equal strings usually would
    get a different hash.

    Actually it wouldn't with the case of literal text pointers, but yes, you're right with normal pointers.

    A simple solution would be for him to load his C text into a std::string instead:

    int main()
    {
    auto hashobj = hash<string>();
    const char *p1 = strdup("hello");
    const char *p2 = strdup("hello");
    string s1(p1);
    string s2(p2);
    printf("String1: %lu\n",hashobj(s1));
    printf("String2: %lu\n",hashobj(s2));

    return 0;
    }

    The strdup() is to make sure p1 and p2 don't get the same pointer value.

    If this doesn't meet his needs then I guess I've misunderstood his problem.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)