Presentation is loading. Please wait.

Presentation is loading. Please wait.

Containers CMPS 2143. Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.

Similar presentations


Presentation on theme: "Containers CMPS 2143. Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary."— Presentation transcript:

1 Containers CMPS 2143

2 Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary trees, sets, dictionaries, etc. So common expect ideal for development as reusable components

3 Containers in dynamically typed languages: Easier in dynamically typed languages to produce reusable containers Often those languages have them built-in Easier because value retains knowledge of its type (self referential) Also dynamic languages permit values of different classes heterogeneous collections

4 Look at several collections abstract Collection can add, remove, get size, check if empty, … Set - inherits/overrides/adds Collection methods a set is an unordered Collection of unique elements Dictionary – inherits/overrides/adds methods a dictionary is a Set where each element of the set is an Association representing a single key/value pair Bag – inherits/overrides/adds Collection methods a bag is similar to a set, but values do not have to be unique Bag will also use composition

5 inheritance as mechanism for specialization (Set and Bag) deferred methods (add and remove in Collection) inheritance as mechanism for construction (Dictionary)

6 composition (Bag has a dictionary) key is the element in the bag value is the number of times element is in the bag

7 Containers in Statically Typed Languages Static typing can interfere with software reuse 3 solutions 1.Use principle of substitution to store values in a container and combine with down casting (reverse polymorphism) when values removed from container 2.Use principle of substitution but avoid down casting through use of overriding 3.Use generics or templates

8 class List { public: //methods void insert (int newVal); private: class Node { //Node is an inner class public: // inside class List int value; // so okay for public data Node * next; }; … Node * head; }; C++ Linked list of integers

9 Concerns Question of reusability suppose we now want a list of doubles Problem: conventional static languages are too strongly typed int data type is intrinsic part of definition But OOP languages can utilize principle of substitution and generics

10 Substitution and downcasting class List { public: //methods void insert (Object newVal); Object removeAt (int index); private: class Node {//inner class public: Object value; Node * next; }; … Node * head; };

11 Substitution and downcasting List catList; Cat aCat (Leo); Dog aDog (Fido); catList.insert(aCat); Cat bCat = (Cat) catList.removeAt (1); //downcasting from //Object to Cat catList.insert(aDog); //aDog is an Object, no compiler error Cat bCat = (Cat) catList.removeAt (1); //OOPS! NOW error!

12 Notes: Can only have heterogenous collections if there is a super Object in the language (as in Java). Even so cannot include primitive types (int, double, char, … do not inherit from Object) solution is to have a wrapper class class Double to hold a double value with get/set methods Java has some built-in

13 Using substitution and overriding A cast expression is often considered to be NOT truly object-oriented Can avoid with use of substitution combined with method overriding only possible if original developer knows how an object will be used (even if they dont know the type of value stored in container) Used in a few restricted situations

14 C#, Java example Events handled by Listener objects that are attached to a container like a Window object. When event occurs in a given window, all registered listeners are notified mouse movement, mouse pressed, key pressed, text box changed, etc. Java also provides adapters that implement interfaces Programmer defines a class to implement interface and overrides the methods they want

15 Parameterized classes using principle of substitution in previous 2 techniques is only suitable if there is a parent class that can be used as basis if a language does not have a single root that is ancestor to all classes, then… use generics.

16 Templates #pragma once template class List { public: //methods void insert (ItemType newVal); ItemType removeAt (int index); private: class Node { public: ItemType value; Node * next; }; … Node * head; }; #include List.cpp

17 Templates To use List intList; List doubleList; In this way, homogeneous lists can be created Book says you cant have heterogeneous lists, but as long as you use references… List shapeList;

18 Element traversal on Containers Provides method that allow you to loop over the elements in the container bool hasMoreElements(); //return true iff there are more elements to process ItemType nextElement(); //return next element void reset(); //start at first element

19 Iterator methods template class List { public: void insert (ItemType newVal); ItemType removeAt (int index); void resetCursor(); void advanceCursor(); bool cursorAtEnd(); ItemType nextElement (); private: class Node { public: ItemType value; Node * next; }; … Node * head; Node * cursor; //for iteration };

20 #include List.h #include using namespace std; void List:: resetCursor(){cursor = head;} void List:: advanceCursor() { if (cursor != null) cursor = cursor->next; else cout << Error: Cannot advance cursor, cursor at end \n; } bool List:: cursorAtEnd(){return cursor == null}; ItemType List:: nextElement (){ if (cursor != null) return cursor->value; else cout << Error: Cannot advance cursor, cursor at end \n; }

21 #include List.h #include using namespace std; template void loadList (List & list); template void sumList(List & list, ItemType & sum); void main() { List intList; int sum = 0; loadList (intList); sumList (intList, sum); cout << Sum of items in list is << sum << endl; system(pause); }

22 template void loadList (List & list) { list.insert(1); list.insert(2); list.insert(5); } template void sumList(List & list, ItemType & sum) { list.resetCursor(); while (!list.cursorAtEnd()) { i = list.nextElement(); sum += I; list.advanceCursor(); }

23 #include List.h #include ComplexNumber.h #include using namespace std; template void loadList (List & list); template void sumList(List & list, ItemType & sum); void main() { List intList; ComplexNumber sum; //default constructor sets sum = 0+0i loadList (intList); sumList (intList, sum); cout << Sum of items in list is << sum << endl; system(pause); }

24 Visual Studio STL containers http://msdn.microsoft.com/en- us/library/1fe2x6kt(v=vs.110).aspx http://msdn.microsoft.com/en- us/library/1fe2x6kt(v=vs.110).aspx

25 Study pg. 387: 1, 3, 5


Download ppt "Containers CMPS 2143. Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary."

Similar presentations


Ads by Google