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;
}
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;
}
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);
}
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();
}
Aggregations