Skip to content

Commit

Permalink
no more strict comparator checking in map exclude method if not needed
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@1901 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
orbiter committed Mar 16, 2006
1 parent 0b903c5 commit 9cca36a
Showing 1 changed file with 58 additions and 55 deletions.
113 changes: 58 additions & 55 deletions source/de/anomic/kelondro/kelondroMSetTools.java
Expand Up @@ -222,88 +222,91 @@ public static TreeMap excludeConstructive(TreeMap map, TreeSet set) {
// comparators must be equal
if (map == null) return null;
if (set == null) return map;
if (map.comparator() != set.comparator()) return null;
if ((map.size() == 0) || (set.size() == 0)) return map;

if (map.comparator() != set.comparator()) return excludeConstructiveByTestMapInSet(map, set);
return excludeConstructiveByTestMapInSet(map, set);
//return excludeConstructiveByEnumeration(map, set);
// return excludeConstructiveByEnumeration(map, set);
}

private static TreeMap excludeConstructiveByTestMapInSet(TreeMap map, TreeSet set) {
Iterator mi = map.keySet().iterator();
TreeMap result = new TreeMap(map.comparator());
Object o;
while (mi.hasNext()) {
o = mi.next();
if (!(set.contains(o))) result.put(o, map.get(o));
}
return result;
Iterator mi = map.keySet().iterator();
TreeMap result = new TreeMap(map.comparator());
Object o;
while (mi.hasNext()) {
o = mi.next();
if (!(set.contains(o))) result.put(o, map.get(o));
}
return result;
}

private static TreeMap excludeConstructiveByEnumeration(TreeMap map, TreeSet set) {
// returns map without the elements in set
// enumerates objects
Comparator comp = map.comparator();
Iterator mi = map.keySet().iterator();
Iterator si = set.iterator();
TreeMap result = new TreeMap(map.comparator());
int c;
if ((mi.hasNext()) && (si.hasNext())) {
Object mobj = mi.next();
Object sobj = si.next();
while (true) {
c = compare(mobj, sobj, comp);
if (c < 0) {
result.put(mobj, map.get(mobj));
if (mi.hasNext()) mobj = mi.next(); else break;
} else if (c > 0) {
if (si.hasNext()) sobj = si.next(); else break;
} else {
if (mi.hasNext()) mobj = mi.next(); else break;
if (si.hasNext()) sobj = si.next(); else {
// final flush
result.put(mobj, map.get(mobj));
while (mi.hasNext()) {
mobj = mi.next();
result.put(mobj, map.get(mobj));
}
break;
}
}
}
}
return result;
private static TreeMap excludeConstructiveByEnumeration(TreeMap map, TreeSet set) {
// returns map without the elements in set
// enumerates objects
Comparator comp = map.comparator();
Iterator mi = map.keySet().iterator();
Iterator si = set.iterator();
TreeMap result = new TreeMap(map.comparator());
int c;
if ((mi.hasNext()) && (si.hasNext())) {
Object mobj = mi.next();
Object sobj = si.next();
while (true) {
c = compare(mobj, sobj, comp);
if (c < 0) {
result.put(mobj, map.get(mobj));
if (mi.hasNext()) mobj = mi.next();
else break;
} else if (c > 0) {
if (si.hasNext()) sobj = si.next();
else break;
} else {
if (mi.hasNext()) mobj = mi.next();
else break;
if (si.hasNext()) sobj = si.next();
else {
// final flush
result.put(mobj, map.get(mobj));
while (mi.hasNext()) {
mobj = mi.next();
result.put(mobj, map.get(mobj));
}
break;
}
}
}
}
return result;
}

public static void excludeDestructive(TreeMap map, TreeSet set) {
// comparators must be equal
if (map == null) return;
if (set == null) return;
if (map.comparator() != set.comparator()) return;
if (map.comparator() != set.comparator()) return;
if ((map.size() == 0) || (set.size() == 0)) return;

if (map.size() < set.size())
excludeDestructiveByTestMapInSet(map, set);
else
excludeDestructiveByTestSetInMap(map, set);
}

private static void excludeDestructiveByTestMapInSet(TreeMap map, TreeSet set) {
Iterator mi = map.keySet().iterator();
while (mi.hasNext()) if (set.contains(mi.next())) mi.remove();
Iterator mi = map.keySet().iterator();
while (mi.hasNext()) if (set.contains(mi.next())) mi.remove();
}

private static void excludeDestructiveByTestSetInMap(TreeMap map, TreeSet set) {
Iterator si = set.iterator();
while (si.hasNext()) map.remove(si.next());
Iterator si = set.iterator();
while (si.hasNext()) map.remove(si.next());
}

// and the same again with set-set
public static void excludeDestructive(TreeSet set1, TreeSet set2) {
// comparators must be equal
if (set1 == null) return;
if (set2 == null) return;
if (set1.comparator() != set2.comparator()) return;
if (set1.comparator() != set2.comparator()) return;
if ((set1.size() == 0) || (set2.size() == 0)) return;

if (set1.size() < set2.size())
Expand All @@ -313,13 +316,13 @@ public static void excludeDestructive(TreeSet set1, TreeSet set2) {
}

private static void excludeDestructiveByTestSmallInLarge(TreeSet small, TreeSet large) {
Iterator mi = small.iterator();
while (mi.hasNext()) if (large.contains(mi.next())) mi.remove();
Iterator mi = small.iterator();
while (mi.hasNext()) if (large.contains(mi.next())) mi.remove();
}

private static void excludeDestructiveByTestLargeInSmall(TreeSet large, TreeSet small) {
Iterator si = small.iterator();
while (si.hasNext()) large.remove(si.next());
Iterator si = small.iterator();
while (si.hasNext()) large.remove(si.next());
}

// ------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 9cca36a

Please sign in to comment.