| 1 | // Copyright 2013 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 interpscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/ |
| 6 | |
| 7 | // Custom hashtable atop map. |
| 8 | // For use when the key's equivalence relation is not consistent with ==. |
| 9 | |
| 10 | // The Go specification doesn't address the atomicity of map operations. |
| 11 | // The FAQ states that an implementation is permitted to crash on |
| 12 | // concurrent map access. |
| 13 | |
| 14 | import ( |
| 15 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 16 | ) |
| 17 | |
| 18 | type hashablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable# interface { |
| 19 | hashscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#hash.(t typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#) int |
| 20 | eqscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#eq.(t typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#, x any) bool |
| 21 | } |
| 22 | |
| 23 | type entryscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry# struct { |
| 24 | keyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#key. hashablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable# |
| 25 | valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#value. valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/value# |
| 26 | nextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#next. *entryscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry# |
| 27 | } |
| 28 | |
| 29 | // A hashtable atop the built-in map. Since each bucket contains |
| 30 | // exactly one hash value, there's no need to perform hash-equality |
| 31 | // tests when walking the linked list. Rehashing is done by the |
| 32 | // underlying map. |
| 33 | type hashmapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap# struct { |
| 34 | keyTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#keyType. typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type# |
| 35 | tablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#table. map[int]*entryscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry# |
| 36 | lengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#length. int // number of entries in map |
| 37 | } |
| 38 | |
| 39 | // makeMap returns an empty initialized map of key type kt, |
| 40 | // preallocating space for reserve elements. |
| 41 | func makeMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/makeMap().(kt typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#, reserve int64) valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/value# { |
| 42 | if usesBuiltinMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/usesBuiltinMap().(kt) { |
| 43 | return make(map[valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/value#]valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/value#, reserve) |
| 44 | } |
| 45 | return &hashmapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#{keyTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#keyType.: kt, tablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#table.: make(map[int]*entryscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#, reserve)} |
| 46 | } |
| 47 | |
| 48 | // delete removes the association for key k, if any. |
| 49 | func (m *hashmapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#) deletescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#delete().(k hashablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#) { |
| 50 | if m != nil { |
| 51 | hash := k.hashscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#hash.(m.keyTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#keyType.) |
| 52 | head := m.tablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#table.[hash] |
| 53 | if head != nil { |
| 54 | if k.eqscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#eq.(m.keyTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#keyType., head.keyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#key.) { |
| 55 | m.tablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#table.[hash] = head.nextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#next. |
| 56 | m.lengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#length.-- |
| 57 | return |
| 58 | } |
| 59 | prev := head |
| 60 | for e := head.nextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#next.; e != nil; e = e.nextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#next. { |
| 61 | if k.eqscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#eq.(m.keyTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#keyType., e.keyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#key.) { |
| 62 | prev.nextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#next. = e.nextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#next. |
| 63 | m.lengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#length.-- |
| 64 | return |
| 65 | } |
| 66 | prev = e |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // lookup returns the value associated with key k, if present, or |
| 73 | // value(nil) otherwise. |
| 74 | func (m *hashmapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#) lookupscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#lookup().(k hashablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#) valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/value# { |
| 75 | if m != nil { |
| 76 | hash := k.hashscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#hash.(m.keyTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#keyType.) |
| 77 | for e := m.tablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#table.[hash]; e != nil; e = e.nextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#next. { |
| 78 | if k.eqscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#eq.(m.keyTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#keyType., e.keyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#key.) { |
| 79 | return e.valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#value. |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | // insert updates the map to associate key k with value v. If there |
| 87 | // was already an association for an eq() (though not necessarily ==) |
| 88 | // k, the previous key remains in the map and its associated value is |
| 89 | // updated. |
| 90 | func (m *hashmapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#) insertscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#insert().(k hashablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#, v valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/value#) { |
| 91 | hash := k.hashscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#hash.(m.keyTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#keyType.) |
| 92 | head := m.tablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#table.[hash] |
| 93 | for e := head; e != nil; e = e.nextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#next. { |
| 94 | if k.eqscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashable#eq.(m.keyTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#keyType., e.keyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#key.) { |
| 95 | e.valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#value. = v |
| 96 | return |
| 97 | } |
| 98 | } |
| 99 | m.tablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#table.[hash] = &entryscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#{ |
| 100 | keyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#key.: k, |
| 101 | valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#value.: v, |
| 102 | nextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry#next.: head, |
| 103 | } |
| 104 | m.lengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#length.++ |
| 105 | } |
| 106 | |
| 107 | // len returns the number of key/value associations in the map. |
| 108 | func (m *hashmapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#) lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#len().() int { |
| 109 | if m != nil { |
| 110 | return m.lengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#length. |
| 111 | } |
| 112 | return 0 |
| 113 | } |
| 114 | |
| 115 | // entries returns a rangeable map of entries. |
| 116 | func (m *hashmapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#) entriesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#entries().() map[int]*entryscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/entry# { |
| 117 | if m != nil { |
| 118 | return m.tablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/interp`/hashmap#table. |
| 119 | } |
| 120 | return nil |
| 121 | } |