File: hash.cc

package info (click to toggle)
parallel-hashmap 1.4.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,872 kB
  • sloc: cpp: 20,492; ansic: 1,114; python: 492; makefile: 85; haskell: 56; perl: 43; sh: 23
file content (48 lines) | stat: -rw-r--r-- 1,100 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <parallel_hashmap/phmap_utils.h> // minimal header providing phmap::HashState()
#include <string>
#include <utility>
#include <tuple>
#include <vector>
#include <array>
#if PHMAP_HAVE_STD_STRING_VIEW
    #include <string_view>
#endif
#include <iostream>

using std::string;
using std::tuple;
using std::pair;

using groupid_t = std::array<uint16_t, 4>;

namespace std
{
    template<> struct hash<groupid_t>
    {
#if PHMAP_HAVE_STD_STRING_VIEW
        std::size_t operator()(groupid_t const &g) const
        {
            const std::string_view bv{reinterpret_cast<const char*>(g.data()), sizeof(g)};
            return std::hash<std::string_view>()(bv);
        }
#else
        std::size_t operator()(groupid_t const &g) const
        {
            return phmap::Hash<decltype(std::tuple_cat(g))>()(std::tuple_cat(g));
        }
#endif
    };
}

int main()
{
    std::vector<groupid_t> groups = {
        {17, 75, 82, 66},
        {22, 88, 54, 42},
        {11, 55, 77, 99} };

    for (const auto &g : groups)
        std::cout << std::hash<groupid_t>()(g) << '\n';
    
    return 0;
}