Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Nov 26, 2024
2 parents 2912da8 + 69644b4 commit 992be15
Show file tree
Hide file tree
Showing 15 changed files with 1,599 additions and 498 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ private ExtNodeImpl create(final Node node, final ExtNode parent) {
final ExtNodeImpl ext = new ExtNodeImpl();
ext.prototype = node;
ext.parent = parent;
ext.index = -1;
ext.hash = this.hashes.calculate(node);
final int count = node.getChildCount();
ext.children = new ExtNodeImpl[count];
int index;
for (index = 0; index < count; index = index + 1) {
ext.children[index] = this.create(node.getChild(index), ext);
final ExtNodeImpl child = this.create(node.getChild(index), ext);
child.index = index;
ext.children[index] = child;
}
for (index = 1; index < count; index = index + 1) {
ext.children[index].left = ext.children[index - 1];
Expand All @@ -96,6 +99,11 @@ private static final class ExtNodeImpl implements ExtNode {
*/
private ExtNode parent;

/**
* Index (sequence number) of this node in the list of children of this node's parent.
*/
private int index;

/**
* Left neighbor node of this node, which is the node whose index in the parent node
* is one less than the index of this node.
Expand Down Expand Up @@ -128,6 +136,11 @@ public ExtNode getParent() {
return this.parent;
}

@Override
public int getIndex() {
return this.index;
}

@Override
public ExtNode getLeft() {
return this.left;
Expand Down Expand Up @@ -159,13 +172,13 @@ public int getChildCount() {
}

@Override
public Node getChild(final int index) {
return this.children[index];
public Node getChild(final int idx) {
return this.children[idx];
}

@Override
public ExtNode getExtChild(final int index) {
return this.children[index];
public ExtNode getExtChild(final int idx) {
return this.children[idx];
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Ivan Kniazkov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cqfn.astranaut.core.algorithms.mapping;

import java.util.Objects;
import org.cqfn.astranaut.core.base.ExtNode;
import org.cqfn.astranaut.core.base.Insertion;
import org.cqfn.astranaut.core.base.Node;

/**
* Insertion descriptor like {@link Insertion}, but working with extended nodes.
* @since 2.0.0
*/
final class ExtInsertion {
/**
* Node being inserted.
*/
private final ExtNode inserted;

/**
* Parent node into which the child node will be inserted.
*/
private final ExtNode into;

/**
* Child node after which to insert.
*/
private final ExtNode after;

/**
* Constructor.
* @param inserted Node being inserted
* @param into Parent node into which the child node will be inserted
* @param after Child node after which to insert
*/
ExtInsertion(final ExtNode inserted, final ExtNode into, final ExtNode after) {
this.inserted = Objects.requireNonNull(inserted);
this.into = into;
this.after = after;
}

/**
* Converts the descriptor to a 'classic' {@link Insertion}.
* @return An insertion descriptor that uses non-extended nodes
*/
public Insertion toInsertion() {
final Node third;
if (this.after == null) {
third = null;
} else {
third = this.after.getPrototype();
}
return new Insertion(this.inserted.getPrototype(), this.into.getPrototype(), third);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
*/
package org.cqfn.astranaut.core.algorithms.mapping;

import java.util.List;
import java.util.Map;
import java.util.Set;
import org.cqfn.astranaut.core.base.Insertion;
import org.cqfn.astranaut.core.base.Node;

/**
* A mapping from one syntactic tree ('left') to another ('right'), i.e.,
* set of correspondences between the nodes of one tree and the nodes of another tree.
*
* set of correspondences between the nodes of one tree and the nodes of another tree.
* @since 1.1.0
*/
public interface Mapping {
Expand All @@ -52,10 +52,11 @@ public interface Mapping {
Node getLeft(Node right);

/**
* Returns a collection of nodes that must be added to the 'left' tree to get the 'right' tree.
* @return The set of inserted nodes
* Returns an ordered list of nodes that must be added to the 'left' tree
* to get the 'right' tree. Insertion should be performed in the order defined in the list.
* @return The list of inserted nodes
*/
Set<Insertion> getInserted();
List<Insertion> getInserted();

/**
* Returns relationship between the nodes of the 'left' tree that have been replaced
Expand All @@ -71,4 +72,13 @@ public interface Mapping {
* @return The set of deleted nodes
*/
Set<Node> getDeleted();

/**
* Returns the total number of actions to be done on the 'left' tree to transform it
* to a 'right' tree.
* @return Total number of actions
*/
default int getNumberOfActions() {
return this.getInserted().size() + this.getReplaced().size() + this.getDeleted().size();
}
}
Loading

0 comments on commit 992be15

Please sign in to comment.