示例#1
0
 public void ShouldClearTheSet()
 {
     var stringSet = new StringSet();
     stringSet.Add("Test");
     stringSet.Add("Test");
     stringSet.Add("Test");
     stringSet.Clear();
     Assert.AreEqual(0, stringSet.Count);
     
 }
 public void TestStringSet()
 {
     using (StringSet set = new StringSet("test", true))
     {
         set.Clear();
         List <string> strings = new List <string>();
         Random        r       = new Random();
         for (int i = 0; i < 1000; i++)
         {
             strings.Add(RandomString(64));
         }
         strings.Sort();
         Assert.AreEqual(strings.Count, set.AddMany(strings));
         Assert.AreEqual(0, set.AddMany(strings));
         string[] existing = set.Enumerate().ToArray();
         Assert.AreEqual(strings, existing);
         Assert.IsFalse(set.Contains("test"));
         Assert.IsFalse(set.Contains("test2"));
         Assert.IsTrue(set.Add("test"));
         Assert.IsTrue(set.Add("test2"));
         Assert.AreEqual(strings.Count + 2, set.GetCount());
         Assert.IsFalse(set.Add("test"));
         Assert.IsFalse(set.Add("test2"));
         Assert.IsTrue(set.Contains("test"));
         Assert.IsTrue(set.Contains("test2"));
         Assert.IsFalse(set.Contains("nothing"));
         Assert.AreEqual(2, set.DeleteMany(new string[] { "test", "test2" }));
         Assert.AreEqual(strings.Count, set.GetCount());
         Assert.IsFalse(set.Contains("test"));
         Assert.IsFalse(set.Contains("test2"));
         Assert.IsFalse(set.Contains("test3"));
         Assert.IsTrue(set.Add("test3"));
         Assert.AreEqual(strings.Count + 1, set.GetCount());
         Assert.IsTrue(set.Delete("test3"));
         Assert.IsFalse(set.Delete("test3"));
         Assert.AreEqual(strings.Count, set.GetCount());
         Assert.AreEqual(strings.Count, set.Clear());
         Assert.AreEqual(0, set.GetCount());
         Assert.AreEqual(new string[0], set.Enumerate());
     }
 }
示例#3
0
 public void SetSchemasSubSet(IEnumerable <string> schemas)
 {
     _schemasSubSet.Clear();
     _schemasListForFilter = null;
     if (schemas == null || !SupportsSchemas())
     {
         return;
     }
     _schemasSubSet.UnionWith(schemas.Where(s => !Driver.IsSystemSchema(s)));
     if (_schemasSubSet.Count > 0)
     {
         _schemasListForFilter = "('" + string.Join("', '", _schemasSubSet) + "')";
     }
 }
        }                     //method

        #endregion

        #region Calculating Tail Firsts
        private void CalculateTailFirsts()
        {
            foreach (NonTerminal nt in _grammar.NonTerminals)
            {
                foreach (Production prod in nt.Productions)
                {
                    StringSet accumulatedFirsts = new StringSet();
                    bool      allNullable       = true;
                    //We are going backwards in LR0Items list
                    for (int i = prod.LR0Items.Count - 1; i >= 0; i--)
                    {
                        LR0Item item = prod.LR0Items[i];
                        if (i >= prod.LR0Items.Count - 2)
                        {
                            //Last and before last items have empty tails
                            item.TailIsNullable = true;
                            item.TailFirsts.Clear();
                            continue;
                        }
                        BnfTerm nextTerm = prod.RValues[i + 1]; //Element after-after-dot; remember we're going in reverse direction
                        //if (ntElem == null) continue; //it is not NonTerminal
                        NonTerminal nextNt      = nextTerm as NonTerminal;
                        bool        notNullable = nextTerm is Terminal || nextNt != null && !nextNt.Nullable;
                        if (notNullable) //next term is not nullable  (a terminal or non-nullable NonTerminal)
                        //term is not nullable, so we clear all old firsts and add this term
                        {
                            accumulatedFirsts.Clear();
                            allNullable         = false;
                            item.TailIsNullable = false;
                            if (nextTerm is Terminal)
                            {
                                item.TailFirsts.Add(nextTerm.Key);//term is terminal so add its key
                                accumulatedFirsts.Add(nextTerm.Key);
                            }
                            else if (nextNt != null)                     //it is NonTerminal
                            {
                                item.TailFirsts.AddRange(nextNt.Firsts); //nonterminal
                                accumulatedFirsts.AddRange(nextNt.Firsts);
                            }
                            continue;
                        }
                        //if we are here, then ntElem is a nullable NonTerminal. We add
                        accumulatedFirsts.AddRange(nextNt.Firsts);
                        item.TailFirsts.AddRange(accumulatedFirsts);
                        item.TailIsNullable = allNullable;
                    } //for i
                }     //foreach prod
            }         //foreach nt
        }             //method
        }                     //method

        #endregion

        #region Calculating Tail Firsts
        private void CalculateTailFirsts()
        {
            foreach (Production prod in Data.Productions)
            {
                StringSet accumulatedFirsts = new StringSet();
                bool      allNullable       = true;
                //We are going backwards in LR0Items list
                for (int i = prod.LR0Items.Count - 1; i >= 0; i--)
                {
                    LR0Item item = prod.LR0Items[i];
                    if (i >= prod.LR0Items.Count - 2)
                    {
                        //Last and before last items have empty tails
                        item.TailIsNullable = true;
                        item.TailFirsts.Clear();
                        continue;
                    }
                    BnfTerm     term   = prod.RValues[item.Position + 1]; //Element after-after-dot
                    NonTerminal ntElem = term as NonTerminal;
                    if (ntElem == null || !ntElem.Nullable)               //term is a terminal or non-nullable NonTerminal
                    //term is not nullable, so we clear all old firsts and add this term
                    {
                        accumulatedFirsts.Clear();
                        allNullable         = false;
                        item.TailIsNullable = false;
                        if (ntElem == null)
                        {
                            item.TailFirsts.Add(term.Key);//term is terminal so add its key
                            accumulatedFirsts.Add(term.Key);
                        }
                        else
                        {
                            item.TailFirsts.AddRange(ntElem.Firsts); //nonterminal
                            accumulatedFirsts.AddRange(ntElem.Firsts);
                        }
                        continue;
                    }
                    //if we are here, then ntElem is a nullable NonTerminal. We add
                    accumulatedFirsts.AddRange(ntElem.Firsts);
                    item.TailFirsts.AddRange(accumulatedFirsts);
                    item.TailIsNullable = allNullable;
                } //for i
            }     //foreach prod
        }         //method
示例#6
0
    static void Main()
    {
        StringSet ss = new StringSet();

        // Check the interface methods first.
        ISet <string> s = ss;

        checkThat(s.Count == 0, "is initially empty");
        checkThat(!s.Contains("key"), "doesn't contain inexistent element");
        checkThat(!s.Remove("key"), "returns false when removing inexistent element");

        checkThat(s.Add("key"), "returns true when adding a new element");
        checkThat(!s.Add("key"), "returns false when adding an existing element");
        checkThat(s.Contains("key"), "contains the just added element");
        checkThat(s.Remove("key"), "returns true when removing an existing element");
        checkThat(s.Count == 0, "is empty again");

        checkThat(s.Add("key1"), "Add(key1) returns true");
        checkThat(s.Add("key2"), "Add(key2) returns true");
        checkThat(s.Add("key3"), "Add(key3) returns true");

        // Also check a different interface, providing a different Add() (sic!).
        ICollection <string> coll = ss;

        coll.Add("key");
        checkThat(ss.Count == 4, "contains 4 elements");

        // Now use object-specific methods, mimicking HashSet<>.
        string val;

        checkThat(ss.TryGetValue("key1", out val), "could retrieve existing item");
        checkThat(val.Equals("key1"), "value was returned correctly by TryGetValue()");
        checkThat(!ss.TryGetValue("no-such-key", out val), "couldn't retrieve inexistent item");
        checkThat(val == null, "value was reset after failed TryGetValue()");

        IList <string> list = new List <string>();

        foreach (string str in ss)
        {
            list.Add(str);
        }
        checkThat(list.Count == 4, "copy contains 4 elements");

        ss.Clear();
        checkThat(ss.Count == 0, "is empty after Clear()");

        // Check set-theoretic methods.
        checkThat(new StringSet().SetEquals(new StringSet()), "SetEquals() works for empty sets");
        checkThat(new StringSet {
            "foo"
        }.SetEquals(new StringSet {
            "foo"
        }), "SetEquals() works for non-empty sets");
        checkThat(!new StringSet {
            "foo"
        }.SetEquals(new[] { "bar" }), "SetEquals() doesn't always return true");

        ss = new StringSet {
            "foo", "bar", "baz"
        };
        ss.ExceptWith(new[] { "baz", "quux" });
        checkThat(ss.SetEquals(new[] { "foo", "bar" }), "ExceptWith works");

        ss = new StringSet {
            "foo", "bar", "baz"
        };
        ss.IntersectWith(new[] { "baz", "quux" });
        checkThat(ss.SetEquals(new[] { "baz" }), "IntersectWith works");

        checkThat(ss.IsProperSubsetOf(new[] { "bar", "baz" }), "IsProperSubsetOf works");
        checkThat(!ss.IsProperSubsetOf(new[] { "baz" }), "!IsProperSubsetOf works");
        checkThat(ss.IsSubsetOf(new[] { "bar", "baz" }), "IsSubsetOf works");
        checkThat(!ss.IsSubsetOf(new[] { "bar" }), "!IsSubsetOf works");

        ss = new StringSet {
            "foo", "bar", "baz"
        };
        checkThat(ss.IsProperSupersetOf(new[] { "bar" }), "IsProperSupersetOf works");
        checkThat(!ss.IsProperSupersetOf(new[] { "quux" }), "IsProperSupersetOf works");
        checkThat(ss.IsSupersetOf(new[] { "foo", "bar", "baz" }), "IsProperSupersetOf works");
        checkThat(!ss.IsSupersetOf(new[] { "foo", "bar", "baz", "quux" }), "IsProperSupersetOf works");

        checkThat(ss.Overlaps(new[] { "foo" }), "Overlaps works");
        checkThat(!ss.Overlaps(new[] { "moo" }), "!Overlaps works");

        ss.SymmetricExceptWith(new[] { "baz", "quux" });
        checkThat(ss.SetEquals(new[] { "foo", "bar", "quux" }), "SymmetricExceptWith works");

        ss = new StringSet {
            "foo", "bar", "baz"
        };
        ss.UnionWith(new[] { "baz", "quux" });
        checkThat(ss.SetEquals(new[] { "foo", "bar", "baz", "quux" }), "UnionWith works");

        // Check a set of another type.
        FooSet     fooSet  = new FooSet();
        ISet <Foo> fooISet = fooSet;

        checkThat(fooISet.Count == 0, "is initially empty");
        checkThat(fooISet.Add(new Foo(17)), "added successfully");
        checkThat(fooISet.Count == 1, "is not empty any more");

        // And a set of primitive type.
        IntSet intSet = new IntSet();

        checkThat(intSet.Count == 0, "is initially empty");
        checkThat(intSet.Add(17), "17 added successfully");
        checkThat(!intSet.Add(17), "17 not added again");
        checkThat(intSet.Count == 1, "not empty any more");
        checkThat(intSet.Add(289), "289 added successfully");
        checkThat(intSet.Count == 2, "even less empty now");
    }
示例#7
0
    private void CalculateTailFirsts()
    {
      foreach (Production prod in _data.Productions)
      {
        StringSet accumulatedFirsts = new StringSet();
        bool allNullable = true;

        for (int i = prod.LR0Items.Count - 1; i >= 0; i--)
        {
          LR0Item item = prod.LR0Items[i];
          if (i >= prod.LR0Items.Count - 2)
          {
            item.TailIsNullable = true;
            item.TailFirsts.Clear();
            continue;
          }
          GrammarTerm term = prod.RValues[item.Position + 1]; 
          NonTerminal ntElem = term as NonTerminal;
          if (ntElem == null || !ntElem.Nullable)
          { 
            accumulatedFirsts.Clear();
            allNullable = false;
            item.TailIsNullable = false;
            if (ntElem == null)
            {
              item.TailFirsts.Add(term.Key);
              accumulatedFirsts.Add(term.Key);
            }
            else
            {
              item.TailFirsts.AddRange(ntElem.Firsts); 
              accumulatedFirsts.AddRange(ntElem.Firsts);
            }
            continue;
          }
          accumulatedFirsts.AddRange(ntElem.Firsts);
          item.TailFirsts.AddRange(accumulatedFirsts);
          item.TailIsNullable = allNullable;
        }
      }
    }