optimize slow loop checking for path entry duplicates
authordadubrow
Tue, 09 Jun 2009 15:42:55 -0500
changeset 26 8717e2008e00
parent 25 68aa5b0d78d4
child 27 57a05dd59a20
optimize slow loop checking for path entry duplicates
cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/APathEntry.java
cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/IncludeEntry.java
cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/IncludeFileEntry.java
cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/LibraryEntry.java
cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/MacroEntry.java
cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/MacroFileEntry.java
cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/PathEntry.java
cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/PathEntryUtil.java
--- a/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/APathEntry.java	Tue Jun 09 10:02:47 2009 -0500
+++ b/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/APathEntry.java	Tue Jun 09 15:42:55 2009 -0500
@@ -12,8 +12,7 @@
  *******************************************************************************/
 package org.eclipse.cdt.internal.core.model;
 
-import java.util.HashSet;
-import java.util.Set;
+import java.util.*;
 
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
@@ -80,6 +79,18 @@
 	}
 
 	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = super.hashCode();
+		result = prime * result
+				+ ((basePath == null) ? 0 : basePath.hashCode());
+		result = prime * result + ((baseRef == null) ? 0 : baseRef.hashCode());
+		result = prime * result + Arrays.hashCode(exclusionPatterns);
+		result = prime * result + Arrays.hashCode(fullCharExclusionPatterns);
+		return result;
+	}
+
+	@Override
 	public boolean equals(Object obj) {
 		if (obj instanceof APathEntry) {
 			APathEntry otherEntry = (APathEntry)obj;
--- a/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/IncludeEntry.java	Tue Jun 09 10:02:47 2009 -0500
+++ b/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/IncludeEntry.java	Tue Jun 09 15:42:55 2009 -0500
@@ -49,6 +49,16 @@
 		return isSystemInclude;
 	}
 
+@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = super.hashCode();
+		result = prime * result
+				+ ((includePath == null) ? 0 : includePath.hashCode());
+		result = prime * result + (isSystemInclude ? 1231 : 1237);
+		return result;
+	}
+
 	@Override
 	public boolean equals(Object obj) {
 		if (obj instanceof IIncludeEntry) {
--- a/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/IncludeFileEntry.java	Tue Jun 09 10:02:47 2009 -0500
+++ b/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/IncludeFileEntry.java	Tue Jun 09 15:42:55 2009 -0500
@@ -41,6 +41,16 @@
 	}
 
 	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = super.hashCode();
+		result = prime * result
+				+ ((includeFilePath == null) ? 0 : includeFilePath.hashCode());
+		return result;
+	}
+
+
+	@Override
 	public boolean equals(Object obj) {
 		if (obj instanceof IIncludeFileEntry) {
 			IIncludeFileEntry otherEntry = (IIncludeFileEntry) obj;
--- a/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/LibraryEntry.java	Tue Jun 09 10:02:47 2009 -0500
+++ b/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/LibraryEntry.java	Tue Jun 09 15:42:55 2009 -0500
@@ -133,6 +133,27 @@
 		return super.equals(obj);
 	}
 
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = super.hashCode();
+		result = prime * result
+				+ ((libraryPath == null) ? 0 : libraryPath.hashCode());
+		result = prime
+				* result
+				+ ((sourceAttachmentPath == null) ? 0 : sourceAttachmentPath
+						.hashCode());
+		result = prime
+				* result
+				+ ((sourceAttachmentPrefixMapping == null) ? 0
+						: sourceAttachmentPrefixMapping.hashCode());
+		result = prime
+				* result
+				+ ((sourceAttachmentRootPath == null) ? 0
+						: sourceAttachmentRootPath.hashCode());
+		return result;
+	}
+
 	public IPath getFullLibraryPath() {
 		IPath p;
 		IPath lib = getLibraryPath();
--- a/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/MacroEntry.java	Tue Jun 09 10:02:47 2009 -0500
+++ b/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/MacroEntry.java	Tue Jun 09 15:42:55 2009 -0500
@@ -45,6 +45,17 @@
 	}
 
 	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = super.hashCode();
+		result = prime * result
+				+ ((macroName == null) ? 0 : macroName.hashCode());
+		result = prime * result
+				+ ((macroValue == null) ? 0 : macroValue.hashCode());
+		return result;
+	}
+
+	@Override
 	public boolean equals(Object obj) {
 		if (obj instanceof IMacroEntry) {
 			IMacroEntry otherEntry = (IMacroEntry)obj;
--- a/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/MacroFileEntry.java	Tue Jun 09 10:02:47 2009 -0500
+++ b/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/MacroFileEntry.java	Tue Jun 09 15:42:55 2009 -0500
@@ -40,6 +40,15 @@
 	}
 
 	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = super.hashCode();
+		result = prime * result
+				+ ((macroFilePath == null) ? 0 : macroFilePath.hashCode());
+		return result;
+	}
+
+	@Override
 	public boolean equals(Object obj) {
 		if (obj instanceof IMacroFileEntry) {
 			IMacroFileEntry otherEntry = (IMacroFileEntry) obj;
--- a/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/PathEntry.java	Tue Jun 09 10:02:47 2009 -0500
+++ b/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/PathEntry.java	Tue Jun 09 15:42:55 2009 -0500
@@ -71,6 +71,16 @@
 		return super.equals(obj);
 	}
 
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + entryKind;
+		result = prime * result + (isExported ? 1231 : 1237);
+		result = prime * result + ((path == null) ? 0 : path.hashCode());
+		return result;
+	}
+
 	/**
 	 * Returns the kind from its <code>String</code> form.
 	 */
--- a/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/PathEntryUtil.java	Tue Jun 09 10:02:47 2009 -0500
+++ b/cdt/cdt_5_0_x/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/PathEntryUtil.java	Tue Jun 09 15:42:55 2009 -0500
@@ -12,9 +12,7 @@
 package org.eclipse.cdt.internal.core.model;
 
 import java.io.File;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
+import java.util.*;
 
 import org.eclipse.cdt.core.CCorePlugin;
 import org.eclipse.cdt.core.model.CModelException;
@@ -333,25 +331,20 @@
 	}
 
 	public static ICModelStatus validatePathEntry(ICProject cProject, IPathEntry[] entries) {
-		// TODO restore this!!! only for performance testing since this seems to be a major bottleneck!!		
-		if (false) { // remove
-			// Check duplication.
-			for (IPathEntry entry : entries) {
-				if (entry == null) {
-					continue;
+		// Check duplication.
+		Set<IPathEntry> entrySet = new HashSet<IPathEntry>(entries.length);
+		for (IPathEntry entry : entries) {
+			if (entry != null) {
+				if (entrySet.contains(entry)) {
+					StringBuffer errMesg = new StringBuffer(CCorePlugin.getResourceString("CoreModel.PathEntry.DuplicateEntry")); //$NON-NLS-1$
+					return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, errMesg.toString());
 				}
-				for (IPathEntry otherEntry : entries) {
-					if (otherEntry == null) {
-						continue;
-					}
-					if (entry != otherEntry && otherEntry.equals(entry)) {
-						StringBuffer errMesg = new StringBuffer(CCorePlugin.getResourceString("CoreModel.PathEntry.DuplicateEntry")); //$NON-NLS-1$
-						return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, errMesg.toString());
-					}
+				else {
+					entrySet.add(entry);
 				}
 			}
-		} // remove
-
+		}
+		
 		// check duplication of sources
 		List<IPathEntry> dups = checkForDuplication(Arrays.asList(entries), IPathEntry.CDT_SOURCE);
 		if (dups.size() > 0) {