1/*
2 * Copyright (c) 2017, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * \file
31 * \brief Hashing utility functions.
32 */
33
34#ifndef UTIL_HASH_DYNAMIC_BITSET_H
35#define UTIL_HASH_DYNAMIC_BITSET_H
36
37#include "hash.h"
38
39#include <boost/dynamic_bitset.hpp>
40
41#include <iterator>
42
43namespace ue2 {
44
45/**
46 * \brief An output iterator which calculates the combined hash of all elements
47 * written to it.
48 *
49 * The location to output the hash is provided to the constructor and should
50 * already be zero initialised.
51 */
52struct hash_output_it {
53 using value_type = void;
54 using difference_type = ptrdiff_t;
55 using pointer = void *;
56 using reference = void;
57 using iterator_category = std::output_iterator_tag;
58
59 hash_output_it(size_t *hash_out = nullptr) : out(hash_out) {}
60 hash_output_it &operator++() {
61 return *this;
62 }
63 hash_output_it &operator++(int) {
64 return *this;
65 }
66
67 struct deref_proxy {
68 deref_proxy(size_t *hash_out) : out(hash_out) {}
69
70 template<typename T>
71 void operator=(const T &val) const {
72 hash_combine(*out, val);
73 }
74
75 private:
76 size_t *out; /* output location of the owning iterator */
77 };
78
79 deref_proxy operator*() { return {out}; }
80
81private:
82 size_t *out; /* location to output the hashes to */
83};
84
85/* Function object for hashing a dynamic bitset */
86struct hash_dynamic_bitset {
87 size_t operator()(const boost::dynamic_bitset<> &bs) const {
88 size_t rv = 0;
89 to_block_range(bs, hash_output_it(&rv));
90 return rv;
91 }
92};
93
94} // namespace ue2
95
96#endif
97