Ejemplo n.º 1
0
        private static void DontModifyInner()
        {
            Console.WriteLine("\nMake a snapshot and add it to outer");
            ICollection <ISequenced <int> > outer = new HashSet <ISequenced <int> >();

            for (int i = 0; i < 100; i++)
            {
                ISequenced <int> inner = new TreeSet <int>();
                inner.Add(i); inner.Add(i + 1);
                outer.Add(inner);
            }
            IPersistentSorted <int>
            inner1     = new TreeSet <int>(),
                inner2 = new TreeSet <int>(),
                inner3 = new TreeSet <int>();

            inner1.AddAll(new[] { 2, 3, 5, 7, 11 });
            inner2.AddAll(inner1); inner2.Add(13);
            inner3.AddAll(inner1);
            // Take a snapshot and add it to outer:
            outer.Add(inner1.Snapshot());
            Console.WriteLine("inner1 in outer: {0}", outer.Contains(inner1));
            Console.WriteLine("inner2 in outer: {0}", outer.Contains(inner2));
            Console.WriteLine("inner3 in outer: {0}", outer.Contains(inner3));
            inner1.Add(13);
            Console.WriteLine("inner1 equals inner2: {0}",
                              outer.EqualityComparer.Equals(inner1, inner2));
            Console.WriteLine("inner1 equals inner3: {0}",
                              outer.EqualityComparer.Equals(inner1, inner3));
            Console.WriteLine("inner1 in outer: {0}", outer.Contains(inner1));
            Console.WriteLine("inner2 in outer: {0}", outer.Contains(inner2));
            Console.WriteLine("inner3 in outer: {0}", outer.Contains(inner3));
            Console.WriteLine("outer.Count: {0}", outer.Count);
        }
Ejemplo n.º 2
0
        // Anti-pattern: modifying an inner collection while it is a
        // member of an outer one may cause it to be lost from the outer
        // collection.

        private static void ModifyInner()
        {
            Console.WriteLine("\nAnti-pattern: Add to outer, modify, lose");
            ICollection <ISequenced <int> > outer = new HashSet <ISequenced <int> >();

            for (int i = 0; i < 100; i++)
            {
                ISequenced <int> inner = new TreeSet <int>();
                inner.Add(i); inner.Add(i + 1);
                outer.Add(inner);
            }
            ISequenced <int>
            inner1     = new TreeSet <int>(),
                inner2 = new TreeSet <int>(),
                inner3 = new TreeSet <int>();

            inner1.AddAll(new[] { 2, 3, 5, 7, 11 });
            inner2.AddAll(inner1); inner2.Add(13);
            inner3.AddAll(inner1);
            outer.Add(inner1);
            Console.WriteLine("inner1 in outer: {0}", outer.Contains(inner1));
            Console.WriteLine("inner2 in outer: {0}", outer.Contains(inner2));
            Console.WriteLine("inner3 in outer: {0}", outer.Contains(inner3));
            inner1.Add(13);
            Console.WriteLine("inner1 equals inner2: {0}",
                              outer.EqualityComparer.Equals(inner1, inner2));
            Console.WriteLine("inner1 equals inner3: {0}",
                              outer.EqualityComparer.Equals(inner1, inner3));
            Console.WriteLine("inner1 in outer: {0}", outer.Contains(inner1));
            Console.WriteLine("inner2 in outer: {0}", outer.Contains(inner2));
            Console.WriteLine("inner3 in outer: {0}", outer.Contains(inner3));
            Console.WriteLine("outer.Count: {0}", outer.Count);
        }
Ejemplo n.º 3
0
 private static void DontModifyInner()
 {
     Console.WriteLine("\nMake a snapshot and add it to outer");
     ICollection<ISequenced<int>> outer = new HashSet<ISequenced<int>>();
     for (int i = 0; i < 100; i++)
     {
         ISequenced<int> inner = new TreeSet<int>();
         inner.Add(i); inner.Add(i + 1);
         outer.Add(inner);
     }
     IPersistentSorted<int>
       inner1 = new TreeSet<int>(),
       inner2 = new TreeSet<int>(),
       inner3 = new TreeSet<int>();
     inner1.AddAll(new[] { 2, 3, 5, 7, 11 });
     inner2.AddAll(inner1); inner2.Add(13);
     inner3.AddAll(inner1);
     // Take a snapshot and add it to outer:
     outer.Add(inner1.Snapshot());
     Console.WriteLine("inner1 in outer: {0}", outer.Contains(inner1));
     Console.WriteLine("inner2 in outer: {0}", outer.Contains(inner2));
     Console.WriteLine("inner3 in outer: {0}", outer.Contains(inner3));
     inner1.Add(13);
     Console.WriteLine("inner1 equals inner2: {0}",
                       outer.EqualityComparer.Equals(inner1, inner2));
     Console.WriteLine("inner1 equals inner3: {0}",
                       outer.EqualityComparer.Equals(inner1, inner3));
     Console.WriteLine("inner1 in outer: {0}", outer.Contains(inner1));
     Console.WriteLine("inner2 in outer: {0}", outer.Contains(inner2));
     Console.WriteLine("inner3 in outer: {0}", outer.Contains(inner3));
     Console.WriteLine("outer.Count: {0}", outer.Count);
 }
Ejemplo n.º 4
0
        // Anti-pattern: modifying an inner collection while it is a
        // member of an outer one may cause it to be lost from the outer
        // collection.

        private static void ModifyInner()
        {
            Console.WriteLine("\nAnti-pattern: Add to outer, modify, lose");
            ICollection<ISequenced<int>> outer = new HashSet<ISequenced<int>>();
            for (int i = 0; i < 100; i++)
            {
                ISequenced<int> inner = new TreeSet<int>();
                inner.Add(i); inner.Add(i + 1);
                outer.Add(inner);
            }
            ISequenced<int>
              inner1 = new TreeSet<int>(),
              inner2 = new TreeSet<int>(),
              inner3 = new TreeSet<int>();
            inner1.AddAll(new[] { 2, 3, 5, 7, 11 });
            inner2.AddAll(inner1); inner2.Add(13);
            inner3.AddAll(inner1);
            outer.Add(inner1);
            Console.WriteLine("inner1 in outer: {0}", outer.Contains(inner1));
            Console.WriteLine("inner2 in outer: {0}", outer.Contains(inner2));
            Console.WriteLine("inner3 in outer: {0}", outer.Contains(inner3));
            inner1.Add(13);
            Console.WriteLine("inner1 equals inner2: {0}",
                              outer.EqualityComparer.Equals(inner1, inner2));
            Console.WriteLine("inner1 equals inner3: {0}",
                              outer.EqualityComparer.Equals(inner1, inner3));
            Console.WriteLine("inner1 in outer: {0}", outer.Contains(inner1));
            Console.WriteLine("inner2 in outer: {0}", outer.Contains(inner2));
            Console.WriteLine("inner3 in outer: {0}", outer.Contains(inner3));
            Console.WriteLine("outer.Count: {0}", outer.Count);
        }
Ejemplo n.º 5
0
        public CT_Cols addCleanColIntoCols(CT_Cols cols, CT_Col newCol)
        {
            // Performance issue. If we encapsulated management of min/max in this
            // class then we could keep trackedCols as state,
            // making this log(N) rather than Nlog(N). We do this for the initial
            // read above.
            TreeSet <CT_Col> trackedCols = new TreeSet <CT_Col>(CTColComparator.BY_MIN_MAX);

            trackedCols.AddAll(cols.GetColList());
            addCleanColIntoCols(cols, newCol, trackedCols);
            cols.SetColArray(trackedCols.ToArray(new CT_Col[0]));
            return(cols);
        }
Ejemplo n.º 6
0
        public static void Main(String[] args)
        {
            ISorted <int> sorted = new TreeSet <int>();

            sorted.AddAll(new[] { 23, 29, 31, 37, 41, 43, 47, 53 });
            Console.WriteLine(sorted);
            if (args.Length == 1)
            {
                int n = int.Parse(args[0]);
                int res;
                if (Predecessor(sorted, n, out res))
                {
                    Console.WriteLine("{0} has predecessor {1}", n, res);
                }
                if (WeakPredecessor(sorted, n, out res))
                {
                    Console.WriteLine("{0} has weak predecessor {1}", n, res);
                }
                if (Successor(sorted, n, out res))
                {
                    Console.WriteLine("{0} has successor {1}", n, res);
                }
                if (WeakSuccessor(sorted, n, out res))
                {
                    Console.WriteLine("{0} has weak successor {1}", n, res);
                }
            }
            IterBeginEnd(sorted);
            IterBeginEndBackwards(sorted);
            IterIncExc(sorted, 29, 47);
            IterIncExcBackwards(sorted, 29, 47);
            IterIncEnd(sorted, 29);
            IterBeginExc(sorted, 47);
            IterIncInc(sorted, 29, 47);
            IterBeginInc(sorted, 47);
            IterExcExc(sorted, 29, 47);
            IterExcEnd(sorted, 29);
            IterExcInc(sorted, 29, 47);
        }