We're updating the issue view to help you get more done. 

RFE: UnicodeSet.compareTo(UnicodeSet)

Description

UnicodeSet should implement Compareable, so that UnicodeSets can be sorted. (And
the equivalent on C++).

Below is a comparison operation, implemented as a Comparator (external to the
class). It can be modified and added as a compareTo() method. (The modifications
can include optimization to just use list[i], returning the first difference.)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public final class UnicodeSetComparator implements Comparator { /** * Compares two UnicodeSets, producing a transitive ordering. * @return -1 if first is smaller (in size) than second, * 1 if first is greater (in size) than second, * Otherwise (since they are equal in size) * returns a comparison based on the first range that differs. * */ public int compare(Object o1, Object o2) { UnicodeSet bs1 = (UnicodeSet) o1; UnicodeSet bs2 = (UnicodeSet) o2; if (bs1.size() < bs2.size()) return -1; if (bs1.size() > bs2.size()) return 1; UnicodeSetIterator it1 = new UnicodeSetIterator(bs1); UnicodeSetIterator it2 = new UnicodeSetIterator(bs2); // Note: because they are the same size, and we stop if any ranges // are different, it is safe to test for both at the same time while (it1.nextRange() && it2.nextRange()) { if (it1.codepoint < it2.codepoint) return -1; if (it1.codepoint > it2.codepoint) return 1; if (it1.codepointEnd < it2.codepointEnd) return -1; if (it1.codepointEnd > it2.codepointEnd) return 1; } return 0; } }

Status

Assignee

Andy Heninger

Reporter

TracBot

Labels

Reviewer

None

Time Needed

Hours

Start date

None

Components

Priority

medium