// SPDX-FileCopyrightText: 2020 Roberto Posenato // // SPDX-License-Identifier: LGPL-3.0-or-later package it.univr.di.labeledvalue; import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; import it.unimi.dsi.fastutil.ints.IntArraySet; import it.unimi.dsi.fastutil.ints.IntSet; import it.unimi.dsi.fastutil.objects.*; import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry; import it.univr.di.Debug; import javax.annotation.Nonnull; import java.io.Serial; import java.util.Arrays; import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; /** * Simple implementation of {@link it.univr.di.labeledvalue.LabeledIntMap} interface. *

* An experimental result on 2024-10-16 showed that using the base, there is a small improvement in the performance. * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Execution time (ms) for some operations w.r.t the core data structure of the class (Object2IntMap).
OperationUsing Object2IntOpenHashMap (ms)Using Object2IntArrayMap (ms)
Creating a map of 26 elements, adding 60 elements (some entries are simplified)0.2229826250.08684075
Finding the min value0.0029900830.003209583
Retrieve the value of label abd¿f3.9083E-56.6917E-5
Simplification after insertion of (c,11) and (¬c,11)0.0569170.020042
* All code for performance tests is in the ` LabeledIntTreeMapTest ` class (not publicly available). * * @author Roberto Posenato * @version $Rev$ * @see LabeledIntMap */ @SuppressWarnings({"SizeReplaceableByIsEmpty", "UnusedReturnValue"}) public class LabeledIntTreeMap extends AbstractLabeledIntMap { /** * A read-only view of an object * * @author posenato */ public static class LabeledIntTreeMapView extends LabeledIntTreeMap implements LabeledIntMapView { /** * */ @Serial private static final long serialVersionUID = 1L; /** * @param inputMap input */ public LabeledIntTreeMapView(LabeledIntTreeMap inputMap) { mainInt2SetMap = inputMap.mainInt2SetMap; base = inputMap.base; count = inputMap.count; } /** * Object Read-only. It does nothing. */ @Override public void putForcibly(@Nonnull Label l, int i) { } } /** * empty base; */ static private final char[] emptyBase = new char[0]; /** * logger */ static private final Logger LOG = Logger.getLogger("LabeledIntTreeMap"); /** * */ @Serial static private final long serialVersionUID = 2L; /** * @return an Object2IntMap object */ private static Int2ObjectArrayMap> makeInt2ObjectMap() { return new Int2ObjectArrayMap<>(); } /** * @return an Object2IntMap object */ private static Object2IntMap

* Warning!
* Use this method not during a map scan by {@link #entrySet()}! */ @Override public int remove(@Nonnull final Label l) { final Object2IntMap

* An experiment result on 2016-01-13 showed that using the base, there is a small improvement in the performance. * * @param l the input label * * @return true if {@code l} is a component of the base, false otherwise. */ private boolean checkValidityOfTheBaseAfterRemoving(final Label l) { final int lSize = l.size(); if ((base.length == 0) || (lSize == 0) || (lSize != base.length) || l.containsUnknown()) { return false; } // l and base have the same length. for (final char c : base) { if (!l.contains(c)) { return false; } } // l is a component of the base, and it was removed. base = emptyBase; return true; } /** * Tries to add all given labeled values into the current map: *

    *
  1. Given a set of labeled values to insert (all labels have the same length) *
  2. For each of them, it compares all same-length labels already in the map with the current one, looking for one with the same value and only one opposite literal. (This allows the simplification of the two labels with a shorter one.) In case of a positive search, shorten the current label to insert. *
  3. For each of the labeled values to insert (possibly updated), it removes all labeled values in the map greater than it. * At each round, it tries to add labeled values of the same size. *
* * @param inputMap contains all the elements that have to be inserted. * @param inputMapLabelLength length of labels contained in inputMap * * @return true if any element of inputMap has been inserted into the map. */ @SuppressWarnings("LocalCanBeFinal") private boolean insertAndSimplify(Object2IntMap