diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:27 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:27 +0000 |
commit | 40a355a42d4a9444dc753c04c6608dade2f06a23 (patch) | |
tree | 871fc667d2de662f171103ce5ec067014ef85e61 /xpcom/ds/tools/perfecthash.py | |
parent | Adding upstream version 124.0.1. (diff) | |
download | firefox-40a355a42d4a9444dc753c04c6608dade2f06a23.tar.xz firefox-40a355a42d4a9444dc753c04c6608dade2f06a23.zip |
Adding upstream version 125.0.1.upstream/125.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'xpcom/ds/tools/perfecthash.py')
-rw-r--r-- | xpcom/ds/tools/perfecthash.py | 31 |
1 files changed, 10 insertions, 21 deletions
diff --git a/xpcom/ds/tools/perfecthash.py b/xpcom/ds/tools/perfecthash.py index 929b643a30..2a5778e86d 100644 --- a/xpcom/ds/tools/perfecthash.py +++ b/xpcom/ds/tools/perfecthash.py @@ -24,18 +24,6 @@ small dataset it was designed for. In the future we may want to optimize further import textwrap from collections import namedtuple -import six -from mozbuild.util import ensure_bytes - - -# Iteration over bytestrings works differently in Python 2 and 3; this function -# captures the two possibilities. Returns an 'int' given the output of iterating -# through a bytestring regardless of the input. -def _ord(c): - if six.PY3: - return c - return ord(c) - class PerfectHash(object): """PerfectHash objects represent a computed perfect hash function, which @@ -88,7 +76,7 @@ class PerfectHash(object): for bucket in buckets: # Once we've reached an empty bucket, we're done. - if len(bucket.entries) == 0: + if not bucket.entries: break # Try values for the basis until we find one with no conflicts. @@ -103,7 +91,7 @@ class PerfectHash(object): # There was a conflict, try the next basis. basis += 1 idx = 0 - del slots[:] + slots.clear() assert basis < self.U32_MAX, "table too small" else: slots.append(slot) @@ -127,15 +115,16 @@ class PerfectHash(object): 32-bit FNV is used for indexing into the first table, and the value stored in that table is used as the offset basis for indexing into the values table.""" - for byte in memoryview(ensure_bytes(key)): - obyte = _ord(byte) + FNV_PRIME = cls.FNV_PRIME + U32_MAX = cls.U32_MAX + for obyte in memoryview(key): basis ^= obyte # xor-in the byte - basis *= cls.FNV_PRIME # Multiply by the FNV prime - basis &= cls.U32_MAX # clamp to 32-bits + basis *= FNV_PRIME # Multiply by the FNV prime + basis &= U32_MAX # clamp to 32-bits return basis def key(self, entry): - return memoryview(ensure_bytes(self._key(entry))) + return memoryview(self._key(entry)) def get_raw_index(self, key): """Determine the index in self.entries without validating""" @@ -145,7 +134,7 @@ class PerfectHash(object): def get_index(self, key): """Given a key, determine the index in self.entries""" idx = self.get_raw_index(key) - if memoryview(ensure_bytes(key)) != self.key(self.entries[idx]): + if memoryview(key) != self.key(self.entries[idx]): return None return idx @@ -334,7 +323,7 @@ class CGHelper(object): not in the table.""" assert all( - _ord(b) <= 0x7F for e in self.phf.entries for b in self.phf.key(e) + b <= 0x7F for e in self.phf.entries for b in self.phf.key(e) ), "non-ASCII key" if return_type is None: |