package org.antlr.symtab;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

/** A unique set of strings mapped to a monotonically increasing index.
 *  These indexes often useful to bytecode interpreters that have instructions
 *  referring to strings by unique integer. Indexing is from 0.
 *
 *  We can also get them back out in original order.
 *
 *  Yes, I know that this is similar to {@link String#intern()} but in this
 *  case, I need the index out not just to make these strings unique.
 */
public class StringTable {
	protected LinkedHashMap<String,Integer> table = new LinkedHashMap<String,Integer>();
	protected int index = -1; // index we have just written

	public int add(String s) {
		Integer I = table.get(s);
		if ( I!=null ) return I;
		index++;
		table.put(s, index);
		return index;
	}

	/** Get the ith string or null if out of range; a very inefficient implementation */
	public String get(int i) {
		if ( i<size() && i>0 ) {
			String[] strings = toArray();
			return strings[i];
		}
		return null;
	}

	public int size() { return table.size(); }

	/** Return an array, possibly of length zero, with all strings
	 *  sitting at their appropriate index within the array.
	 */
	public String[] toArray() {
		if ( table.size()==0 ) return new String[0];
		String[] a = new String[table.size()];
		int i = 0;
		for (String s : table.keySet()) a[i++] = s;
		return a;
	}

	/** Return a List, possibly of length zero, with all strings
	 *  sitting at their appropriate index within the array.
	 */
	public List<String> toList() {
		if ( table.size()==0 ) return Collections.emptyList();
		List<String> strings = new ArrayList<>();
		int i = 0;
		for (String s : table.keySet()) {
			strings.add(i, s);
			i++;
		}
		return strings;
	}

	public int getNumberOfStrings() {
		return index + 1;
	}

	@Override
	public String toString() {
		return table.toString();
	}
}

@@ -19,16 +19,17 @@
 symtab;%0A
+%0A
 import j
@@ -52,38 +52,8 @@
 st;%0A
-import java.util.Collections;%0A
 impo
@@ -697,16 +697,69 @@
  written
+%0A%09protected List%3CString%3E strings = new ArrayList%3C%3E();
 %0A%0A%09publi
@@ -869,16 +869,41 @@
 index);%0A
+%09%09strings.set(index, s);%0A
 %09%09return
@@ -965,43 +965,8 @@
 ange
-; a very inefficient implementation
  */%0A
@@ -1024,41 +1024,8 @@
 ) %7B%0A
-%09%09%09String%5B%5D strings = toArray();%0A
 %09%09%09r
@@ -1041,11 +1041,15 @@
 ings
-%5Bi%5D
+.get(i)
 ;%0A%09%09
@@ -1276,163 +1276,58 @@
 %7B%0A%09%09
-if ( table.size()==0 ) return new String%5B0%5D;%0A%09%09String%5B%5D a = new String%5Btable.size()%5D;%0A%09%09int i = 0;%0A%09%09for (String s : table.keySet()) a%5Bi++%5D = s;%0A%09%09return a
+return strings.toArray(new String%5Bstrings.size()%5D)
 ;%0A%09%7D
@@ -1489,192 +1489,8 @@
 ) %7B%0A
-%09%09if ( table.size()==0 ) return Collections.emptyList();%0A%09%09List%3CString%3E strings = new ArrayList%3C%3E();%0A%09%09int i = 0;%0A%09%09for (String s : table.keySet()) %7B%0A%09%09%09strings.add(i, s);%0A%09%09%09i++;%0A%09%09%7D%0A
 %09%09re

simplify stringtable