github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/callgraph/vta/internal/trie/bits.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package trie
     6  
     7  import (
     8  	"math/bits"
     9  )
    10  
    11  // This file contains bit twiddling functions for Patricia tries.
    12  // Consult this paper for details.
    13  //   C. Okasaki and A. Gill, “Fast mergeable integer maps,” in ACM SIGPLAN
    14  //   Workshop on ML, September 1998, pp. 77–86.
    15  
    16  // key is a key in a Map.
    17  type key uint64
    18  
    19  // bitpos is the position of a bit. A position is represented by having a 1
    20  // bit in that position.
    21  // Examples:
    22  // * 0b0010 is the position of the `1` bit in 2.
    23  //    It is the 3rd most specific bit position in big endian encoding
    24  //    (0b0 and 0b1 are more specific).
    25  // * 0b0100 is the position of the bit that 1 and 5 disagree on.
    26  // * 0b0 is a special value indicating that all bit agree.
    27  type bitpos uint64
    28  
    29  // prefixes represent a set of keys that all agree with the
    30  // prefix up to a bitpos m.
    31  //
    32  // The value for a prefix is determined by the mask(k, m) function.
    33  // (See mask for details on the values.)
    34  // A `p` prefix for position `m` matches a key `k` iff mask(k, m) == p.
    35  // A prefix always mask(p, m) == p.
    36  //
    37  // A key is its own prefix for the bit position 64,
    38  //   e.g. seeing a `prefix(key)` is not a problem.
    39  // Prefixes should never be turned into keys.
    40  type prefix uint64
    41  
    42  // branchingBit returns the position of the first bit in `x` and `y`
    43  // that are not equal.
    44  func branchingBit(x, y prefix) bitpos {
    45  	p := x ^ y
    46  	if p == 0 {
    47  		return 0
    48  	}
    49  	return bitpos(1) << uint(bits.Len64(uint64(p))-1) // uint conversion needed for go1.12
    50  }
    51  
    52  // zeroBit returns true if k has a 0 bit at position `b`.
    53  func zeroBit(k prefix, b bitpos) bool {
    54  	return (uint64(k) & uint64(b)) == 0
    55  }
    56  
    57  // matchPrefix returns true if a prefix k matches a prefix p up to position `b`.
    58  func matchPrefix(k prefix, p prefix, b bitpos) bool {
    59  	return mask(k, b) == p
    60  }
    61  
    62  // mask returns a prefix of `k` with all bits after and including `b` zeroed out.
    63  //
    64  // In big endian encoding, this value is the [64-(m-1)] most significant bits of k
    65  // followed by a `0` bit at bitpos m, followed m-1 `1` bits.
    66  // Examples:
    67  //  prefix(0b1011) for a bitpos 0b0100 represents the keys:
    68  //    0b1000, 0b1001, 0b1010, 0b1011, 0b1100, 0b1101, 0b1110, 0b1111
    69  //
    70  // This mask function has the property that if matchPrefix(k, p, b), then
    71  // k <= p if and only if zeroBit(k, m). This induces binary search tree tries.
    72  // See Okasaki & Gill for more details about this choice of mask function.
    73  //
    74  // mask is idempotent for a given `b`, i.e. mask(mask(p, b), b) == mask(p,b).
    75  func mask(k prefix, b bitpos) prefix {
    76  	return prefix((uint64(k) | (uint64(b) - 1)) & (^uint64(b)))
    77  }
    78  
    79  // ord returns true if m comes before n in the bit ordering.
    80  func ord(m, n bitpos) bool {
    81  	return m > n // big endian encoding
    82  }
    83  
    84  // prefixesOverlap returns true if there is some key a prefix `p` for bitpos `m`
    85  // can hold that can also be held by a prefix `q` for some bitpos `n`.
    86  //
    87  // This is equivalent to:
    88  //   m ==n && p == q,
    89  //   higher(m, n) && matchPrefix(q, p, m), or
    90  //   higher(n, m) && matchPrefix(p, q, n)
    91  func prefixesOverlap(p prefix, m bitpos, q prefix, n bitpos) bool {
    92  	fbb := n
    93  	if ord(m, n) {
    94  		fbb = m
    95  	}
    96  	return mask(p, fbb) == mask(q, fbb)
    97  	// Lemma:
    98  	//   mask(p, fbb) == mask(q, fbb)
    99  	// iff
   100  	//   m > n && matchPrefix(q, p, m) or  (note: big endian encoding)
   101  	//   m < n && matchPrefix(p, q, n) or  (note: big endian encoding)
   102  	//   m ==n && p == q
   103  	// Quick-n-dirty proof:
   104  	// p == mask(p0, m) for some p0 by precondition.
   105  	// q == mask(q0, n) for some q0 by precondition.
   106  	// So mask(p, m) == p and mask(q, n) == q as mask(*, n') is idempotent.
   107  	//
   108  	// [=> proof]
   109  	// Suppose mask(p, fbb) == mask(q, fbb).
   110  	// if m ==n, p == mask(p, m) == mask(p, fbb) == mask(q, fbb) == mask(q, n) == q
   111  	// if m > n, fbb = firstBranchBit(m, n) = m (big endian).
   112  	//   p == mask(p, m) == mask(p, fbb) == mask(q, fbb) == mask(q, m)
   113  	//   so mask(q, m) == p or matchPrefix(q, p, m)
   114  	// if m < n, is symmetric to the above.
   115  	//
   116  	// [<= proof]
   117  	// case m ==n && p == q. Then mask(p, fbb) == mask(q, fbb)
   118  	//
   119  	// case m > n && matchPrefix(q, p, m).
   120  	// fbb == firstBranchBit(m, n) == m (by m>n).
   121  	// mask(q, fbb) == mask(q, m) == p == mask(p, m) == mask(p, fbb)
   122  	//
   123  	// case m < n && matchPrefix(p, q, n) is symmetric.
   124  }