From 586cd90e324bd4cc4652f6b2ca17b05f58abd11a Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 27 Jul 2017 10:47:11 -0700 Subject: [PATCH] Switch Lexer to use a map for patterns, to allow for removal. --- src/org/nwapw/abacus/lexing/Lexer.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/org/nwapw/abacus/lexing/Lexer.java b/src/org/nwapw/abacus/lexing/Lexer.java index 1a3cab8..7c84f16 100644 --- a/src/org/nwapw/abacus/lexing/Lexer.java +++ b/src/org/nwapw/abacus/lexing/Lexer.java @@ -7,6 +7,7 @@ import org.nwapw.abacus.lexing.pattern.PatternNode; import java.util.ArrayList; import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; /** @@ -19,13 +20,13 @@ public class Lexer { /** * The registered patterns. */ - private ArrayList> patterns; + private HashMap> patterns; /** * Creates a new lexer with no registered patterns. */ public Lexer(){ - patterns = new ArrayList<>(); + patterns = new HashMap<>(); } /** @@ -35,7 +36,7 @@ public class Lexer { */ public void register(String pattern, T id){ Pattern compiledPattern = new Pattern<>(pattern, id); - if(compiledPattern.getHead() != null) patterns.add(compiledPattern); + if(compiledPattern.getHead() != null) patterns.put(pattern, compiledPattern); } /** @@ -50,7 +51,7 @@ public class Lexer { HashSet> currentSet = new HashSet<>(); HashSet> futureSet = new HashSet<>(); int index = startAt; - for(Pattern pattern : patterns){ + for(Pattern pattern : patterns.values()){ pattern.getHead().addInto(currentSet); } while(!currentSet.isEmpty()){