1
0
Fork 0
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:
Markus Schorn 2006-11-23 16:38:44 +00:00
parent d4dc19649e
commit 3329794554
6 changed files with 30 additions and 55 deletions

View file

@ -172,7 +172,7 @@ public class BTreeTests extends BaseTestCase {
btree.accept(new IBTreeVisitor(){
int k;
public int compare(int record) throws CoreException {
return 1;
return 0;
}
public boolean visit(int record) throws CoreException {
if(record!=0) {

View file

@ -8,6 +8,7 @@
* Contributors:
* QNX - Initial API and implementation
* Andrew Ferguson (Symbian) - Provide B-tree deletion routine
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.db;
@ -520,16 +521,17 @@ public class BTree {
* @param visitor
*/
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
// once found is true visit everything
// return false when ready to quit
if (node == 0)
return visitor.visit(0);
if (node == 0) {
return true;
}
if(visitor instanceof IBTreeVisitor2) {
((IBTreeVisitor2)visitor).preNode(node);
}
@ -537,49 +539,25 @@ public class BTree {
try {
Chunk chunk = db.getChunk(node);
if (found) {
int child = getChild(chunk, node, 0);
if (child != 0)
if (!accept(child, visitor, true))
return false;
}
int i;
for (i = 0; i < MAX_RECORDS; ++i) {
int i= 0;
for (; i < MAX_RECORDS; ++i) {
int record = getRecord(chunk, node, i);
if (record == 0)
if (record == 0)
break;
if (found) {
int compare = visitor.compare(record);
if (compare > 0) {
// start point is to the left
return accept(getChild(chunk, node, i), visitor);
}
else if (compare == 0) {
if (!accept(getChild(chunk, node, i), visitor))
return false;
if (!visitor.visit(record))
return false;
if (!accept(getChild(chunk, node, i + 1), visitor, true))
return false;
} else {
int compare = visitor.compare(record);
if (compare > 0) {
// start point is to the left
if (!accept(getChild(chunk, node, i), visitor, false))
return false;
if (!visitor.visit(record))
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;
}
}
}
if (!found)
return accept(getChild(chunk, node, i), visitor, false);
return true;
return accept(getChild(chunk, node, i), visitor);
} finally {
if(visitor instanceof IBTreeVisitor2) {
((IBTreeVisitor2)visitor).postNode(node);
@ -621,7 +599,7 @@ public class BTree {
public String getMsg() { return msg; }
public boolean isValid() { return valid; }
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 void preNode(int node) throws CoreException {

View file

@ -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
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -7,6 +7,7 @@
*
* Contributors:
* QNX - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.db;
@ -15,16 +16,13 @@ import org.eclipse.core.runtime.CoreException;
/**
* @author Doug Schaefer
*
* The visitor walks through the tree until the compare returns
* >= 0, i.e. we reach the first record that meets the criteria.
*
* It then continues until the visit returns false.
*
* The visitor visits all records where compare returns 0.
*/
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.
*
* @param record
@ -35,9 +33,8 @@ public interface IBTreeVisitor {
/**
* Visit a given record and return whether to continue or not.
*
* @param record
* @return
* @return <code>true</code> to continue the visit, <code>false</code> to abort it.
* @throws IOException
*/
public abstract boolean visit(int record) throws CoreException;

View file

@ -108,7 +108,7 @@ public abstract class PDOMLinkage extends PDOMNamedNode implements IBindingIdent
super.accept(visitor);
getIndex().accept(new IBTreeVisitor() {
public int compare(int record) throws CoreException {
return 1;
return 0;
}
public boolean visit(int record) throws CoreException {
PDOMBinding binding = pdom.getBinding(record);

View file

@ -74,7 +74,7 @@ implements ICPPNamespace, ICPPNamespaceScope {
super.accept(visitor);
getIndex().accept(new IBTreeVisitor() {
public int compare(int record) throws CoreException {
return 1;
return 0;
}
public boolean visit(int record) throws CoreException {
PDOMBinding binding = pdom.getBinding(record);

View file

@ -76,7 +76,7 @@ public class CountNodeAction extends IndexAction {
pdom.getFileIndex().accept(new IBTreeVisitor() {
public int compare(int record) throws CoreException {
return 1;
return 0;
}
public boolean visit(int record) throws CoreException {
if (record != 0) {