mirror of
https://github.com/golang/go
synced 2024-11-19 11:14:47 -07:00
7e7d99b4c9
Running the interpreter on (most of) the tests package in "encoding" unearthed a couple of ssa.builder bugs, already fixed. This CL contains the interpreter fixes that were required. (The "encoding" tests aren't added to the suite since they're slow.) Added intrinsics for: math.Exp math.Min hash/crc32.haveSSE42 (reflect.Type).Field (reflect.Type).NumField (reflect.Type).NumMethod reflect.New (reflect.Value).NumMethod syscall.RawSyscall (returns ENOSYS) reflect.Set (a no-op) Treat unsafe.Pointer -> *T conversions by returning new(T). This is incorrect but at least preserves type-safety, which is sufficient for these tests. hashmap: treat nil *hashmap as an empty map. R=gri CC=golang-dev https://golang.org/cl/12901046
113 lines
2.5 KiB
Go
113 lines
2.5 KiB
Go
// Copyright 2013 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package interp
|
|
|
|
// Custom hashtable atop map.
|
|
// For use when the key's equivalence relation is not consistent with ==.
|
|
|
|
// The Go specification doesn't address the atomicity of map operations.
|
|
// The FAQ states that an implementation is permitted to crash on
|
|
// concurrent map access.
|
|
|
|
import (
|
|
"code.google.com/p/go.tools/go/types"
|
|
)
|
|
|
|
type hashable interface {
|
|
hash() int
|
|
eq(x interface{}) bool
|
|
}
|
|
|
|
type entry struct {
|
|
key hashable
|
|
value value
|
|
next *entry
|
|
}
|
|
|
|
// A hashtable atop the built-in map. Since each bucket contains
|
|
// exactly one hash value, there's no need to perform hash-equality
|
|
// tests when walking the linked list. Rehashing is done by the
|
|
// underlying map.
|
|
type hashmap struct {
|
|
table map[int]*entry
|
|
length int // number of entries in map
|
|
}
|
|
|
|
// makeMap returns an empty initialized map of key type kt,
|
|
// preallocating space for reserve elements.
|
|
func makeMap(kt types.Type, reserve int) value {
|
|
if usesBuiltinMap(kt) {
|
|
return make(map[value]value, reserve)
|
|
}
|
|
return &hashmap{table: make(map[int]*entry, reserve)}
|
|
}
|
|
|
|
// delete removes the association for key k, if any.
|
|
func (m *hashmap) delete(k hashable) {
|
|
if m != nil {
|
|
hash := k.hash()
|
|
head := m.table[hash]
|
|
if head != nil {
|
|
if k.eq(head.key) {
|
|
m.table[hash] = head.next
|
|
m.length--
|
|
return
|
|
}
|
|
prev := head
|
|
for e := head.next; e != nil; e = e.next {
|
|
if k.eq(e.key) {
|
|
prev.next = e.next
|
|
m.length--
|
|
return
|
|
}
|
|
prev = e
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// lookup returns the value associated with key k, if present, or
|
|
// value(nil) otherwise.
|
|
func (m *hashmap) lookup(k hashable) value {
|
|
if m != nil {
|
|
hash := k.hash()
|
|
for e := m.table[hash]; e != nil; e = e.next {
|
|
if k.eq(e.key) {
|
|
return e.value
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// insert updates the map to associate key k with value v. If there
|
|
// was already an association for an eq() (though not necessarily ==)
|
|
// k, the previous key remains in the map and its associated value is
|
|
// updated.
|
|
func (m *hashmap) insert(k hashable, v value) {
|
|
hash := k.hash()
|
|
head := m.table[hash]
|
|
for e := head; e != nil; e = e.next {
|
|
if k.eq(e.key) {
|
|
e.value = v
|
|
return
|
|
}
|
|
}
|
|
m.table[hash] = &entry{
|
|
key: k,
|
|
value: v,
|
|
next: head,
|
|
}
|
|
m.length++
|
|
}
|
|
|
|
// len returns the number of key/value associations in the map.
|
|
func (m *hashmap) len() int {
|
|
if m != nil {
|
|
return m.length
|
|
}
|
|
return 0
|
|
}
|