mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Fixes BTree visitor (did not visit all matching nodes), also changed semantics.
This commit is contained in:
parent
d4dc19649e
commit
3329794554
6 changed files with 30 additions and 55 deletions
|
@ -172,7 +172,7 @@ public class BTreeTests extends BaseTestCase {
|
||||||
btree.accept(new IBTreeVisitor(){
|
btree.accept(new IBTreeVisitor(){
|
||||||
int k;
|
int k;
|
||||||
public int compare(int record) throws CoreException {
|
public int compare(int record) throws CoreException {
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
public boolean visit(int record) throws CoreException {
|
public boolean visit(int record) throws CoreException {
|
||||||
if(record!=0) {
|
if(record!=0) {
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* QNX - Initial API and implementation
|
* QNX - Initial API and implementation
|
||||||
* Andrew Ferguson (Symbian) - Provide B-tree deletion routine
|
* Andrew Ferguson (Symbian) - Provide B-tree deletion routine
|
||||||
|
* Markus Schorn (Wind River Systems)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.core.pdom.db;
|
package org.eclipse.cdt.internal.core.pdom.db;
|
||||||
|
@ -520,16 +521,17 @@ public class BTree {
|
||||||
* @param visitor
|
* @param visitor
|
||||||
*/
|
*/
|
||||||
public void accept(IBTreeVisitor visitor) throws CoreException {
|
public void accept(IBTreeVisitor visitor) throws CoreException {
|
||||||
accept(db.getInt(rootPointer), visitor, false);
|
accept(db.getInt(rootPointer), visitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean accept(int node, IBTreeVisitor visitor, boolean found) throws CoreException {
|
private boolean accept(int node, IBTreeVisitor visitor) throws CoreException {
|
||||||
// if found is false, we are still in search mode
|
// if found is false, we are still in search mode
|
||||||
// once found is true visit everything
|
// once found is true visit everything
|
||||||
// return false when ready to quit
|
// return false when ready to quit
|
||||||
if (node == 0)
|
|
||||||
return visitor.visit(0);
|
|
||||||
|
|
||||||
|
if (node == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if(visitor instanceof IBTreeVisitor2) {
|
if(visitor instanceof IBTreeVisitor2) {
|
||||||
((IBTreeVisitor2)visitor).preNode(node);
|
((IBTreeVisitor2)visitor).preNode(node);
|
||||||
}
|
}
|
||||||
|
@ -537,49 +539,25 @@ public class BTree {
|
||||||
try {
|
try {
|
||||||
Chunk chunk = db.getChunk(node);
|
Chunk chunk = db.getChunk(node);
|
||||||
|
|
||||||
if (found) {
|
int i= 0;
|
||||||
int child = getChild(chunk, node, 0);
|
for (; i < MAX_RECORDS; ++i) {
|
||||||
if (child != 0)
|
|
||||||
if (!accept(child, visitor, true))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < MAX_RECORDS; ++i) {
|
|
||||||
int record = getRecord(chunk, node, i);
|
int record = getRecord(chunk, node, i);
|
||||||
if (record == 0)
|
if (record == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (found) {
|
|
||||||
if (!visitor.visit(record))
|
|
||||||
return false;
|
|
||||||
if (!accept(getChild(chunk, node, i + 1), visitor, true))
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
int compare = visitor.compare(record);
|
int compare = visitor.compare(record);
|
||||||
if (compare > 0) {
|
if (compare > 0) {
|
||||||
// start point is to the left
|
// start point is to the left
|
||||||
if (!accept(getChild(chunk, node, i), visitor, false))
|
return accept(getChild(chunk, node, i), visitor);
|
||||||
|
}
|
||||||
|
else if (compare == 0) {
|
||||||
|
if (!accept(getChild(chunk, node, i), visitor))
|
||||||
return false;
|
return false;
|
||||||
if (!visitor.visit(record))
|
if (!visitor.visit(record))
|
||||||
return false;
|
return false;
|
||||||
if (!accept(getChild(chunk, node, i + 1), visitor, true))
|
|
||||||
return false;
|
|
||||||
found = true;
|
|
||||||
} else if (compare == 0) {
|
|
||||||
if (!visitor.visit(record))
|
|
||||||
return false;
|
|
||||||
if (!accept(getChild(chunk, node, i + 1), visitor, true))
|
|
||||||
return false;
|
|
||||||
found = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return accept(getChild(chunk, node, i), visitor);
|
||||||
|
|
||||||
if (!found)
|
|
||||||
return accept(getChild(chunk, node, i), visitor, false);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
} finally {
|
} finally {
|
||||||
if(visitor instanceof IBTreeVisitor2) {
|
if(visitor instanceof IBTreeVisitor2) {
|
||||||
((IBTreeVisitor2)visitor).postNode(node);
|
((IBTreeVisitor2)visitor).postNode(node);
|
||||||
|
@ -621,7 +599,7 @@ public class BTree {
|
||||||
public String getMsg() { return msg; }
|
public String getMsg() { return msg; }
|
||||||
public boolean isValid() { return valid; }
|
public boolean isValid() { return valid; }
|
||||||
public void postNode(int node) throws CoreException { depth--; }
|
public void postNode(int node) throws CoreException { depth--; }
|
||||||
public int compare(int record) throws CoreException { return 1; }
|
public int compare(int record) throws CoreException { return 0; }
|
||||||
public boolean visit(int record) throws CoreException { return true; }
|
public boolean visit(int record) throws CoreException { return true; }
|
||||||
|
|
||||||
public void preNode(int node) throws CoreException {
|
public void preNode(int node) throws CoreException {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2005 QNX Software Systems and others.
|
* Copyright (c) 2005, 2006 QNX Software Systems and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -7,6 +7,7 @@
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* QNX - Initial API and implementation
|
* QNX - Initial API and implementation
|
||||||
|
* Markus Schorn (Wind River Systems)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.core.pdom.db;
|
package org.eclipse.cdt.internal.core.pdom.db;
|
||||||
|
|
||||||
|
@ -15,16 +16,13 @@ import org.eclipse.core.runtime.CoreException;
|
||||||
/**
|
/**
|
||||||
* @author Doug Schaefer
|
* @author Doug Schaefer
|
||||||
*
|
*
|
||||||
* The visitor walks through the tree until the compare returns
|
* The visitor visits all records where compare returns 0.
|
||||||
* >= 0, i.e. we reach the first record that meets the criteria.
|
|
||||||
*
|
|
||||||
* It then continues until the visit returns false.
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface IBTreeVisitor {
|
public interface IBTreeVisitor {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare the record against an internally held key.
|
* Compare the record against an internally held key. The comparison must be
|
||||||
|
* compatible with the one used for the btree.
|
||||||
* Used for visiting.
|
* Used for visiting.
|
||||||
*
|
*
|
||||||
* @param record
|
* @param record
|
||||||
|
@ -35,9 +33,8 @@ public interface IBTreeVisitor {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Visit a given record and return whether to continue or not.
|
* Visit a given record and return whether to continue or not.
|
||||||
*
|
|
||||||
* @param record
|
* @return <code>true</code> to continue the visit, <code>false</code> to abort it.
|
||||||
* @return
|
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public abstract boolean visit(int record) throws CoreException;
|
public abstract boolean visit(int record) throws CoreException;
|
||||||
|
|
|
@ -108,7 +108,7 @@ public abstract class PDOMLinkage extends PDOMNamedNode implements IBindingIdent
|
||||||
super.accept(visitor);
|
super.accept(visitor);
|
||||||
getIndex().accept(new IBTreeVisitor() {
|
getIndex().accept(new IBTreeVisitor() {
|
||||||
public int compare(int record) throws CoreException {
|
public int compare(int record) throws CoreException {
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
public boolean visit(int record) throws CoreException {
|
public boolean visit(int record) throws CoreException {
|
||||||
PDOMBinding binding = pdom.getBinding(record);
|
PDOMBinding binding = pdom.getBinding(record);
|
||||||
|
|
|
@ -74,7 +74,7 @@ implements ICPPNamespace, ICPPNamespaceScope {
|
||||||
super.accept(visitor);
|
super.accept(visitor);
|
||||||
getIndex().accept(new IBTreeVisitor() {
|
getIndex().accept(new IBTreeVisitor() {
|
||||||
public int compare(int record) throws CoreException {
|
public int compare(int record) throws CoreException {
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
public boolean visit(int record) throws CoreException {
|
public boolean visit(int record) throws CoreException {
|
||||||
PDOMBinding binding = pdom.getBinding(record);
|
PDOMBinding binding = pdom.getBinding(record);
|
||||||
|
|
|
@ -76,7 +76,7 @@ public class CountNodeAction extends IndexAction {
|
||||||
|
|
||||||
pdom.getFileIndex().accept(new IBTreeVisitor() {
|
pdom.getFileIndex().accept(new IBTreeVisitor() {
|
||||||
public int compare(int record) throws CoreException {
|
public int compare(int record) throws CoreException {
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
public boolean visit(int record) throws CoreException {
|
public boolean visit(int record) throws CoreException {
|
||||||
if (record != 0) {
|
if (record != 0) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue