Skip to content

Commit

Permalink
Slight code quality improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
kno10 committed Feb 17, 2024
1 parent 2c11055 commit 262b4ef
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .github/codeql-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ query-filters:
id: java/class-name-matches-super-class # Par, Instance, Factory...
- exclude:
id: java/comparison-of-identical-expressions # NaN checks
- exclude:
id: java/uncontrolled-arithmetic # many false reports
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public static <V extends FeatureVector<?>> MultivariateSeriesTypeInformation<V>
/**
* Constructor for an actual type.
*
* @param <V> vector type
* @param cls base class
* @param mindim Minimum dimensionality
* @param maxdim Maximum dimensionality
Expand Down
10 changes: 10 additions & 0 deletions elki-gui-minigui/src/main/java/elki/gui/util/ClassTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ public PackageNode(String display, String pkgname) {
public String getPackageName() {
return pkgname;
}

@Override
public Object clone() {
return super.clone(); // no deep copy necessary
}
}

/**
Expand Down Expand Up @@ -217,5 +222,10 @@ public ClassNode(String display, String clsname) {
public String getClassName() {
return clsname;
}

@Override
public Object clone() {
return super.clone(); // no deep copy necessary
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import java.util.HashMap;
import java.util.Map;

import elki.database.datastore.DataStore;
import elki.database.datastore.DataStoreFactory;
import elki.database.datastore.DataStoreUtil;
import elki.database.datastore.WritableDataStore;
import elki.database.ids.*;
import elki.database.query.distance.DistanceQuery;
import elki.database.query.knn.KNNSearcher;
Expand Down Expand Up @@ -105,11 +109,10 @@ public double distance(DBIDRef id1, DBIDRef id2) {
* @deprecated Change to use by-object NN lookups instead.
*/
@Deprecated
protected final Map<DBID, KNNList> batchNN(N node, DBIDs ids, int kmax) {
Map<DBID, KNNList> res = new HashMap<>(ids.size());
protected final DataStore<KNNList> batchNN(N node, DBIDs ids, int kmax) {
WritableDataStore<KNNList> res = DataStoreUtil.makeStorage(ids, DataStoreFactory.HINT_TEMP, KNNList.class);
for(DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
DBID id = DBIDUtil.deref(iter);
res.put(id, knnq.getKNN(id, kmax));
res.put(iter, knnq.getKNN(iter, kmax));
}
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
package elki.index.tree.metrical.mtreevariants.mktrees;

import java.util.List;
import java.util.Map;

import elki.database.ids.DBID;
import elki.database.datastore.DataStore;
import elki.database.ids.DBIDUtil;
import elki.database.ids.KNNList;
import elki.database.ids.ModifiableDBIDs;
Expand Down Expand Up @@ -86,7 +85,7 @@ public void insertAll(List<E> entries) {
super.insert(entry, false);
}

Map<DBID, KNNList> knnLists = batchNN(getNode(getRootID()), ids, settings.kmax);
DataStore<KNNList> knnLists = batchNN(getNode(getRootID()), ids, settings.kmax);
kNNdistanceAdjustment(getRootEntry(), knnLists);
doExtraIntegrityChecks();
}
Expand All @@ -97,7 +96,7 @@ public void insertAll(List<E> entries) {
* @param entry the root entry of the current subtree
* @param knnLists a map of knn lists for each leaf entry
*/
protected abstract void kNNdistanceAdjustment(E entry, Map<DBID, KNNList> knnLists);
protected abstract void kNNdistanceAdjustment(E entry, DataStore<KNNList> knnLists);

/**
* Get the value of k_max.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,9 @@
package elki.index.tree.metrical.mtreevariants.mktrees.mkapp;

import java.util.List;
import java.util.Map;

import elki.database.ids.ArrayModifiableDBIDs;
import elki.database.ids.DBID;
import elki.database.ids.DBIDIter;
import elki.database.ids.DBIDRef;
import elki.database.ids.DBIDUtil;
import elki.database.ids.DBIDs;
import elki.database.ids.DoubleDBIDList;
import elki.database.ids.DoubleDBIDListIter;
import elki.database.ids.KNNList;
import elki.database.ids.ModifiableDBIDs;
import elki.database.ids.ModifiableDoubleDBIDList;

import elki.database.datastore.DataStore;
import elki.database.ids.*;
import elki.database.relation.Relation;
import elki.index.tree.LeafEntry;
import elki.index.tree.metrical.mtreevariants.mktrees.AbstractMkTree;
Expand All @@ -45,6 +35,7 @@
import elki.utilities.datastructures.heap.Heap;
import elki.utilities.datastructures.heap.UpdatableHeap;
import elki.utilities.io.ByteArrayUtil;

import net.jafama.FastMath;

/**
Expand Down Expand Up @@ -120,7 +111,7 @@ public void insertAll(List<MkAppEntry> entries) {
super.insert(entry, false);
}

Map<DBID, KNNList> knnLists = batchNN(getNode(getRootID()), ids, settings.kmax + 1);
DataStore<KNNList> knnLists = batchNN(getNode(getRootID()), ids, settings.kmax + 1);

adjustApproximatedKNNDistances(getRootEntry(), knnLists);
doExtraIntegrityChecks();
Expand Down Expand Up @@ -246,7 +237,7 @@ protected void initializeCapacities(MkAppEntry exampleLeaf) {
* @param knnLists kNN lists
* @return Mean kNN values
*/
private double[] getMeanKNNList(DBIDs ids, Map<DBID, KNNList> knnLists) {
private double[] getMeanKNNList(DBIDs ids, DataStore<KNNList> knnLists) {
double[] means = new double[settings.kmax];
for(DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
KNNList knns = knnLists.get(iter);
Expand All @@ -264,7 +255,7 @@ private double[] getMeanKNNList(DBIDs ids, Map<DBID, KNNList> knnLists) {
* @param entry the root entry of the current subtree
* @param knnLists a map of knn lists for each leaf entry
*/
private void adjustApproximatedKNNDistances(MkAppEntry entry, Map<DBID, KNNList> knnLists) {
private void adjustApproximatedKNNDistances(MkAppEntry entry, DataStore<KNNList> knnLists) {
MkAppTreeNode<O> node = getNode(entry);

if(node.isLeaf()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import elki.database.ids.DBID;
import elki.database.ids.DBIDIter;
import elki.database.ids.DBIDRef;
import elki.database.ids.DBIDUtil;
import elki.database.ids.DoubleDBIDList;
import elki.database.ids.DoubleDBIDListIter;
import elki.database.ids.KNNList;
import elki.database.ids.ModifiableDBIDs;
import elki.database.ids.ModifiableDoubleDBIDList;

import elki.database.datastore.DataStore;
import elki.database.ids.*;
import elki.database.relation.Relation;
import elki.index.tree.metrical.mtreevariants.mktrees.AbstractMkTree;
import elki.index.tree.metrical.mtreevariants.mktrees.MkTreeSettings;
Expand All @@ -43,7 +35,6 @@
import elki.utilities.exceptions.AbortException;
import elki.utilities.io.ByteArrayUtil;
import elki.utilities.io.FormatUtil;
import net.jafama.FastMath;

/**
* MkCopTree is a metrical index structure based on the concepts of the M-Tree
Expand Down Expand Up @@ -122,7 +113,7 @@ public void insertAll(List<MkCoPEntry> entries) {
super.insert(entry, false);
}

Map<DBID, KNNList> knnLists = batchNN(getNode(getRootID()), ids, settings.kmax);
DataStore<KNNList> knnLists = batchNN(getNode(getRootID()), ids, settings.kmax);

adjustApproximatedKNNDistances(getRootEntry(), knnLists);
doExtraIntegrityChecks();
Expand All @@ -147,7 +138,7 @@ public DoubleDBIDList reverseKNNQuery(DBIDRef id, int k) {
doReverseKNNQuery(k, id, result, candidates);

// refinement of candidates
Map<DBID, KNNList> knnLists = batchNN(getNode(getRootID()), candidates, k);
DataStore<KNNList> knnLists = batchNN(getNode(getRootID()), candidates, k);

result.sort();
// Collections.sort(candidates);
Expand All @@ -157,11 +148,10 @@ public DoubleDBIDList reverseKNNQuery(DBIDRef id, int k) {
// rkNNStatistics.addTrueHits(result.size());

for(DBIDIter iter = candidates.iter(); iter.valid(); iter.advance()) {
DBID cid = DBIDUtil.deref(iter);
KNNList cands = knnLists.get(cid);
KNNList cands = knnLists.get(iter);
for(DoubleDBIDListIter iter2 = cands.iter(); iter2.valid(); iter2.advance()) {
if(DBIDUtil.equal(id, iter2)) {
result.add(iter2.doubleValue(), cid);
result.add(iter2.doubleValue(), iter);
break;
}
}
Expand Down Expand Up @@ -290,9 +280,8 @@ private void doReverseKNNQuery(int k, DBIDRef q, ModifiableDoubleDBIDList result
* @param entry the root entry of the current subtree
* @param knnLists a map of knn lists for each leaf entry
*/
private void adjustApproximatedKNNDistances(MkCoPEntry entry, Map<DBID, KNNList> knnLists) {
private void adjustApproximatedKNNDistances(MkCoPEntry entry, DataStore<KNNList> knnLists) {
MkCoPTreeNode<O> node = getNode(entry);

if(node.isLeaf()) {
for(int i = 0; i < node.getNumEntries(); i++) {
MkCoPLeafEntry leafEntry = (MkCoPLeafEntry) node.getEntry(i);
Expand All @@ -305,7 +294,6 @@ private void adjustApproximatedKNNDistances(MkCoPEntry entry, Map<DBID, KNNList>
adjustApproximatedKNNDistances(dirEntry, knnLists);
}
}

ApproximationLine approx = node.conservativeKnnDistanceApproximation(settings.kmax);
entry.setConservativeKnnDistanceApproximation(approx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,9 @@
package elki.index.tree.metrical.mtreevariants.mktrees.mkmax;

import java.util.List;
import java.util.Map;

import elki.database.ids.DBID;
import elki.database.ids.DBIDIter;
import elki.database.ids.DBIDRef;
import elki.database.ids.DBIDUtil;
import elki.database.ids.DoubleDBIDList;
import elki.database.ids.DoubleDBIDListIter;
import elki.database.ids.KNNHeap;
import elki.database.ids.KNNList;
import elki.database.ids.ModifiableDBIDs;
import elki.database.ids.ModifiableDoubleDBIDList;

import elki.database.datastore.DataStore;
import elki.database.ids.*;
import elki.database.relation.Relation;
import elki.index.tree.metrical.mtreevariants.mktrees.AbstractMkTreeUnified;
import elki.index.tree.metrical.mtreevariants.mktrees.MkTreeSettings;
Expand Down Expand Up @@ -99,15 +90,14 @@ public DoubleDBIDList reverseKNNQuery(DBIDRef id, int k) {
for (DBIDIter candidate = candidates.iter(); candidate.valid(); candidate.advance()) {
candidateIDs.add(candidate);
}
Map<DBID, KNNList> knnLists = batchNN(getNode(getRootID()), candidateIDs, k);
DataStore<KNNList> knnLists = batchNN(getNode(getRootID()), candidateIDs, k);

ModifiableDoubleDBIDList result = DBIDUtil.newDistanceDBIDList();
for (DBIDIter iter = candidateIDs.iter(); iter.valid(); iter.advance()) {
DBID cid = DBIDUtil.deref(iter);
KNNList cands = knnLists.get(cid);
KNNList cands = knnLists.get(iter);
for (DoubleDBIDListIter iter2 = cands.iter(); iter2.valid(); iter2.advance()) {
if (DBIDUtil.equal(id, iter2)) {
result.add(iter2.doubleValue(), cid);
result.add(iter2.doubleValue(), iter);
break;
}
}
Expand All @@ -134,7 +124,7 @@ protected void preInsert(MkMaxEntry entry) {
* Adjusts the knn distance in the subtree of the specified root entry.
*/
@Override
protected void kNNdistanceAdjustment(MkMaxEntry entry, Map<DBID, KNNList> knnLists) {
protected void kNNdistanceAdjustment(MkMaxEntry entry, DataStore<KNNList> knnLists) {
MkMaxTreeNode<O> node = getNode(entry);
double knnDist_node = 0.;
if (node.isLeaf()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@
*/
package elki.index.tree.metrical.mtreevariants.mktrees.mktab;

import java.util.Map;

import elki.database.ids.DBID;
import elki.database.ids.DBIDRef;
import elki.database.ids.DBIDUtil;
import elki.database.ids.DoubleDBIDList;
import elki.database.ids.DoubleDBIDListIter;
import elki.database.ids.KNNList;
import elki.database.ids.ModifiableDoubleDBIDList;
import elki.database.datastore.DataStore;
import elki.database.ids.*;
import elki.database.relation.Relation;
import elki.index.tree.metrical.mtreevariants.mktrees.AbstractMkTreeUnified;
import elki.index.tree.metrical.mtreevariants.mktrees.MkTreeSettings;
Expand Down Expand Up @@ -131,7 +124,7 @@ protected void initializeCapacities(MkTabEntry exampleLeaf) {
}

@Override
protected void kNNdistanceAdjustment(MkTabEntry entry, Map<DBID, KNNList> knnLists) {
protected void kNNdistanceAdjustment(MkTabEntry entry, DataStore<KNNList> knnLists) {
MkTabTreeNode<O> node = getNode(entry);
double[] knnDistances_node = initKnnDistanceList();
if(node.isLeaf()) {
Expand Down

0 comments on commit 262b4ef

Please sign in to comment.