diff options
author | 2021-08-15 00:21:40 +0200 | |
---|---|---|
committer | 2021-08-15 00:21:40 +0200 | |
commit | 686843216aef7b93bf19c1153402b5dcdde1d7b0 (patch) | |
tree | 3f7f89ddd6907c980d7c1f6b1559114cb7c6f514 /model-data | |
parent | Added class for useful iterator functions (motivated by GUAVA) (diff) | |
download | refinery-686843216aef7b93bf19c1153402b5dcdde1d7b0.tar.gz refinery-686843216aef7b93bf19c1153402b5dcdde1d7b0.tar.zst refinery-686843216aef7b93bf19c1153402b5dcdde1d7b0.zip |
Map -> iterable transformer
Diffstat (limited to 'model-data')
-rw-r--r-- | model-data/src/main/java/org/eclipse/viatra/solver/data/map/CursorAsIterator.java | 57 | ||||
-rw-r--r-- | model-data/src/main/java/org/eclipse/viatra/solver/data/map/MapAsIterable.java | 26 |
2 files changed, 83 insertions, 0 deletions
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/CursorAsIterator.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/CursorAsIterator.java new file mode 100644 index 00000000..b29b3119 --- /dev/null +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/CursorAsIterator.java | |||
@@ -0,0 +1,57 @@ | |||
1 | package org.eclipse.viatra.solver.data.map; | ||
2 | |||
3 | import java.util.Iterator; | ||
4 | import java.util.NoSuchElementException; | ||
5 | import java.util.function.BiFunction; | ||
6 | import java.util.function.BiPredicate; | ||
7 | |||
8 | public class CursorAsIterator<K,V,D> implements Iterator<D> { | ||
9 | private final Cursor<K, V> internal; | ||
10 | private final BiFunction<K, V, D> entryTransformation; | ||
11 | private final BiPredicate<K,V> filtering; | ||
12 | |||
13 | D lastValidElement; | ||
14 | |||
15 | public CursorAsIterator(Cursor<K, V> internal, BiFunction<K, V, D> entryTransformation, BiPredicate<K,V> filtering) { | ||
16 | this.internal = internal; | ||
17 | this.entryTransformation = entryTransformation; | ||
18 | this.filtering = filtering; | ||
19 | |||
20 | moveToNext(); | ||
21 | } | ||
22 | public CursorAsIterator(Cursor<K, V> internal, BiFunction<K, V, D> entryTransformation) { | ||
23 | this.internal = internal; | ||
24 | this.entryTransformation = entryTransformation; | ||
25 | this.filtering = ((k,v)->true); | ||
26 | |||
27 | moveToNext(); | ||
28 | } | ||
29 | |||
30 | private void moveToNext() { | ||
31 | internal.move(); | ||
32 | while(!internal.isTerminated() && !filtering.test(internal.getKey(), internal.getValue())) { | ||
33 | internal.move(); | ||
34 | } | ||
35 | if(!internal.isTerminated()) { | ||
36 | lastValidElement = entryTransformation.apply(internal.getKey(), internal.getValue()); | ||
37 | } | ||
38 | } | ||
39 | |||
40 | |||
41 | @Override | ||
42 | public boolean hasNext() { | ||
43 | return !internal.isTerminated(); | ||
44 | } | ||
45 | @Override | ||
46 | public D next() { | ||
47 | if(hasNext()) { | ||
48 | D last = lastValidElement; | ||
49 | moveToNext(); | ||
50 | return last; | ||
51 | } else { | ||
52 | throw new NoSuchElementException(); | ||
53 | } | ||
54 | |||
55 | } | ||
56 | |||
57 | } | ||
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/MapAsIterable.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/MapAsIterable.java new file mode 100644 index 00000000..22b5e6c1 --- /dev/null +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/MapAsIterable.java | |||
@@ -0,0 +1,26 @@ | |||
1 | package org.eclipse.viatra.solver.data.map; | ||
2 | |||
3 | import java.util.Iterator; | ||
4 | import java.util.function.BiFunction; | ||
5 | import java.util.function.BiPredicate; | ||
6 | |||
7 | public class MapAsIterable<K,V,D> implements Iterable<D> { | ||
8 | private final VersionedMap<K, V> internal; | ||
9 | private final BiFunction<K, V, D> entryTransformation; | ||
10 | private final BiPredicate<K,V> filtering; | ||
11 | |||
12 | public MapAsIterable(VersionedMap<K, V> internal, BiFunction<K, V, D> entryTransformation, BiPredicate<K,V> filtering) { | ||
13 | this.internal = internal; | ||
14 | this.entryTransformation = entryTransformation; | ||
15 | this.filtering = filtering; | ||
16 | } | ||
17 | public MapAsIterable(VersionedMap<K, V> internal, BiFunction<K, V, D> entryTransformation) { | ||
18 | this.internal = internal; | ||
19 | this.entryTransformation = entryTransformation; | ||
20 | this.filtering = ((k,v)->true); | ||
21 | } | ||
22 | @Override | ||
23 | public Iterator<D> iterator() { | ||
24 | return new CursorAsIterator<>(internal.getAll(), entryTransformation, filtering); | ||
25 | } | ||
26 | } | ||