Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; ----- dic= new XXX(); application class: Dictionary SortedDictionary ----

Similar presentations


Presentation on theme: "Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; ----- dic= new XXX(); application class: Dictionary SortedDictionary ----"— Presentation transcript:

1 interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; ----- dic= new XXX(); application class: Dictionary SortedDictionary ---- ADT Data structure and algorithms Choose and use an adt, e.g. IDictionary Choose and use a data structure, e.g. Dictionary Know about Read and write (use) specifications Data Structures and Collections FEN 2014UCN Teknologi/act2learn1

2 To libraries System.Collections.Generic System.Collections (deprecated) FEN 2014UCN Teknologi/act2learn2

3 .NET 2: System.Collections.Generics ICollection IList LinkedList IDictionary List Dictionary SortedDictionary Indexable Array-based Balanced search tree Hashtabel (key, value) -pair FEN 2014UCN Teknologi/act2learn3 Also as ISet, (HashSet, SortedSet

4 FEN 2014UCN Teknologi/act2learn4 Pros and Cons Array lists Array-based list (List ): –Static memory allocation (memory overhead). –Position based - duplicates allowed. –Direct access on index (O(1)). –Add (at the back): O(1), but may (non-deterministically) be O(n) (relocation). –Insert and remove may require moving a lot of elements (O(n)). –If sorted, binary search can be applied (O(log n).

5 FEN 2014UCN Teknologi/act2learn5 Pros and Cons – Linked lists Linked List (LinkedList ): –Dynamic memory allocation (non memory overhead). –Position based - duplicates allowed. –Access on index is slow (O(n)). –Add in front or at the back is fast O(1). –Add before or after current element is fast O(1) –Insert and remove may require searching O(n). –Binary search cannot be applied (will be extremely slow). Demo

6 FEN 2014UCN Teknologi/act2learn6 Pros and Cons – Hash tables Hash Tables (HashSet, Dictionary ): –Static memory allocation (memory overhead, depending on load factor). –Value-based, no duplicates (duplicated keys). –Search is fast in average (O(1) – depends only on load factor not size). May be slow in worst case (O(n)). –Insert and remove is fast in average (O(1) – depends only on load factor not size). May be slow in worst case (O(n)). –Insert and remove may non-deterministically be very slow (when re-allocation and re- hashing is done) Demo

7 When Hashing Is Inefficient Traversing in key order (O(n log n)). Find smallest/largest key. Range-search (Find all keys between high and low). Searching on something else than the designated primary key. FEN 2014UCN Teknologi/act2learn7

8 FEN 2014UCN Teknologi/act2learn8 Pros and Cons – Search trees Balanced search trees (SortedSet, SortedDictionary ): –Dynamic memory allocation (no memory overhead). –Value-based, no duplicates (duplicated keys). –Search, insert and remove is fast in average and worst case (O(log n), if balanced). –Most libraries use a balanced version, for instance Red-Black Trees that guarantees O(log n) search, insert and delete. –Easy to traverse in key-order (O(n)) –Searching on something else than the designated primary key (O(n)) Demo

9 Demo and exercise Some methods on ICollection: –demos\moreCollectionMethodsdemos\moreCollectionMethods Exercise: –Run the code with different implementations of ICollection. Note what happens to duplicates. –Do the union and the set difference of the two collections. FEN 2014UCN Teknologi/act2learn9

10 Collections Library System.Collections Data structures in.NET are normally called Collections Are found in namespace System.Collections Compiled into mscorlib.dll assembly Uses object and polymorphism for generic containers. Deprecated! Classes: –Array –ArrayList –Hashtable –Stack –Queue FEN 2014UCN Teknologi/act2learn10 WARNING: Deprecated!

11 Collection Interfaces System.Collections implements a range of different interfaces in order to provide standard usage of different containers –Classes that implements the same interface provides the same services –Makes it easier to learn and to use the library –Makes it possible to write generic code towards the interface Interfaces: –ICollection –IEnumerable –IEnumerator –IList –IComparer –IComparable FEN 2014UCN Teknologi/act2learn11 Note: Not exactly the same interfaces as the generic ones.


Download ppt "Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; ----- dic= new XXX(); application class: Dictionary SortedDictionary ----"

Similar presentations


Ads by Google