📄 comparing.java
字号:
package com.reddragon2046.base.utilities.data.algorithms;
import com.reddragon2046.base.utilities.data.*;
import com.reddragon2046.base.utilities.data.util.IteratorFactory;
import java.util.Collection;
// Referenced classes of package com.reddragon2046.base.utilities.data.algorithms:
// Predicates
public final class Comparing
{
private Comparing()
{
}
public static Object median(Object a, Object b, Object c, BinaryPredicate comparator)
{
if(comparator.execute(a, b))
{
if(comparator.execute(b, c))
return b;
if(comparator.execute(a, c))
return c;
else
return a;
}
if(comparator.execute(a, c))
return a;
if(comparator.execute(b, c))
return c;
else
return b;
}
public static Pair mismatch(InputIterator first1, InputIterator last1, InputIterator first2)
{
if(!first1.isCompatibleWith(last1))
throw new IllegalArgumentException("iterators not compatible");
InputIterator first1x = (InputIterator)first1.clone();
InputIterator first2x = (InputIterator)first2.clone();
for(; !first1x.equals(last1); first2x.advance())
{
Object one = first1x.get();
Object two = first2x.get();
if(one != null ? !one.equals(two) : two != null)
break;
first1x.advance();
}
return new Pair(first1x, first2x);
}
public static Pair mismatch(Collection collection1, Collection collection2)
{
return mismatch(IteratorFactory.start(collection1), IteratorFactory.finish(collection1), IteratorFactory.start(collection2));
}
public static Pair mismatch(InputIterator first1, InputIterator last1, InputIterator first2, BinaryPredicate predicate)
{
if(!first1.isCompatibleWith(last1))
throw new IllegalArgumentException("iterators not compatible");
InputIterator first1x = (InputIterator)first1.clone();
InputIterator first2x;
for(first2x = (InputIterator)first2.clone(); !first1x.equals(last1) && predicate.execute(first1x.get(), first2x.get()); first2x.advance())
first1x.advance();
return new Pair(first1x, first2x);
}
public static Pair mismatch(Collection collection1, Collection collection2, BinaryPredicate predicate)
{
return mismatch(IteratorFactory.start(collection1), IteratorFactory.finish(collection1), IteratorFactory.start(collection2), predicate);
}
public static boolean equal(InputIterator first1, InputIterator last1, InputIterator first2)
{
if(!first1.isCompatibleWith(last1))
throw new IllegalArgumentException("iterators not compatible");
InputIterator first1x = (InputIterator)first1.clone();
InputIterator first2x = (InputIterator)first2.clone();
for(; !first1x.equals(last1); first2x.advance())
{
Object one = first1x.get();
Object two = first2x.get();
if(one != null ? !one.equals(two) : two != null)
return false;
first1x.advance();
}
return true;
}
public static boolean equal(Collection collection1, Collection collection2)
{
return collection1.size() == collection2.size() && equal(IteratorFactory.start(collection1), IteratorFactory.finish(collection1), IteratorFactory.start(collection2));
}
private static boolean lexicographicalCompare(InputIterator first1, InputIterator last1, InputIterator first2, InputIterator last2)
{
return lexicographicalCompare(first1, last1, first2, last2, ((BinaryPredicate) (new Predicates.HashComparator())));
}
public static boolean lexicographicalCompare(Collection collection1, Collection collection2)
{
return lexicographicalCompare(IteratorFactory.start(collection1), IteratorFactory.finish(collection1), IteratorFactory.start(collection2), IteratorFactory.finish(collection2));
}
private static boolean lexicographicalCompare(InputIterator first1, InputIterator last1, InputIterator first2, InputIterator last2, BinaryPredicate comparator)
{
if(!first1.isCompatibleWith(last1) || !first2.isCompatibleWith(last2))
throw new IllegalArgumentException("iterators not compatible");
InputIterator first1x = (InputIterator)first1.clone();
InputIterator first2x;
for(first2x = (InputIterator)first2.clone(); !first1x.equals(last1) && !first2x.equals(last2); first2x.advance())
{
Object obj1 = first1x.get();
Object obj2 = first2x.get();
if(comparator.execute(obj1, obj2))
return true;
if(comparator.execute(obj2, obj1))
return false;
first1x.advance();
}
return first1x.equals(last1) && !first2x.equals(last2);
}
public static boolean lexicographicalCompare(Collection collection1, Collection collection2, BinaryPredicate comparator)
{
return lexicographicalCompare(IteratorFactory.start(collection1), IteratorFactory.finish(collection1), IteratorFactory.start(collection2), IteratorFactory.finish(collection2), comparator);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -