Here you can find the source of canonicalCompare(List extends U> o1, List extends U> o2, Comparator super U> elemComparator)
Parameter | Description |
---|---|
o1 | the first list |
o2 | the second list |
elemComparator | the comparator for comparing the single elements |
public static <U> int canonicalCompare(List<? extends U> o1, List<? extends U> o2, Comparator<? super U> elemComparator)
//package com.java2s; /* Copyright (C) 2013 TU Dortmund * This file is part of AutomataLib, http://www.automatalib.net/. * //from w w w .j a va2 s .c o m * AutomataLib is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License version 3.0 as published by the Free Software Foundation. * * AutomataLib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with AutomataLib; if not, see * http://www.gnu.de/documents/lgpl.en.html. */ import java.util.Comparator; import java.util.Iterator; import java.util.List; public class Main { /** * Compares two {@link List}s with respect to canonical ordering. * <p> * In canonical ordering, a sequence <tt>o1</tt> is less than a sequence <tt>o2</tt> if <tt>o1</tt> is shorter * than <tt>o2</tt>, or if they have the same length and <tt>o1</tt> is lexicographically smaller than <tt>o2</tt>. * @param o1 the first list * @param o2 the second list * @param elemComparator the comparator for comparing the single elements * @return the result of the comparison */ public static <U> int canonicalCompare(List<? extends U> o1, List<? extends U> o2, Comparator<? super U> elemComparator) { int siz1 = o1.size(), siz2 = o2.size(); if (siz1 != siz2) { return siz1 - siz2; } return lexCompare(o1, o2, elemComparator); } /** * Compares two {@link List}s of {@link Comparable} elements with respect to canonical ordering. * <p> * In canonical ordering, a sequence <tt>o1</tt> is less than a sequence <tt>o2</tt> if <tt>o1</tt> is shorter * than <tt>o2</tt>, or if they have the same length and <tt>o1</tt> is lexicographically smaller than <tt>o2</tt>. * @param o1 the first list * @param o2 the second list * @return the result of the comparison */ public static <U extends Comparable<? super U>> int canonicalCompare(List<? extends U> o1, List<? extends U> o2) { int siz1 = o1.size(), siz2 = o2.size(); if (siz1 != siz2) { return siz1 - siz2; } return lexCompare(o1, o2); } /** * Lexicographically compares two {@link Iterable}s. Comparison of the * elements is done using the specified comparator. * * @param o1 the first iterable. * @param o2 the second iterable. * @param elemComparator the comparator. * @return <code>< 0</code> iff o1 is lexicographically smaller, * <code>0</code> if o1 equals o2 and <code>> 0</code> otherwise. */ public static <U> int lexCompare(Iterable<? extends U> o1, Iterable<? extends U> o2, Comparator<U> elemComparator) { Iterator<? extends U> it1 = o1.iterator(), it2 = o2.iterator(); while (it1.hasNext() && it2.hasNext()) { int cmp = elemComparator.compare(it1.next(), it2.next()); if (cmp != 0) return cmp; } if (it1.hasNext()) return 1; else if (it2.hasNext()) return -1; return 0; } /** * Lexicographically compares two {@link Iterable}s, whose element types * are comparable. * @param o1 * @param o2 * @return */ public static <U extends Comparable<? super U>> int lexCompare(Iterable<? extends U> o1, Iterable<? extends U> o2) { Iterator<? extends U> it1 = o1.iterator(), it2 = o2.iterator(); while (it1.hasNext() && it2.hasNext()) { int cmp = it1.next().compareTo(it2.next()); if (cmp != 0) return cmp; } if (it1.hasNext()) return 1; else if (it2.hasNext()) return -1; return 0; } }