inchash.h 3.0 KB

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