Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC/CMPE320 - Prof. McLeod

Similar presentations


Presentation on theme: "CISC/CMPE320 - Prof. McLeod"— Presentation transcript:

1 CISC/CMPE320 - Prof. McLeod
Winter 2013 CISC/CMPE320 9/13/2018 CISC/CMPE320 Notices Teamwork: RAD will be due tonight as a document in Confluence. Assignment 2 due Friday, this week. Help documents written by Mao Yamanaka have been linked to the assignment 3 statement. Fall 2017 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

2 CISC/CMPE320 - Prof. McLeod
Today Algorithm Choices for Assn 1. Operator Overloading, Cont. Next topic: Repositories. Fall 2017 CISC/CMPE320 - Prof. McLeod

3 Assignment 1 Algorithm Choices
The “Good, Bad & Ugly” ways to do this assignment. Focus on generating and sorting unique insults. Ugly, for example: Append a new insult and re-sort entire vector every time. Add enough insults, sort, then eliminate duplicates, then generate missing insults, and repeat until entire set is complete and unique. Fall 2017 CISC/CMPE320 - Prof. McLeod

4 Assignment 1 Algorithm Choices, Cont.
You can make “non-ugly” choices, which can still be improved upon. Benchmark tests to generate 10,000 unique insults in order: Going from Bad to Good in milliseconds: 7349: Strings only (vector<string>). Sequential search followed by single coded insertion sort. 2154: Strings only. Sequential search followed by single algorithm::sort. Fall 2017 CISC/CMPE320 - Prof. McLeod

5 Assignment 1 Algorithm Choices, Cont.
You know that comparing strings is slow compared to integers, right? Since you have three columns of phrases of 50 terms each, and they are in order (or would be easy to sort since of only size 50), then generate a “hash” code to represent an insult: hash = rand(50) * rand(50) * rand(50) Then sort and search integers instead. Fall 2017 CISC/CMPE320 - Prof. McLeod

6 Assignment 1 Algorithm Choices, Cont.
Benchmarks in milliseconds, continued: 1165: Integer "hash". Inserting ints, keeping vector in order. Used algorithm::binary_search each pass. 852: Integer "hash". Sequential search followed by single algorithm::sort using vector<int>. Generated insult strings with single pass. Fall 2017 CISC/CMPE320 - Prof. McLeod

7 Assignment 1 Algorithm Choices, Cont.
29 milliseconds!: Use a bool[50][50][50] array. Initialize all entries to false and then initialize 10,000 random positions to true. Rebuild the vector<string> of insults in a triply nested loop, which will be in order since the phrases are already in order. No sorting or searching! Could also use a bitset object, which would be more memory efficient. Runs about the same time. Fall 2017 CISC/CMPE320 - Prof. McLeod

8 CISC/CMPE320 - Prof. McLeod
bool boolArray[sourceSize][sourceSize][sourceSize]; for (num1 = 0; num1 < sourceSize; num1++) for (num2 = 0; num2 < sourceSize; num2++) for (num3 = 0; num3 < sourceSize; num3++) boolArray[num1][num2][num3] = false; while (count < numInsults) { num1 = randInt(sourceSize); num2 = randInt(sourceSize); num3 = randInt(sourceSize); if (!boolArray[num1][num2][num3]) { boolArray[num1][num2][num3] = true; count++; } // end if } // end while Fall 2017 CISC/CMPE320 - Prof. McLeod

9 CISC/CMPE320 - Prof. McLeod
count = 0; for (num1 = 0; num1 < sourceSize && count < numInsults; num1++) for (num2 = 0; num2 < sourceSize && count < numInsults; num2++) for (num3 = 0; num3 < sourceSize && count < numInsults; num3++) if (boolArray[num1][num2][num3]) { insult = "Thou " + part1.at(num1) + " " + part2.at(num2) + " " + part3.at(num3) + "!"; insults.push_back(insult); count++; } // end if Fall 2017 CISC/CMPE320 - Prof. McLeod

10 Assignment 1 Algorithm Choices, Cont.
It should also be possible to use a set collection from the STL. I have not tested this option, but it supposed to perform well. Fall 2017 CISC/CMPE320 - Prof. McLeod

11 Operator Overloading, So Far…
Choose between overloading as a member function, non-member function or a non-member friend function. Discussed overloading the binary arithmetic and boolean operators. Looked at >> and << as well as assignment and the increment and decrement operators. What’s left? Of interest: conversion operators, [ ], ( ), explicit constructors, inline functions. Fall 2017 CISC/CMPE320 - Prof. McLeod

12 Overloading Conversion Operators
You don’t need to write these for assignment 2. Consider that your Fraction class has top for the numerator and bottom for the denominator. To overload a cast to type double: Fraction::operator double() const { return static_cast<double>(top) / bottom; } If test is a Fraction object, this allows stuff like: double aNum = static_cast<double>(test); Fall 2017 CISC/CMPE320 - Prof. McLeod

13 Overloading Conversion Operators, Cont.
Similarly for int. Note the lack of return type. But if you have a conversion operator and an applicable conversion constructor this leads to an ambiguity: Which operator should be used in a mixed type expression? See MyComplexAmbiguous Fall 2017 CISC/CMPE320 - Prof. McLeod

14 Overloading Conversion Operators, Cont.
With just the constructor you can do things like: int aVal = 10; Fraction test = static_cast<Fraction>(aVal); Same as: Fraction test = Fraction(aVal); But you can’t do: aVal = static_cast<int>(test); Fall 2017 CISC/CMPE320 - Prof. McLeod

15 Overloading Conversion Operators, Cont.
So, what’s to do? If you ditch the one number constructor, then you give yourself a lot more work when you need to overload operators for mixed expressions. Might be easiest to keep the constructor and compromise: Write member functions called intVal() and doubleVal() instead? Or declare the constructor explicit? Fall 2017 CISC/CMPE320 - Prof. McLeod

16 CISC/CMPE320 - Prof. McLeod
The explicit Keyword If you want a constructor to just be a constructor and not for use as a rule for implicit type conversions, declare it with explicit. For example: class Fraction { // etc. explicit Fraction(int); }; Fall 2017 CISC/CMPE320 - Prof. McLeod

17 The explicit Keyword, Cont.
If you use explicit: From the MyComplexAmbiguous demo, what is the result of a mixed type expression with a number on one side and a MyComplex instance on the other? Fall 2017 CISC/CMPE320 - Prof. McLeod

18 Another Caution on the Use of Overloaded Conversion Operators
Suppose aComplex is 3 + 4i Do you want aComplex == 5 to be true? Fall 2017 CISC/CMPE320 - Prof. McLeod

19 Overloading the Subscript Operator: [ ]
Appropriate for: A data structure requiring access using an index. A data structure that wants to make sure index values are legal. See the SafeArray class. Note the two versions of operator[] used. Note the use of a friend non-member function. Note the use of a destructor (getting ahead of myself…) Fall 2017 CISC/CMPE320 - Prof. McLeod

20 Overloading the Function Call Operator: ( )
Allows an object to behave as if it is a function. Called a function object. The only operator where the number of arguments is not fixed or limited to one or two. See the RandomInt class. Used in the STL. Fall 2017 CISC/CMPE320 - Prof. McLeod

21 Aside - inline Functions
Used in the RandomInt class. Used when the function body is short: ≤ 3 assignment statements. one conditional. one return. An inline function is not placed on the activation record stack, but is expanded in place and so is executed faster. Must be in the declaration file. The downside is that a copy is made every time the function is invoked – so it should be short. Fall 2017 CISC/CMPE320 - Prof. McLeod

22 Seldom Overloaded Operators
Address-of, & boolean logic, &&, || The comma, , Member access through pointer, ->* Not really useful… Overloading new, new[], delete and delete[] may be a bit more useful, but they are difficult to do properly and seldom really worth doing. Fall 2017 CISC/CMPE320 - Prof. McLeod

23 CISC/CMPE320 - Prof. McLeod
Final Cautions Beginning C++ programmers can get enthused about operator overloading! Make sure that the overloadings you code make sense to anyone reading your code. If they do not, write a member function that does the same thing instead. Fall 2017 CISC/CMPE320 - Prof. McLeod

24 CISC/CMPE320 - Prof. McLeod
Final Cautions, Cont. Don’t forget that you cannot change operator precedence. Suppose we overloaded the operator ^ for our complex number class to give us exponentiation: a = -1 + e ^ (i * pi) Normally the exponentiation would take place first in a normal mathematical expression. But ^ actually has a lower precedence than arithmetic operators since it is a boolean operator. What you actually get is: a = (-1 + e) ^ ((i * pi)) Fall 2017 CISC/CMPE320 - Prof. McLeod

25 CISC/CMPE320 - Prof. McLeod
Final Cautions, Cont. In this case, it would be much better to forget the overloading and just use a member function instead: a = -1 + pow(e, (i * pi)) Fall 2017 CISC/CMPE320 - Prof. McLeod


Download ppt "CISC/CMPE320 - Prof. McLeod"

Similar presentations


Ads by Google