Data Structures in 'NET - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Data Structures in 'NET

Description:

Uses Object[] for internal storage allowing it to be used for any type ... Auto-Sizes internal array structure. Stores items in an internal array of Object ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 19
Provided by: busOreg
Category:
Tags: net | data | structures

less

Transcript and Presenter's Notes

Title: Data Structures in 'NET


1
Data Structures in .NET
  • A look at Arrays, Lists, Hashtables and Beyond

2
Pop Quiz
  • What is the difference between implicitly
    implementing an interface vs. explicitly
    implementing an interface?
  • Can you instantiate an instance of an interface?

3
Reference
  • Mitchell, Scott. An Extensive Examination of
    Data Structures Using C 2.0.
    http//msdn.microsoft.com/library/default.asp?url
    /library/en-us/dnvs05/html/datastructures20_1.asp.
    January 2005

4
Array Basics
  • Contents Stored in Contiguous Memory
  • Homogeneous all elements must be of the same
    type or a derived type
  • Elements can be directly accessed using an index
    (i.e. array1 accesses the second item in the
    array)
  • All arrays are reference types in and of
    themselves, even if the type they contain is a
    value type.
  • Declared as
  • arrayType arrayName arrayTypearraySize
  • The Array type is not found in System.Collections
    like most data structures in the .NET Framework
    it is found in System.
  • Sample

5
Arrays in Memory
6
Lists
  • ArrayList
  • Stores and accesses data just like an array
  • Auto-sizes the internal array
  • Uses Object for internal storage allowing it to
    be used for any type
  • Downside Boxing of Value Types causes
    performance loss
  • Sample

7
Lists Continued
  • Queue
  • Auto-Sizes internal array structure
  • Stores items in an internal array of Object
  • Vulnerable to the same boxing flaw as an
    ArrayList
  • First-In, First-Out access
  • Sample

8
Lists Continued
  • Stack
  • Similar to the Queue
  • Last-In, First-Out
  • Sample

9
Ordinal Indexing Limitations
  • We dont always have the Index
  • Iteration using IEnumerable, IEnumerator
  • Read-only iteration and is not Thread Safe

10
IEnumerable
  • One Method IEnumerator GetEnumerator()
  • Gets the IEnumerator that allows enumeration
    through the items in the data structure
  • All of the data structures found in
    System.Collections implement IEnumerable and so
    does Array

11
IEnumerator
  • Members
  • void Reset()
  • Resets the enumerator to the initial position
    BEFORE the first item in the data structure
  • bool MoveNext()
  • Moves the enumerator to the next element in the
    collection, and returns true if successfully
    moved or false if it moved passed the last item
  • object Current
  • Property that returns a reference to the object
    at the current position in the data structure
  • You must call MoveNext after the Enumerator is
    created or after Reset is called in order to
    view Current otherwise current is undefined.
  • If Current is called after MoveNext returns false
    an exception is thrown.
  • IEnumerable IEnumerator Example

12
The foreach Statement
  • Will use IEnumerable and IEnumerator
  • Except with arrays it appears
  • for is more efficient than foreach usually
  • Example

13
Enumeration is Expensive
  • For direct access we have O(1)
  • For Enumeration we potentially have O(n)
  • Hashtable to the rescue

14
Hashtable
  • Index objects based off of a hash function
    (h(key)) and stores them in a key-value pair
    where the index h(key)
  • Scott Mitchells Employee SSN Example
  • Searching employees
  • Could create Array from 000000000 to 999999999
    and index by SSN
  • Not efficient use of space
  • key SSN, h(key) last 4 digits of key
  • Store 0000 to 9999 and save space
  • Collisions

15
Hashtable Collisions
  • Need a Hash function that minimizes collisions
  • System.Collections.Hastable uses Rehashing
  • n distinct hash functions
  • Hk(key) GetHash(key) k (1
    (((GetHash(key) gtgt 5) 1) (hashsize 1)))
    hashsize

16
Things to Note About Hashtable
  • Keys and Values are stored based on the hash
    value of the key not in the order in which they
    were added as in our Lists
  • IEnumerable returns an IDictionaryEnumerator
  • IDictionaryEnumerator.Current is type
    DictionaryEntry, not object.
  • DictionaryEntry has a Key and Value property
  • Sample

17
Assorted Data Structures
  • System.Collections contains several
    implementations of data structures for specific
    purposes
  • These are mostly based off of what we have looked
    at so far.

18
Beyond
  • .NET Framework 2.0 offers more data structures
  • Generics
Write a Comment
User Comments (0)
About PowerShow.com