Skip to content

Commit 6a279b9

Browse files
committed
fieldpath: Implement PathElementMap to map PEs to interface{}
1 parent 66711bf commit 6a279b9

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

Diff for: fieldpath/pathelementmap.go

+35-8
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,15 @@ import (
2828
// for PathElementSet and SetNodeMap, so we could probably share the
2929
// code.
3030
type PathElementValueMap struct {
31-
members sortedPathElementValues
31+
valueMap PathElementMap
3232
}
3333

3434
func MakePathElementValueMap(size int) PathElementValueMap {
3535
return PathElementValueMap{
36-
members: make(sortedPathElementValues, 0, size),
36+
valueMap: MakePathElementMap(size),
3737
}
3838
}
3939

40-
type pathElementValue struct {
41-
PathElement PathElement
42-
Value value.Value
43-
}
44-
4540
type sortedPathElementValues []pathElementValue
4641

4742
// Implement the sort interface; this would permit bulk creation, which would
@@ -55,6 +50,38 @@ func (spev sortedPathElementValues) Swap(i, j int) { spev[i], spev[j] = spev[j],
5550
// Insert adds the pathelement and associated value in the map.
5651
// If insert is called twice with the same PathElement, the value is replaced.
5752
func (s *PathElementValueMap) Insert(pe PathElement, v value.Value) {
53+
s.valueMap.Insert(pe, v)
54+
}
55+
56+
// Get retrieves the value associated with the given PathElement from the map.
57+
// (nil, false) is returned if there is no such PathElement.
58+
func (s *PathElementValueMap) Get(pe PathElement) (value.Value, bool) {
59+
v, ok := s.valueMap.Get(pe)
60+
if !ok {
61+
return nil, false
62+
}
63+
return v.(value.Value), true
64+
}
65+
66+
// PathElementValueMap is a map from PathElement to interface{}.
67+
type PathElementMap struct {
68+
members sortedPathElementValues
69+
}
70+
71+
type pathElementValue struct {
72+
PathElement PathElement
73+
Value interface{}
74+
}
75+
76+
func MakePathElementMap(size int) PathElementMap {
77+
return PathElementMap{
78+
members: make(sortedPathElementValues, 0, size),
79+
}
80+
}
81+
82+
// Insert adds the pathelement and associated value in the map.
83+
// If insert is called twice with the same PathElement, the value is replaced.
84+
func (s *PathElementMap) Insert(pe PathElement, v interface{}) {
5885
loc := sort.Search(len(s.members), func(i int) bool {
5986
return !s.members[i].PathElement.Less(pe)
6087
})
@@ -73,7 +100,7 @@ func (s *PathElementValueMap) Insert(pe PathElement, v value.Value) {
73100

74101
// Get retrieves the value associated with the given PathElement from the map.
75102
// (nil, false) is returned if there is no such PathElement.
76-
func (s *PathElementValueMap) Get(pe PathElement) (value.Value, bool) {
103+
func (s *PathElementMap) Get(pe PathElement) (interface{}, bool) {
77104
loc := sort.Search(len(s.members), func(i int) bool {
78105
return !s.members[i].PathElement.Less(pe)
79106
})

0 commit comments

Comments
 (0)