1/* An incremental hash abstract data type. 2 Copyright (C) 2014-2015 Free Software Foundation, Inc. 3 4This file is part of GCC. 5 6GCC is free software; you can redistribute it and/or modify it under 7the terms of the GNU General Public License as published by the Free 8Software Foundation; either version 3, or (at your option) any later 9version. 10 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12WARRANTY; without even the implied warranty of MERCHANTABILITY or 13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14for more details. 15 16You should have received a copy of the GNU General Public License 17along with GCC; see the file COPYING3. If not see 18<http://www.gnu.org/licenses/>. */ 19 20#ifndef INCHASH_H 21#define INCHASH_H 1 22 23#include "config.h" 24#include "system.h" 25#include "coretypes.h" 26#include "hashtab.h" 27 28/* This file implements an incremential hash function ADT, to be used 29 by code that incrementially hashes a lot of unrelated data 30 (not in a single memory block) into a single value. The goal 31 is to make it easy to plug in efficient hash algorithms. 32 Currently it just implements the plain old jhash based 33 incremental hash from gcc's tree.c. */ 34 35extern hashval_t iterative_hash_host_wide_int (HOST_WIDE_INT, hashval_t); 36extern hashval_t iterative_hash_hashval_t (hashval_t, hashval_t); 37 38namespace inchash 39{ 40 41class hash 42{ 43 public: 44 45 /* Start incremential hashing, optionally with SEED. */ 46 hash (hashval_t seed = 0) 47 { 48 val = seed; 49 bits = 0; 50 } 51 52 /* End incremential hashing and provide the final value. */ 53 hashval_t end () 54 { 55 return val; 56 } 57 58 /* Add unsigned value V. */ 59 void add_int (unsigned v) 60 { 61 val = iterative_hash_hashval_t (v, val); 62 } 63 64 /* Add HOST_WIDE_INT value V. */ 65 void add_wide_int (HOST_WIDE_INT v) 66 { 67 val = iterative_hash_host_wide_int (v, val); 68 } 69 70 /* Hash in pointer PTR. */ 71 void add_ptr (void *ptr) 72 { 73 add (&ptr, sizeof (ptr)); 74 } 75 76 /* Add a memory block DATA with size LEN. */ 77 void add (const void *data, size_t len) 78 { 79 val = iterative_hash (data, len, val); 80 } 81 82 /* Merge hash value OTHER. */ 83 void merge_hash (hashval_t other) 84 { 85 val = iterative_hash_hashval_t (other, val); 86 } 87 88 /* Hash in state from other inchash OTHER. */ 89 void merge (hash &other) 90 { 91 merge_hash (other.val); 92 } 93 94 template<class T> void add_object(T &obj) 95 { 96 add (&obj, sizeof(T)); 97 } 98 99 /* Support for accumulating boolean flags */ 100 101 void add_flag (bool flag) 102 { 103 bits = (bits << 1) | flag; 104 } 105 106 void commit_flag () 107 { 108 add_int (bits); 109 bits = 0; 110 } 111 112 /* Support for commutative hashing. Add A and B in a defined order 113 based on their value. This is useful for hashing commutative 114 expressions, so that A+B and B+A get the same hash. */ 115 116 void add_commutative (hash &a, hash &b) 117 { 118 if (a.end() > b.end()) 119 { 120 merge (b); 121 merge (a); 122 } 123 else 124 { 125 merge (a); 126 merge (b); 127 } 128 } 129 130 private: 131 hashval_t val; 132 unsigned bits; 133}; 134 135} 136 137#endif 138