473,326 Members | 2,126 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,326 software developers and data experts.

Initializing a generic SortedList with sorted data

Howdy all,

The msdn help says this about SorteList<k,v>:

"If the list is populated all at once from sorted data, SortedList is
faster than SortedDictionary."

My question is this: how do I initialize a SortedList all at once from
sorted data?

The only constructors that populate the container take a IDictionary as
the source of the data. .NET Reflector shows that these constructors
call Sort after they copy the data in, so this can't be what the help
was referring to (never mind trying to figure out which concrete
dictionary class can supply the sorted data).

After construction, the only way to supply values is one at a time with
the Add method for the array indexer property. The Keys and Values
properties are essentially read only. The Add method doesn't look like
it will be any faster for a sequence of sorted data. Each call performs
a full binary search to locate the insertion point. I don't see any
optimizations for when the value is be greater that the last element.

My workaround is to do something hideously ugly involving reflection. It
results in a nice speed improvement, but also a certain amount of shame.

H^2
Jul 23 '08 #1
1 3080
Harold Howe wrote:
Howdy all,

The msdn help says this about SorteList<k,v>:

"If the list is populated all at once from sorted data, SortedList is
faster than SortedDictionary."

My question is this: how do I initialize a SortedList all at once from
sorted data?
You supply a list to the constructor.
The only constructors that populate the container take a IDictionary as
the source of the data.
That's the one.
.NET Reflector shows that these constructors
call Sort after they copy the data in, so this can't be what the help
was referring to (never mind trying to figure out which concrete
dictionary class can supply the sorted data).
Yes, it is. What the documentation means is that the sorting stage is
fast if the data is already sorted. The constructor always sorts the
data to verify that it is actually sorted, there is no special
constructor for sorted data.
After construction, the only way to supply values is one at a time with
the Add method for the array indexer property. The Keys and Values
properties are essentially read only. The Add method doesn't look like
it will be any faster for a sequence of sorted data. Each call performs
a full binary search to locate the insertion point. I don't see any
optimizations for when the value is be greater that the last element.

My workaround is to do something hideously ugly involving reflection. It
results in a nice speed improvement, but also a certain amount of shame.

H^2
If the data is already sorted, sorting it again should be so quick that
it's hardly worth trying to circumvent it.

--
Göran Andersson
_____
http://www.guffa.com
Jul 28 '08 #2

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

Similar topics

6
by: Jose Jarabo | last post by:
Hello, thanks in advance for any and all replies. I am not sure how to categorize this, either a bug or something else but here is my problem. I am testing with 2 elements on a sorted list. If...
2
by: orekinbck | last post by:
Hi There I am probably missing something fundamental here, but I cannot see a method to search the values of a generic dictionary so that I can find the key ? Of course I could enumerate...
6
by: Paul Delhanty | last post by:
Hi, I am converting an existing native C++ program making heavy use of STL to C#2.0 with STL usage replaced by the new generic collections. The conversion has gone well to the point where I am...
4
by: J L | last post by:
I have a sortedlist (ConflictList) that contains a string identifier (ShipmentNumber) for the key and a structure (AppointmentInfo) for the value. The structure memebers are strings, dates and...
2
by: Prez | last post by:
I started writing .net code yesterday and I am grasping it well enough. I have a few questions about SortedLists. I am using managed C++ if that makes any difference. Of the examples I...
2
by: Bruce Arnold | last post by:
I have a program that works fine using Remove and Add to update a value. The program processes a log file from a router and counts the hits based on url. It bothers me to use Remove/Add when all...
4
by: Oscar Thornell | last post by:
Hi, Any comments regarding this implementation... SortedList<String, Stringlist = new SortedList<String, String>(); lock (((IList)list).SyncRoot) { foreach (Object item in list) {
4
by: semedao | last post by:
Hi, I want to implement list of key-values that can be sort by 2 ways. let's say that in the first step I wanted to make SortList based on Key = int index that cannot change and Value is another...
1
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash:...
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...
0
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...
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...
1
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...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.