Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2008, 2013 Technical University Berlin, Germany 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 http://www.eclipse.org/legal/epl-v10.html Contributors: Stephan Herrmann - Initial API and implementation /
package org.eclipse.jdt.internal.compiler.util;
Sorting utilities. Originally developed for the Object Teams project.
public class Sorting {

Topological sort for types Guarantee: supertypes come before subtypes.
	public static ReferenceBinding[] sortTypes(ReferenceBinding[] types) {
		int len = types.length;
		ReferenceBinding[] unsorted = new ReferenceBinding[len];
		ReferenceBinding[] sorted = new ReferenceBinding[len];
		System.arraycopy(types, 0, unsorted, 0, len);
		int o = 0;
		for(int i=0; i<leni++)
			o = sort(unsortedisortedo);
		return sorted;
	}
	// Transfer input[i] and all its supers into output[o] ff.
	private static int sort(ReferenceBinding[] inputint i,
							ReferenceBinding[] outputint o)
	{
		if (input[i] == null)
			return o;
		ReferenceBinding superclass = input[i].superclass();
		o = sortSuper(superclassinputoutputo);
		ReferenceBinding[] superInterfaces = input[i].superInterfaces();
		for (int j=0; j<superInterfaces.lengthj++) {
			o = sortSuper(superInterfaces[j], inputoutputo);
		}
		// done with supers, now input[i] can safely be transferred:
		output[o++] = input[i];
		input[i] = null;
		return o;
	}
	// if superclass is within the set of types to sort,
	// transfer it and all its supers to output[o] ff.
	private static int sortSuper(ReferenceBinding superclass,
						  		 ReferenceBinding[] input,
						  		 ReferenceBinding[] outputint o)
	{
		if (superclass.id != .) {
			// search superclass within input:
			int j = 0;
			for(j=0; j<input.lengthj++)
				if (input[j] == superclass)
					break;
			if (j < input.length)
				// depth first traversal:
				o = sort(inputjoutputo);
			// otherwise assume super was already transferred.
		}
		return o;
	}
New to GrepCode? Check out our FAQ X