public static <A> Ordering compareTreeNodes( Tree<PackageFileSystemObject<A>> a, Tree<PackageFileSystemObject<A>> b ) { return RelativePath.ord.compare( a.root().getUnixFsObject().path, b.root().getUnixFsObject().path ); }
public static void main(String[] args) { List<Integer> naturals = naturals().take(20000).filter(i -> i > 10000).take(100); System.out.println(naturals.length()); List<Integer> naturals2 = naturals().take(100).map(i -> i - 1); Lists.listShow(Show.intShow).println(naturals); System.out.println(Lists.listEqual(Equal.intEqual).eq(naturals, naturals)); System.out.println(Lists.listOrd(Ord.intOrd).compare(naturals, naturals2)); }
/** * Find element equal to the given one. * * @param a An element to compare with. * @return Some element in this set equal to the given one, or None. */ public final Option<A> lookup(final A a) { Set<A> s = this; while (true) if (s.isEmpty()) return none(); else { final A h = s.head(); final Ordering i = ord.compare(a, h); if (i == LT) s = s.l(); else if (i == GT) s = s.r(); else return some(h); } }
/** * Find largest element smaller than the given one. * * @param a An element to compare with. * @return Some largest element in this set smaller than the given one, or None. */ public final Option<A> lookupLT(final A a) { Set<A> s = this; Option<A> r = none(); while (true) if (s.isEmpty()) return r; else { final A h = s.head(); final Ordering i = ord.compare(a, h); if (i == GT) { r = some(h); s = s.r(); } else s = s.l(); } }
/** * Find smallest element greater than the given one. * * @param a An element to compare with. * @return Some smallest element in this set greater than the given one, or None. */ public final Option<A> lookupGT(final A a) { Set<A> s = this; Option<A> r = none(); while (true) if (s.isEmpty()) return r; else { final A h = s.head(); final Ordering i = ord.compare(a, h); if (i == LT) { r = some(h); s = s.l(); } else s = s.r(); } }
/** * Find smallest element greater or equal to the given one. * * @param a An element to compare with. * @return Some smallest element in this set greater or equal to the given one, or None. */ public final Option<A> lookupGE(final A a) { Set<A> s = this; Option<A> r = none(); while (true) if (s.isEmpty()) return r; else { final A h = s.head(); final Ordering i = ord.compare(a, h); if (i == LT) { r = some(h); s = s.l(); } else if (i == GT) s = s.r(); else return some(h); } }
/** * Find largest element smaller or equal to the given one. * * @param a An element to compare with. * @return Some largest element in this set smaller or equal to the given one, or None. */ public final Option<A> lookupLE(final A a) { Set<A> s = this; Option<A> r = none(); while (true) if (s.isEmpty()) return r; else { final A h = s.head(); final Ordering i = ord.compare(a, h); if (i == LT) s = s.l(); else if (i == GT) { r = some(h); s = s.r(); } else return some(h); } }
/** * An order instance for the {@link Array} type. * * @param oa Order across the elements of the array. * @return An order instance for the {@link Array} type. */ public static <A> Ord<Array<A>> arrayOrd(final Ord<A> oa) { return ordDef((a1, a2) -> { int i = 0; //noinspection ForLoopWithMissingComponent for (; i < a1.length() && i < a2.length(); i++) { final Ordering c = oa.compare(a1.get(i), a2.get(i)); if (c == Ordering.GT || c == Ordering.LT) return c; } return i == a1.length() ? i == a2.length() ? Ordering.EQ : Ordering.LT : i == a1.length() ? Ordering.EQ : Ordering.GT; }); }
/** * An order instance for the {@link List} type. * * @param oa Order across the elements of the list. * @return An order instance for the {@link List} type. */ public static <A> Ord<List<A>> listOrd(final Ord<A> oa) { return ordDef((l1, l2) -> { List<A> x1 = l1; List<A> x2 = l2; while (x1.isNotEmpty() && x2.isNotEmpty()) { final Ordering o = oa.compare(x1.head(), x2.head()); if (o == Ordering.LT || o == Ordering.GT) { return o; } x1 = x1.tail(); x2 = x2.tail(); } if (x1.isEmpty() && x2.isEmpty()) { return Ordering.EQ; } else if (x1.isEmpty()) { return Ordering.LT; } else { return Ordering.GT; } }); }
/** * Returns a stream using the given enumerator from the given value to the other given value stepping at the given * increment. * * @param e The enumerator to compute successors from. * @param from The value to begin computing successors from. * @param to The value to stop computing successors from. * @param step The increment to step. * @return A stream using the given enumerator from the given value to the other given value stepping at the given * increment. */ public static <A> Stream<A> range(final Enumerator<A> e, final A from, final A to, final long step) { final Ordering o = e.order().compare(from, to); return o == EQ || step > 0L && o == GT || step < 0L && o == LT ? single(from) : cons(from, () -> join(e.plus(from, step).filter(a -> !(o == LT ? e.order().isLessThan(to, a) : e.order().isGreaterThan(to, a))).map(a1 -> range(e, a1, to, step)).toStream())); } /**
/** * An order instance for the {@link Stream} type. * * @param oa Order across the elements of the stream. * @return An order instance for the {@link Stream} type. */ public static <A> Ord<Stream<A>> streamOrd(final Ord<A> oa) { return ordDef((s1, s2) -> { if (s1.isEmpty()) return s2.isEmpty() ? Ordering.EQ : Ordering.LT; else if (s2.isEmpty()) return s1.isEmpty() ? Ordering.EQ : Ordering.GT; else { final Ordering c = oa.compare(s1.head(), s2.head()); // FIXME: not stack safe return c == Ordering.EQ ? streamOrd(oa).def.compare(s1.tail()._1()).f(s2.tail()._1()) : c; } }); }
/** * Splits this set at the given element. Returns a product-3 of: * <ul> * <li>A set containing all the elements of this set which are less than the given value.</li> * <li>An option of a value equal to the given value, if one was found in this set, otherwise None. * <li>A set containing all the elements of this set which are greater than the given value.</li> * </ul> * * @param a A value at which to split this set. * @return Two sets and an optional value, where all elements in the first set are less than the given value * and all the elements in the second set are greater than the given value, and the optional value is the * given value if found, otherwise None. */ public final P3<Set<A>, Option<A>, Set<A>> split(final A a) { if (isEmpty()) return P.p(empty(ord), Option.none(), empty(ord)); else { final A h = head(); final Ordering i = ord.compare(a, h); if (i == LT) { final P3<Set<A>, Option<A>, Set<A>> lg = l().split(a); return P.p(lg._1(), lg._2(), lg._3().insert(h).union(r())); } else if (i == GT) { final P3<Set<A>, Option<A>, Set<A>> lg = r().split(a); return P.p(lg._1().insert(h).union(l()), lg._2(), lg._3()); } else return P.p(l(), some(h), r()); } }