implemented rewritter
This commit is contained in:
parent
2216561506
commit
664d3ed06d
31
src/lsystem/engine/Rewrite.java
Normal file
31
src/lsystem/engine/Rewrite.java
Normal file
@ -0,0 +1,31 @@
|
||||
package lsystem.engine;
|
||||
|
||||
import lsystem.utils.Pair;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class Rewrite {
|
||||
|
||||
private final String axiom;
|
||||
private final List<Pair<String, String >> rules;
|
||||
private final int recurrences;
|
||||
|
||||
public Rewrite(String axiom, List<Pair<String, String>> rules, int recurrences) {
|
||||
this.axiom = axiom;
|
||||
this.rules = rules;
|
||||
this.recurrences = recurrences;
|
||||
}
|
||||
|
||||
public String rewrite() {
|
||||
String rewritted = axiom;
|
||||
for(int i = 0; i < recurrences; ++i) {
|
||||
for(int j = 0; j < rules.size(); ++j){
|
||||
Pair<String, String> pair = rules.get(j);
|
||||
rewritted = rewritted.replace(pair.getLeft(), pair.getRight());
|
||||
}
|
||||
}
|
||||
return rewritted;
|
||||
}
|
||||
|
||||
}
|
40
src/lsystem/utils/Pair.java
Normal file
40
src/lsystem/utils/Pair.java
Normal file
@ -0,0 +1,40 @@
|
||||
package lsystem.utils;
|
||||
|
||||
public class Pair<U, K> {
|
||||
|
||||
private final U left;
|
||||
private final K right;
|
||||
|
||||
public Pair(U left, K right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public U getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public K getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final Pair<?, ?> other = (Pair<?, ?>) obj;
|
||||
return this.left.equals(other.getLeft()) && this.left.equals(other.getRight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 31 + left.hashCode() * right.hashCode();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user