Soluzione MidTerm 2005

Post on 25-Feb-2016

27 views 2 download

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 Presentation

Transcript of Soluzione MidTerm 2005

Soluzione MidTerm 2005Soluzione MidTerm 2005

Giuseppe AttardiGiuseppe Attardi

Esercizio 1Esercizio 1

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;

}}

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;

}}}}

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();}}

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 { ...... }} }}}}

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;}}

getIteratorgetIterator

IDictionaryIterator IDictionaryIterator getIterator() {getIterator() {

return new Iterator(root);return new Iterator(root);}}

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); }}

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;}}

CurrentCurrent

public string Current() {public string Current() { return curr.value;return curr.value;}}

Esercizio 2Esercizio 2

Standard call sequenceStandard call sequence

sp

fp

arg1

arg2

...

argn

return addrold fp

local m

local m-1

...

local 1

Coroutine stacksCoroutine stacks

fp

Coroutine AsavedSP

Coroutine B

return addr

transferroutine

saved regsold fp

return addr

transferroutine

saved regsold fp

sp

fp

savedSP

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?

Esercizio 3Esercizio 3

class CoroutineA : Thread {class CoroutineA : Thread { public void Run() {public void Run() { wait(); // wait(); // prepareprepare ...... corB.notify();corB.notify(); wait(); // wait(); // transfertransfer ...... }}}}

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();}}

Esercizio 4Esercizio 4

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); }} }}}}

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; }}}}

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;}}

Esercizio 6Esercizio 6

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

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

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

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

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

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

Totale eserciziTotale esercizi

105 righe di codice105 righe di codiceUna settimana di tempoUna settimana di tempo15 righe al giorno15 righe al giorno

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); }}}}