package org.jacoco.core.internal.analysis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.jacoco.core.internal.analysis.filter.IFilterOutput;
import org.objectweb.asm.tree.AbstractInsnNode;
/* loaded from: classes7.dex */
class MethodCoverageCalculator implements IFilterOutput {
    private final Map<AbstractInsnNode, Instruction> instructions;
    private final Set<AbstractInsnNode> ignored = new HashSet();
    private final Map<AbstractInsnNode, AbstractInsnNode> merged = new HashMap();
    private final Map<AbstractInsnNode, Set<AbstractInsnNode>> replacements = new HashMap();

    /* JADX INFO: Access modifiers changed from: package-private */
    public MethodCoverageCalculator(Map<AbstractInsnNode, Instruction> map) {
        this.instructions = map;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void calculate(MethodCoverageImpl methodCoverageImpl) {
        applyMerges();
        applyReplacements();
        ensureCapacity(methodCoverageImpl);
        for (Map.Entry<AbstractInsnNode, Instruction> entry : this.instructions.entrySet()) {
            if (!this.ignored.contains(entry.getKey())) {
                Instruction value = entry.getValue();
                methodCoverageImpl.increment(value.getInstructionCounter(), value.getBranchCounter(), value.getLine());
            }
        }
        methodCoverageImpl.incrementMethodCounter();
    }

    private void applyMerges() {
        for (Map.Entry<AbstractInsnNode, AbstractInsnNode> entry : this.merged.entrySet()) {
            AbstractInsnNode key = entry.getKey();
            AbstractInsnNode findRepresentative = findRepresentative(key);
            this.ignored.add(key);
            Map<AbstractInsnNode, Instruction> map = this.instructions;
            map.put(findRepresentative, map.get(findRepresentative).merge(this.instructions.get(key)));
            entry.setValue(findRepresentative);
        }
        for (Map.Entry<AbstractInsnNode, AbstractInsnNode> entry2 : this.merged.entrySet()) {
            this.instructions.put(entry2.getKey(), this.instructions.get(entry2.getValue()));
        }
    }

    private void applyReplacements() {
        for (Map.Entry<AbstractInsnNode, Set<AbstractInsnNode>> entry : this.replacements.entrySet()) {
            Set<AbstractInsnNode> value = entry.getValue();
            ArrayList arrayList = new ArrayList(value.size());
            for (AbstractInsnNode abstractInsnNode : value) {
                arrayList.add(this.instructions.get(abstractInsnNode));
            }
            AbstractInsnNode key = entry.getKey();
            Map<AbstractInsnNode, Instruction> map = this.instructions;
            map.put(key, map.get(key).replaceBranches(arrayList));
        }
    }

    private void ensureCapacity(MethodCoverageImpl methodCoverageImpl) {
        int line;
        int i = -1;
        int i2 = -1;
        for (Map.Entry<AbstractInsnNode, Instruction> entry : this.instructions.entrySet()) {
            if (!this.ignored.contains(entry.getKey()) && (line = entry.getValue().getLine()) != -1) {
                if (i > line || i2 == -1) {
                    i = line;
                }
                if (i2 < line) {
                    i2 = line;
                }
            }
        }
        methodCoverageImpl.ensureCapacity(i, i2);
    }

    private AbstractInsnNode findRepresentative(AbstractInsnNode abstractInsnNode) {
        while (true) {
            AbstractInsnNode abstractInsnNode2 = this.merged.get(abstractInsnNode);
            if (abstractInsnNode2 == null) {
                return abstractInsnNode;
            }
            abstractInsnNode = abstractInsnNode2;
        }
    }

    @Override // org.jacoco.core.internal.analysis.filter.IFilterOutput
    public void ignore(AbstractInsnNode abstractInsnNode, AbstractInsnNode abstractInsnNode2) {
        while (abstractInsnNode != abstractInsnNode2) {
            this.ignored.add(abstractInsnNode);
            abstractInsnNode = abstractInsnNode.getNext();
        }
        this.ignored.add(abstractInsnNode2);
    }

    @Override // org.jacoco.core.internal.analysis.filter.IFilterOutput
    public void merge(AbstractInsnNode abstractInsnNode, AbstractInsnNode abstractInsnNode2) {
        AbstractInsnNode findRepresentative = findRepresentative(abstractInsnNode);
        AbstractInsnNode findRepresentative2 = findRepresentative(abstractInsnNode2);
        if (findRepresentative != findRepresentative2) {
            this.merged.put(findRepresentative2, findRepresentative);
        }
    }

    @Override // org.jacoco.core.internal.analysis.filter.IFilterOutput
    public void replaceBranches(AbstractInsnNode abstractInsnNode, Set<AbstractInsnNode> set) {
        this.replacements.put(abstractInsnNode, set);
    }
}
