Characteristic of the data rather than the storage

Info icon This preview shows pages 330–333. Sign up to view the full content.

View Full Document Right Arrow Icon
characteristic of the data, rather than the storage mechanism, but the fact that we simply cannot create a new, empty array this large demonstrates that a normal array doesn’t store sparse information efficiently. There is no built-in type designed specifically for storing sparse data, but we can use a dictionary to make such a thing. Example 9-7 uses a dictionary to provide storage for a single-dimensional sparse array of double elements. It uses long as the index argument type to enable the array to grow to a logical size that is larger than would be possible with an int , which tops out at around 2.1 billion. Example 9-7. A sparse array of numbers class SparseArray { private Dictionary<long, double> nonEmptyValues = 306 | Chapter 9: Collection Classes
Image of page 330

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
new Dictionary<long, double>(); public double this[long index] { get { double result; nonEmptyValues.TryGetValue(index, out result); return result; } set { nonEmptyValues[index] = value; } } } Notice that this example doesn’t bother to check the return value from TryGetValue . That’s because when it fails to find the entry, it sets the result to the default value, and in the case of a double , that means 0. And 0 is what we want to return for an entry whose value has not been set yet. The following code uses the SparseArray class: SparseArray big = new SparseArray(); big[0] = 123; big[10000000000] = 456; Console.WriteLine(big[0]); Console.WriteLine(big[2]); Console.WriteLine(big[10000000000]); This sets the value of the first element, and also the element with an index of 10 billion— this simply isn’t possible with an ordinary array. And yet it works fine here, with min- imal memory usage. The code prints out values for three indexes, including one that hasn’t been set. Here are the results: 123 0 456 Reading the value that hasn’t been set returns the default value of 0, as required. Some arrays will be sparser than others, and there will inevitably come a point of insufficient sparseness at which this dictionary-based ap- proach will end up being less efficient than simply using a large array. It’s hard to predict where the dividing line between the two techniques will fall, as it will depend on factors such as the type and quantity of data involved, and the range of index values. As with any implementa- tion choice made on the grounds of efficiency, you should compare the performance against the simpler approach to find out whether you’re getting the benefit you hoped for. Dictionaries | 307
Image of page 331
IDictionary<TKey, TValue> The examples we’ve seen so far have all used the Dictionary type defined in the System.Collections.Generic namespace. But that’s not the only dictionary. As we saw a couple of chapters ago, the IEnumerable<T> type lets us write polymorphic code that can work with any sequential collection class. We can do the same with a dictionary— the .NET Framework class library defines an interface called IDictionary<TKey, TValue> , which is reproduced in Example 9-8 .
Image of page 332

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 333
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern