// This object locates a floating-point key in a sorted map. // Then it calls an action function on keys from the map, // starting with the key closest to the requested one, and // with decreasing probability. // Probability 1 is used only iff there is an exact match. // Probabilities fall with increasing distance between // searched and found key. srecord noeq ProbabilisticDistanceBasedLookup( IProbabilisticScheduler scheduler, NavigableMap map, IVF1 action, double key) { swappable double distanceToProbability(double distance) { ret genericDistanceToProbability(distance); } run { Double closest = closestDoubleKey(map, key); if (closest == null) ret; double p = distanceToProbability(abs(closest-key)); scheduler.at(p, () -> action.get(closest)); scheduler.at(p, () -> walkLeft(closest)); scheduler.at(p, () -> walkRight(closest)); } void walkLeft(double x) { Double y = map.lowerKey(x); if (y == null) ret; double p = distanceToProbability(abs(y-key)); scheduler.at(p, () -> action.get(y)); scheduler.at(p, () -> walkLeft(y)); } void walkRight(double x) { Double y = map.higherKey(x); if (y == null) ret; double p = distanceToProbability(abs(y-key)); scheduler.at(p, () -> action.get(y)); scheduler.at(p, () -> walkRight(y)); } }