Example #1
0
        /** Self-test. */
        public static void _Main(string[] args)
        {
            const int    ITER  = 500;
            const int    RANGE = 65536;
            SparseBitSet a     = new SparseBitSet();

            CUtility.ASSERT(!a.Get(0) && !a.Get(1));
            CUtility.ASSERT(!a.Get(123329));
            a.Set(0); CUtility.ASSERT(a.Get(0) && !a.Get(1));
            a.Set(1); CUtility.ASSERT(a.Get(0) && a.Get(1));
            a.clearAll();
            CUtility.ASSERT(!a.Get(0) && !a.Get(1));
            Random r = new Random();
            Vector v = new Vector();

            for (int n = 0; n < ITER; n++)
            {
                int rr = ((r.Next() >> 1) % RANGE) << 1;
                a.Set(rr); v.addElement(rr);
                // check that all the numbers are there.
                CUtility.ASSERT(a.Get(rr) && !a.Get(rr + 1) && !a.Get(rr - 1));
                for (int i = 0; i < v.size(); i++)
                {
                    CUtility.ASSERT(a.Get((int)v.elementAt(i)));
                }
            }
            SparseBitSet b = (SparseBitSet)a.Clone();

            CUtility.ASSERT(a.Equals(b) && b.Equals(a));
            for (int n = 0; n < ITER / 2; n++)
            {
                int rr = (r.Next() >> 1) % v.size();
                int m  = (int)v.elementAt(rr);
                b.clear(m); v.removeElementAt(rr);
                // check that numbers are removed properly.
                CUtility.ASSERT(!b.Get(m));
            }
            CUtility.ASSERT(!a.Equals(b));
            SparseBitSet c = (SparseBitSet)a.Clone();
            SparseBitSet d = (SparseBitSet)a.Clone();

            c.and(a);
            CUtility.ASSERT(c.Equals(a) && a.Equals(c));
            c.xor(a);
            CUtility.ASSERT(!c.Equals(a) && c.size() == 0);
            d.or(b);
            CUtility.ASSERT(d.Equals(a) && !b.Equals(d));
            d.and(b);
            CUtility.ASSERT(!d.Equals(a) && b.Equals(d));
            d.xor(a);
            CUtility.ASSERT(!d.Equals(a) && !b.Equals(d));
            c.or(d); c.or(b);
            CUtility.ASSERT(c.Equals(a) && a.Equals(c));
            c = (SparseBitSet)d.Clone();
            c.and(b);
            CUtility.ASSERT(c.size() == 0);
            System.Console.WriteLine("Success.");
        }
Example #2
0
        /** Map Set using character classes [CSA] */
        public void map(CSet Set, int[] mapping)
        {
            m_complement = Set.m_complement;
            m_set.clearAll();
            IEnumerator e = Set.m_set.elements();

            while (e.MoveNext())
            {
                int old_value = (int)e.Current;
                if (old_value < mapping.Length) // skip unmapped characters
                {
                    m_set.Set(mapping[old_value]);
                }
            }
        }
Example #3
0
        /** Compute minimum Set of character classes needed to disambiguate
         *  edges.  We optimistically assume that every character belongs to
         *  a single character class, and then incrementally split classes
         *  as we see edges that require discrimination between characters in
         *  the class. [CSA, 25-Jul-1999] */
        private void computeClasses(CSpec m_spec)
        {
            this.original_charset_size = m_spec.m_dtrans_ncols;
            this.ccls = new int[original_charset_size]; // initially all zero.

            int          nextcls = 1;
            SparseBitSet clsA = new SparseBitSet(), clsB = new SparseBitSet();
            Hashtable    h = new Hashtable();

            if (m_spec.m_verbose)
            {
                System.Console.Write("Working on character classes.");
            }
            IEnumerator e = m_spec.m_nfa_states.elements();

            while (e.MoveNext())
            {
                CNfa nfa = (CNfa)e.Current;
                if (nfa.m_edge == CNfa.EMPTY || nfa.m_edge == CNfa.EPSILON)
                {
                    continue; // no discriminatory information.
                }
                clsA.clearAll(); clsB.clearAll();
                for (int i = 0; i < ccls.Length; i++)
                {
                    if (nfa.m_edge == i ||                               // edge labeled with a character
                        nfa.m_edge == CNfa.CCL && nfa.m_set.contains(i)) // Set of characters
                    {
                        clsA.Set(ccls[i]);
                    }
                    else
                    {
                        clsB.Set(ccls[i]);
                    }
                }
                // now figure out which character classes we need to split.
                clsA.and(clsB); // split the classes which show up on both sides of edge
                if (m_spec.m_verbose)
                {
                    System.Console.Write(clsA.size() == 0?".":":");
                }
                if (clsA.size() == 0)
                {
                    continue;         // nothing to do.
                }
                // and split them.
                h.Clear(); // h will map old to new class name
                for (int i = 0; i < ccls.Length; i++)
                {
                    if (clsA.Get(ccls[i])) // a split class
                    {
                        if (nfa.m_edge == i ||
                            nfa.m_edge == CNfa.CCL && nfa.m_set.contains(i))
                        { // on A side
                            int split = ccls[i];
                            if (!h.ContainsKey(split))
                            {
                                h.Add(split, (nextcls++)); // make new class
                            }
                            ccls[i] = (int)h[split];
                        }
                    }
                }
            }
            if (m_spec.m_verbose)
            {
                System.Console.WriteLine();
                System.Console.WriteLine("NFA has " + nextcls + " distinct character classes.");
            }

            this.mapped_charset_size = nextcls;
        }