Files
monkey/internal/object/hash.go
Charles Smith aebbe43999
Some checks failed
Build / build (push) Successful in 10m25s
Publish Image / publish (push) Failing after 39s
Test / build (push) Successful in 11m19s
Fix VM memory allocation optimizations by reducing what we allocate on the heap
2024-03-31 20:44:50 -04:00

143 lines
2.3 KiB
Go

package object
import (
"bytes"
"fmt"
"hash/fnv"
"strings"
)
type HashKey struct {
Type Type
Value uint64
}
func (b Boolean) HashKey() HashKey {
var value uint64
if b.Value {
value = 1
} else {
value = 0
}
return HashKey{Type: b.Type(), Value: value}
}
func (i Integer) HashKey() HashKey {
return HashKey{Type: i.Type(), Value: uint64(i.Value)}
}
func (s String) HashKey() HashKey {
h := fnv.New64a()
h.Write([]byte(s.Value))
return HashKey{Type: s.Type(), Value: h.Sum64()}
}
type HashPair struct {
Key Object
Value Object
}
type Hash struct {
Pairs map[HashKey]HashPair
}
func (h *Hash) Len() int {
return len(h.Pairs)
}
func (h *Hash) Bool() bool {
return len(h.Pairs) > 0
}
func (h *Hash) Type() Type {
return HashType
}
func (h *Hash) Inspect() string {
var out bytes.Buffer
pairs := []string{}
for _, pair := range h.Pairs {
pairs = append(pairs, fmt.Sprintf("%s: %s", pair.Key.Inspect(), pair.Value.Inspect()))
}
out.WriteString("{")
out.WriteString(strings.Join(pairs, ", "))
out.WriteString("}")
return out.String()
}
func (h *Hash) String() string {
return h.Inspect()
}
func (h *Hash) Add(other Object) (Object, error) {
if other.Type() != h.Type() {
return nil, NewBinaryOpError(h, other, "+")
}
pairs := make(map[HashKey]HashPair)
for k, v := range h.Pairs {
pairs[k] = v
}
for k, v := range other.(*Hash).Pairs {
pairs[k] = v
}
return &Hash{Pairs: pairs}, nil
}
func (h *Hash) Get(index Object) (Object, error) {
key, ok := index.(Hashable)
if !ok {
return nil, fmt.Errorf("invalid hash key %s", index.Type())
}
pair, found := h.Pairs[key.HashKey()]
if !found {
return Null{}, nil
}
return pair.Value, nil
}
func (h *Hash) Set(index, other Object) error {
key, ok := index.(Hashable)
if !ok {
return fmt.Errorf("invalid hash key %s", index.Type())
}
h.Pairs[key.HashKey()] = HashPair{Key: index, Value: other}
return nil
}
func (h *Hash) Compare(other Object) int {
if obj, ok := other.(*Hash); ok {
if len(h.Pairs) != len(obj.Pairs) {
return -1
}
for _, pair := range h.Pairs {
left := pair.Value
hashed := left.(Hashable)
right, ok := obj.Pairs[hashed.HashKey()]
if !ok {
return -1
}
cmp, ok := left.(Comparable)
if !ok {
return -1
}
if cmp.Compare(right.Value) != 0 {
return cmp.Compare(right.Value)
}
}
return 0
}
return -1
}