Documentation
¶
Index ¶
- func ComputeKeyRoots(root *TreeNode) []int
- func ComputeLeftMostLeaves(root *TreeNode)
- func PostOrderTraversal(root *TreeNode)
- func PrepareTreeForAPTED(root *TreeNode) []int
- type APTEDAnalyzer
- func (a *APTEDAnalyzer) BatchComputeDistances(pairs [][2]*TreeNode) []float64
- func (a *APTEDAnalyzer) ClusterSimilarTrees(trees []*TreeNode, similarityThreshold float64) *ClusterResult
- func (a *APTEDAnalyzer) ComputeDetailedDistance(tree1, tree2 *TreeNode) *TreeEditResult
- func (a *APTEDAnalyzer) ComputeDistance(tree1, tree2 *TreeNode) float64
- func (a *APTEDAnalyzer) ComputeDistanceAndSimilarity(tree1, tree2 *TreeNode) (float64, float64)
- func (a *APTEDAnalyzer) ComputeSimilarity(tree1, tree2 *TreeNode) float64
- type ClusterResult
- type CostModel
- type DefaultCostModel
- type NormalizationMode
- type OptimizedAPTEDAnalyzer
- type TreeEditResult
- type TreeNode
- type WeightedCostModel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ComputeKeyRoots ¶
ComputeKeyRoots identifies key roots for path decomposition.
func ComputeLeftMostLeaves ¶
func ComputeLeftMostLeaves(root *TreeNode)
ComputeLeftMostLeaves computes left-most leaf descendants for all nodes.
func PostOrderTraversal ¶
func PostOrderTraversal(root *TreeNode)
PostOrderTraversal performs post-order traversal and assigns post-order IDs.
func PrepareTreeForAPTED ¶
PrepareTreeForAPTED prepares a tree for APTED algorithm by computing all necessary indices.
Types ¶
type APTEDAnalyzer ¶
type APTEDAnalyzer struct {
// contains filtered or unexported fields
}
APTEDAnalyzer implements the APTED (All Path Tree Edit Distance) algorithm. Based on Pawlik & Augsten's optimal O(n^2 log n) algorithm.
func NewAPTEDAnalyzer ¶
func NewAPTEDAnalyzer(costModel CostModel) *APTEDAnalyzer
NewAPTEDAnalyzer creates a new APTED analyzer with the given cost model. Default normalization mode is NormalizeByMax.
func NewAPTEDAnalyzerWithNormalization ¶
func NewAPTEDAnalyzerWithNormalization(costModel CostModel, mode NormalizationMode) *APTEDAnalyzer
NewAPTEDAnalyzerWithNormalization creates an APTED analyzer with a specific normalization mode.
func (*APTEDAnalyzer) BatchComputeDistances ¶
func (a *APTEDAnalyzer) BatchComputeDistances(pairs [][2]*TreeNode) []float64
BatchComputeDistances computes distances between multiple tree pairs.
func (*APTEDAnalyzer) ClusterSimilarTrees ¶
func (a *APTEDAnalyzer) ClusterSimilarTrees(trees []*TreeNode, similarityThreshold float64) *ClusterResult
ClusterSimilarTrees clusters trees based on similarity threshold.
func (*APTEDAnalyzer) ComputeDetailedDistance ¶
func (a *APTEDAnalyzer) ComputeDetailedDistance(tree1, tree2 *TreeNode) *TreeEditResult
ComputeDetailedDistance computes detailed tree edit distance information.
func (*APTEDAnalyzer) ComputeDistance ¶
func (a *APTEDAnalyzer) ComputeDistance(tree1, tree2 *TreeNode) float64
ComputeDistance computes the tree edit distance between two trees.
func (*APTEDAnalyzer) ComputeDistanceAndSimilarity ¶
func (a *APTEDAnalyzer) ComputeDistanceAndSimilarity(tree1, tree2 *TreeNode) (float64, float64)
ComputeDistanceAndSimilarity computes both APTED distance and normalized similarity from one distance pass.
func (*APTEDAnalyzer) ComputeSimilarity ¶
func (a *APTEDAnalyzer) ComputeSimilarity(tree1, tree2 *TreeNode) float64
ComputeSimilarity computes similarity score between two trees (0.0 to 1.0). The normalization mode is controlled by the analyzer's NormalizationMode setting.
type ClusterResult ¶
ClusterResult represents the result of tree clustering.
type CostModel ¶
type CostModel interface {
Insert(node *TreeNode) float64
Delete(node *TreeNode) float64
Rename(node1, node2 *TreeNode) float64
}
CostModel defines the interface for calculating edit operation costs. Language-specific cost models (e.g. PythonCostModel, JavaScriptCostModel) should implement this interface in their respective projects.
type DefaultCostModel ¶
type DefaultCostModel struct{}
DefaultCostModel implements a uniform cost model where all operations cost 1.0.
func NewDefaultCostModel ¶
func NewDefaultCostModel() *DefaultCostModel
func (*DefaultCostModel) Delete ¶
func (c *DefaultCostModel) Delete(node *TreeNode) float64
func (*DefaultCostModel) Insert ¶
func (c *DefaultCostModel) Insert(node *TreeNode) float64
func (*DefaultCostModel) Rename ¶
func (c *DefaultCostModel) Rename(node1, node2 *TreeNode) float64
type NormalizationMode ¶
type NormalizationMode int
NormalizationMode controls how similarity is normalized from distance.
const ( // NormalizeByMax uses max(size1, size2) — stricter, reduces false positives. // Used by both pyscn and jscan. NormalizeByMax NormalizationMode = iota // NormalizeBySum uses size1 + size2 (Jaccard-like). NormalizeBySum )
type OptimizedAPTEDAnalyzer ¶
type OptimizedAPTEDAnalyzer struct {
*APTEDAnalyzer
// contains filtered or unexported fields
}
OptimizedAPTEDAnalyzer extends APTEDAnalyzer with early stopping.
func NewOptimizedAPTEDAnalyzer ¶
func NewOptimizedAPTEDAnalyzer(costModel CostModel, maxDistance float64) *OptimizedAPTEDAnalyzer
NewOptimizedAPTEDAnalyzer creates an optimized APTED analyzer.
func (*OptimizedAPTEDAnalyzer) ComputeDetailedDistance ¶ added in v0.2.2
func (a *OptimizedAPTEDAnalyzer) ComputeDetailedDistance(tree1, tree2 *TreeNode) *TreeEditResult
ComputeDetailedDistance computes detailed tree edit distance information with the analyzer's early-stopping cap applied (see ComputeDistanceAndSimilarity).
func (*OptimizedAPTEDAnalyzer) ComputeDistance ¶
func (a *OptimizedAPTEDAnalyzer) ComputeDistance(tree1, tree2 *TreeNode) float64
ComputeDistance computes tree edit distance with early stopping optimization.
func (*OptimizedAPTEDAnalyzer) ComputeDistanceAndSimilarity ¶ added in v0.2.2
func (a *OptimizedAPTEDAnalyzer) ComputeDistanceAndSimilarity(tree1, tree2 *TreeNode) (float64, float64)
ComputeDistanceAndSimilarity computes both distance and similarity with the analyzer's early-stopping cap applied. Without this override, the method promoted from the embedded APTEDAnalyzer would bypass the cap and return the full uncapped distance.
type TreeEditResult ¶
type TreeEditResult struct {
Distance float64
Similarity float64
Tree1Size int
Tree2Size int
Operations int
}
TreeEditResult holds the result of tree edit distance computation.
type TreeNode ¶
type TreeNode struct {
ID int
Label string
Children []*TreeNode
Parent *TreeNode
// APTED-specific fields for optimization
PostOrderID int
LeftMostLeaf int
KeyRoot bool
// OriginalNode holds the original language-specific AST node.
// This field is opaque to polyscan core; language adapters store their
// own node type here and recover it via type assertion:
//
// // pyscn
// pyNode := treeNode.OriginalNode.(*parser.Node)
//
// Populating it is a deliberate choice, not a default. A parser node that
// carries a parent pointer pins its whole file AST for as long as the
// converted tree lives, which is the entire run for clone detection —
// jscan's converter therefore leaves this empty, so do not assume an
// adapter has filled it in.
OriginalNode any
// contains filtered or unexported fields
}
TreeNode represents a node in the ordered tree for APTED algorithm. This is a language-agnostic representation; language-specific parsers should convert their AST nodes into TreeNode via a TreeConverter.
func GetNodeByPostOrderID ¶
GetNodeByPostOrderID finds a node by its post-order ID.
func GetSubtreeNodes ¶
GetSubtreeNodes returns all nodes in the subtree rooted at the given node.
func GetSubtreeNodesWithDepthLimit ¶
GetSubtreeNodesWithDepthLimit returns all nodes with maximum recursion depth limit.
func NewTreeNode ¶
NewTreeNode creates a new tree node with the given ID and label.
func (*TreeNode) HeightWithDepthLimit ¶
HeightWithDepthLimit returns the height with maximum recursion depth limit.
func (*TreeNode) SizeWithDepthLimit ¶
SizeWithDepthLimit returns the size with maximum recursion depth limit.
type WeightedCostModel ¶
type WeightedCostModel struct {
InsertWeight float64
DeleteWeight float64
RenameWeight float64
BaseCostModel CostModel
}
WeightedCostModel allows custom weights for different operation types.
func NewWeightedCostModel ¶
func NewWeightedCostModel(insertWeight, deleteWeight, renameWeight float64, baseCostModel CostModel) *WeightedCostModel
func (*WeightedCostModel) Delete ¶
func (c *WeightedCostModel) Delete(node *TreeNode) float64
func (*WeightedCostModel) Insert ¶
func (c *WeightedCostModel) Insert(node *TreeNode) float64
func (*WeightedCostModel) Rename ¶
func (c *WeightedCostModel) Rename(node1, node2 *TreeNode) float64