Skip to content

Commit

Permalink
Merge pull request #156 from michaelhaaf/contrib/issue-137
Browse files Browse the repository at this point in the history
close #137 to replace Stack with ArrayDeque
  • Loading branch information
gonzalezsieira committed Dec 1, 2015
2 parents 0921881 + a80a041 commit 185ed17
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
Empty file.
Empty file.
21 changes: 9 additions & 12 deletions hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import es.usc.citius.hipster.model.Node;
import es.usc.citius.hipster.model.function.NodeExpander;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Stack;
import java.util.*;

/**
* <p>
Expand Down Expand Up @@ -90,13 +87,13 @@ public boolean isProcessed() {
* DFS iterator used to expand always the deepest non-visited node.
*/
public class Iterator implements java.util.Iterator<N> {
protected Stack<StackFrameNode> stack = new Stack<StackFrameNode>();
protected Deque<StackFrameNode> stack = new ArrayDeque<StackFrameNode>();
protected StackFrameNode next;
protected Set<S> closed = new HashSet<S>();
protected boolean graphSupport = true;

protected Iterator(){
this.stack.add(new StackFrameNode(initialNode));
this.stack.addLast(new StackFrameNode(initialNode));
}


Expand Down Expand Up @@ -155,30 +152,30 @@ protected StackFrameNode processNextNode(){

if (stack.isEmpty()) return null;

// Take current node in the stack but do not remove
StackFrameNode current = stack.peek();
// Take last node in the stack but do not remove
StackFrameNode current = stack.peekLast();
// Find a successor
if (current.successors.hasNext()){
N successor = current.successors.next();
// push the node (if not explored)
if (!graphSupport || !closed.contains(successor.state())) {
stack.add(new StackFrameNode(successor));
stack.addLast(new StackFrameNode(successor));
}
return current;
} else {
// Visited?
if (current.visited){
current.processed = true;
}
return stack.pop();
return stack.removeFirst();
}
}

public Stack<StackFrameNode> getStack() {
public Deque<StackFrameNode> getStack() {
return stack;
}

public void setStack(Stack<StackFrameNode> stack) {
public void setStack(Deque<StackFrameNode> stack) {
this.stack = stack;
}

Expand Down
23 changes: 12 additions & 11 deletions hipster-core/src/main/java/es/usc/citius/hipster/algorithm/IDAStar.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import es.usc.citius.hipster.model.HeuristicNode;
import es.usc.citius.hipster.model.function.NodeExpander;

import java.util.Stack;
import java.util.ArrayDeque;
import java.util.Deque;

/**
* <p>
Expand Down Expand Up @@ -81,7 +82,7 @@ private class StackFrameNode {
* backtracking.
*/
public class Iterator implements java.util.Iterator<N> {
protected Stack<StackFrameNode> stack = new Stack<StackFrameNode>();
protected Deque<StackFrameNode> stack = new ArrayDeque<>();
protected C fLimit;
protected C minfLimit;
protected int reinitialization = 0;
Expand All @@ -91,7 +92,7 @@ protected Iterator(){
// Set initial bound
fLimit = initialNode.getEstimation();
minfLimit = null;
this.stack.add(new StackFrameNode(initialNode));
this.stack.addLast(new StackFrameNode(initialNode));
}

@Override
Expand Down Expand Up @@ -148,7 +149,7 @@ protected StackFrameNode nextUnvisited(){
fLimit = minfLimit;
reinitialization++;
minfLimit = null;
stack.add(new StackFrameNode(initialNode));
stack.addLast(new StackFrameNode(initialNode));
nextNode = processNextNode();
}
}
Expand All @@ -174,8 +175,8 @@ protected StackFrameNode processNextNode(){
// 1- If the stack is empty, change fLimit and reinitialize the search
if (stack.isEmpty()) return null;

// Take current node in the stack but do not remove
StackFrameNode current = stack.peek();
// Take last node in the stack but do not remove
StackFrameNode current = stack.peekLast();

// 2 - Check if the current node exceeds the limit bound
C fCurrent = current.node.getScore();
Expand All @@ -184,31 +185,31 @@ protected StackFrameNode processNextNode(){
updateMinFLimit(fCurrent);
// Remove from stack
current.processed = true;
return stack.pop();
return stack.removeLast();
}

// Find a successor
if (current.successors.hasNext()){
// 3 - Node has at least one neighbor
N successor = current.successors.next();
// push the node
stack.add(new StackFrameNode(successor));
stack.addLast(new StackFrameNode(successor));
return current;

} else {
// 4 - Visited?
if (current.visited){
current.processed = true;
}
return stack.pop();
return stack.removeLast();
}
}

public Stack<StackFrameNode> getStack() {
public Deque<StackFrameNode> getStack() {
return stack;
}

public void setStack(Stack<StackFrameNode> stack) {
public void setStack(Deque<StackFrameNode> stack) {
this.stack = stack;
}

Expand Down
2 changes: 1 addition & 1 deletion hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public MultiobjectiveLS(N initialNode, NodeExpander<A, S, N> nodeExpander) {
public class Iterator implements java.util.Iterator<N> {
protected Queue<N> queue = new LinkedList<N>();
public Map<S, Collection<N>> nonDominated;
//auxiliar variable which stores an empty list to avoid nullable values in code
//auxiliary variable which stores an empty list to avoid nullable values in code
private final Collection<N> EMPTYLIST = new ArrayList<N>();

protected Iterator(){
Expand Down

0 comments on commit 185ed17

Please sign in to comment.