//----------------------------------------------------------------------------- // // Copyright (c) 2009 Nesterovsky Bros. All rights reserved. // // // A red-black tree test. // //----------------------------------------------------------------------------- namespace NesterovskyBros.Collections { using System; using System.IO; class Program { public static void Main(string[] args) { test(); test2(); } public static void test() { DateTime startDate = new DateTime(2009, 2, 12); ScheduleBookmark bookmarks = new ScheduleBookmark(); // Create a schedule. for (int i = 1; i <= 20; ++i) { ScheduleBookmark bookmark = new ScheduleBookmark( startDate + TimeSpan.FromDays(i * 5), "Item: " + i); ScheduleBookmark.Insert(bookmarks, bookmark); } Console.WriteLine("Original schedule"); print(bookmarks); // Schedule something in the middle. { ScheduleBookmark bookmark = new ScheduleBookmark( startDate + TimeSpan.FromDays(37), "Unplanned item"); ScheduleBookmark.Insert(bookmarks, bookmark); } Console.WriteLine("Schedule with unplanned item."); print(bookmarks); // Item: 10 is cancelled. { DateTime itemDate; ScheduleBookmark bookmark = ScheduleBookmark.Find( bookmarks, startDate + TimeSpan.FromDays(50), out itemDate); ScheduleBookmark.Remove(bookmark); } Console.WriteLine("\"Item: 10\" is cancelled."); print(bookmarks); // We're out of schedule with "Item: 3", // move everything two days forward. { DateTime itemDate; ScheduleBookmark bookmark = ScheduleBookmark.Find( bookmarks, startDate + TimeSpan.FromDays(15), out itemDate); ScheduleBookmark.OnScheduleChanged(bookmark, TimeSpan.FromDays(2), false); } Console.WriteLine("Extend \"Item: 3\" for two days."); print(bookmarks); // What's is the ten's item? { ScheduleBookmark bookmark = ScheduleBookmark.Get(bookmarks, 10); Console.WriteLine("Ten's item"); Console.WriteLine(bookmark.ToString()); } Console.WriteLine( "Totally there are " + ScheduleBookmark.Count(bookmarks) + " items."); } public static void test2() { DateTime startDate = new DateTime(2009, 2, 12); ScheduleBookmark bookmarks = new ScheduleBookmark(); ScheduleBookmark bookmark = new ScheduleBookmark(startDate, "Start"); ScheduleBookmark.Insert(bookmarks, bookmark); bookmark = new ScheduleBookmark( startDate + TimeSpan.FromDays(100), "End"); ScheduleBookmark.Insert(bookmarks, bookmark); for(int i = 0; i < 20; ++i) { bookmark = new ScheduleBookmark( startDate + TimeSpan.FromDays((i + 1) * 4), "Item: " + i); ScheduleBookmark.Insert(bookmarks, bookmark); //StringWriter writer = new StringWriter(); //Print(writer, bookmarks.Left, ""); //Console.WriteLine("{0}", i); //Console.WriteLine("{0}", writer); } StringWriter writer = new StringWriter(); Print(writer, bookmarks.Left, ""); Console.WriteLine("{0}", writer); } private static void print(T node) where T: RedBlackNode { if (node == null) { return; } T current = RedBlackNode.Leftmost(node); while(current != null) { Console.WriteLine(current.ToString()); current = RedBlackNode.Successor(current); } Console.WriteLine(); } private static void Print( TextWriter writer, ScheduleBookmark bookmark, string indent) { if (bookmark == null) { return; } writer.Write(indent); writer.WriteLine(bookmark); ScheduleBookmark left = bookmark.Left; ScheduleBookmark right = bookmark.Right; if ((left != null) || (right != null)) { string newIndent = indent + " "; if (left != null) { writer.Write(indent); writer.WriteLine("left:"); Print(writer, left, newIndent); } if (right != null) { writer.Write(indent); writer.WriteLine("right:"); Print(writer, right, newIndent); } } } } }