473,662 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 SortedDictionar y."

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 3092
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 SortedDictionar y."

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
1873
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 I remove the last element on the list everything works fine and I can add other elements to it. However if I remove the first element on the list and I try to add something then my first element is changed when I add a new element to that new...
2
31653
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 through the Values collection but that seems a little long winded. I ended up using a sorted list because it has the IndexOfValue method. However I don't need the sorting.
6
5937
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 replacing an STL map instance by a System.Collections.Generic.SortedDictionary instance. In need the collection to be ordered, and to have o(log(n)) look up - that is fine so far SortedDictionary.TryGetValue does the job. I also need the...
4
1212
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 intgers (i.e. no objects). I pass this sorted list by reference to a function. And do the following type of loop dim i as integer for i = 0 to ConflictList.count - 1
2
2040
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 have seen it looks like the Sorted List does not use the ^ while other variables do use the ^. I have used the SortedList with the ^ and gcnew and it seems to work fine. So the question is...
2
2035
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 I want to do is change a value. I can get the index using IndexOfKey. There is no "SetByIndex" in Generic. Note: the list has two variables named "value" and "Value" that I can see in the debugger. I need to change the lower case "value" but...
4
2731
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
6763
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 SortedList like this: class OtherSortedList : SortedList { ..... int GetTotalAmountOfAllItems().... }
1
3898
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: System::Collections::SortedList, System::Collections::Generic::SortedList, using namespace System::Collections; using namespace System::Collections::Generic; Below is a working version of the generic template SortedList, which
0
8435
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.