Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, October 5, 2009

NSTL 3.0 released

Last Friday I finally managed to get the NSTL 3.0 out of the door. Although most of the features were mature and ready more than 3 Months ago, I never managed to finish the automatic build process for the 3.x branch. Well, until last Friday!

Besides being compiled the first release written using C#3.0 features such as LINQ and extension methods, it offers a lot of functionality to integrate the NSTL and its C++ background seamless into .NET. Actually, using the new extension methods lets you use .NET and NSTL collections and algorithms seamlessly. For instance, you can use .NET collections with iterators in algorithms:

using.NUnit.Framework;
using NStl.Linq;//Import the extensions

List<int> list = new List<int>(){0,1,2,3};
ListTIterator<int> it = Algorithm.Find(list.Begin(), list.End(), 2);
Assert.That(it, Is.Not.EqualTo(list.End());

By being able to pimp up .NET BCL objects with extension methods, I was able to deprecate a whole lot of NSTL containers like Vector<T>, DList<T> and bulky adapter utility methods like NStlUtil.Begin(..).

Furthermore this release provides extension methods for the NSTL itself, enabling a smooth transition to .NET collections:

using.NUnit.Framework;
using NStl.Linq;//Import the extensions

List<int> list = new List<int>(){0,1,2,3};
ListTIterator<int> it =
Algorithm.Find(list.Begin(), list.End(), 2);

//range contains 0,1
IEnumerable<int> range = list.Begin().AsEnumerable(it);

Last but not least you will find LINQ overloads for the Cast<T> operators to cast dictionaries.

Friday, October 26, 2007

NSTL 2.4 is out the door

I finally made it and shipped the NSTL 2.4. This release cycle took extremely long, mainly through a massive investment in extending the unit test coverage form 75 to 88%. The feature set consists of a couple new containers and algorithms.
  • HashSet is a hashed container where each item is unique
  • HashMap is very similar to Dictionary except that it allows to manipulate the values while iterating over the keys
  • HashMultiMap is a hash container that maps n values to a key
  • PriorityQueue is, well a priority queue
  • Deque is a double ended queue that allows random access to its elements
Algorithms are:
  • RandomSample(N)
  • CopyN
  • LexicographicalCompare3Way
  • Iota
Last but not least it contains the adapter I wrote about earlier. I have learned a lot of different strategies to write UnitTests and their pros and downsides in TDD. I also leaned a lot about how much work it is to write good containers for a library. Two thinks I will blog about in the near future.

Sunday, June 24, 2007

Covariance and Generics

Have you ever thought about why the following code works? Or are you astounded why I ask this question?

class Base{}
class Derived : Base{}

void Foo(Base[] b)
{
    foreach(Base b in b)
        Console.WriteLine(b);
}

Foo(new Derived[]{new Derived(), new Derived()});

Arrays are covariant in C#. This means that you can assign an array of derived types to an array of base types as shown above. Having a strong C++ background, I always wondered why this is possible, because typeof(object[])!= typeof(A[]) is definitely true. And what even makes me wonder more is how often I use the covariance of arrays today!

I noticed that I unintentionally adopted it, when I first tried out .NET Generics about a year

ago and asked myself whether I can pass an IEnumerable<Derived> as an IEnumerable<Base>. Well, I couldn't and was a little bit disappointed. Of course I understand that generics are not covariant and shouldn't be, but still it was annoying to copy the content of one collection to another:

IEnumerable<Base> derived = ...

ICollection<Base> copy = new List<Base>();

foreach(Derived d in derived) 
   copy.Add(d);

So I decided to provide an adapter implementation to avoid the copying. The new C# 2.0 iterator feature made this task very easy. Basically it generates the IEnumerable<T> implementation for you and eliminates the need for the for copying the collection:

public static class Adapt
{
    public static IEnumerable<Base>
       Covariant(IEnumerable<Derived> derived) where Derived : Base
    {
        foreach (Derived b in derived)
            yield return b;
    }
}

This covariant adapter and some more for adapting non generic collections of the System.Collections namespace to their generic counterpart and vice versa are available through the NSTL project, a port and adaption of the C++ STL for .NET.