C
cody
why arent there some util functions for IList for example Sort()?
cody said:why arent there some util functions for IList for example Sort()?
each case depends on the implementation of the list. Trying to sort a
linked list using the "obvious" kind of approach would be horrendous in
performance terms.
There's also the issue of working out where you'd put such methods,
given that they couldn't be in IList itself.
cody said:You are right. This makes me think why a generic IList needs an indexer in
the first place then.
Misusing it would lead to horrible performance, and you often do not see the
internal implementation.
I consider IList a bad interface it should have be broken down into
interfaces with less functionality which would make implementing readonly
wrappers easies. Providing every possible method first, but throwing
InvalidOperationException during runtime is no good idea.
This would explode BCL's System.Collections namespace:
Take a look at the list of classes that implement IList.
You missed the static ArrayList.Adapter method
that creates an ArrayList wrapper for an IList.
ArrayList hat a Sort-method, so you do't need
another helper classes/methods.
Iam really starting to hate IList now more than ever
The first (and very logical argument was that sorting for IList is not
allowed because of performance issues)
Now I can simply wrap it up in an ArrayList (which then isn't an ArrayList
anymore, isn't it).
See ArrayList.Adapter's docs.
Of course it's still an ArrayList. The only difference
is where the data is coming from: from the IList.
cody said:Adapter doesn't copy the contents of the list - so in case we have used a
LinkedList to create the list, using the wrapper we have still an underlying
LinkedList which makes sorting extremely slow.
James Curran said:There is no reason why sorting a linked list should be slow. In many
cases, it's faster than sorting an array as there's less copying (although
in a reference-based environment like .Net, it'll be a wash).
QuickSort, one of the fastest sort algorithms, works fine with a
linked-list.
Jon Skeet said:QuickSort, one of the fastest sort algorithms, works fine with a
linked-list.
Not if it swaps elements by saying:
object tmp = list[x];
list[x] = list[y];
list[y] = tmp;
That's going to be painfully slow with a linked list, as it would have
to walk down to element x or y each time... and that's my guess as to
how it works in ArrayList.Sort.
design.....James Curran said:Upon closer reading of ArrayList.Sort (which states it calls
Array.Sort), it appears you are right. Sheesh.... It is a dumb
James Curran said:Jon Skeet said:QuickSort, one of the fastest sort algorithms, works fine with a
linked-list.
Not if it swaps elements by saying:
object tmp = list[x];
list[x] = list[y];
list[y] = tmp;
Well, I was speaking about the algorithm in general rather than a
particular implementation. When described in general terms, no items are
"swapped" in QuickSorting, they are moved to new lists, an operation perfect
for linked lists.
Upon closer reading of ArrayList.Sort (which states it calls
Array.Sort), it appears you are right. Sheesh.... It is a dumb design.....
Jon said:From "Introduction to Algorithms" (Corwen, Leiserson, Rivest):
<quote>
It also has the advantage of sorting in place [...]
</quote>
The algorithm is then described in terms of partitioning which
includes a swap operation.
If you think you can come up with an IList interface and a
corresponding quicksort implementation which is efficient for both
array-based lists *and* linked lists, without any special-casing, I'll
be impressed.
"array-based lists" is an abuse of the concept. I've never heard the
term "list" (used in a data structures context) to refer to anything beside
a linked-list (or more correctly, to a DS which demostrates the features of
a linked-list regardless of implementation)