Added/Commited antlr.collections.impl
authorCésar Natarén <cesar@mono-cvs.ximian.com>
Tue, 22 Apr 2003 05:01:35 +0000 (05:01 -0000)
committerCésar Natarén <cesar@mono-cvs.ximian.com>
Tue, 22 Apr 2003 05:01:35 +0000 (05:01 -0000)
svn path=/trunk/mcs/; revision=13877

mcs/class/Microsoft.JScript/Antlr.Runtime/antlr.collections.impl/ASTArray.cs [new file with mode: 0755]
mcs/class/Microsoft.JScript/Antlr.Runtime/antlr.collections.impl/BitSet.cs [new file with mode: 0755]

diff --git a/mcs/class/Microsoft.JScript/Antlr.Runtime/antlr.collections.impl/ASTArray.cs b/mcs/class/Microsoft.JScript/Antlr.Runtime/antlr.collections.impl/ASTArray.cs
new file mode 100755 (executable)
index 0000000..c511c50
--- /dev/null
@@ -0,0 +1,42 @@
+using System;\r
+using AST = antlr.collections.AST;\r
+       \r
+namespace antlr.collections.impl\r
+{\r
+       /*ANTLR Translator Generator\r
+       * Project led by Terence Parr at http://www.jGuru.com\r
+       * Software rights: http://www.antlr.org/RIGHTS.html\r
+       *\r
+       * $Id: ASTArray.cs,v 1.1 2003/04/22 05:01:35 cesar Exp $\r
+       */\r
+\r
+       //\r
+       // ANTLR C# Code Generator by Micheal Jordan\r
+       //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com\r
+       //                            Anthony Oguntimehin\r
+       //\r
+       // With many thanks to Eric V. Smith from the ANTLR list.\r
+       //\r
+\r
+       /*ASTArray is a class that allows ANTLR to\r
+       * generate code that can create and initialize an array\r
+       * in one expression, like:\r
+       *    (new ASTArray(3)).add(x).add(y).add(z)\r
+       */\r
+       public class ASTArray\r
+       {\r
+               public int size = 0;\r
+               public AST[] array;\r
+               \r
+               \r
+               public ASTArray(int capacity)\r
+               {\r
+                       array = new AST[capacity];\r
+               }\r
+               public virtual ASTArray add(AST node)\r
+               {\r
+                       array[size++] = node;\r
+                       return this;\r
+               }\r
+       }\r
+}
\ No newline at end of file
diff --git a/mcs/class/Microsoft.JScript/Antlr.Runtime/antlr.collections.impl/BitSet.cs b/mcs/class/Microsoft.JScript/Antlr.Runtime/antlr.collections.impl/BitSet.cs
new file mode 100755 (executable)
index 0000000..bb19b15
--- /dev/null
@@ -0,0 +1,539 @@
+using System;\r
+using ArrayList                                        = System.Collections.ArrayList;\r
+\r
+//using CharFormatter                          = antlr.CharFormatter;\r
+\r
+namespace antlr.collections.impl\r
+{\r
+       /*ANTLR Translator Generator\r
+       * Project led by Terence Parr at http://www.jGuru.com\r
+       * Software rights: http://www.antlr.org/RIGHTS.html\r
+       *\r
+       * $Id: BitSet.cs,v 1.1 2003/04/22 05:01:35 cesar Exp $\r
+       */\r
+\r
+       //
+       // ANTLR C# Code Generator by Micheal Jordan
+       //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
+       //                            Anthony Oguntimehin
+       //
+       // With many thanks to Eric V. Smith from the ANTLR list.
+       //
+       \r
+       /*A BitSet to replace java.util.BitSet.\r
+       * Primary differences are that most set operators return new sets\r
+       * as opposed to oring and anding "in place".  Further, a number of\r
+       * operations were added.  I cannot contain a BitSet because there\r
+       * is no way to access the internal bits (which I need for speed)\r
+       * and, because it is final, I cannot subclass to add functionality.\r
+       * Consider defining set degree.  Without access to the bits, I must\r
+       * call a method n times to test the ith bit...ack!\r
+       *\r
+       * Also seems like or() from util is wrong when size of incoming set is bigger\r
+       * than this.bits.length.\r
+       *\r
+       * @author Terence Parr\r
+       * @author <br><a href="mailto:pete@yamuna.demon.co.uk">Pete Wells</a>\r
+       */\r
+\r
+       public class BitSet : ICloneable\r
+       {\r
+               protected internal const int BITS = 64; // number of bits / long\r
+               protected internal const int NIBBLE = 4;\r
+               protected internal const int LOG_BITS = 6; // 2^6 == 64\r
+               \r
+               /*We will often need to do a mod operator (i mod nbits).  Its\r
+               * turns out that, for powers of two, this mod operation is\r
+               * same as (i & (nbits-1)).  Since mod is slow, we use a\r
+               * precomputed mod mask to do the mod instead.\r
+               */\r
+               protected internal static readonly int MOD_MASK = BITS - 1;\r
+               \r
+               /*The actual data bits */\r
+               protected internal long[] dataBits;\r
+               \r
+               /*Construct a bitset of size one word (64 bits) */\r
+               public BitSet() : this(BITS)\r
+               {\r
+               }\r
+               \r
+               /*Construction from a static array of longs */\r
+               public BitSet(long[] bits_)\r
+               {\r
+                       dataBits = bits_;\r
+               }\r
+               \r
+               /*Construct a bitset given the size\r
+               * @param nbits The size of the bitset in bits\r
+               */\r
+               public BitSet(int nbits)\r
+               {\r
+                       dataBits = new long[((nbits - 1) >> LOG_BITS) + 1];\r
+               }\r
+               \r
+               /*or this element into this set (grow as necessary to accommodate) */\r
+               public virtual void  add(int el)\r
+               {\r
+                       int n = wordNumber(el);\r
+                       if (n >= dataBits.Length)\r
+                       {\r
+                               growToInclude(el);\r
+                       }\r
+                       dataBits[n] |= bitMask(el);\r
+               }\r
+               \r
+               public virtual BitSet and(BitSet a)\r
+               {\r
+                       BitSet s = (BitSet) this.Clone();\r
+                       s.andInPlace(a);\r
+                       return s;\r
+               }\r
+               \r
+               public virtual void  andInPlace(BitSet a)\r
+               {\r
+                       int min = (int) (Math.Min(dataBits.Length, a.dataBits.Length));\r
+                        for (int i = min - 1; i >= 0; i--)\r
+                       {\r
+                               dataBits[i] &= a.dataBits[i];\r
+                       }\r
+                       // clear all bits in this not present in a (if this bigger than a).\r
+                        for (int i = min; i < dataBits.Length; i++)\r
+                       {\r
+                               dataBits[i] = 0;\r
+                       }\r
+               }\r
+               \r
+               private static long bitMask(int bitNumber)\r
+               {\r
+                       int bitPosition = bitNumber & MOD_MASK; // bitNumber mod BITS\r
+                       return 1L << bitPosition;\r
+               }\r
+               \r
+               public virtual void  clear()\r
+               {\r
+                        for (int i = dataBits.Length - 1; i >= 0; i--)\r
+                       {\r
+                               dataBits[i] = 0;\r
+                       }\r
+               }\r
+               \r
+               public virtual void  clear(int el)\r
+               {\r
+                       int n = wordNumber(el);\r
+                       if (n >= dataBits.Length)\r
+                       {\r
+                               // grow as necessary to accommodate\r
+                               growToInclude(el);\r
+                       }\r
+                       dataBits[n] &= ~ bitMask(el);\r
+               }\r
+               \r
+               public virtual object Clone()\r
+               {\r
+                       BitSet s;\r
+                       try\r
+                       {\r
+                               s = new BitSet();\r
+                               s.dataBits = new long[dataBits.Length];\r
+                               Array.Copy(dataBits, 0, s.dataBits, 0, dataBits.Length);\r
+                       }\r
+                       catch //(System.Exception e)\r
+                       {\r
+                               throw new System.ApplicationException();\r
+                       }\r
+                       return s;\r
+               }\r
+               \r
+               public virtual int degree()\r
+               {\r
+                       int deg = 0;\r
+                        for (int i = dataBits.Length - 1; i >= 0; i--)\r
+                       {\r
+                               long word = dataBits[i];\r
+                               if (word != 0L)\r
+                               {\r
+                                        for (int bit = BITS - 1; bit >= 0; bit--)\r
+                                       {\r
+                                               if ((word & (1L << bit)) != 0)\r
+                                               {\r
+                                                       deg++;\r
+                                               }\r
+                                       }\r
+                               }\r
+                       }\r
+                       return deg;\r
+               }\r
+               \r
+               override public int GetHashCode()\r
+               {\r
+                       return dataBits.GetHashCode();\r
+               }\r
+\r
+               /*code "inherited" from java.util.BitSet */\r
+               override public bool Equals(object obj)\r
+               {\r
+                       if ((obj != null) && (obj is BitSet))\r
+                       {\r
+                               BitSet bset = (BitSet) obj;\r
+                               \r
+                               int n = (int) (System.Math.Min(dataBits.Length, bset.dataBits.Length));\r
+                                for (int i = n; i-- > 0; )\r
+                               {\r
+                                       if (dataBits[i] != bset.dataBits[i])\r
+                                       {\r
+                                               return false;\r
+                                       }\r
+                               }\r
+                               if (dataBits.Length > n)\r
+                               {\r
+                                        for (int i = (int) (dataBits.Length); i-- > n; )\r
+                                       {\r
+                                               if (dataBits[i] != 0)\r
+                                               {\r
+                                                       return false;\r
+                                               }\r
+                                       }\r
+                               }\r
+                               else if (bset.dataBits.Length > n)\r
+                               {\r
+                                        for (int i = (int) (bset.dataBits.Length); i-- > n; )\r
+                                       {\r
+                                               if (bset.dataBits[i] != 0)\r
+                                               {\r
+                                                       return false;\r
+                                               }\r
+                                       }\r
+                               }\r
+                               return true;\r
+                       }\r
+                       return false;\r
+               }\r
+               \r
+               /*\r
+               * Grows the set to a larger number of bits.\r
+               * @param bit element that must fit in set\r
+               */\r
+               public virtual void  growToInclude(int bit)\r
+               {\r
+                       int newSize = (int) (System.Math.Max(dataBits.Length << 1, numWordsToHold(bit)));\r
+                       long[] newbits = new long[newSize];\r
+                       Array.Copy(dataBits, 0, newbits, 0, dataBits.Length);\r
+                       dataBits = newbits;\r
+               }\r
+               \r
+               public virtual bool member(int el)\r
+               {\r
+                       int n = wordNumber(el);\r
+                       if (n >= dataBits.Length)\r
+                               return false;\r
+                       return (dataBits[n] & bitMask(el)) != 0;\r
+               }\r
+               \r
+               public virtual bool nil()\r
+               {\r
+                        for (int i = dataBits.Length - 1; i >= 0; i--)\r
+                       {\r
+                               if (dataBits[i] != 0)\r
+                                       return false;\r
+                       }\r
+                       return true;\r
+               }\r
+               \r
+               public virtual BitSet not()\r
+               {\r
+                       BitSet s = (BitSet) this.Clone();\r
+                       s.notInPlace();\r
+                       return s;\r
+               }\r
+               \r
+               public virtual void  notInPlace()\r
+               {\r
+                        for (int i = dataBits.Length - 1; i >= 0; i--)\r
+                       {\r
+                               dataBits[i] = ~ dataBits[i];\r
+                       }\r
+               }\r
+               \r
+               /*complement bits in the range 0..maxBit. */\r
+               public virtual void  notInPlace(int maxBit)\r
+               {\r
+                       notInPlace(0, maxBit);\r
+               }\r
+               \r
+               /*complement bits in the range minBit..maxBit.*/\r
+               public virtual void  notInPlace(int minBit, int maxBit)\r
+               {\r
+                       // make sure that we have room for maxBit\r
+                       growToInclude(maxBit);\r
+                        for (int i = minBit; i <= maxBit; i++)\r
+                       {\r
+                               int n = wordNumber(i);\r
+                               dataBits[n] ^= bitMask(i);\r
+                       }\r
+               }\r
+               \r
+               private int numWordsToHold(int el)\r
+               {\r
+                       return (el >> LOG_BITS) + 1;\r
+               }\r
+               \r
+               public static BitSet of(int el)\r
+               {\r
+                       BitSet s = new BitSet(el + 1);\r
+                       s.add(el);\r
+                       return s;\r
+               }\r
+               \r
+               /*return this | a in a new set */\r
+               public virtual BitSet or(BitSet a)\r
+               {\r
+                       BitSet s = (BitSet) this.Clone();\r
+                       s.orInPlace(a);\r
+                       return s;\r
+               }\r
+               \r
+               public virtual void  orInPlace(BitSet a)\r
+               {\r
+                       // If this is smaller than a, grow this first\r
+                       if (a.dataBits.Length > dataBits.Length)\r
+                       {\r
+                               setSize((int) (a.dataBits.Length));\r
+                       }\r
+                       int min = (int) (System.Math.Min(dataBits.Length, a.dataBits.Length));\r
+                        for (int i = min - 1; i >= 0; i--)\r
+                       {\r
+                               dataBits[i] |= a.dataBits[i];\r
+                       }\r
+               }\r
+               \r
+               // remove this element from this set\r
+               public virtual void  remove(int el)\r
+               {\r
+                       int n = wordNumber(el);\r
+                       if (n >= dataBits.Length)\r
+                       {\r
+                               growToInclude(el);\r
+                       }\r
+                       dataBits[n] &= ~ bitMask(el);\r
+               }\r
+               \r
+               /*\r
+               * Sets the size of a set.\r
+               * @param nwords how many words the new set should be\r
+               */\r
+               private void  setSize(int nwords)\r
+               {\r
+                       long[] newbits = new long[nwords];\r
+                       int n = (int) (System.Math.Min(nwords, dataBits.Length));\r
+                       Array.Copy(dataBits, 0, newbits, 0, n);\r
+                       dataBits = newbits;\r
+               }\r
+               \r
+               public virtual int size()\r
+               {\r
+                       return dataBits.Length << LOG_BITS; // num words * bits per word\r
+               }\r
+               \r
+               /*return how much space is being used by the dataBits array not\r
+               *  how many actually have member bits on.\r
+               */\r
+               public virtual int lengthInLongWords()\r
+               {\r
+                       return dataBits.Length;\r
+               }\r
+               \r
+               /*Is this contained within a? */\r
+               public virtual bool subset(BitSet a)\r
+               {\r
+                       if (a == null) //(a == null || !(a is BitSet))\r
+                               return false;\r
+                       return this.and(a).Equals(this);\r
+               }\r
+               \r
+               /*Subtract the elements of 'a' from 'this' in-place.\r
+               * Basically, just turn off all bits of 'this' that are in 'a'.\r
+               */\r
+               public virtual void  subtractInPlace(BitSet a)\r
+               {\r
+                       if (a == null)\r
+                               return ;\r
+                       // for all words of 'a', turn off corresponding bits of 'this'\r
+                        for (int i = 0; i < dataBits.Length && i < a.dataBits.Length; i++)\r
+                       {\r
+                               dataBits[i] &= ~ a.dataBits[i];\r
+                       }\r
+               }\r
+               \r
+               public virtual int[] toArray()\r
+               {\r
+                       int[] elems = new int[degree()];\r
+                       int en = 0;\r
+                        for (int i = 0; i < (dataBits.Length << LOG_BITS); i++)\r
+                       {\r
+                               if (member(i))\r
+                               {\r
+                                       elems[en++] = i;\r
+                               }\r
+                       }\r
+                       return elems;\r
+               }\r
+               \r
+               public virtual long[] toPackedArray()\r
+               {\r
+                       return dataBits;\r
+               }\r
+               \r
+               override public string ToString()\r
+               {\r
+                       return ToString(",");\r
+               }\r
+               \r
+               /*Transform a bit set into a string by formatting each element as an integer\r
+               * @separator The string to put in between elements\r
+               * @return A commma-separated list of values\r
+               */\r
+               public virtual string ToString(string separator)\r
+               {\r
+                       string str = "";\r
+                        for (int i = 0; i < (dataBits.Length << LOG_BITS); i++)\r
+                       {\r
+                               if (member(i))\r
+                               {\r
+                                       if (str.Length > 0)\r
+                                       {\r
+                                               str += separator;\r
+                                       }\r
+                                       str = str + i;\r
+                               }\r
+                       }\r
+                       return str;\r
+               }\r
+               \r
+               /*Create a string representation where instead of integer elements, the\r
+               * ith element of vocabulary is displayed instead.  Vocabulary is a Vector\r
+               * of Strings.\r
+               * @separator The string to put in between elements\r
+               * @return A commma-separated list of character constants.\r
+               */\r
+               public virtual string ToString(string separator, ArrayList vocabulary)\r
+               {\r
+                       if (vocabulary == null)\r
+                       {\r
+                               return ToString(separator);\r
+                       }\r
+                       string str = "";\r
+                        for (int i = 0; i < (dataBits.Length << LOG_BITS); i++)\r
+                       {\r
+                               if (member(i))\r
+                               {\r
+                                       if (str.Length > 0)\r
+                                       {\r
+                                               str += separator;\r
+                                       }\r
+                                       if (i >= vocabulary.Count)\r
+                                       {\r
+                                               str += "<bad element " + i + ">";\r
+                                       }\r
+                                       else if (vocabulary[i] == null)\r
+                                       {\r
+                                               str += "<" + i + ">";\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               str += (string) vocabulary[i];\r
+                                       }\r
+                               }\r
+                       }\r
+                       return str;\r
+               }\r
+               \r
+               /*\r
+               * Dump a comma-separated list of the words making up the bit set.\r
+               * Split each 64 bit number into two more manageable 32 bit numbers.\r
+               * This generates a comma-separated list of C++-like unsigned long constants.\r
+               */\r
+               public virtual string toStringOfHalfWords()\r
+               {\r
+                       string s = new string("".ToCharArray());\r
+                        for (int i = 0; i < dataBits.Length; i++)\r
+                       {\r
+                               if (i != 0)\r
+                                       s += ", ";\r
+                               long tmp = dataBits[i];\r
+                               tmp &= 0xFFFFFFFFL;\r
+                               s += (tmp + "UL");\r
+                               s += ", ";\r
+                               tmp = SupportClass.URShift(dataBits[i], 32);\r
+                               tmp &= 0xFFFFFFFFL;\r
+                               s += (tmp + "UL");\r
+                       }\r
+                       return s;\r
+               }\r
+               \r
+               /*\r
+               * Dump a comma-separated list of the words making up the bit set.\r
+               * This generates a comma-separated list of Java-like long int constants.\r
+               */\r
+               public virtual string toStringOfWords()\r
+               {\r
+                       string s = new string("".ToCharArray());\r
+                        for (int i = 0; i < dataBits.Length; i++)\r
+                       {\r
+                               if (i != 0)\r
+                                       s += ", ";\r
+                               s += (dataBits[i] + "L");\r
+                       }\r
+                       return s;\r
+               }\r
+               \r
+               /*Print out the bit set but collapse char ranges. */\r
+/*             public virtual string toStringWithRanges(string separator, CharFormatter formatter)\r
+               {\r
+                       string str = "";\r
+                       int[] elems = this.toArray();\r
+                       if (elems.Length == 0)\r
+                       {\r
+                               return "";\r
+                       }\r
+                       // look for ranges\r
+                       int i = 0;\r
+                       while (i < elems.Length)\r
+                       {\r
+                               int lastInRange;\r
+                               lastInRange = 0;\r
+                                for (int j = i + 1; j < elems.Length; j++)\r
+                               {\r
+                                       if (elems[j] != elems[j - 1] + 1)\r
+                                       {\r
+                                               break;\r
+                                       }\r
+                                       lastInRange = j;\r
+                               }\r
+                               // found a range\r
+                               if (str.Length > 0)\r
+                               {\r
+                                       str += separator;\r
+                               }\r
+                               if (lastInRange - i >= 2)\r
+                               {\r
+                                       str += formatter.literalChar(elems[i]);\r
+                                       str += "..";\r
+                                       str += formatter.literalChar(elems[lastInRange]);\r
+                                       i = lastInRange; // skip past end of range for next range\r
+                               }\r
+                               else\r
+                               {\r
+                                       // no range, just print current char and move on\r
+                                       str += formatter.literalChar(elems[i]);\r
+                               }\r
+                               i++;\r
+                       }\r
+                       return str;\r
+               }\r
+*/             \r
+               private static int wordNumber(int bit)\r
+               {\r
+                       return bit >> LOG_BITS; // bit / BITS\r
+               }\r
+       }\r
+}
\ No newline at end of file