views:

607

answers:

8

Dear ladies and sirs.

Is there a library of generic collection algorithms for .NET? I would like to be able to write something like this:

IList<T> items = GetItemsFromSomeWhere();
Algorithms<T>.Sort(items);
//
// bla bla bla
//
T item = GetItemSomwHow();
int i = Algorithms<T>.IndexOf(items, item);

Note, that items is not List<T>, otherwise I could simply use the List<T>.Sort and List<T>.BinarySearch methods.

Of course, I can implement them myself, I just do not want to invent a wheel.

Ah, and another thing. I would like the implementation to be efficient.

Thanks.

P.S.

Please, do not advise on which collections to use. I am perfectly aware of the Array or List<T> abilities. What I need is a library of algorithms to work on any IList<T> based collection.

EDIT: Found what I needed - see my own answer.

+1  A: 

System.Linq.Enumerable class does a bunch of good stuff. Admittedly, it does miss some stuff but it's applicable nevertheless.

Mehrdad Afshari
No; it doesn't do binary searches
SLaks
Well, binary search is only applicable on lists with O(1) index-based access time. `Array.BinarySearch` will do it for arrays.
Mehrdad Afshari
Guys, I have IList<T>, not List<T> or T[]. I need a solution for any IList<T> based collection. So, how Array.BinarySearch is helping me?
mark
A: 

The Array class might work for you (Sort and IndexOf).

Could do the following:

        IList<string> foo = new List<string>();
        foo.Add("hi");
        foo.Add("bye");
        string[] foo_temp = new string[foo.Count];
        foo.CopyTo(foo_temp, 0);
        Array.Sort<String>(foo_temp);
        foo = new List<string>(foo_temp);
SwDevMan81
I think the asker is looking for a set algorithms that work a generic collection, not asking for advice on which collection to use.
Binary Worrier
No; it only works on arrays. He wants to call things on any `IList<T>`
SLaks
Array.BinarySearch<T>
SwDevMan81
SLaks - exactly. Thank you.
mark
A: 

You just gave the definition for LINQ with the OrderBy extension method.

Yuriy Faktorovich
No; he wants to sort in-place
SLaks
I think sort is just an example - LINQ has a lot of collection-manipulation functions but I suspect he's interested in other stuff. I've seen Set<T> implementations that do intersection, union, etc. but not sure if that's what he wants.
n8wrl
A: 

IndexOfKey method of SortedList<> does a BinarySearch internally.

Tamás Szelei
+2  A: 

Unfortunately, .NET doesn't provide built in BinarySearch implementations that work with the IList<T> interface.

You can use Linq for sorting your generic list, as other posters have mentioned. But for binary search on IList<T>, I would suggest you check out the following SO post.

I am not aware of a good, general purpose algorithms library that you can use to fill in the gaps in Linq, although I suspect that a lot of people out there have implemented their own utilities to solve similar problems.

LBushkin
Check out my own answer.
mark
A: 

Think about what data structures you are using and what algorithms are actually possible on it. I know you don't want to read about collections, but obviously binary search doesn't work on a linked list. What kind of algorithms do you need? The efficient sorting algorithms are there already. Linq and the .NET collections should give you everything you need.

Polaris878
No, he specifically wants IList algorithms, and the two examples he gives could be implemented for IList in the BCL, but currently aren't. They are tied to List and Array.
Daniel Earwicker
A: 

I also hit this problem. My solution was to use Reflector to find out the implementations of List.Sort and Array.BinarySearch and rewrite them as extension methods for IList<T>.

Even if you don't write them as extensions but as ordinary static methods (e.g. if you're using C# 2), you don't need your exact syntax:

Algorithms<T>.Sort(items);

Just make Sort a static generic method, in an ordinary static (non-generic) class Algorithms, and type inference will do the rest by looking at the type of the argument:

Algorithms.Sort(items);
Daniel Earwicker
Check out my own answer.
mark
+1  A: 

After doing some research on my own I have found the PowerCollections library from Wintellect.

Aside from supplying various collections it provides exactly what I need - a static Algorithms class with quite a few algorithms, including BinarySearch<T> and SortInPlace<T>, which expect IList<T>, any IList<T>.

mark