soluzione midterm 2005
DESCRIPTION
Soluzione MidTerm 2005. Giuseppe Attardi. Esercizio 1. Albero binario di string. class Tree { stringkey; stringvalue; Treeleft; Treeright; public Tree(string k, string v) { key = k; value = v; left = null; right = null; }. Albero binario generico. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/1.jpg)
Soluzione MidTerm 2005Soluzione MidTerm 2005
Giuseppe AttardiGiuseppe Attardi
![Page 2: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/2.jpg)
Esercizio 1Esercizio 1
![Page 3: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/3.jpg)
Albero binario di stringAlbero binario di string
class Tree {class Tree {stringstring key;key;stringstring value;value;TreeTree left;left;TreeTree right;right;
public Tree(string k, string v) {public Tree(string k, string v) {key = k; value = v;key = k; value = v;left = null; right = null;left = null; right = null;
}}
![Page 4: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/4.jpg)
Albero binario genericoAlbero binario generico
class Tree<K:Comparable, V> {class Tree<K:Comparable, V> {KK key;key;VV value;value;Tree<K, V> left;Tree<K, V> left;Tree<K, V> right;Tree<K, V> right;
public Tree(K k, V v) {public Tree(K k, V v) {key = k; value = v;key = k; value = v;left = null; right = null;left = null; right = null;
}}}}
![Page 5: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/5.jpg)
class Dictionaryclass Dictionaryclass Dictionary : IDictionary {class Dictionary : IDictionary {
class Tree { ... }class Tree { ... }
Tree root;Tree root;
public Dictionary() { root = null; }public Dictionary() { root = null; }
public bool insert(string k, string v);public bool insert(string k, string v);
public string get(string key);public string get(string key);
IDictionaryIterator getIterator();IDictionaryIterator getIterator();}}
![Page 6: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/6.jpg)
insertinsertpublic bool insert(string k, string v) {public bool insert(string k, string v) { if (k == null)if (k == null) return false;return false; if (root == null) {if (root == null) { root = new Tree(k, v);root = new Tree(k, v); return true;return true; }} Tree curr = root;Tree curr = root; while (true) {while (true) { int r = curr.key.CompareTo(k);int r = curr.key.CompareTo(k); if (r == 0)if (r == 0)
return false;return false; if (r < 0) {if (r < 0) { if (curr.left == null) {if (curr.left == null) { curr.left = new Tree(k, v);curr.left = new Tree(k, v); return true;return true; } else} else curr = curr.left;curr = curr.left; } else {} else { ...... }} }}}}
![Page 7: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/7.jpg)
getget
public string get(string k) {public string get(string k) { Tree curr = root;Tree curr = root; while (curr != null) {while (curr != null) { int r = curr.key.CompareTo(k);int r = curr.key.CompareTo(k); if (r == 0)if (r == 0) return curr.value;return curr.value;
if (r < 0)if (r < 0) curr = curr.left;curr = curr.left; elseelse curr = curr.right;curr = curr.right; }} return null;return null;}}
![Page 8: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/8.jpg)
getIteratorgetIterator
IDictionaryIterator IDictionaryIterator getIterator() {getIterator() {
return new Iterator(root);return new Iterator(root);}}
![Page 9: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/9.jpg)
IteratorIterator
class Iterator {class Iterator { stack<Tree> rest;stack<Tree> rest; Tree curr = null;Tree curr = null;
Iterator(Tree root) {Iterator(Tree root) { rest = new stack<Tree>;rest = new stack<Tree>; if (root != null)if (root != null) rest.push(root);rest.push(root); }}
![Page 10: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/10.jpg)
MoveNextMoveNext
public bool MoveNext() {public bool MoveNext() { if (rest.empty)if (rest.empty) return false;return false; curr = rest.pop;curr = rest.pop; if (curr.right != null)if (curr.right != null) rest.push(curr.right);rest.push(curr.right); if (curr.left != null)if (curr.left != null) rest.push(curr.left);rest.push(curr.left); return true;return true;}}
![Page 11: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/11.jpg)
CurrentCurrent
public string Current() {public string Current() { return curr.value;return curr.value;}}
![Page 12: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/12.jpg)
Esercizio 2Esercizio 2
![Page 13: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/13.jpg)
Standard call sequenceStandard call sequence
sp
fp
arg1
arg2
...
argn
return addrold fp
local m
local m-1
...
local 1
![Page 14: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/14.jpg)
Coroutine stacksCoroutine stacks
fp
Coroutine AsavedSP
Coroutine B
return addr
transferroutine
saved regsold fp
return addr
transferroutine
saved regsold fp
sp
fp
savedSP
![Page 15: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/15.jpg)
Transfer RoutineTransfer Routine
transfer(Coroutine c) {transfer(Coroutine c) {save registers on stack (no SP)save registers on stack (no SP)this.savedSP = SP;this.savedSP = SP;
SP = c.savedSP;SP = c.savedSP; restore registers (except SP)restore registers (except SP) return;return;}} Why?
![Page 16: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/16.jpg)
Esercizio 3Esercizio 3
![Page 17: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/17.jpg)
class CoroutineA : Thread {class CoroutineA : Thread { public void Run() {public void Run() { wait(); // wait(); // prepareprepare ...... corB.notify();corB.notify(); wait(); // wait(); // transfertransfer ...... }}}}
![Page 18: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/18.jpg)
public int main(string[] args) {public int main(string[] args) { CoroutineA corA = new CoroutineA();CoroutineA corA = new CoroutineA(); coroutineA.start();coroutineA.start(); CoroutineB corB = new CoroutineB();CoroutineB corB = new CoroutineB(); coroutineB.start();coroutineB.start(); corA.notify();corA.notify(); corA.join();corA.join();}}
![Page 19: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/19.jpg)
Esercizio 4Esercizio 4
![Page 20: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/20.jpg)
coroutine A {coroutine A { Stack<Tree> rest = new Stack<Tree>();Stack<Tree> rest = new Stack<Tree>(); rest.push(tree1);rest.push(tree1); while (true) {while (true) { if (rest.empty) {if (rest.empty) { end1 = true;end1 = true; transfer Main;transfer Main; return;return; }} Tree curr = rest.pop;Tree curr = rest.pop; if (curr.left == null && curr.right == null) {if (curr.left == null && curr.right == null) { leaf1 = curr.value;leaf1 = curr.value; transfer Main;transfer Main; } else {} else { if (curr.right != null)if (curr.right != null) rest.push(curr.right);rest.push(curr.right); if (curr.left != null)if (curr.left != null) rest.push(curr.left);rest.push(curr.left); }} }}}}
![Page 21: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/21.jpg)
coroutine C {coroutine C { Stack<Tree> rest = new Stack<Tree>();Stack<Tree> rest = new Stack<Tree>(); rest.push(tree2);rest.push(tree2); while (true) {while (true) { if (rest.empty) {if (rest.empty) { end2 = true;end2 = true; transfer Main;transfer Main; return;return; }} Tree curr = rest.pop;Tree curr = rest.pop; if (curr.right != null)if (curr.right != null) rest.push(curr.right);rest.push(curr.right); if (curr.left != null)if (curr.left != null) rest.push(curr.left);rest.push(curr.left); leaf2 = curr.value;leaf2 = curr.value; transfer Main;transfer Main; }}}}
![Page 22: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/22.jpg)
MainMain
end1 = end2 = false;end1 = end2 = false;while (true) {while (true) { transfer A;transfer A; transfer B;transfer B; if (end1) return end2;if (end1) return end2; if (end2) return end1;if (end2) return end1; if (leaf1 != leaf2)if (leaf1 != leaf2) return false;return false;}}
![Page 23: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/23.jpg)
Esercizio 6Esercizio 6
![Page 24: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/24.jpg)
Passaggio per referencePassaggio per reference
In Java non esiste il passaggio per In Java non esiste il passaggio per referencereference
Java does not provide call by referenceJava does not provide call by referenceJava n’avait pas call by referenceJava n’avait pas call by referenceJava hat nicht call by referenceJava hat nicht call by reference
![Page 25: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/25.jpg)
Language Specific VariationsLanguage Specific Variations
Pascal: Call by Value is the default, the Pascal: Call by Value is the default, the keyword keyword varvar denotes Call by denotes Call by ReferenceReference
Fortran: all parameters passed by Fortran: all parameters passed by ReferenceReference
Smalltalk, Lisp, Java: actual parameter Smalltalk, Lisp, Java: actual parameter is already a reference to the object, is already a reference to the object, always passed by Valuealways passed by Value
C: always passed by ValueC: always passed by Value
![Page 26: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/26.jpg)
Ada Parameter ModesAda Parameter Modes
Three parameter passing modesThree parameter passing modes inin – Passes information from the caller to the callee,
can read but not write– Call by Value
outout – Passes information from the callee to the caller,
can write but not read – Call by Result (formal parameter is copied to
actual parameter when subroutine exits) inoutinout - passes information both directions - passes information both directions
![Page 27: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/27.jpg)
C++ Parameter ModesC++ Parameter Modes
C passes pointers as addresses, must C passes pointers as addresses, must be explicitly dereferenced when usedbe explicitly dereferenced when used
C++ has notion of referencesC++ has notion of references– Parameter passing: void swap (int &a, int &b)
– Variable References: int &j = i;Function Returns: for objects that Function Returns: for objects that
don’t support copy operations, i.e. file don’t support copy operations, i.e. file buffersbuffers
![Page 28: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/28.jpg)
C# Parameter ModesC# Parameter Modes
inin – Passes information from the caller to the callee,
can read but not write– Call by Value
outout – Passes information from the callee to the caller,
can write but not read – Call by Result (formal parameter is copied to
actual parameter when subroutine exits) in outin out – Call by Value-Result – Call by Value-Result refref– Call by Reference
![Page 29: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/29.jpg)
Function ReturnsFunction Returns
Some languages restrict return typesSome languages restrict return types– Algol 60, Fortran: scalars only– Pascal, Modula: scalars or pointers only–Most imperative languages are flexible
Return statements specify a value and Return statements specify a value and also cause the immediate termination also cause the immediate termination of the subroutineof the subroutine
![Page 30: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/30.jpg)
Totale eserciziTotale esercizi
105 righe di codice105 righe di codiceUna settimana di tempoUna settimana di tempo15 righe al giorno15 righe al giorno
![Page 31: Soluzione MidTerm 2005](https://reader035.vdocuments.mx/reader035/viewer/2022062411/56816849550346895dde3170/html5/thumbnails/31.jpg)
MoveNextMoveNext
public bool MoveNext() {public bool MoveNext() { while (true) {while (true) { if (curr == null) {if (curr == null) { if (rest.empty)if (rest.empty) return false;return false; else {else { curr = rest.pop;curr = rest.pop; return true;return true; }} curr = curr.left;curr = curr.left; if (curr.right != null)if (curr.right != null) rest.push(curr.right);rest.push(curr.right); }}}}