-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added FixpointIterator to utils (function package)
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...nt/aksw-commons-utils/src/main/java/org/aksw/commons/util/function/FixpointIteration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.aksw.commons.util.function; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class FixpointIteration { | ||
|
||
private static Logger logger = LoggerFactory.getLogger(FixpointIteration.class); | ||
|
||
public static <T> Function<T, T> createClosure(Function<? super T, ? extends T> transform) { | ||
return op -> apply(op, transform); | ||
} | ||
|
||
public static <T> T apply(T op, Function<? super T, ? extends T> transform) { | ||
T current; | ||
do { | ||
current = op; | ||
op = transform.apply(current); | ||
} while(!current.equals(op)); | ||
|
||
return current; | ||
} | ||
|
||
public static <T> T apply(int max, T init, Function<? super T, ? extends T> fn) { | ||
T result = init; | ||
|
||
int i = 0; | ||
for(; i < max; ++i) { | ||
T tmp = fn.apply(result); | ||
if(Objects.equals(tmp, result)) { | ||
break; | ||
} | ||
result = tmp; | ||
} | ||
|
||
if(i >= max) { | ||
logger.warn("Fixpoint iteration reached iteration threshold"); | ||
} | ||
|
||
return result; | ||
} | ||
} |