Monday, November 24, 2008

Dr. Dobb's Journal CD, for the really nerdy "Old School" Dot Netters

I remember way back when I was just starting my computer science education. I would get the latest edition of Dr. Dobb's Journal, first at the news stand then later in the mail, and I would read it from cover to cover. Not that I understood a lot of what I was reading, but I realized that this was information that I would someday need to know.

My freshman level language was Pascal and I had not gotten to learning about pointers yet. Even though the published source code was in C, just reading the descriptions of the various algorithms that Dr. Dobb's Journal published helped my understanding of how complex data structures were built.

As I went from Computer Science class to Computer Science class I kept using my back issues as reference material. In a way, Dr. Dobb's Journal was, for me, the equivalent of Computer Science version of Scientific American. Well, like they say, three moves is the same as a fire, and I've moved may more times than three, so I have no idea where all my old DDJ's are or if I even still own them.

One day though, I got a letter asking me to re-subscribe, with the offer of all the issues from January 1988 through December 2000 on CD. Well that was just too good to pass up! I re-subscribed, got my CD and stashed it away.

Well, when Dot Net came out it had all those nifty System.Collection classes. Without any work I had a Stack, a Queue, a Hashtable and an ArrayList that was kinda like a cross between a linked list and an array. What need did I have for any of those "roll your own" abstract data types?

Live and learn I guess, but not all Stacks, etc, are built alike. The Dot Net version is array based. Now, I'm not saying that that is bad, just that it is not pointer (or Object Reference) based so it has different characteristics. Plus, where is my binary tree or red-black tree or skip list? How about a nice graph?

Well, out comes the CD. A quick search and I've got a nice bit of source code to start a C# version of a skip list.

Truth be told, for a complete toolbox for Dot Net and C# specifically, the System.Collection namespace should contain abstract data types based on (When Possible):

  • An Object based Array Implementation
  • A Generic Array Implementation
  • A Object based Object Reference Implementation
  • A Generic Object Reference Implementation
  • A Object based Pointer (unsafe) Implementation
  • A Generic Pointer (unsafe) Implementation

    Since they all have different performance, security and memory concerns.

    Maybe, someday, with the help of my trusty DDJ CD I'll even get around to coding all those ADT's!
  • Monday, November 17, 2008

    Shuffling arrays in C#

    Have you ever had a need to output some array of objects in a random order? Say a array of strings ... with 52 elements that each represent a playing card?

    Well, if you have ever had a need to shuffle the elements in an array, you have probably found that there are only a few different algorithms: Either card swapping or assigning a random Key to each element and then sorting by the key.

    Unfortunately there are quite a few issues involved with the swap shuffle so I wanted a to use the sort version. Well, what has tuples? Hashtables? Hashtables! So, how do you sort a Hashtable? Hmmmm, maybe I need to think harder about this.

    Hey, did everyone realize their is a sorted version called a SortedList?

    OK, So we will use the SortedList. Now I want this function to use Generics so that it will take an array of any type. So lets try!

    (Mind you that this is not nearly as small as the implementation here but I am not posting here to show how to shuffle in the least number of lines or use the latest technology, but with an eye towards the most understandable source.)

    private static T[] Shuffle <T> (T[] OriginalArray)
    var matrix = new SortedList();
    var r = new Random();

    for (var x = 0; x <= OriginalArray.GetUpperBound(0); x++)
    var i = r.Next();

    while (matrix.ContainsKey(i)) { i = r.Next(); }

    matrix.Add(i, OriginalArray[x]);

    var OutputArray = new T[OriginalArray.Length];

    matrix.Values.CopyTo(OutputArray, 0);

    return OutputArray;

    Tuesday, November 11, 2008

    Dynamically adding XAML strings from C#

    In designing the screen for my silverlight game Switcheroo, I wanted to be able to dynamically add XAML chunks that represent the pieces (Ellipse). True, I could have just created the graphic elements property by property, but since the only difference between all the game pieces is if they are black or red, I thought that if it only took one or two lines of code, it would be a more elegant solution.

    I wanted to separate the internal representation of the gameboard in the gaming object from the XAML front end. Then I could just clear the gameboard and recreate all the pieces after the gameboard object was updated. The question was, how?

    Well I googled high and low but though I found many "solutions" to my problem, none of them worked. After trying different combinations of the aforementioned solutions, I got closer. This is my first try that made any headway:

    const string red =
    "<Ellipse Stroke='Black' Fill='Red' Height='30' Width='30' Margin='10,10,10,10' />";


    But the error I got back was:


    Not a very helpful error message! Well, googling this error I found others that were getting the same error and a simple answer - the element must contain the same namespace. The only namespace is the one at the beginning of the XAML document thought. Well, I thought, it's worth a shot. So I added the namespace lines from the beginning of the element. Our code now looked like this:

    const string red =
    "<Ellipse xmlns='' xmlns:x='' Stroke='Black' Fill='Red' Height='30' Width='30' Margin='10,10,10,10' />";


    And low and behold! It worked!

    So when you have lots of repetitious XAML or elements like named colors that cannot be easily replicated in code, just use the XAML string!