-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from tsandall/virtual-docs
Fleshing out initial top down implementation
- Loading branch information
Showing
11 changed files
with
1,366 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2016 The OPA Authors. All rights reserved. | ||
// Use of this source code is governed by an Apache2 | ||
// license that can be found in the LICENSE file. | ||
|
||
package eval | ||
|
||
import "fmt" | ||
import "strings" | ||
|
||
import "github.com/open-policy-agent/opa/opalog" | ||
|
||
type hashEntry struct { | ||
k opalog.Value | ||
v opalog.Value | ||
next *hashEntry | ||
} | ||
|
||
type hashMap struct { | ||
table map[int]*hashEntry | ||
} | ||
|
||
func newHashMap() *hashMap { | ||
return &hashMap{make(map[int]*hashEntry)} | ||
} | ||
|
||
func (hm *hashMap) Get(k opalog.Value) opalog.Value { | ||
hash := k.Hash() | ||
for entry := hm.table[hash]; entry != nil; entry = entry.next { | ||
if entry.k.Equal(k) { | ||
return entry.v | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Iter invokes the iter function for each element in the hashMap. | ||
// If the iter function returns true, iteration stops and the return value is true. | ||
// If the iter function never returns true, iteration proceeds through all elements | ||
// and the return value is false. | ||
func (hm *hashMap) Iter(iter func(opalog.Value, opalog.Value) bool) bool { | ||
for _, entry := range hm.table { | ||
for ; entry != nil; entry = entry.next { | ||
if iter(entry.k, entry.v) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (hm *hashMap) Put(k opalog.Value, v opalog.Value) { | ||
hash := k.Hash() | ||
head := hm.table[hash] | ||
for entry := head; entry != nil; entry = entry.next { | ||
if entry.k.Equal(k) { | ||
entry.v = v | ||
return | ||
} | ||
} | ||
hm.table[hash] = &hashEntry{k: k, v: v, next: head} | ||
} | ||
|
||
func (hm *hashMap) String() string { | ||
var buf []string | ||
hm.Iter(func(k opalog.Value, v opalog.Value) bool { | ||
buf = append(buf, fmt.Sprintf("%v: %v", k, v)) | ||
return false | ||
}) | ||
return "{" + strings.Join(buf, ", ") + "}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2016 The OPA Authors. All rights reserved. | ||
// Use of this source code is governed by an Apache2 | ||
// license that can be found in the LICENSE file. | ||
|
||
package eval | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/opa/opalog" | ||
) | ||
|
||
func TestHashmapOverwrite(t *testing.T) { | ||
m := newHashMap() | ||
key := opalog.String("hello") | ||
expected := opalog.String("goodbye") | ||
m.Put(key, opalog.String("world")) | ||
m.Put(key, expected) | ||
result := m.Get(key) | ||
if result != expected { | ||
t.Errorf("Expected existing value to be overwritten but got %v for key %v", result, key) | ||
} | ||
} | ||
|
||
func TestIter(t *testing.T) { | ||
m := newHashMap() | ||
keys := []opalog.Number{opalog.Number(1), opalog.Number(2), opalog.Number(1.4)} | ||
value := opalog.Null{} | ||
for _, k := range keys { | ||
m.Put(k, value) | ||
} | ||
// 1 and 1.4 should both hash to 1. | ||
if len(m.table) != 2 { | ||
panic(fmt.Sprintf("Expected collision: %v", m)) | ||
} | ||
results := map[opalog.Value]opalog.Value{} | ||
m.Iter(func(k opalog.Value, v opalog.Value) bool { | ||
results[k] = v | ||
return false | ||
}) | ||
expected := map[opalog.Value]opalog.Value{ | ||
opalog.Number(1): value, | ||
opalog.Number(2): value, | ||
opalog.Number(1.4): value, | ||
} | ||
if !reflect.DeepEqual(results, expected) { | ||
t.Errorf("Expected %v but got %v", expected, results) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.