no more strict comparator checking in map exclude method if not needed

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@1901 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 19 years ago
parent 0b903c5317
commit 9cca36a107

@ -222,66 +222,69 @@ public class kelondroMSetTools {
// comparators must be equal // comparators must be equal
if (map == null) return null; if (map == null) return null;
if (set == null) return map; if (set == null) return map;
if (map.comparator() != set.comparator()) return null;
if ((map.size() == 0) || (set.size() == 0)) return map; if ((map.size() == 0) || (set.size() == 0)) return map;
if (map.comparator() != set.comparator()) return excludeConstructiveByTestMapInSet(map, set);
return excludeConstructiveByTestMapInSet(map, set); return excludeConstructiveByTestMapInSet(map, set);
//return excludeConstructiveByEnumeration(map, set); // return excludeConstructiveByEnumeration(map, set);
} }
private static TreeMap excludeConstructiveByTestMapInSet(TreeMap map, TreeSet set) { private static TreeMap excludeConstructiveByTestMapInSet(TreeMap map, TreeSet set) {
Iterator mi = map.keySet().iterator(); Iterator mi = map.keySet().iterator();
TreeMap result = new TreeMap(map.comparator()); TreeMap result = new TreeMap(map.comparator());
Object o; Object o;
while (mi.hasNext()) { while (mi.hasNext()) {
o = mi.next(); o = mi.next();
if (!(set.contains(o))) result.put(o, map.get(o)); if (!(set.contains(o))) result.put(o, map.get(o));
} }
return result; return result;
} }
private static TreeMap excludeConstructiveByEnumeration(TreeMap map, TreeSet set) { private static TreeMap excludeConstructiveByEnumeration(TreeMap map, TreeSet set) {
// returns map without the elements in set // returns map without the elements in set
// enumerates objects // enumerates objects
Comparator comp = map.comparator(); Comparator comp = map.comparator();
Iterator mi = map.keySet().iterator(); Iterator mi = map.keySet().iterator();
Iterator si = set.iterator(); Iterator si = set.iterator();
TreeMap result = new TreeMap(map.comparator()); TreeMap result = new TreeMap(map.comparator());
int c; int c;
if ((mi.hasNext()) && (si.hasNext())) { if ((mi.hasNext()) && (si.hasNext())) {
Object mobj = mi.next(); Object mobj = mi.next();
Object sobj = si.next(); Object sobj = si.next();
while (true) { while (true) {
c = compare(mobj, sobj, comp); c = compare(mobj, sobj, comp);
if (c < 0) { if (c < 0) {
result.put(mobj, map.get(mobj)); result.put(mobj, map.get(mobj));
if (mi.hasNext()) mobj = mi.next(); else break; if (mi.hasNext()) mobj = mi.next();
} else if (c > 0) { else break;
if (si.hasNext()) sobj = si.next(); else break; } else if (c > 0) {
} else { if (si.hasNext()) sobj = si.next();
if (mi.hasNext()) mobj = mi.next(); else break; else break;
if (si.hasNext()) sobj = si.next(); else { } else {
// final flush if (mi.hasNext()) mobj = mi.next();
result.put(mobj, map.get(mobj)); else break;
while (mi.hasNext()) { if (si.hasNext()) sobj = si.next();
mobj = mi.next(); else {
result.put(mobj, map.get(mobj)); // final flush
} result.put(mobj, map.get(mobj));
break; while (mi.hasNext()) {
} mobj = mi.next();
} result.put(mobj, map.get(mobj));
} }
} break;
return result; }
}
}
}
return result;
} }
public static void excludeDestructive(TreeMap map, TreeSet set) { public static void excludeDestructive(TreeMap map, TreeSet set) {
// comparators must be equal // comparators must be equal
if (map == null) return; if (map == null) return;
if (set == 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() == 0) || (set.size() == 0)) return;
if (map.size() < set.size()) if (map.size() < set.size())
excludeDestructiveByTestMapInSet(map, set); excludeDestructiveByTestMapInSet(map, set);
else else
@ -289,13 +292,13 @@ public class kelondroMSetTools {
} }
private static void excludeDestructiveByTestMapInSet(TreeMap map, TreeSet set) { private static void excludeDestructiveByTestMapInSet(TreeMap map, TreeSet set) {
Iterator mi = map.keySet().iterator(); Iterator mi = map.keySet().iterator();
while (mi.hasNext()) if (set.contains(mi.next())) mi.remove(); while (mi.hasNext()) if (set.contains(mi.next())) mi.remove();
} }
private static void excludeDestructiveByTestSetInMap(TreeMap map, TreeSet set) { private static void excludeDestructiveByTestSetInMap(TreeMap map, TreeSet set) {
Iterator si = set.iterator(); Iterator si = set.iterator();
while (si.hasNext()) map.remove(si.next()); while (si.hasNext()) map.remove(si.next());
} }
// and the same again with set-set // and the same again with set-set
@ -303,7 +306,7 @@ public class kelondroMSetTools {
// comparators must be equal // comparators must be equal
if (set1 == null) return; if (set1 == null) return;
if (set2 == 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() == 0) || (set2.size() == 0)) return;
if (set1.size() < set2.size()) if (set1.size() < set2.size())
@ -313,13 +316,13 @@ public class kelondroMSetTools {
} }
private static void excludeDestructiveByTestSmallInLarge(TreeSet small, TreeSet large) { private static void excludeDestructiveByTestSmallInLarge(TreeSet small, TreeSet large) {
Iterator mi = small.iterator(); Iterator mi = small.iterator();
while (mi.hasNext()) if (large.contains(mi.next())) mi.remove(); while (mi.hasNext()) if (large.contains(mi.next())) mi.remove();
} }
private static void excludeDestructiveByTestLargeInSmall(TreeSet large, TreeSet small) { private static void excludeDestructiveByTestLargeInSmall(TreeSet large, TreeSet small) {
Iterator si = small.iterator(); Iterator si = small.iterator();
while (si.hasNext()) large.remove(si.next()); while (si.hasNext()) large.remove(si.next());
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

Loading…
Cancel
Save