File: hash_append_5.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (136 lines) | stat: -rw-r--r-- 2,284 bytes parent folder | download | duplicates (5)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2017, 2024 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

// lib1.hpp

#include <boost/config.hpp>
#include <string>

// forward declarations

namespace boost
{
namespace hash2
{

struct hash_append_tag;

} // namespace hash2
} // namespace boost

// lib1::X

namespace lib1
{

class X
{
private:

    std::string s1_;
    std::string s2_;

    template<class Provider, class Hash, class Flavor>
    friend void tag_invoke( boost::hash2::hash_append_tag const&, Provider const& pr, Hash& h, Flavor const& f, X const* x )
    {
        pr.hash_append( h, f, x->s1_ );
        pr.hash_append( h, f, x->s2_ );

        (void)pr; // avoid msvc-14.0 warning
    }

public:

    X(): s1_( "s1" ), s2_( "s2" )
    {
    }
};

} // namespace lib1

// lib2.hpp

#include <cstdint>
#include <cstddef>

namespace lib2
{

class fnv1a
{
private:

    std::uint32_t st_;

public:

    typedef std::uint32_t result_type;

    fnv1a(): st_( 0x811C9DC5ul )
    {
    }

    void update( void const* pv, std::size_t n )
    {
        unsigned char const* p = static_cast<unsigned char const*>( pv );

        std::uint32_t h = st_;

        for( std::size_t i = 0; i < n; ++i )
        {
            h ^= static_cast<std::uint32_t>( p[i] );
            h *= 0x01000193ul;
        }

        st_ = h;
    }

    std::uint32_t result()
    {
        std::uint32_t r = st_;

        st_ = ( st_ ^ 0xFF ) * 0x01000193ul;

        return r;
    }
};

} // namespace lib2

// test

#include <boost/hash2/hash_append.hpp>
#include <boost/hash2/fnv1a.hpp>
#include <boost/core/lightweight_test.hpp>
#include <vector>
#include <list>

template<class Hash, class Flavor, class R> void test( R r )
{
    {
        Hash h;
        Flavor f;

        boost::hash2::hash_append( h, f, std::vector<lib1::X>( 3 ) );

        BOOST_TEST_EQ( h.result(), r );
    }

    {
        Hash h;
        Flavor f;

        boost::hash2::hash_append( h, f, std::list<lib1::X>( 3 ) );

        BOOST_TEST_EQ( h.result(), r );
    }
}

int main()
{
    test<boost::hash2::fnv1a_32, boost::hash2::little_endian_flavor>( 2425039999ul );
    test<lib2::fnv1a, boost::hash2::little_endian_flavor>( 2425039999ul );

    return boost::report_errors();
}