From 808d23e6d181bbc98fb7153588c8f3a7e62ab5bc Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Sat, 11 Feb 2012 17:42:02 -0800 Subject: [PATCH] add some useful classes --- tool/src/org/antlr/v4/misc/FrequencySet.java | 22 ++++++++++++++++++++ tool/src/org/antlr/v4/misc/MutableInt.java | 17 +++++++++++++++ tool/src/org/antlr/v4/misc/Pair.java | 11 ++++++++++ 3 files changed, 50 insertions(+) create mode 100644 tool/src/org/antlr/v4/misc/FrequencySet.java create mode 100644 tool/src/org/antlr/v4/misc/MutableInt.java create mode 100644 tool/src/org/antlr/v4/misc/Pair.java diff --git a/tool/src/org/antlr/v4/misc/FrequencySet.java b/tool/src/org/antlr/v4/misc/FrequencySet.java new file mode 100644 index 000000000..2923c7ae4 --- /dev/null +++ b/tool/src/org/antlr/v4/misc/FrequencySet.java @@ -0,0 +1,22 @@ +package org.antlr.v4.misc; + +import java.util.Hashtable; + +/** Count how many of each key we have; not thread safe */ +public class FrequencySet extends Hashtable { + public int count(T key) { + MutableInt value = get(key); + if (value == null) return 0; + return value.v; + } + public void add(T key) { + MutableInt value = get(key); + if (value == null) { + value = new MutableInt(1); + put(key, value); + } + else { + value.v++; + } + } +} diff --git a/tool/src/org/antlr/v4/misc/MutableInt.java b/tool/src/org/antlr/v4/misc/MutableInt.java new file mode 100644 index 000000000..9f6fb9409 --- /dev/null +++ b/tool/src/org/antlr/v4/misc/MutableInt.java @@ -0,0 +1,17 @@ +package org.antlr.v4.misc; + +public class MutableInt extends Number implements Comparable { + public int v; + + public MutableInt(int v) { this.v = v; } + @Override public int compareTo(Number o) { return v; } + @Override public int intValue() { return v; } + @Override public long longValue() { return v; } + @Override public float floatValue() { return v; } + @Override public double doubleValue() { return v; } + + @Override + public String toString() { + return String.valueOf(v); + } +} diff --git a/tool/src/org/antlr/v4/misc/Pair.java b/tool/src/org/antlr/v4/misc/Pair.java new file mode 100644 index 000000000..542c5b40e --- /dev/null +++ b/tool/src/org/antlr/v4/misc/Pair.java @@ -0,0 +1,11 @@ +package org.antlr.v4.misc; + +public class Pair { + public A a; + public B b; + + public Pair(A a, B b) { + this.a = a; + this.b = b; + } +}