2013-09-12 09:00:31 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-05-17 14:20:39 -06:00
|
|
|
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 {
|
2013-09-16 13:22:19 -06:00
|
|
|
hash(t types.Type) int
|
|
|
|
eq(t types.Type, x interface{}) bool
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2013-09-16 13:22:19 -06:00
|
|
|
keyType types.Type
|
|
|
|
table map[int]*entry
|
|
|
|
length int // number of entries in map
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2013-09-16 13:22:19 -06:00
|
|
|
return &hashmap{keyType: kt, table: make(map[int]*entry, reserve)}
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete removes the association for key k, if any.
|
|
|
|
func (m *hashmap) delete(k hashable) {
|
2013-09-12 09:00:31 -06:00
|
|
|
if m != nil {
|
2013-09-16 13:22:19 -06:00
|
|
|
hash := k.hash(m.keyType)
|
2013-09-12 09:00:31 -06:00
|
|
|
head := m.table[hash]
|
|
|
|
if head != nil {
|
2013-09-16 13:22:19 -06:00
|
|
|
if k.eq(m.keyType, head.key) {
|
2013-09-12 09:00:31 -06:00
|
|
|
m.table[hash] = head.next
|
2013-05-17 14:20:39 -06:00
|
|
|
m.length--
|
|
|
|
return
|
|
|
|
}
|
2013-09-12 09:00:31 -06:00
|
|
|
prev := head
|
|
|
|
for e := head.next; e != nil; e = e.next {
|
2013-09-16 13:22:19 -06:00
|
|
|
if k.eq(m.keyType, e.key) {
|
2013-09-12 09:00:31 -06:00
|
|
|
prev.next = e.next
|
|
|
|
m.length--
|
|
|
|
return
|
|
|
|
}
|
|
|
|
prev = e
|
|
|
|
}
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup returns the value associated with key k, if present, or
|
|
|
|
// value(nil) otherwise.
|
|
|
|
func (m *hashmap) lookup(k hashable) value {
|
2013-09-12 09:00:31 -06:00
|
|
|
if m != nil {
|
2013-09-16 13:22:19 -06:00
|
|
|
hash := k.hash(m.keyType)
|
2013-09-12 09:00:31 -06:00
|
|
|
for e := m.table[hash]; e != nil; e = e.next {
|
2013-09-16 13:22:19 -06:00
|
|
|
if k.eq(m.keyType, e.key) {
|
2013-09-12 09:00:31 -06:00
|
|
|
return e.value
|
|
|
|
}
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
2013-09-16 13:22:19 -06:00
|
|
|
hash := k.hash(m.keyType)
|
2013-05-17 14:20:39 -06:00
|
|
|
head := m.table[hash]
|
|
|
|
for e := head; e != nil; e = e.next {
|
2013-09-16 13:22:19 -06:00
|
|
|
if k.eq(m.keyType, e.key) {
|
2013-05-17 14:20:39 -06:00
|
|
|
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 {
|
2013-09-12 09:00:31 -06:00
|
|
|
if m != nil {
|
|
|
|
return m.length
|
|
|
|
}
|
|
|
|
return 0
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|