Changeset View
Changeset View
Standalone View
Standalone View
java/src/main/java/org/softwareheritage/graph/NodesFiltering.java
- This file was added.
package org.softwareheritage.graph; | ||||||||||
import java.util.ArrayList; | ||||||||||
import java.util.Iterator; | ||||||||||
/** | ||||||||||
* class that manages the filtering of nodes that have been returned after a visit of the graph. | ||||||||||
* parameterized by a string that represents either no filtering (*) or a set of node types | ||||||||||
* | ||||||||||
*/ | ||||||||||
vlorentz: What about overloading the constructor with a variant with no argument, instead of a magic… | ||||||||||
public class NodesFiltering { | ||||||||||
boolean restricted; | ||||||||||
ArrayList<Node.Type> restrictedNodesTypes; | ||||||||||
public NodesFiltering(String t) { | ||||||||||
if (t.equals("*")) { | ||||||||||
restricted = false; | ||||||||||
restrictedNodesTypes = Node.Type.parse("*"); // contains all types, no restriction | ||||||||||
} else { | ||||||||||
restricted = true; | ||||||||||
restrictedNodesTypes = new ArrayList<Node.Type>(); | ||||||||||
String[] types = t.split(","); | ||||||||||
for (int i = 0; i < types.length; i++) { | ||||||||||
restrictedNodesTypes.add(Node.Type.fromStr(types[i])); | ||||||||||
} | ||||||||||
vlorentzUnsubmitted Not Done Inline ActionsWouldn't this work? for (String type : types) { restrictedNodesTypes.add(Node.Type.fromStr(type)); } or even this? Collections.addAll(restrictedNodesTypes, types) vlorentz: Wouldn't this work?
```
for (String type : types) {… | ||||||||||
} | ||||||||||
} | ||||||||||
public boolean typeIsAllowed(Node.Type typ) { | ||||||||||
return this.restrictedNodesTypes.contains(typ); | ||||||||||
} | ||||||||||
public ArrayList<Long> filterByNodeTypes(ArrayList<Long> nodeIds, Graph g) { | ||||||||||
for (Iterator<Long> it = nodeIds.iterator(); it.hasNext();) { | ||||||||||
if (this.typeIsAllowed(g.getNodeType(it.next()))) { | ||||||||||
it.remove(); | ||||||||||
} | ||||||||||
} | ||||||||||
return nodeIds; | ||||||||||
vlorentzUnsubmitted Not Done Inline ActionsDoesn't this return a value *and* modify the argument? this could be confusing to use. What about creating a new ArrayList and adding matching elements to it? vlorentz: Doesn't this return a value *and* modify the argument? this could be confusing to use.
What… | ||||||||||
vlorentzUnsubmitted Not Done Inline ActionsAnd I'm not very familiar with Java, but isn't ArrayList backed by a single dynamic array? This means every remove operation needs to copy all elements on the right, so filterByNodeTypes runs in quadratic time. vlorentz: And I'm not very familiar with Java, but isn't ArrayList backed by a single dynamic array? This… | ||||||||||
} | ||||||||||
} | ||||||||||
Not Done Inline Actions
vlorentz: |
What about overloading the constructor with a variant with no argument, instead of a magic value *?