13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Generic collection classes <strong>in</strong> .NET 2.0101pattern for multithread<strong>in</strong>g. This is not particularly hard to write, and third-partyimplementations are available, but hav<strong>in</strong>g these classes directly available <strong>in</strong> the frameworkwould be more welcome.Next we’ll look at the generic versions of SortedList, which are similar enough tobe tw<strong>in</strong>s.3.5.4 SortedList and SortedDictionaryThe nam<strong>in</strong>g of SortedList has always bothered me. It feels more like a map or dictionarythan a list. You can access the elements by <strong>in</strong>dex as you can for other lists(although not with an <strong>in</strong>dexer)—but you can also access the value of each element(which is a key/value pair) by key. The important part of SortedList is that when youenumerate it, the entries come out sorted by key. Indeed, a common way of us<strong>in</strong>gSortedList is to access it as a map when writ<strong>in</strong>g to it, but then enumerate the entries<strong>in</strong> order.There are two generic classes that map to the same sort of behavior: Sorted-List and SortedDictionary. (From here on I’ll justcall them SortedList and SortedDictionary to save space.) They’re very similar<strong>in</strong>deed—it’s mostly the performance that differs. SortedList uses less memory,but SortedDictionary is faster <strong>in</strong> the general case when it comes to add<strong>in</strong>g entries.However, if you add them <strong>in</strong> the sort order of the keys to start with, SortedListwill be faster.NOTEA difference of limited benefit—SortedList allows you to f<strong>in</strong>d the <strong>in</strong>dex ofa particular key or value us<strong>in</strong>g IndexOfKey and IndexOfValue, and toremove an entry by <strong>in</strong>dex with RemoveAt. To retrieve an entry by <strong>in</strong>dex,however, you have to use the Keys or Values properties, which implementIList and IList, respectively. The nongeneric versionsupports more direct access, and a private method exists <strong>in</strong> the generic version,but it’s not much use while it’s private. SortedDictionary doesn’tsupport any of these operations.If you want to see either of these classes <strong>in</strong> action, use list<strong>in</strong>g 3.1 as a good start<strong>in</strong>gpo<strong>in</strong>t. Just chang<strong>in</strong>g Dictionary to SortedDictionary or SortedList will ensure thatthe words are pr<strong>in</strong>ted <strong>in</strong> alphabetical order, for example.Our f<strong>in</strong>al collection class is genu<strong>in</strong>ely new, rather than a generic version of anexist<strong>in</strong>g nongeneric type. It’s that staple of computer science courses everywhere: thel<strong>in</strong>ked list.3.5.5 L<strong>in</strong>kedListI suspect you know what a l<strong>in</strong>ked list is. Instead of keep<strong>in</strong>g an array that is quick toaccess but slow to <strong>in</strong>sert <strong>in</strong>to, a l<strong>in</strong>ked list stores its data by build<strong>in</strong>g up a cha<strong>in</strong> ofnodes, each of which is l<strong>in</strong>ked to the next one. Doubly l<strong>in</strong>ked lists (likeL<strong>in</strong>kedList) store a l<strong>in</strong>k to the previous node as well as the next one, so you caneasily iterate backward as well as forward.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!