Skip to content

Commit

Permalink
refactor: 💡 use node ID in node hash memo
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanonymous-GitHub committed Jul 5, 2024
1 parent a77f445 commit b606ef0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
12 changes: 6 additions & 6 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type AST interface {
UpdateLabel(n *Node, newLabel NodeLabelType) error

// MakeHashMemo creates a new hash memo for the entire AST.
MakeHashMemo() *NodeHashMemo
MakeHashMemo() NodeHashMemo
}

type astConcrete struct {
Expand Down Expand Up @@ -74,8 +74,8 @@ func (a *astConcrete) Add(parent *Node, i int, label NodeLabelType, value NodeVa
a.root = newNode
}

a.nodes[newNode.id] = newNode
return a.nodes[newNode.id], nil
a.nodes[newNode.Id] = newNode
return a.nodes[newNode.Id], nil
}

func (a *astConcrete) Move(n, newParent *Node, i int) error {
Expand All @@ -90,7 +90,7 @@ func (a *astConcrete) Delete(n *Node) error {
}

n.DestroySubtree()
delete(a.nodes, n.id)
delete(a.nodes, n.Id)
return nil
}

Expand Down Expand Up @@ -120,14 +120,14 @@ func (a *astConcrete) UpdateLabel(n *Node, newLabel NodeLabelType) error {
return nil
}

func (a *astConcrete) MakeHashMemo() *NodeHashMemo {
func (a *astConcrete) MakeHashMemo() NodeHashMemo {
if a.root == nil {
return nil
}

memo := make(NodeHashMemo)
_ = a.root.HashValue(&memo)
return &memo
return memo
}

// NewAST creates a new AST.
Expand Down
7 changes: 0 additions & 7 deletions ast/hash_memo.go

This file was deleted.

23 changes: 15 additions & 8 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ type Node struct {
Value NodeValueType
Parent *Node
Children map[int]*Node
id NodeIdType
Id NodeIdType
idxToParent int
}

type NodeHashMemo map[NodeIdType]uint64

type NodeParentInfo struct {
// The Parent of a Node.
Parent *Node
Expand All @@ -46,7 +48,7 @@ func NewNode(parentInfo NodeParentInfo, label NodeLabelType, value NodeValueType
Value: value,
Parent: nil,
Children: make(map[int]*Node),
id: newIdStr,
Id: newIdStr,
idxToParent: -1,
}

Expand Down Expand Up @@ -121,6 +123,14 @@ func (n *Node) ValueOfOrder() int {
return n.Height()
}

func (n *Node) OrderedChildren() []*Node {
children := maps.Values(n.Children)
sort.Slice(children, func(i, j int) bool {
return children[i].idxToParent < children[j].idxToParent
})
return children
}

// Isomorphic returns true if the Node is isomorphic to the other Node.
func (n *Node) Isomorphic(other *Node) bool {
if n == nil || other == nil {
Expand All @@ -138,15 +148,12 @@ func (n *Node) HashValue(memo *NodeHashMemo) uint64 {
if memo != nil {
lock.Lock()
defer lock.Unlock()
(*memo)[n.id] = propertyHash
(*memo)[n.Id] = propertyHash
}
return propertyHash
}

children := maps.Values(n.Children)
sort.Slice(children, func(i, j int) bool {
return children[i].idxToParent < children[j].idxToParent
})
children := n.OrderedChildren()

var combinedChildrenHash xxhash.Digest
_, _ = combinedChildrenHash.WriteString(strconv.FormatUint(propertyHash, 10))
Expand All @@ -160,7 +167,7 @@ func (n *Node) HashValue(memo *NodeHashMemo) uint64 {
if memo != nil {
lock.Lock()
defer lock.Unlock()
(*memo)[n.id] = result
(*memo)[n.Id] = result
}
return result
}

0 comments on commit b606ef0

Please sign in to comment.