Containers CMPS 2143. Reusable containers Simple data structures in almost all nontrivial programs...

25
Containers CMPS 2143

Transcript of Containers CMPS 2143. Reusable containers Simple data structures in almost all nontrivial programs...

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

ContainersCMPS 2143

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

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

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

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

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

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

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

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

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

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

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

Containers in Statically Typed Languages

•Static typing can interfere with software reuse

•3 solutions1. 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

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

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

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

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

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

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

};

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

Substitution and downcastingList 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!

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

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

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

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 don’t know the type of value stored in container)

•Used in a few restricted situations

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

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

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

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.

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

Templates#pragma oncetemplate <class ItemType> 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”

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

Templates

•To useList <int> intList;List <double> doubleList;

• In this way, homogeneous lists can be created

•Book says you can’t have heterogeneous lists, but as long as you use references…

List <Shape *> shapeList;

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

Element traversal on Containers

•Provides method that allow you to loop over the elements in the containerbool hasMoreElements();//return true iff there are more elements to process

ItemType nextElement();//return next element

void reset();//start at first element

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

Iterator methodstemplate <class ItemType> 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};

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

#include “List.h”#include <iostream>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”;}

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

#include “List.h”#include <iostream>using namespace std;

template <class ItemType>void loadList (List <ItemType> & list);

template <class ItemType>void sumList(List <ItemType> & list, ItemType & sum);

void main() { List <int> intList; int sum = 0; loadList (intList); sumList (intList, sum);

cout << “Sum of items in list is “ << sum << endl; system(“pause”);}

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

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

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

#include “List.h”#include “ComplexNumber.h”#include <iostream>using namespace std;

template <class ItemType>void loadList (List <ItemType> & list);

template <class ItemType>void sumList(List <ItemType> & list, ItemType & sum);

void main() { List <ComlexNumber> 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”);}

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

Study

•pg. 387: 1, 3, 5