From 2b3ab14082ab6f38d26e096e14d0bc4d6ac9b376 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Wed, 18 May 2016 17:16:55 -0700 Subject: [PATCH] Code streamlining. Change-Id: I773eb373e6397260af6fd3815f1b16815500dabd --- .../eclipse/cdt/core/dom/IPDOMVisitor.java | 12 ++--- .../cdt/internal/core/pdom/db/BTree.java | 51 +++++++------------ .../internal/core/pdom/db/IBTreeVisitor.java | 32 ++++++++---- 3 files changed, 45 insertions(+), 50 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/IPDOMVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/IPDOMVisitor.java index 535731098c7..9e6186e296e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/IPDOMVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/IPDOMVisitor.java @@ -6,29 +6,26 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * QNX - Initial API and implementation + * QNX - Initial API and implementation *******************************************************************************/ - package org.eclipse.cdt.core.dom; import org.eclipse.core.runtime.CoreException; /** * @author Doug Schaefer - * */ public interface IPDOMVisitor { - /** - * Walk the nodes in a PDOM. Return true to visit the children of - * this node, or false to skip to the next sibling of this node. + * Walks the nodes in a PDOM. Returns true to visit the children of + * the node, or false to skip to the next sibling of this node. * Throw CoreException to stop the visit. * * @param node being visited * @return whether to visit children */ public boolean visit(IPDOMNode node) throws CoreException; - + /** * All children have been visited, about to go back to the parent. * @@ -36,5 +33,4 @@ public interface IPDOMVisitor { * @throws CoreException */ public void leave(IPDOMNode node) throws CoreException; - } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/BTree.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/BTree.java index 5061685cc3b..0e64d1094f6 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/BTree.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/BTree.java @@ -47,7 +47,7 @@ public class BTree { /** * Constructor. * - * @param db the database containing the btree + * @param db the database containing the B-tree * @param rootPointer offset into database of the pointer to the root node */ public BTree(Database db, long rootPointer, int degree, IBTreeComparator cmp) { @@ -60,8 +60,8 @@ public class BTree { this.DEGREE = degree; this.MIN_RECORDS = DEGREE - 1; - this.MAX_RECORDS = 2*DEGREE - 1; - this.MAX_CHILDREN = 2*DEGREE; + this.MAX_RECORDS = 2 * DEGREE - 1; + this.MAX_CHILDREN = 2 * DEGREE; this.OFFSET_CHILDREN = MAX_RECORDS * Database.INT_SIZE; this.MEDIAN_RECORD = DEGREE - 1; } @@ -157,7 +157,7 @@ public class BTree { } } - // Binary search to find the insert point. + // Binary search to find the insertion point. int lower= 0; int upper= MAX_RECORDS - 1; while (lower < upper && getRecord(chunk, node, upper - 1) == 0) { @@ -165,7 +165,7 @@ public class BTree { } while (lower < upper) { - int middle= (lower + upper) / 2; + int middle= (lower + upper) >>> 1; long checkRec= getRecord(chunk, node, middle); if (checkRec == 0) { upper= middle; @@ -414,7 +414,7 @@ public class BTree { } /** - * Merge node 'src' onto the right side of node 'dst' using node + * Merges node 'src' onto the right side of node 'dst' using node * 'keyProvider' as the source of the median key. Bounds checking is not * performed. * @param src the key to merge into dst @@ -446,7 +446,7 @@ public class BTree { } /** - * Insert the key and (its predecessor) child at the left side of the specified node. Bounds checking + * Inserts the key and (its predecessor) child at the left side of the specified node. Bounds checking * is not performed. * @param node the node to prepend to * @param key the new leftmost (least) key @@ -459,7 +459,7 @@ public class BTree { } /** - * Insert the key and (its successor) child at the right side of the specified node. Bounds + * Inserts the key and (its successor) child at the right side of the specified node. Bounds * checking is not performed. * @param node * @param key @@ -471,7 +471,7 @@ public class BTree { } /** - * Overwrite a section of the specified node (dst) with the specified section of the source + * Overwrites a section of the specified node (dst) with the specified section of the source * node. Bounds checking is not performed. To allow just copying of the final child (which has * no corresponding key) the routine behaves as though there were a corresponding key existing * with value zero.

@@ -500,7 +500,7 @@ public class BTree { } /** - * Delete a section of node content - (key, (predecessor)child) pairs. Bounds checking + * Deletes a section of node content - (key, (predecessor)child) pairs. Bounds checking * is not performed. To allow deletion of the final child (which has no corresponding key) * the routine behaves as though there were a corresponding key existing with value zero.

* Content is deleted and remaining content is moved leftward the appropriate amount. @@ -522,7 +522,7 @@ public class BTree { } /** - * Visit all nodes beginning when the visitor comparator + * Visits all nodes beginning when the visitor comparator * returns >= 0 until the visitor visit returns falls. * * @param visitor @@ -539,9 +539,7 @@ public class BTree { if (node == 0) { return true; } - if (visitor instanceof IBTreeVisitor2) { - ((IBTreeVisitor2) visitor).preNode(node); - } + visitor.preNode(node); try { Chunk chunk = db.getChunk(node); @@ -553,7 +551,7 @@ public class BTree { upper--; } while (lower < upper) { - int middle= (lower + upper) / 2; + int middle= (lower + upper) >>> 1; long checkRec = getRecord(chunk, node, middle); if (checkRec == 0) { upper= middle; @@ -578,7 +576,7 @@ public class BTree { if (compare > 0) { // Start point is to the left. return accept(getChild(chunk, node, i), visitor); - } else if (compare == 0) { + } else if (compare == 0) { if (!accept(getChild(chunk, node, i), visitor)) return false; if (!visitor.visit(record)) @@ -587,25 +585,14 @@ public class BTree { } return accept(getChild(chunk, node, i), visitor); } finally { - if (visitor instanceof IBTreeVisitor2) { - ((IBTreeVisitor2) visitor).postNode(node); - } + visitor.postNode(node); } } - /* - * TODO: It would be good to move these into IBTreeVisitor and eliminate - * IBTreeVisitor2 if this is acceptable. - */ - private interface IBTreeVisitor2 extends IBTreeVisitor { - void preNode(long node) throws CoreException; - void postNode(long node) throws CoreException; - } - /** - * Debugging method for checking B-tree invariants - * @return the empty String if B-tree invariants hold, otherwise - * a human readable report + * Debugging method for checking B-tree invariants. + * + * @return the empty String if B-tree invariants hold, otherwise a human readable report * @throws CoreException */ public String getInvariantsErrorReport() throws CoreException { @@ -618,7 +605,7 @@ public class BTree { * A B-tree visitor for checking some B-tree invariants. * Note ordering invariants are not checked here. */ - private class InvariantsChecker implements IBTreeVisitor2 { + private class InvariantsChecker implements IBTreeVisitor { boolean valid = true; String msg = ""; //$NON-NLS-1$ Integer leafDepth; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/IBTreeVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/IBTreeVisitor.java index 1e43ed26be3..a9002b38e37 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/IBTreeVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/IBTreeVisitor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2012 QNX Software Systems and others. + * Copyright (c) 2005, 2015 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 @@ -20,21 +20,33 @@ import org.eclipse.core.runtime.CoreException; */ public interface IBTreeVisitor { /** - * Compare the record against an internally held key. The comparison must be - * compatible with the one used for the btree. + * Compares the record against an internally held key. The comparison must be + * compatible with the one used for the B-tree. * Used for visiting. * * @param record * @return -1 if record < key, 0 if record == key, 1 if record > key - * @throws CoreException */ - public abstract int compare(long record) throws CoreException; + public int compare(long record) throws CoreException; /** - * Visit a given record and return whether to continue or not. - - * @return true to continue the visit, false to abort it. - * @throws CoreException + * Visits a given record and returns whether to continue or not. + * + * @return {@code true} to continue the visit, {@code false} to abort it. */ - public abstract boolean visit(long record) throws CoreException; + public boolean visit(long record) throws CoreException; + + /** + * Called before visiting a node. + * + * @param record the node being visited + */ + public default void preNode(long record) throws CoreException {} + + /** + * Called after visiting a node. + * + * @param record the node being visited + */ + public default void postNode(long record) throws CoreException {} }