473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C# stl


Having developed in C++ prior to learning C#, I have always felt like
..NET was missing something. Specifically, something that fills the role
of the STL.

With that in mind, I have launched an open source project to create an
STL like library in C# 2.0. The project is called CSTL, and is available
from sourceforge.net:

http://sourceforge.net/projects/cstl

The library is pre-alpha, incomplete and not ready for primetime use.
However, the concepts are pretty much in place.

Here is an example of the Count algorithm:

int [] values = new int[] {1,2,3,2,2,4,5,2};
int count = Algorithm.Count(values, 2);
Here is an example of the CopyIf algorithm and back insertion iterators

int [] values = new int[] {1,2,3,2,2,4,5,2};
List<int> dest = new List<int>();

Algorithm.CopyIf(values, IteratorUtil.BackInserter(dest)
delegate(int x) { return x>2;});

// dest now contains {3,4,5}

There are other projects like this out there. However, none of them seem
quite right to me. Usually, they don't have an iterator concept, or they
use C++ in non CLS compliant ways.

Feedback is welcome.

H^2
Remove .bounce from email address.

May 25 '06 #1
13 6009
Harold,

I think that before you take this too far, you should take a look at
LINQ, and see the overlap between what you are doing, and what is going to
be presented there. There are many concepts that are similar, and I would
hate to see you do so much work which is only going to end up being provided
by MS.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Harold Howe" <hh***********@mchsi.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...

Having developed in C++ prior to learning C#, I have always felt like .NET
was missing something. Specifically, something that fills the role of the
STL.

With that in mind, I have launched an open source project to create an STL
like library in C# 2.0. The project is called CSTL, and is available from
sourceforge.net:

http://sourceforge.net/projects/cstl

The library is pre-alpha, incomplete and not ready for primetime use.
However, the concepts are pretty much in place.

Here is an example of the Count algorithm:

int [] values = new int[] {1,2,3,2,2,4,5,2};
int count = Algorithm.Count(values, 2);
Here is an example of the CopyIf algorithm and back insertion iterators

int [] values = new int[] {1,2,3,2,2,4,5,2};
List<int> dest = new List<int>();

Algorithm.CopyIf(values, IteratorUtil.BackInserter(dest)
delegate(int x) { return x>2;});

// dest now contains {3,4,5}

There are other projects like this out there. However, none of them seem
quite right to me. Usually, they don't have an iterator concept, or they
use C++ in non CLS compliant ways.

Feedback is welcome.

H^2
Remove .bounce from email address.

May 25 '06 #2
I think that before you take this too far, you should take a look at
LINQ, and see the overlap between what you are doing, and what is going to
be presented there.


Thanks for the suggestion and the heads up. How long until C# 3.0 is
ready for production use? From what little I could find, Orcas is
looking like it may come in the second half of 2007. From past
experience, I know it would take an additional 4 months or so until my
group adopts it.

Worst case scenario is that LINQ renders CSTL completely irrelevant. If
that is true, and it happens in the first half of 2008 or later, then
the developement of CSTL, for me personally, will have been worth it
because I need some of its features now.

If C# 3.0 is released for production use in 2005, then I might change my
tune I guess. Is there a chance that C# 3.0 would be released before Orcas?

H^2
May 25 '06 #3
Hello Harold,

Nicolas wanted just point that there are too much things around that need
to be improved, rather that doing smth that will be realized soon in the
bounds of framework :)

For example, you can help guys from Nemerle (www.nemerle.org) to realize
these features :)
I think that before you take this too far, you should take a look at
LINQ, and see the overlap between what you are doing, and what is
going to be presented there.

HH> Thanks for the suggestion and the heads up. How long until C# 3.0 is
HH> ready for production use? From what little I could find, Orcas is
HH> looking like it may come in the second half of 2007. From past
HH> experience, I know it would take an additional 4 months or so until
HH> my group adopts it.
HH>
HH> Worst case scenario is that LINQ renders CSTL completely irrelevant.
HH> If that is true, and it happens in the first half of 2008 or later,
HH> then the developement of CSTL, for me personally, will have been
HH> worth it because I need some of its features now.
HH>
HH> If C# 3.0 is released for production use in 2005, then I might
HH> change my tune I guess. Is there a chance that C# 3.0 would be
HH> released before Orcas?

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
May 25 '06 #4
That's probably in the middle of 2008? On what am I basing this on? wild
guess.

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

"Harold Howe" <hh***********@mchsi.com> wrote in message
news:O7****************@TK2MSFTNGP03.phx.gbl...
I think that before you take this too far, you should take a look at
LINQ, and see the overlap between what you are doing, and what is going
to be presented there.


Thanks for the suggestion and the heads up. How long until C# 3.0 is ready
for production use? From what little I could find, Orcas is looking like
it may come in the second half of 2007. From past experience, I know it
would take an additional 4 months or so until my group adopts it.

Worst case scenario is that LINQ renders CSTL completely irrelevant. If
that is true, and it happens in the first half of 2008 or later, then the
developement of CSTL, for me personally, will have been worth it because I
need some of its features now.

If C# 3.0 is released for production use in 2005, then I might change my
tune I guess. Is there a chance that C# 3.0 would be released before
Orcas?

H^2

May 25 '06 #5
the C# folks at MSFT was stated that the C# v2.0 -> v3.0 cycle will be
much shorter that the v1.0 -> v2.0, because v2.0 required changes to
the CLR, while v3.0 does not. That could make an official release in
2006 possible.

May 25 '06 #6

"Harold Howe" <hh***********@mchsi.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...

Having developed in C++ prior to learning C#, I have always felt like .NET
was missing something. Specifically, something that fills the role of the
STL.


Harold,

Your project seems very interesting - I'll be sure to download the lib and
play some.

I'm curious what your position on type inference limitations in C# are. I
remember a thread from a while ago where Dietmar Kuehl was talking about
problems implementing STL-like algorithms in C#. Rereading the thread now
("Constraints on Generics ??")message 8 in the following thread)
http://groups.google.com/group/micro...abecf7e92f67c6

I'm wondering which route you are taking.

Andrew

May 25 '06 #7
Hello James,

JC> the C# folks at MSFT was stated that the C# v2.0 -> v3.0 cycle will
JC> be much shorter that the v1.0 -> v2.0, because v2.0 required changes
JC> to the CLR, while v3.0 does not. That could make an official
JC> release in 2006 possible.

Moreover, it totally compatible with C# 2.0 by byte-code. All that new features
are pure syntactic and realized on the compiling stage.
Thus, there is nothing break-throughing in LINQ

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
May 25 '06 #8
I'm curious what your position on type inference limitations in C# are.
They are a drag, to be honest. But I try to work around them as best I can.
I
remember a thread from a while ago where Dietmar Kuehl was talking about
problems implementing STL-like algorithms in C#. Rereading the thread now
("Constraints on Generics ??")message 8 in the following thread)

I'm wondering which route you are taking.


I have read that thread. Actually, it was the inspiration that got me
started on this whole thing.

Dietmar's two approaches were

public E MinElement<E, C>(E enumerator, C comparer)
where E: Enumerator<T> // or: E: Enumerator<E.ValueType>
where C: Comparer<T> // ... C: Comparer<E.ValueType>
{
// whatever...
}

and

public Enumerator<T> MinElement<T>(Enumerator<T> enumerator,
Comparer<T> comparer)

In CSTL, I have an abstraction for iterators. So I tend not to return
enumerators directly. However, I ran into the same issue that Dietmar
mentions above. In fact, I posted some questions on type inference last
week to this group (subject: Generic type inferrence and anonymous
methods).

My initial attempt at coding MinElement looked like this:

public static I MinElement<I,T>(I begin, I end)
where I: ForwardIterator<T>
where T: IComparable<T>
{ ... }

When I try to call this, I get

main.cs(31,25): error CS0411: The type arguments for method
'Algorithm.MinElement<I,T>(I, I)' cannot be inferred from the usage.
Try specifying the type arguments explicitly.

So I changed the method to this, which is similar to Dietmar's second form:

public static ForwardIterator<T> MinElement<T>(
ForwardIterator<T> begin, ForwardIterator<T> end)
where T: IComparable<T>
{ ... }

Note that ForwardIterator is an interface and not a class.

As Dietmar mentioned, the first form is more desirable, especially in
algorithms that return iterators. The second form results in a reduction
in type information. For example

int[] values = new int[]{1,2,3,4,5};
ListIterator<int> begin = IteratorUtil.Begin(values);
ListIterator<int> end = IteratorUtil.End(values);

//ERROR!
ListIterator<int> min = Algorithm.MinElement(begin,end)

This errors out because MinElement returns the weaker interface type. In
reality, it is returning a ListIterator, but I have to cast in order to
get access to that type.

H^2

May 25 '06 #9

"Harold Howe" <hh***********@mchsi.com> wrote in message
news:eE*************@TK2MSFTNGP02.phx.gbl...
I'm curious what your position on type inference limitations in C# are.
They are a drag, to be honest. But I try to work around them as best I
can.

[snip] When I try to call this, I get

main.cs(31,25): error CS0411: The type arguments for method
'Algorithm.MinElement<I,T>(I, I)' cannot be inferred from the usage.
Try specifying the type arguments explicitly.

Don't you just want to yell at the compiler "No! You try a little harder!"?
There are probably good reasons for this beyond my grasp but it seems like a
little more inference should be possible.
So I changed the method to this, which is similar to Dietmar's second
form:

public static ForwardIterator<T> MinElement<T>(
ForwardIterator<T> begin, ForwardIterator<T> end)
where T: IComparable<T>
{ ... }

Note that ForwardIterator is an interface and not a class.

As Dietmar mentioned, the first form is more desirable, especially in
algorithms that return iterators. The second form results in a reduction
in type information. For example

int[] values = new int[]{1,2,3,4,5};
ListIterator<int> begin = IteratorUtil.Begin(values);
ListIterator<int> end = IteratorUtil.End(values);

//ERROR!
ListIterator<int> min = Algorithm.MinElement(begin,end)

This errors out because MinElement returns the weaker interface type. In
reality, it is returning a ListIterator, but I have to cast in order to
get access to that type.


Here you are just trying to return the exact same type as that of the
argument so it's hard to understand why that doesn't work. I think it must
have something to do with the fact that generic types are real honest to
goodness types, not type templates, so your specifying the argument and
return values as ForwardIterator<T> makes them exactly that. I guess that's
why we call generics generics and templates templates.

Andrew
May 25 '06 #10


Harold Howe wrote:
Algorithm.CopyIf(values, IteratorUtil.BackInserter(dest)
delegate(int x) { return x>2;});
I like inserters from STL, C# doesn't have anything similar -- and I
miss it, it's the inverse of an IEnumerator -- just as useful. To be
really useful, the definition needs to be shared in System.Collections,
so developers won't have to adapt different declarations.

Unfortunately, some of the strength of STL is that it can independently
declare (and implement) the same algorithm on many data-structures.
That power stems from being able to add overloads to a name-space from
independent places. That just won't work with C#. And there is no
partial specialization either, so you'll be hard pressed to "collect"
different implementations under the same name.

Also, standard iteration in C# is IEnumerator<T>, which doesn't have
begin and end. You'll get into a lot of work mapping collection-types to
iterators to stay in the STL-spirit.

You can accept the IEnumerator<T> as standard, and still declare
algorithms like copy-if:

CopyIf(IEnumerator<T> values, IInserter<T> inserter, Predicate<T> pred);

I personally prefer adaption where possible, utilizing that there is a
garbage-collector. Of course, you need to be careful when mutating the
constructed proxy, but the caller can always copy it if he wants his own.

/// Return only the items of Parent that satisfy Pred
class FilterCollection<T>: ICollection<T> {
public readonly ICollection<T> Parent;
public readonly Predicate<T> Pred;
...
}

The caller can always add the filtered collection to an existing
collection, using the same CPU-effort.

I have a handy library of adapters, that apply transformations to
ICollection and IDictionary, and it's code can get acceptable
readability using it, here are a few usage examples:

float[] cc1s =
CollectionUtil.ToArray(CollectionUtil.Transform<fl oat,
float>(shortest_path.Values, delegate(float d) { return
(float)Math.Exp(-d); }));

// caught in missing type-inference for anonymous-delegate :)
public IDictionary<Address, float> edgeEvaluator(
ICollection<Address> keys,
TransformCollection<Address, float>.Transformer eval)
{ return DictionaryUtil.Function<Address, float>(keys, eval); }

IDictionary<Address, float> NeighboursOf(Address a)
{
if (a == Root) // Special treatment for root-node
return edgeEvaluator(Network.EntryPoints.Keys,
delegate(Address to) { return
(float)-Math.Log(Network.Nodes[to].RouteChance); });
else if (Network.Nodes[a].Type.CanRoute )
return edgeEvaluator(Network.Neighbours[a].Keys,
delegate(Address to) { return Distance(a, to); });
else // only routers and Root are allowed out-edges
return DictionaryUtil.Empty<Address, float>();
}
There are other projects like this out there. However, none of them seem
quite right to me. Usually, they don't have an iterator concept, or they
use C++ in non CLS compliant ways.
Sorry for not checking myself, but are your iterators like c++
iterators? can you mutate through them?
Feedback is welcome.


Best of luck to you, it just might work -- but a lot of stuff thats used
in intricate ways in STL is simply not there in C#: traits, free
functions, partial-specialization, ...

--
Helge
May 26 '06 #11
Helge Jensen wrote:
Sorry for not checking myself, but are your iterators like c++
iterators? can you mutate through them?


Yes.

public interface InputIterator<T> : IEquatable<InputIterator<T>>, ICloneable
{
T Read();
void MoveNext();
void Assign(InputIterator<T> iterator);
}

public interface OutputIterator<T> : ICloneable
{
void Write(T t);
void MoveNext();
void Assign(InputIterator<T> iterator);
}

The forward, bidirectional and random access interfaces follow as you
would expect.

H^2
May 28 '06 #12
Take a look of this:
http://msdn2.microsoft.com/en-us/library/dscyy5s0.aspx

chanmm

"Harold Howe" <hh***********@mchsi.com> wrote in message
news:eH**************@TK2MSFTNGP02.phx.gbl...
Helge Jensen wrote:
Sorry for not checking myself, but are your iterators like c++ iterators?
can you mutate through them?


Yes.

public interface InputIterator<T> : IEquatable<InputIterator<T>>,
ICloneable
{
T Read();
void MoveNext();
void Assign(InputIterator<T> iterator);
}

public interface OutputIterator<T> : ICloneable
{
void Write(T t);
void MoveNext();
void Assign(InputIterator<T> iterator);
}

The forward, bidirectional and random access interfaces follow as you
would expect.

H^2

May 29 '06 #13
chanmm wrote:
Take a look of this:
http://msdn2.microsoft.com/en-us/library/dscyy5s0.aspx


C# iterators simply yield an enumerable object. Enumerables cannot

1- write

2- go backwards

3- be copied

4- be compared.
H^2
May 30 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.