Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rolling_hash.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
17 
18 #ifndef PAGESPEED_KERNEL_BASE_ROLLING_HASH_H_
19 #define PAGESPEED_KERNEL_BASE_ROLLING_HASH_H_
20 
21 #include <cstddef>
22 #include "base/logging.h"
24 
25 namespace net_instaweb {
26 
29 
31 extern const uint64 kRollingHashCharTable[256];
32 
34 uint64 RollingHash(const char* buf, size_t start, size_t n);
35 
45 inline uint64 NextRollingHash(
46  const char* buf, size_t start, size_t n, uint64 prev) {
49  CHECK_LT(static_cast<size_t>(0), start);
50  uint64 start_hash =
51  kRollingHashCharTable[static_cast<uint8>(buf[start - 1])];
52  uint64 end_hash =
53  kRollingHashCharTable[static_cast<uint8>(buf[start - 1 + n])];
54  uint64 prev_rot1 = (prev << 1) | (prev >> 63);
55  uint64 start_hash_rotn;
59  size_t shift = n % 64;
60  if (shift == 0) {
61  start_hash_rotn = start_hash;
62  } else {
64  start_hash_rotn = (start_hash << shift) | (start_hash >> (64 - shift));
65  }
66  return (start_hash_rotn ^ prev_rot1 ^ end_hash);
67 }
68 }
69 
70 #endif
uint64 NextRollingHash(const char *buf, size_t start, size_t n, uint64 prev)
Definition: rolling_hash.h:45
uint64 RollingHash(const char *buf, size_t start, size_t n)
Compute the rolling hash of buf[start : start + n - 1].
const uint64 kRollingHashCharTable[256]
Per character hash values. Exported for use in NextRollingHash.