public boolean isEqual(Ord<K> ok, K k) { return top().option(false, p -> ok.eq(p._1(), k)); }
/** * Checks if the given element is a member of this set. * * @param x An element to check for membership in this set. * @return true if the given element is a member of this set. */ public final boolean member(final A x) { return !isEmpty() && (ord.isLessThan(x, head()) ? l().member(x) : ord.eq(head(), x) || r().member(x)); }
private Either<A, P2<Boolean, Set<A>>> tryUpdate(final A a, final F<A, A> f) { if (isEmpty()) return right(P.p(false, this)); else if (ord.isLessThan(a, head())) return l().tryUpdate(a, f).right().map(set -> set._1() ? P.p(true, (Set<A>) new Tree<>(ord, color(), set._2(), head(), r())) : set); else if (ord.eq(a, head())) { final A h = f.f(head()); return ord.eq(head(), h) ? Either .right(P.p(true, (Set<A>) new Tree<>(ord, color(), l(), h, r()))) : Either.left(h); } else return r().tryUpdate(a, f).right().map(set -> set._1() ? P.p(true, (Set<A>) new Tree<>(ord, color(), l(), head(), set._2())) : set); }
private Set<A> ins(final A x) { return isEmpty() ? new Tree<>(ord, Color.R, empty(ord), x, empty(ord)) : ord.isLessThan(x, head()) ? balance(ord, color(), l().ins(x), head(), r()) : ord.eq(x, head()) ? new Tree<>(ord, color(), l(), x, r()) : balance(ord, color(), l(), head(), r().ins(x)); }