Search in sources :

Example 1 with Pair

use of mrmathami.utils.Pair in project Cpp4CIA by thanhminhmr.

the class PreprocessorBuilder method includeList.

@Nonnull
private static List<Path> includeList(@Nonnull List<Path> projectFiles, @Nonnull List<Path> includePaths) throws CppException {
    final List<Pair<Path, Set<Path>>> fileIncludesList = TranslationUnitBuilder.createFileIncludesList(projectFiles, includePaths);
    final int includeSize = fileIncludesList.size();
    final List<Path> includeList = new ArrayList<>(includeSize);
    final List<IntObjectPair<Path>> includeCounts = new ArrayList<>(includeSize);
    final Set<Path> includedPaths = new HashSet<>(includeSize);
    final List<Path> minimumCountPaths = new ArrayList<>(includeSize);
    while (includedPaths.size() < includeSize) {
        includeCounts.clear();
        for (final Pair<Path, Set<Path>> entry : fileIncludesList) {
            final Path fromPath = entry.getA();
            if (!includedPaths.contains(fromPath)) {
                int count = 0;
                for (final Path toPath : entry.getB()) {
                    if (!includedPaths.contains(toPath))
                        count++;
                }
                includeCounts.add(new IntObjectPair<>(fromPath, count));
            }
        }
        minimumCountPaths.clear();
        int minCount = Integer.MAX_VALUE;
        for (final IntObjectPair<Path> pair : includeCounts) {
            if (pair.value <= minCount) {
                if (pair.value < minCount) {
                    minCount = pair.value;
                    minimumCountPaths.clear();
                }
                minimumCountPaths.add(pair.object);
            }
        }
        includedPaths.addAll(minimumCountPaths);
        includeList.addAll(minimumCountPaths);
    }
    return includeList;
}
Also used : Path(java.nio.file.Path) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) Pair(mrmathami.utils.Pair) HashSet(java.util.HashSet) Nonnull(mrmathami.annotations.Nonnull)

Example 2 with Pair

use of mrmathami.utils.Pair in project Cpp4CIA by thanhminhmr.

the class TranslationUnitBuilder method createFileIncludesList.

@Nonnull
static List<Pair<Path, Set<Path>>> createFileIncludesList(@Nonnull List<Path> projectFiles, @Nonnull List<Path> includePaths) throws CppException {
    final List<Pair<Path, Set<Path>>> includeList = new ArrayList<>(projectFiles.size());
    final Map<Path, Path> projectFileMap = new HashMap<>(projectFiles.size());
    for (final Path projectFile : projectFiles) projectFileMap.put(projectFile, projectFile);
    for (final Path projectFile : projectFiles) {
        try (final CharArrayWriter writer = new CharArrayWriter()) {
            try (final Reader reader = EncodingDetector.createReader(Files.newInputStream(projectFile))) {
                reader.transferTo(writer);
            }
            final IASTTranslationUnit translationUnit = GPP_LANGUAGE.getASTTranslationUnit(FileContent.create(projectFile.toString(), writer.toCharArray()), SCANNER_INFO, EMPTY_PROVIDER, null, ILanguage.OPTION_NO_IMAGE_LOCATIONS | ILanguage.OPTION_SKIP_FUNCTION_BODIES | ILanguage.OPTION_SKIP_TRIVIAL_EXPRESSIONS_IN_AGGREGATE_INITIALIZERS, LOG_SERVICE);
            final Path currentFolder = projectFile.getParent();
            final Set<Path> includeSet = new HashSet<>();
            for (final IASTPreprocessorIncludeStatement includeDirective : translationUnit.getIncludeDirectives()) {
                final String includeFileName = includeDirective.getName().toString();
                if (!includeDirective.isSystemInclude()) {
                    final Path includeFile = currentFolder.resolve(includeFileName).normalize();
                    final Path normalizedIncludeFile = projectFileMap.get(includeFile);
                    if (normalizedIncludeFile != null) {
                        includeSet.add(normalizedIncludeFile);
                        continue;
                    }
                }
                for (final Path includePath : includePaths) {
                    final Path includeFile = includePath.resolve(includeFileName).normalize();
                    final Path normalizedIncludeFile = projectFileMap.get(includeFile);
                    if (normalizedIncludeFile != null) {
                        includeSet.add(normalizedIncludeFile);
                        break;
                    }
                }
            }
            includeList.add(Pair.immutableOf(projectFile, includeSet));
        } catch (CoreException e) {
            throw new CppException("Cannot create TranslationUnit!", e);
        } catch (IOException e) {
            throw new CppException("Cannot read project file!", e);
        }
    }
    return includeList;
}
Also used : Path(java.nio.file.Path) CppException(mrmathami.cia.cpp.CppException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) IOException(java.io.IOException) CharArrayWriter(java.io.CharArrayWriter) CoreException(org.eclipse.core.runtime.CoreException) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) IASTPreprocessorIncludeStatement(org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement) Pair(mrmathami.utils.Pair) HashSet(java.util.HashSet) Nonnull(mrmathami.annotations.Nonnull)

Example 3 with Pair

use of mrmathami.utils.Pair in project Cpp4CIA by thanhminhmr.

the class VersionDiffer method compare.

@Nonnull
public static VersionDifference compare(@Nonnull ProjectVersion versionA, @Nonnull ProjectVersion versionB, @Nonnull Map<DependencyType, Double> dependencyTypeImpactWeightMap, int maxDepth) throws CppException {
    final RootNode rootA = versionA.getRootNode();
    final RootNode rootB = versionB.getRootNode();
    final CppNode.Matcher matcher = new CppNode.Matcher();
    final Map<CppNode.Wrapper, CppNode> nodeMapA = new HashMap<>();
    final Map<CppNode.Wrapper, CppNode> nodeMapB = new HashMap<>();
    nodeMapA.put(new CppNode.Wrapper(rootA, CppNode.MatchLevel.SIMILAR, matcher), rootA);
    nodeMapB.put(new CppNode.Wrapper(rootB, CppNode.MatchLevel.SIMILAR, matcher), rootB);
    for (final CppNode nodeA : rootA) {
        if (!(nodeA instanceof IntegralNode)) {
            final CppNode.Wrapper wrapper = new CppNode.Wrapper(nodeA, CppNode.MatchLevel.SIMILAR, matcher);
            final CppNode node = nodeMapA.put(wrapper, nodeA);
            if (node != null) {
                throw new AssertionError();
            }
        }
    }
    for (final CppNode nodeB : rootB) {
        if (!(nodeB instanceof IntegralNode)) {
            final CppNode.Wrapper wrapper = new CppNode.Wrapper(nodeB, CppNode.MatchLevel.SIMILAR, matcher);
            final CppNode node = nodeMapB.put(wrapper, nodeB);
            if (node != null) {
                throw new AssertionError();
            }
        }
    }
    final Set<CppNode> addedNodes = new HashSet<>();
    final Set<Pair<CppNode, CppNode>> changedNodes = new HashSet<>();
    final Set<Pair<CppNode, CppNode>> unchangedNodes = new HashSet<>();
    final Set<CppNode> removedNodes = new HashSet<>();
    final List<CppNode> changedListB = new LinkedList<>();
    for (final CppNode.Wrapper wrapperA : nodeMapA.keySet()) {
        final CppNode nodeA = wrapperA.getNode();
        final CppNode nodeB = nodeMapB.get(wrapperA);
        if (nodeB != null) {
            if (matcher.isNodeMatch(nodeA, nodeB, CppNode.MatchLevel.IDENTICAL)) {
                unchangedNodes.add(Pair.immutableOf(nodeA, nodeB));
            } else {
                changedNodes.add(Pair.immutableOf(nodeA, nodeB));
                changedListB.add(nodeB);
            }
        } else {
            removedNodes.add(nodeA);
        }
    }
    for (final CppNode.Wrapper wrapperB : nodeMapB.keySet()) {
        final CppNode nodeA = nodeMapA.get(wrapperB);
        final CppNode nodeB = wrapperB.getNode();
        if (nodeA == null) {
            addedNodes.add(nodeB);
            changedListB.add(nodeB);
        }
    }
    final DependencyType[] types = DependencyType.values();
    final double[] typeImpactWeights = new double[types.length];
    for (final DependencyType type : types) {
        typeImpactWeights[type.ordinal()] = dependencyTypeImpactWeightMap.get(type);
    }
    final double[] impactWeights = ImpactWeightBuilder.calculate(typeImpactWeights, rootB, changedListB, maxDepth);
    return new VersionDifference(versionA, versionB, addedNodes, changedNodes, unchangedNodes, removedNodes, typeImpactWeights, impactWeights, maxDepth);
}
Also used : RootNode(mrmathami.cia.cpp.ast.RootNode) HashMap(java.util.HashMap) CppNode(mrmathami.cia.cpp.ast.CppNode) LinkedList(java.util.LinkedList) DependencyType(mrmathami.cia.cpp.ast.DependencyType) IntegralNode(mrmathami.cia.cpp.ast.IntegralNode) HashSet(java.util.HashSet) Pair(mrmathami.utils.Pair) Nonnull(mrmathami.annotations.Nonnull)

Example 4 with Pair

use of mrmathami.utils.Pair in project Cpp4CIA by thanhminhmr.

the class CppNode method equalsAllDependencyTo.

public final boolean equalsAllDependencyTo(@Nonnull CppNode node, @Nonnull Matcher matcher) {
    final int dependencyToSize = dependencyTo.size();
    if (dependencyToSize != node.dependencyTo.size())
        return false;
    final Map<Pair<Wrapper, IntsWrapper>, int[]> map = new HashMap<>(dependencyToSize);
    for (final Map.Entry<CppNode, int[]> entry : node.dependencyTo.entrySet()) {
        final Wrapper wrapper = new Wrapper(entry.getKey(), MatchLevel.PROTOTYPE_IDENTICAL, matcher);
        final Pair<Wrapper, IntsWrapper> pair = Pair.immutableOf(wrapper, IntsWrapper.of(entry.getValue()));
        final int[] countWrapper = map.computeIfAbsent(pair, any -> new int[] { 0 });
        countWrapper[0] += 1;
    }
    for (final Map.Entry<CppNode, int[]> entry : dependencyTo.entrySet()) {
        final Wrapper wrapper = new Wrapper(entry.getKey(), MatchLevel.PROTOTYPE_IDENTICAL, matcher);
        final Pair<Wrapper, IntsWrapper> pair = Pair.immutableOf(wrapper, IntsWrapper.of(entry.getValue()));
        final int[] countWrapper = map.get(pair);
        if (countWrapper == null)
            return false;
        if (--countWrapper[0] == 0)
            map.remove(pair);
    }
    return map.isEmpty();
}
Also used : IntsWrapper(mrmathami.utils.IntsWrapper) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) EnumMap(java.util.EnumMap) IntsWrapper(mrmathami.utils.IntsWrapper) Pair(mrmathami.utils.Pair)

Aggregations

Pair (mrmathami.utils.Pair)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Nonnull (mrmathami.annotations.Nonnull)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 CharArrayWriter (java.io.CharArrayWriter)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 EnumMap (java.util.EnumMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Set (java.util.Set)1 CppException (mrmathami.cia.cpp.CppException)1 CppNode (mrmathami.cia.cpp.ast.CppNode)1 DependencyType (mrmathami.cia.cpp.ast.DependencyType)1 IntegralNode (mrmathami.cia.cpp.ast.IntegralNode)1 RootNode (mrmathami.cia.cpp.ast.RootNode)1 IntsWrapper (mrmathami.utils.IntsWrapper)1