Exemplo n.º 1
0
        public MultiSet Union(MultiSet other)
        {
            List <string> elementsChecked = new List <string>();
            MultiSet      union           = new MultiSet();

            foreach (string el in mSet)
            {
                if (!elementsChecked.Contains(el))
                {
                    elementsChecked.Add(el);
                }
            }

            foreach (string el in other.ToList())
            {
                if (!elementsChecked.Contains(el))
                {
                    elementsChecked.Add(el);
                }
            }

            foreach (string el in elementsChecked)
            {
                uint set1EliMultiplicity = GetMultiplicity(el);
                uint set2EliMultiplicity = other.GetMultiplicity(el);
                uint maxMultiplicity     = (set1EliMultiplicity > set2EliMultiplicity) ? set1EliMultiplicity : set2EliMultiplicity;

                for (int i = 0; i < maxMultiplicity; ++i)
                {
                    union.Add(el);
                }
            }

            return(union);
        }
Exemplo n.º 2
0
        public bool IsSupersetOf(MultiSet other)
        {
            MultiSet primarySet = new MultiSet();

            foreach (string element in mSet)
            {
                primarySet.Add(element);
            }

            MultiSet difference = other.Subtract(primarySet);

            if (difference.ToList().Count == 0)
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            MultiSet set = new MultiSet();

            set.Add("cattle");
            set.Add("bee");
            set.Add("cattle");
            set.Add("bee");
            set.Add("happy");
            set.Add("zachariah");

            Debug.Assert(set.Remove("zachariah"));
            Debug.Assert(!set.Remove("fun"));

            Debug.Assert(set.GetMultiplicity("cattle") == 2);

            List <string> expectedList = new List <string> {
                "bee", "bee", "cattle", "cattle", "happy"
            };
            List <string> list = set.ToList();

            Debug.Assert(list.Count == 5);

            for (int i = 0; i < expectedList.Count; i++)
            {
                Debug.Assert(expectedList[i] == list[i]);
            }

            MultiSet set2 = new MultiSet();

            set2.Add("cattle");
            set2.Add("cattle");
            set2.Add("bee");

            list = set.Union(set2).ToList();
            Debug.Assert(list.Count == 5);

            for (int i = 0; i < expectedList.Count; i++)
            {
                Debug.Assert(expectedList[i] == list[i]);
            }

            expectedList = new List <string> {
                "bee", "cattle", "cattle"
            };
            list = set.Intersect(set2).ToList();
            Debug.Assert(list.Count == 3);

            for (int i = 0; i < expectedList.Count; i++)
            {
                Debug.Assert(expectedList[i] == list[i]);
            }

            expectedList = new List <string> {
                "bee", "happy"
            };
            list = set.Subtract(set2).ToList();
            Debug.Assert(list.Count == 2);

            for (int i = 0; i < expectedList.Count; i++)
            {
                Debug.Assert(expectedList[i] == list[i]);
            }

            List <MultiSet> expectedPowerset = getExpectedPowerset();
            List <MultiSet> set2PowerSet     = set2.FindPowerSet();

            Debug.Assert(set2PowerSet.Count == expectedPowerset.Count);

            for (int i = 0; i < expectedPowerset.Count; i++)
            {
                expectedList = expectedPowerset[i].ToList();
                list         = set2PowerSet[i].ToList();

                Debug.Assert(expectedList.Count == list.Count);

                for (int j = 0; j < expectedList.Count; j++)
                {
                    Debug.Assert(expectedList[j] == list[j]);
                }
            }

            Debug.Assert(!set.IsSubsetOf(set2));
            Debug.Assert(set.IsSupersetOf(set2));
        }