From 9809567b8d8972d1ea11ece089f05db311a3e20a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 16 Mar 2020 11:02:14 +0100 Subject: [PATCH 001/202] TODO aggiornati --- 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java | 1 + 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java | 2 +- 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java | 7 ++++--- 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedSet.java | 1 + 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java index d99f9e8..b615969 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java @@ -1,5 +1,6 @@ package it.unive.dais.po2.myjdk; +// TODO: togliere il prefisso My da tutti i nomi dei tipi e mostrare la differenza tra tipi omonimi in package diversi public class MyArrayList implements MyList { private Object[] a; diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java index 1364d87..464361f 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java @@ -19,10 +19,10 @@ public void put(K k, V v) { while (it.hasNext()) { Pair p = it.next(); if (p.first.equals(k)) { + // TODO: fare la stessa cosa ma con le coppie immutabili p.second = v; return; } - // TODO: fare la stessa cosa con le coppie immutabili } m.add(new Pair<>(k, v)); } diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java index 8ca6cc1..a378c66 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java @@ -2,6 +2,7 @@ import org.jetbrains.annotations.Nullable; +// TODO: sistemare le possibili eccezioni NullPointerException public class MyLinkedList implements MyList { @Override @@ -63,7 +64,7 @@ public void add(int i, T x) { int r = 0; Node n = head; for (; n.next != null && r < i; ++r); - n.next = new Node(x, n.next); // TODO: da testare + n.next = new Node(x, n.next); } @@ -73,14 +74,14 @@ public boolean remove(int i) { Node n = head; for (; n.next != null && r < i - 1; ++r); if (n.next != null) { - n.next = n.next.next; // TODO: da testare + n.next = n.next.next; } return true; } @Override public boolean remove(T x) { - // TODO: da implementare + // TODO: da fare per casa throw new RuntimeException("not implemented"); } diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedSet.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedSet.java index 61bf8c8..daa6da0 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedSet.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedSet.java @@ -1,5 +1,6 @@ package it.unive.dais.po2.myjdk; +// TODO: LinkedSet non è un bel nome, non si capisce: meglio trovarne un'altro public class MyLinkedSet implements MySet { protected MyLinkedList l; From 57158ffa6bb5fff3766b16db62c80c0029770735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 16 Mar 2020 11:02:54 +0100 Subject: [PATCH 002/202] Update Lezioni.iml --- 2020/Lezioni/Lezioni.iml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/2020/Lezioni/Lezioni.iml b/2020/Lezioni/Lezioni.iml index 1a7f0f7..5c0ebf2 100644 --- a/2020/Lezioni/Lezioni.iml +++ b/2020/Lezioni/Lezioni.iml @@ -16,5 +16,14 @@ + + + + + + + + + \ No newline at end of file From 6fc72d77b7009dd06a52313c1c2739ea9ab4151e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 16 Mar 2020 15:47:58 +0100 Subject: [PATCH 003/202] Update MyLinkedList.java --- 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java | 1 + 1 file changed, 1 insertion(+) diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java index 6c0d124..ad59373 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java @@ -11,6 +11,7 @@ public MyIterator iterator() { return null; } + // TODO: come esercizio provare a rendere questa classe statica in modo che abbia il suo generic; e poi modificare MyLinkedList opportunamente protected class Node { public T data; public Node next; From 1adfd09c372f917fa5dea1138310c2a4d0681160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 17 Mar 2020 15:25:39 +0100 Subject: [PATCH 004/202] Lezione 2 --- .../Lezioni/META-INF/Lezioni.kotlin_module | Bin 16 -> 16 bytes .../dais/po2/myjdk/MyAbstractCollection.java | 9 +++++ .../it/unive/dais/po2/myjdk/MyArrayList.java | 9 +++-- .../it/unive/dais/po2/myjdk/MyCollection.java | 5 +-- .../it/unive/dais/po2/myjdk/MyHashMap.java | 25 ++++--------- .../unive/dais/po2/myjdk/MyIdentityMap.java | 34 ++++++++++++++++++ .../it/unive/dais/po2/myjdk/MyLinkedList.java | 2 +- .../src/it/unive/dais/po2/myjdk/MyMap.java | 1 + .../dais/po2/myjdk/OutOfBoundsException.java | 4 ++- .../src/it/unive/dais/po2/myjdk/TestList.java | 18 ++++++++++ .../src/it/unive/dais/po2/myjdk/TestMap.java | 23 ++++++++++++ 11 files changed, 106 insertions(+), 24 deletions(-) create mode 100644 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyAbstractCollection.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/myjdk/TestList.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java diff --git a/2020/Lezioni/out/production/Lezioni/META-INF/Lezioni.kotlin_module b/2020/Lezioni/out/production/Lezioni/META-INF/Lezioni.kotlin_module index 8fb60192d378759239a3ecbf60eac8c8de446e9c..a49347afef10a9b5f95305e1058ba36adec7d6dd 100644 GIT binary patch literal 16 RcmZQzU|?ooU|@t|0RRA102TlM literal 16 RcmZQzU|?ooU|@t|UH|}6022TJ diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyAbstractCollection.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyAbstractCollection.java new file mode 100644 index 0000000..9c00900 --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyAbstractCollection.java @@ -0,0 +1,9 @@ +package it.unive.dais.po2.myjdk; + +public abstract class MyAbstractCollection implements MyCollection { + + @Override + public boolean isEmpty() { + return size() == 0; + } +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java index b615969..32192ac 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java @@ -1,7 +1,7 @@ package it.unive.dais.po2.myjdk; // TODO: togliere il prefisso My da tutti i nomi dei tipi e mostrare la differenza tra tipi omonimi in package diversi -public class MyArrayList implements MyList { +public class MyArrayList extends MyAbstractCollection implements MyList { private Object[] a; private int actualSize; @@ -11,9 +11,12 @@ public MyArrayList() { this.actualSize = 0; } + + @Override public T get(int i) throws OutOfBoundsException { - if (i >= actualSize) throw new OutOfBoundsException(); + if (i >= actualSize) + throw new OutOfBoundsException("get: invalid position " + i); //noinspection unchecked return (T) a[i]; } @@ -65,6 +68,8 @@ public void clear() { this.actualSize = 0; } + + @Override public MyIterator iterator() { return new MyIterator<>() { diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java index 069871e..0687b6e 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java @@ -8,7 +8,8 @@ public interface MyCollection extends MyIterable { boolean remove(T x); void clear(); - default boolean isEmpty() { + /*default boolean isEmpty() { return size() == 0; - } + }*/ + boolean isEmpty(); } diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java index 464361f..b594fea 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java @@ -1,29 +1,18 @@ package it.unive.dais.po2.myjdk; public class MyHashMap implements MyMap { - private MyArrayList> m; - @Override public V get(K k) throws NotFoundException { - MyIterator> it = m.iterator(); - while (it.hasNext()) { - Pair p = it.next(); - if (p.first.equals(k)) return p.second; - } - throw new NotFoundException(); + return null; } @Override public void put(K k, V v) { - MyIterator> it = m.iterator(); - while (it.hasNext()) { - Pair p = it.next(); - if (p.first.equals(k)) { - // TODO: fare la stessa cosa ma con le coppie immutabili - p.second = v; - return; - } - } - m.add(new Pair<>(k, v)); + + } + + @Override + public void clear() { + } } diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java new file mode 100644 index 0000000..07a394d --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java @@ -0,0 +1,34 @@ +package it.unive.dais.po2.myjdk; + +public class MyIdentityMap implements MyMap { + private MyArrayList> m; + + @Override + public V get(K k) throws NotFoundException { + MyIterator> it = m.iterator(); + while (it.hasNext()) { + Pair p = it.next(); + if (p.first.equals(k)) return p.second; + } + throw new NotFoundException(); + } + + @Override + public void put(K k, V v) { + MyIterator> it = m.iterator(); + while (it.hasNext()) { + Pair p = it.next(); + if (p.first.equals(k)) { + // TODO: fare la stessa cosa ma con le coppie immutabili + p.second = v; + return; + } + } + m.add(new Pair<>(k, v)); + } + + @Override + public void clear() { + m = new MyArrayList<>(); + } +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java index ad59373..6dcf2d1 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java @@ -57,7 +57,7 @@ public void clear() { public T get(int pos) throws OutOfBoundsException { Node n = head; for (; pos > 0; --pos) - if ((n = head.next) == null) throw new OutOfBoundsException(); + if ((n = head.next) == null) throw new OutOfBoundsException("get: invalid position " + pos); return n.data; } diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyMap.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyMap.java index 52abf48..5dd0d4d 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyMap.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyMap.java @@ -3,4 +3,5 @@ public interface MyMap { V get(K k) throws NotFoundException; void put(K k, V v); + void clear(); } diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/OutOfBoundsException.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/OutOfBoundsException.java index 00c0b9d..46968d8 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/OutOfBoundsException.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/OutOfBoundsException.java @@ -1,5 +1,7 @@ package it.unive.dais.po2.myjdk; public class OutOfBoundsException extends Exception { - + public OutOfBoundsException(String message) { + super(message); + } } diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestList.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestList.java new file mode 100644 index 0000000..cc58492 --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestList.java @@ -0,0 +1,18 @@ +package it.unive.dais.po2.myjdk; + +public class TestList { + public static void main(String[] args) { + try { + MyList a = new MyArrayList<>(); + a.add(23); + a.add(11); + + int n = a.get(1); + System.out.println("elemento in posizione 1: " + n); + + } catch (OutOfBoundsException e) { + System.out.println("eccezione: " + e.getMessage()); + } + + } +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java new file mode 100644 index 0000000..cf8e57c --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java @@ -0,0 +1,23 @@ +package it.unive.dais.po2.myjdk; + +public class TestMap { + + public static void main(String[] args) { + try { + + MyMap m = new MyHashMap<>(); + m.put("alvise", 42); + m.put("francesco", 12); + m.put("gianni", 56); + m.put("pippo", 78); + + int eta_di_gianni = m.get("gianni"); + System.out.println("risultato: " + eta_di_gianni); + + m.clear(); + + } catch (NotFoundException e) { + e.printStackTrace(); + } + } +} From d1446db89bfe4bb132b10749a45b701eecf93fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 17 Mar 2020 17:40:07 +0100 Subject: [PATCH 005/202] Lezione 2 --- .../dais/po2/myjdk/MyAbstractCollection.java | 9 --- .../it/unive/dais/po2/myjdk/MyArrayList.java | 3 +- .../it/unive/dais/po2/myjdk/MyCollection.java | 6 +- .../it/unive/dais/po2/other/CallbackTest.java | 51 +++++++++++++ .../it/unive/dais/po2/other/IteratorTest.java | 27 ++++++- .../it/unive/dais/po2/other/RevArrayList.java | 71 +++++++++++++++++++ .../it/unive/dais/po2/other/RevIterator.java | 25 +++++++ .../src/it/unive/dais/po2/other/RevTest.java | 19 +++++ 8 files changed, 196 insertions(+), 15 deletions(-) delete mode 100644 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyAbstractCollection.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/other/CallbackTest.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/other/RevIterator.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/other/RevTest.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyAbstractCollection.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyAbstractCollection.java deleted file mode 100644 index 9c00900..0000000 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyAbstractCollection.java +++ /dev/null @@ -1,9 +0,0 @@ -package it.unive.dais.po2.myjdk; - -public abstract class MyAbstractCollection implements MyCollection { - - @Override - public boolean isEmpty() { - return size() == 0; - } -} diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java index 32192ac..4bb83e4 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java @@ -1,7 +1,7 @@ package it.unive.dais.po2.myjdk; // TODO: togliere il prefisso My da tutti i nomi dei tipi e mostrare la differenza tra tipi omonimi in package diversi -public class MyArrayList extends MyAbstractCollection implements MyList { +public class MyArrayList implements MyList { private Object[] a; private int actualSize; @@ -12,7 +12,6 @@ public MyArrayList() { } - @Override public T get(int i) throws OutOfBoundsException { if (i >= actualSize) diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java index 0687b6e..5f5936d 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java @@ -8,8 +8,8 @@ public interface MyCollection extends MyIterable { boolean remove(T x); void clear(); - /*default boolean isEmpty() { + default boolean isEmpty() { return size() == 0; - }*/ - boolean isEmpty(); + } + } diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/CallbackTest.java b/2020/Lezioni/src/it/unive/dais/po2/other/CallbackTest.java new file mode 100644 index 0000000..a31d672 --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/other/CallbackTest.java @@ -0,0 +1,51 @@ +package it.unive.dais.po2.other; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.function.Function; + +public class CallbackTest { + + + + public static Collection map(Collection l, Function f) { + Collection c = new ArrayList<>(); + for (A a : l) { + B b = f.apply(a); + c.add(b); + } + return c; + } + + public static void print(Collection c) { + map(c, new Function() { + @Override + public Void apply(X x) { + System.out.println(x); + return null; + } + }); + } + + public static void main(String[] args) { + + List l = new ArrayList<>(); + for (int i = 0; i < 10; ++i) { + l.add(i); + } + + print(l); + + Collection r = map(l, new Function() { + @Override + public Integer apply(Integer x) { + return x + 1; + } + }); + + print(l); + + } +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/IteratorTest.java b/2020/Lezioni/src/it/unive/dais/po2/other/IteratorTest.java index 919b0b2..27f06c7 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/other/IteratorTest.java +++ b/2020/Lezioni/src/it/unive/dais/po2/other/IteratorTest.java @@ -19,7 +19,7 @@ public static void sum(Iterable a) { System.out.println(r); } - public static void main(String[] args) { + public static void main2(String[] args) { ArrayList a = new ArrayList<>(); populate(a); @@ -64,4 +64,29 @@ public void populate() { } } + + + + + public static void main(String[] args) { + + List l = new ArrayList<>(); + for (int i = 0; i < 10; ++i) { + l.add(i); + } + + + Iterator it = l.iterator(); + while (it.hasNext()) { + int n = it.next(); + System.out.println(n); + } + + + for (int n : l) { + System.out.println(n); + } + + } + } diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java b/2020/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java new file mode 100644 index 0000000..9f9093d --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java @@ -0,0 +1,71 @@ +package it.unive.dais.po2.other; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class RevArrayList extends ArrayList { + public RevArrayList() { + super(); + } + + public RevArrayList(int cap) { + super(cap); + } + + public static class RevIterator__static implements Iterator { + private List l; + private int pos; + + public RevIterator__static(List l) { + this.l = l; + this.pos = l.size() - 1; + } + + @Override + public boolean hasNext() { + return pos >= 0; + } + + @Override + public T next() { + return l.get(pos--); + } + } + + private class RevIterator__nonstatic implements Iterator { + + private int pos = RevArrayList.this.size() - 1; + + @Override + public boolean hasNext() { + return pos >= 0; + } + + @Override + public T next() { + return RevArrayList.this.get(pos--); + } + } + + @Override + public Iterator iterator() { + //return new RevIterator(this); // classe globale + //return new RevIterator__static(this); // classe nested STATICA + //return new RevIterator__nonstatic(); // classe nested NON STATIC + + return new Iterator() { // anonymous class + private int pos = size() - 1; + + @Override + public boolean hasNext() { + return pos >= 0; + } + + @Override + public T next() { + return get(pos--); + } + }; + } +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/RevIterator.java b/2020/Lezioni/src/it/unive/dais/po2/other/RevIterator.java new file mode 100644 index 0000000..e45cfeb --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/other/RevIterator.java @@ -0,0 +1,25 @@ +package it.unive.dais.po2.other; + +import java.util.Iterator; +import java.util.List; + +public class RevIterator implements Iterator { + private List l; + private int pos; + + public RevIterator(List l) { + this.l = l; + this.pos = l.size() - 1; + } + + @Override + public boolean hasNext() { + return pos >= 0; + } + + @Override + public T next() { + return l.get(pos--); + } +} + diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/RevTest.java b/2020/Lezioni/src/it/unive/dais/po2/other/RevTest.java new file mode 100644 index 0000000..1949c0f --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/other/RevTest.java @@ -0,0 +1,19 @@ +package it.unive.dais.po2.other; + +import java.util.Iterator; + +public class RevTest { + public static void main(String[] args) { + + RevArrayList l = new RevArrayList<>(); + l.add(1); + l.add(2); + l.add(3); + + Iterator it = l.iterator(); + while (it.hasNext()) { + int n = it.next(); + System.out.println(n); + } + } +} From ed28d4349123fccb088cf0624af263f247bfc5ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 17 Mar 2020 17:49:39 +0100 Subject: [PATCH 006/202] Lezione 2 --- .../src/it/unive/dais/po2/myjdk/MyIdentityMap.java | 11 +++++++---- 2020/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java | 2 +- .../other/{CallbackTest.java => FunctionalTest.java} | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) rename 2020/Lezioni/src/it/unive/dais/po2/other/{CallbackTest.java => FunctionalTest.java} (97%) diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java index 07a394d..3bf2765 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java @@ -1,11 +1,14 @@ package it.unive.dais.po2.myjdk; +import java.util.ArrayList; +import java.util.Iterator; + public class MyIdentityMap implements MyMap { - private MyArrayList> m; + private ArrayList> m; @Override public V get(K k) throws NotFoundException { - MyIterator> it = m.iterator(); + Iterator> it = m.iterator(); while (it.hasNext()) { Pair p = it.next(); if (p.first.equals(k)) return p.second; @@ -15,7 +18,7 @@ public V get(K k) throws NotFoundException { @Override public void put(K k, V v) { - MyIterator> it = m.iterator(); + Iterator> it = m.iterator(); while (it.hasNext()) { Pair p = it.next(); if (p.first.equals(k)) { @@ -29,6 +32,6 @@ public void put(K k, V v) { @Override public void clear() { - m = new MyArrayList<>(); + m = new ArrayList<>(); } } diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java index cf8e57c..02a93d4 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java @@ -5,7 +5,7 @@ public class TestMap { public static void main(String[] args) { try { - MyMap m = new MyHashMap<>(); + MyMap m = new MyIdentityMap<>(); m.put("alvise", 42); m.put("francesco", 12); m.put("gianni", 56); diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/CallbackTest.java b/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java similarity index 97% rename from 2020/Lezioni/src/it/unive/dais/po2/other/CallbackTest.java rename to 2020/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java index a31d672..fa85b83 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/other/CallbackTest.java +++ b/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java @@ -6,7 +6,7 @@ import java.util.List; import java.util.function.Function; -public class CallbackTest { +public class FunctionalTest { From 8c001d546f282830993184815448fecd0fd6bf2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 18 Mar 2020 17:56:10 +0100 Subject: [PATCH 007/202] .idea committed --- .gitignore | 4 +- .../.idea/codeStyles/codeStyleConfig.xml | 5 + 2020/Lezioni/.idea/description.html | 1 + .../inspectionProfiles/Project_Default.xml | 20 ++ 2020/Lezioni/.idea/misc.xml | 12 ++ 2020/Lezioni/.idea/modules.xml | 8 + 2020/Lezioni/.idea/project-template.xml | 3 + 2020/Lezioni/.idea/uiDesigner.xml | 124 ++++++++++++ 2020/Lezioni/.idea/vcs.xml | 6 + 2020/Lezioni/.idea/workspace.xml | 190 ++++++++++++++++++ 10 files changed, 371 insertions(+), 2 deletions(-) create mode 100644 2020/Lezioni/.idea/codeStyles/codeStyleConfig.xml create mode 100644 2020/Lezioni/.idea/description.html create mode 100644 2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml create mode 100644 2020/Lezioni/.idea/misc.xml create mode 100644 2020/Lezioni/.idea/modules.xml create mode 100644 2020/Lezioni/.idea/project-template.xml create mode 100644 2020/Lezioni/.idea/uiDesigner.xml create mode 100644 2020/Lezioni/.idea/vcs.xml create mode 100644 2020/Lezioni/.idea/workspace.xml diff --git a/.gitignore b/.gitignore index 767e5c7..03af692 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,6 @@ hs_err_pid* # IntelliJ -.idea/* -**/.idea/* +#.idea/* +#**/.idea/* diff --git a/2020/Lezioni/.idea/codeStyles/codeStyleConfig.xml b/2020/Lezioni/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/2020/Lezioni/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/2020/Lezioni/.idea/description.html b/2020/Lezioni/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/2020/Lezioni/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml b/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..ce47ea9 --- /dev/null +++ b/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,20 @@ + + + + \ No newline at end of file diff --git a/2020/Lezioni/.idea/misc.xml b/2020/Lezioni/.idea/misc.xml new file mode 100644 index 0000000..9971bf6 --- /dev/null +++ b/2020/Lezioni/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/2020/Lezioni/.idea/modules.xml b/2020/Lezioni/.idea/modules.xml new file mode 100644 index 0000000..8617dec --- /dev/null +++ b/2020/Lezioni/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/2020/Lezioni/.idea/project-template.xml b/2020/Lezioni/.idea/project-template.xml new file mode 100644 index 0000000..1f08b88 --- /dev/null +++ b/2020/Lezioni/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/2020/Lezioni/.idea/uiDesigner.xml b/2020/Lezioni/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/2020/Lezioni/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/2020/Lezioni/.idea/vcs.xml b/2020/Lezioni/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/2020/Lezioni/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml new file mode 100644 index 0000000..e432a5a --- /dev/null +++ b/2020/Lezioni/.idea/workspace.xml @@ -0,0 +1,190 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1583492471839 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 9356ab91c009d2f56e1340b5642fb5ecedff6bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 20 Mar 2020 13:10:28 +0100 Subject: [PATCH 008/202] Lezione Extra sulle Lambda --- 2020/Lezioni/.idea/misc.xml | 5 +- 2020/Lezioni/.idea/workspace.xml | 62 ++++++++++++------- .../unive/dais/po2/other/FunctionalTest.java | 23 ++++++- .../it/unive/dais/po2/other/RevArrayList.java | 4 +- .../src/it/unive/dais/po2/other/RevTest.java | 14 +++-- 5 files changed, 74 insertions(+), 34 deletions(-) diff --git a/2020/Lezioni/.idea/misc.xml b/2020/Lezioni/.idea/misc.xml index 9971bf6..dd39c61 100644 --- a/2020/Lezioni/.idea/misc.xml +++ b/2020/Lezioni/.idea/misc.xml @@ -1,12 +1,9 @@ - - - - + \ No newline at end of file diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index e432a5a..f0f0bc7 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -1,7 +1,13 @@ - + + + + + + + - + - - + + @@ -109,7 +119,7 @@ - + C:\Users\spano\AppData\Roaming\Subversion @@ -142,38 +152,46 @@ - + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java b/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java index fa85b83..ccdfdd5 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java +++ b/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java @@ -6,9 +6,14 @@ import java.util.List; import java.util.function.Function; + public class FunctionalTest { + public interface Function { + S apply(T x); + } + public static Collection map(Collection l, Function f) { Collection c = new ArrayList<>(); @@ -27,6 +32,7 @@ public Void apply(X x) { return null; } }); + } public static void main(String[] args) { @@ -35,7 +41,6 @@ public static void main(String[] args) { for (int i = 0; i < 10; ++i) { l.add(i); } - print(l); Collection r = map(l, new Function() { @@ -44,8 +49,22 @@ public Integer apply(Integer x) { return x + 1; } }); - + // questa è la stessa cosa fatta con una lambda + Collection r2 = map(l, x -> x + 1); print(l); + /* + TRADUZIONE DA ANONYMOUS CLASS A LAMBDA + LA LAMBDA E' UNO ZUCCHERO SINTATTICO PER UNA ANONYMOUS CLASS + Collection r = map(l, new Function() { + @Override + public B apply(A x) { + BODY; + } + }); + + Collection r = map(l, x -> BODY); + */ + } } diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java b/2020/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java index 9f9093d..77ca60c 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java +++ b/2020/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java @@ -64,7 +64,9 @@ public boolean hasNext() { @Override public T next() { - return get(pos--); + T r = get(pos); + pos = pos - 2; + return r; } }; } diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/RevTest.java b/2020/Lezioni/src/it/unive/dais/po2/other/RevTest.java index 1949c0f..0c6f196 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/other/RevTest.java +++ b/2020/Lezioni/src/it/unive/dais/po2/other/RevTest.java @@ -1,14 +1,18 @@ package it.unive.dais.po2.other; +import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; +import java.util.List; + public class RevTest { - public static void main(String[] args) { - RevArrayList l = new RevArrayList<>(); - l.add(1); - l.add(2); - l.add(3); + public static void main(String[] args) { + Collection l = new RevArrayList<>(); + for (int i = 0; i < 10; ++i) { + l.add(i); + } Iterator it = l.iterator(); while (it.hasNext()) { From 859a1522677b5b70990411dba5a084ca2a0abb5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 20 Mar 2020 16:24:02 +0100 Subject: [PATCH 009/202] Update workspace.xml --- 2020/Lezioni/.idea/workspace.xml | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index f0f0bc7..d70c669 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -1,13 +1,7 @@ - - - - - - - + l, Function f) { Collection c = new ArrayList<>(); for (A a : l) { @@ -35,6 +29,13 @@ public Void apply(X x) { } + private static class MiaFunzionePerMap implements Function { + @Override + public Integer apply(Integer x) { + return x + 1; + } + } + public static void main(String[] args) { List l = new ArrayList<>(); @@ -43,6 +44,7 @@ public static void main(String[] args) { } print(l); + Collection r = map(l, new Function() { @Override public Integer apply(Integer x) { @@ -51,6 +53,9 @@ public Integer apply(Integer x) { }); // questa è la stessa cosa fatta con una lambda Collection r2 = map(l, x -> x + 1); + // questa terza variante usa direttamente una classe NON-anonima + Collection r3 = map(l, new MiaFunzionePerMap()); + print(l); /* From bb3b17e6383fc85fccddcfcef8b040545dd774e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 23 Mar 2020 18:06:40 +0100 Subject: [PATCH 011/202] Hash --- 2020/Lezioni/.idea/workspace.xml | 1 - 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashSet.java | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index b359179..9f0d337 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -4,7 +4,6 @@ - l, Function f) { Collection c = new ArrayList<>(); for (A a : l) { @@ -45,17 +46,63 @@ public static void main(String[] args) { } print(l); + // LE 4 FORME DI LAMBDA IN JAVA + Function f = x -> x + 1; + Function f2 = new Function() { + @Override + public Integer apply(Integer x) { + return x + 1; + } + }; + + Supplier g = () -> { + if (l.size() > 4) return 1; + else return 2; + }; + Supplier g2 = new Supplier() { + @Override + public Integer get() { + if (l.size() > 4) return 1; + else return 2; + } + }; - Collection r = map(l, new Function() { + Consumer h = (x) -> { + for (int i = 0; i < x; ++i) + System.out.println(i); + }; + Consumer h2 = new Consumer() { + @Override + public void accept(Integer x) { + for (int i = 0; i < x; ++i) + System.out.println(i); + } + }; + + Runnable r = () -> { + System.out.println("ciao"); + }; + Runnable r2 = new Runnable() { + @Override + public void run() { + System.out.println("ciao"); + } + }; + + + // ESEMPI DI CHIAMATA ALLA map() + + // questa è la stessa cosa fatta con una lambda + Collection x1 = map(l, x -> x + 1); + // questa è con la classe anonima + Collection x2 = map(l, new Function() { @Override public Integer apply(Integer x) { return x + 1; } }); - // questa è la stessa cosa fatta con una lambda - Collection r2 = map(l, x -> x + 1); // questa terza variante usa direttamente una classe NON-anonima - Collection r3 = map(l, new MiaFunzionePerMap()); + Collection x3 = map(l, new MiaFunzionePerMap()); print(l); diff --git a/2020/Lezioni/src/it/unive/dais/po2/threading/ConsumerProducer.java b/2020/Lezioni/src/it/unive/dais/po2/threading/ConsumerProducer.java index de409e2..6d0d2c9 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/threading/ConsumerProducer.java +++ b/2020/Lezioni/src/it/unive/dais/po2/threading/ConsumerProducer.java @@ -39,7 +39,6 @@ private static int rand__lock(int a, int b) { } } - private static void log(String msg) { Thread self = Thread.currentThread(); System.out.println(String.format("%s[%d]: %s", self.getName(), self.getId(), msg)); @@ -111,6 +110,7 @@ public static void main(String[] args) { Consumer c = new Consumer(l1, l2); Producer p1 = new Producer(l1, l2); Producer p2 = new Producer(l1, l2); + c.start(); p1.start(); p2.start(); From 1e3015ba65dfb2eb871bf9e1206bc2f0ff5ff686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 8 Apr 2020 10:57:20 +0200 Subject: [PATCH 020/202] Lazione 13 --- 2020/Lezioni/.idea/workspace.xml | 31 ++++++++++++++--- .../it/unive/dais/po2/myjdk/MyLinkedList.java | 18 +++++++--- .../unive/dais/po2/other/FunctionalTest.java | 1 - .../it/unive/dais/po2/patterns/Singleton.java | 34 +++++++++++++++++++ .../dais/po2/patterns/factory/Circle.java | 24 +++++++++++++ .../unive/dais/po2/patterns/factory/Main.java | 14 ++++++++ .../dais/po2/patterns/factory/Rectangle.java | 26 ++++++++++++++ .../dais/po2/patterns/factory/Shape.java | 7 ++++ .../po2/patterns/factory/ShapeFactory.java | 17 ++++++++++ .../patterns/factory/ShapeFactoryWider.java | 23 +++++++++++++ .../{Main.java => ThreadCreationMain.java} | 2 +- 11 files changed, 187 insertions(+), 10 deletions(-) create mode 100644 2020/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Circle.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Main.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Rectangle.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Shape.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactory.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactoryWider.java rename 2020/Lezioni/src/it/unive/dais/po2/threading/{Main.java => ThreadCreationMain.java} (98%) diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index e786f2e..aa48ebd 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -2,9 +2,17 @@ + + + + + + + + - + + + + @@ -119,7 +130,7 @@ - l, Function f) { Collection c = new ArrayList<>(); for (A a : l) { diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java b/2020/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java new file mode 100644 index 0000000..5a80afd --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java @@ -0,0 +1,34 @@ +package it.unive.dais.po2.patterns; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class Singleton { + + public static class Display { + // eventuali campi privati non-statici + + @Nullable + private static Display instance = null; + + private Display() { + // inizializzazione di tutti i tuoi campi privati non-statici + } + + @NotNull + public static Display getInstance() { + if (instance == null) { + instance = new Display(); + } + return instance; + } + + } + + public static void main(String[] args) { + Display d = Display.getInstance(); + Display d2 = Display.getInstance(); + } + + +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Circle.java b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Circle.java new file mode 100644 index 0000000..b426049 --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Circle.java @@ -0,0 +1,24 @@ +package it.unive.dais.po2.patterns.factory; + +class Circle implements Shape { + private final double ray; + + Circle(double ray) { + this.ray = ray; + } + + @Override + public void draw() { + System.out.println(String.format("Circle[ray:%g]", ray)); + } + + @Override + public double area() { + return Math.PI * ray * ray; + } + + @Override + public double perimeter() { + return Math.PI * 2. * ray; + } +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Main.java b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Main.java new file mode 100644 index 0000000..681234c --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Main.java @@ -0,0 +1,14 @@ +package it.unive.dais.po2.patterns.factory; + +public class Main { + + public static void main(String[] args) { + try { + ShapeFactory factory = new ShapeFactoryWider(4.); + Shape shape1 = factory.create("rectangle", 10, 11); + Shape shape2 = factory.create("circle", 19); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Rectangle.java b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Rectangle.java new file mode 100644 index 0000000..1dc7a08 --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Rectangle.java @@ -0,0 +1,26 @@ +package it.unive.dais.po2.patterns.factory; + +class Rectangle implements Shape { + + private final double base, height; + + Rectangle(double base, double height) { + this.base = base; + this.height = height; + } + + @Override + public void draw() { + System.out.println(String.format("Rectangle[%gx%g]", base, height)); + } + + @Override + public double area() { + return base * height; + } + + @Override + public double perimeter() { + return (base + height) * 2.; + } +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Shape.java b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Shape.java new file mode 100644 index 0000000..e40f9fa --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Shape.java @@ -0,0 +1,7 @@ +package it.unive.dais.po2.patterns.factory; + +public interface Shape { + void draw(); + double area(); + double perimeter(); +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactory.java b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactory.java new file mode 100644 index 0000000..018221a --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactory.java @@ -0,0 +1,17 @@ +package it.unive.dais.po2.patterns.factory; + +import org.jetbrains.annotations.NotNull; + +public class ShapeFactory { + + @NotNull + public Shape create(String name, double... data) throws Exception { + if (name.toLowerCase().equals("rectangle")) + return new Rectangle(data[0], data[1]); + else if (name.toLowerCase().equals("circle")) + return new Circle(data[0]); + else throw new Exception("invalid shape: " + name); + } + + +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactoryWider.java b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactoryWider.java new file mode 100644 index 0000000..f87f514 --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactoryWider.java @@ -0,0 +1,23 @@ +package it.unive.dais.po2.patterns.factory; + +import org.jetbrains.annotations.NotNull; + +public class ShapeFactoryWider extends ShapeFactory { + + private final double amp; + + public ShapeFactoryWider(double amp) { + this.amp = amp; + } + + @Override + @NotNull + public Shape create(String name, double... data) throws Exception { + if (name.toLowerCase().equals("rectangle")) + return new Rectangle(data[0] * amp, data[1] * amp); + else if (name.toLowerCase().equals("circle")) + return new Circle(data[0] * amp); + else throw new Exception("invalid shape: " + name); + } + +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/threading/Main.java b/2020/Lezioni/src/it/unive/dais/po2/threading/ThreadCreationMain.java similarity index 98% rename from 2020/Lezioni/src/it/unive/dais/po2/threading/Main.java rename to 2020/Lezioni/src/it/unive/dais/po2/threading/ThreadCreationMain.java index b281119..a0adac2 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/threading/Main.java +++ b/2020/Lezioni/src/it/unive/dais/po2/threading/ThreadCreationMain.java @@ -4,7 +4,7 @@ import java.util.Collection; import java.util.Random; -public class Main { +public class ThreadCreationMain { private static void count(String id, long millis, int times) { try { From c1b243ec3b9959b12080ab816adc15981578deb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 10 Apr 2020 18:07:17 +0200 Subject: [PATCH 021/202] Sorting --- 2020/Lezioni/.idea/workspace.xml | 73 ++++++++++++------- .../src/esercizi/Es_RandomIterator.java | 2 - .../it/unive/dais/po2/myjdk/MyHashSet.java | 2 - .../it/unive/dais/po2/myjdk/MyLinkedList.java | 2 +- .../it/unive/dais/po2/other/IteratorTest.java | 4 +- .../it/unive/dais/po2/other/SortingTest.java | 56 ++++++++++++++ .../ConsumerProducer.java | 6 +- .../it/unive/dais/po2/patterns/Singleton.java | 29 ++++++-- .../src/it/unive/dais/po2/zoo/Animal.java | 13 ++-- .../src/it/unive/dais/po2/zoo/Cat.java | 7 +- .../it/unive/dais/po2/zoo/ColoredAnimal.java | 6 +- .../src/it/unive/dais/po2/zoo/Dog.java | 28 +++++-- .../src/it/unive/dais/po2/zoo/Persian.java | 4 +- 13 files changed, 170 insertions(+), 62 deletions(-) create mode 100644 2020/Lezioni/src/it/unive/dais/po2/other/SortingTest.java rename 2020/Lezioni/src/it/unive/dais/po2/{threading => patterns}/ConsumerProducer.java (94%) diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index aa48ebd..285a6b1 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -2,17 +2,19 @@ - - - - - - - + + + - - + + + + + + + + - + - + + + @@ -186,18 +197,18 @@ - + - - + + - - + + - + @@ -210,6 +221,14 @@ + + + + + + + + @@ -222,22 +241,22 @@ - + - - + + - - + + - - + + - + @@ -266,6 +285,10 @@ + + + + implements Creature { +import org.jetbrains.annotations.NotNull; + +public class Animal implements Creature, Comparable { protected int weight; - protected A partner; - public Animal(int weight, A p) { + public Animal(int weight) { this.weight = weight; - this.partner = p; } public void eat(Animal a) { this.weight += a.weight; } - public A getPartner() { - return partner; + @Override + public int compareTo(@NotNull Animal o) { + return this.weight - o.weight; } } diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Cat.java b/2020/Lezioni/src/it/unive/dais/po2/zoo/Cat.java index 9b04d47..3852fb5 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/zoo/Cat.java +++ b/2020/Lezioni/src/it/unive/dais/po2/zoo/Cat.java @@ -1,10 +1,10 @@ package it.unive.dais.po2.zoo; -public class Cat extends ColoredAnimal { +public class Cat extends ColoredAnimal { - public Cat(int w, String c, Cat p) { - super(w, c, p); + public Cat(int w, String c) { + super(w, c); } @Override @@ -13,7 +13,6 @@ public void eat(Animal a) { color = color + " fat"; } - public void meow() { System.out.println("miao!"); } diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java b/2020/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java index 103f503..83e5a4b 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java +++ b/2020/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java @@ -1,10 +1,10 @@ package it.unive.dais.po2.zoo; -public abstract class ColoredAnimal extends Animal { +public abstract class ColoredAnimal extends Animal { protected String color; - protected ColoredAnimal(int w, String c, A p) { - super(w, p); + protected ColoredAnimal(int w, String c) { + super(w); this.color = c; } diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Dog.java b/2020/Lezioni/src/it/unive/dais/po2/zoo/Dog.java index d7ee84e..d6b02a5 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/zoo/Dog.java +++ b/2020/Lezioni/src/it/unive/dais/po2/zoo/Dog.java @@ -1,19 +1,37 @@ package it.unive.dais.po2.zoo; -public class Dog extends ColoredAnimal { +import org.jetbrains.annotations.NotNull; - public Dog(int w, String c, Dog p) { - super(w, c, p); - } +public class Dog extends ColoredAnimal { + private int bava; + public Dog(int w, String c) { + super(w, c); + bava = w / 10; + } @Override public void eat(Animal a) { weight += a.weight / 2; } - public void bark() { System.out.println("bau!"); } + + @Override + public int compareTo(@NotNull Animal o) { + if (o instanceof Dog) { + Dog u = (Dog) o; + return (this.bava + this.weight) - (o.weight + u.bava); + } + else { + return super.compareTo(o); + } + } + + @Override + public String toString() { + return String.format("Dog[%d, %s]", weight, color); + } } diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Persian.java b/2020/Lezioni/src/it/unive/dais/po2/zoo/Persian.java index 05663a9..b29467f 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/zoo/Persian.java +++ b/2020/Lezioni/src/it/unive/dais/po2/zoo/Persian.java @@ -1,8 +1,8 @@ package it.unive.dais.po2.zoo; public class Persian extends Cat { - public Persian(int w, Cat p) { - super(w, "beige", p); + public Persian(int w) { + super(w, "beige"); } @Override From 9ef5a1e653ebe003c30eaf84756dc6763b3c19b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 14 Apr 2020 18:00:50 +0200 Subject: [PATCH 022/202] Sorting 2 --- .../inspectionProfiles/Project_Default.xml | 1 + 2020/Lezioni/.idea/workspace.xml | 42 ++++----- .../it/unive/dais/po2/other/SortingTest.java | 85 +++++++++++++++++-- 3 files changed, 97 insertions(+), 31 deletions(-) diff --git a/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml b/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml index ce47ea9..f60e880 100644 --- a/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml +++ b/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml @@ -1,6 +1,7 @@ l, Function f) { + public static List map(Iterable l, Function f) { + List c = new ArrayList<>(); + for (A a : l) { + B b = f.apply(a); + c.add(b); + } + return c; + } + + public static B fold(Iterable l, B zero, BiFunction f) { + B acc = zero; + for (A a : l) { + acc = f.apply(a, acc); + } + return acc; + } + + public static void main(String[] args) { + List l1 = new ArrayList<>(); + for (int i = 0; i < 10; ++i) { + l1.add(i); + } + Collection r1 = map(l1, (x) -> x * 2); + int sum = fold(r1, 0, (x, acc) -> x + acc); + + + List l2 = new ArrayList<>(); + for (int i = 0; i < 10; ++i) { + l2.add(new Dog(i, "red")); + } + List r2 = map(l2, (ColoredAnimal x) -> new Dog(x.getWeight() - 5, x.getColor())); + + + } + +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/IteratorTest__Mattia.java b/2020/Lezioni/src/it/unive/dais/po2/other/IteratorTest__Mattia.java new file mode 100644 index 0000000..f688b32 --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/other/IteratorTest__Mattia.java @@ -0,0 +1,83 @@ +package it.unive.dais.po2.other; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Iterator; + +public class IteratorTest__Mattia { + + public static class MyArrayList extends ArrayList { /* eredito tutti i membri di ArrayList */ + + public enum IteratorKind {DEFAULT, EVEN, ODD} + + /* + * Gli assegnamenti fatti in concomitanza con la dichiarazione dei field, + * avvengono prima dell' inizializzazione del costruttore + */ + private IteratorKind kind; /* private field di MyArrayList*/ + + /* + * Costruttore definito affinchè sia possibile decidere in fase + * di creazione dell'oggetto MyArrayList, quale comportamento dovrà + * avere l'iteratore (nel momento del suo utilizzo) + */ + public MyArrayList(IteratorKind kind) { + this.kind = kind; + } + + /* Costruttore di default, dichiarato esplicitamente (perchè dichiarato un altro costruttore)*/ + public MyArrayList() { + /* assicura il corretto comportamento del metodo Next, + * nel caso in cui non venga definito dall'utente un + * comportamento speciale per l'iteratore in caso di costruzione + * dell'oggetto MyArrayList + */ + this.kind = IteratorKind.DEFAULT; + } + + + /* + * Overrido il metodo iterator dell'interfaccia Iterable (ereditata con ArrayList) + * affinchè venga ritornato il mio oggetto Iterator (opportunamente modificato) + * al posto dell'Iterator classico (ereditato) di ArrayList*/ + @NotNull + @Override + public Iterator iterator() { + return new Iterator<>() { + + private int i = 0; /* private field della nuova */ + + @Override + public boolean hasNext() { + return i < size(); /* Zucchero sintattico per: MyArrayList.this.size(); */ + } + + @Override + public T next() { + @Nullable T toReturn = null; + + switch (kind) { + case DEFAULT: + toReturn = get(i++); + break; + case EVEN: + toReturn = get(i++); + if (hasNext())/* scarto il prossimo valore solo se questo è presente */ + get(i++); + break; + case ODD: + get(i++); + if (hasNext()) /* scarto il prossimo valore solo se questo è presente */ + toReturn = get(i++); + } + + return toReturn; + } + }; + + } + + } +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Animal.java b/2020/Lezioni/src/it/unive/dais/po2/zoo/Animal.java index 69cc545..02ded64 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/zoo/Animal.java +++ b/2020/Lezioni/src/it/unive/dais/po2/zoo/Animal.java @@ -9,6 +9,8 @@ public Animal(int weight) { this.weight = weight; } + public int getWeight() { return weight; } + public void eat(Animal a) { this.weight += a.weight; } diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java b/2020/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java index 83e5a4b..c6c6b1d 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java +++ b/2020/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java @@ -8,4 +8,6 @@ protected ColoredAnimal(int w, String c) { this.color = c; } + public String getColor() { return color; } + } From 703d68fdfc57a33bd450be87951618f46ee8c827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 21 Apr 2020 15:08:16 +0200 Subject: [PATCH 024/202] Lezione 17 --- 2020/Lezioni/.idea/workspace.xml | 13 ++---- .../dais/po2/other/FunctionalPrimitives.java | 45 ++++++++++++++++++- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index e3d6a93..df35321 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -2,12 +2,8 @@ - - - - - + @@ -159,12 +154,12 @@ - + @@ -202,10 +197,10 @@ - + - + diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java b/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java index 9c96949..3fab539 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java +++ b/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java @@ -6,13 +6,13 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import java.util.List; import java.util.function.BiFunction; import java.util.function.Function; public class FunctionalPrimitives { - //public static List map(Iterable l, Function f) { public static List map(Iterable l, Function f) { List c = new ArrayList<>(); for (A a : l) { @@ -22,6 +22,13 @@ public static List map(Iterable l, Function return c; } + public static List map2(Iterable l, Function f) { + return fold(l, new ArrayList<>(), (x, acc) -> { + acc.add(f.apply(x)); + return acc; + }); + } + public static B fold(Iterable l, B zero, BiFunction f) { B acc = zero; for (A a : l) { @@ -30,7 +37,43 @@ public static B fold(Iterable l, B zero, BiFunction f) { return acc; } + public static B fold_recur(Iterable l, B zero, BiFunction f) { + return fold_recur(l.iterator(), zero, f); + } + + public static B fold_recur(Iterator it, B zero, BiFunction f) { + return it.hasNext() ? fold_recur(it, f.apply(it.next(), zero), f) : zero; + } + + public static void f() { // function_f: + int i = 0; // li r0, #0 + while (i < 10) { // loop: + // cmp r0, #10 + // jeq exit + System.out.println("ciao " + i); // call ... + i = i + 1; // add r0, #1, r0 + } // jmp loop + // exit: + // balbalbla + } + + public static void g(int i) { // function_g: + if (i > 0) { // ld sp, r0 + // cmp r0, #0 + // jeq exit + System.out.println("ciao " + i); // call ... + g(i - 1); // sub r0, #1 + // st r0, sp + } // jmp function_g + // exit: + // blablabla + } + public static void main(String[] args) { + + g(10); // st #10, sp + // jmp function_g + List l1 = new ArrayList<>(); for (int i = 0; i < 10; ++i) { l1.add(i); From ee07d4460ed946b3dd1a2e9c0103eadbda945198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 21 Apr 2020 17:46:53 +0200 Subject: [PATCH 025/202] Filter, Iter and function composition --- .../dais/po2/other/FunctionalPrimitives.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java b/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java index 3fab539..48bda6a 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java +++ b/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java @@ -9,7 +9,9 @@ import java.util.Iterator; import java.util.List; import java.util.function.BiFunction; +import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Predicate; public class FunctionalPrimitives { @@ -29,6 +31,28 @@ public static List map2(Iterable l, Function void filter__imperative(Iterable l, Function p) { + Iterator it = l.iterator(); + while (it.hasNext()) { + A a = it.next(); + if (!p.apply(a)) it.remove(); + } + } + + public static List filter__pure(Iterable l, Function p) { + List r = new ArrayList<>(); + for (A a : l) { + if (p.apply(a)) r.add(a); + } + return r; + } + + public static void iter(Iterable l, Consumer f) { + for (A a : l) { + f.accept(a); + } + } + public static B fold(Iterable l, B zero, BiFunction f) { B acc = zero; for (A a : l) { @@ -69,6 +93,11 @@ public static void g(int i) { // function_g: // blablabla } + @FunctionalInterface + interface TriFunction { + R apply(A a, B b, C c); + } + public static void main(String[] args) { g(10); // st #10, sp @@ -88,7 +117,34 @@ public static void main(String[] args) { } List r2 = map(l2, (ColoredAnimal x) -> new Dog(x.getWeight() - 5, x.getColor())); + iter(l2, (Dog d) -> System.out.println(d)); + for (Dog d : l2) System.out.println(d); + List r = map(l2, (Dog d) -> { System.out.println(d); return null; }); + + + String s = "ciao"; + boolean b = s.isEmpty(); + Function f = String::isEmpty; + Predicate p = String::isEmpty; + + int i = s.indexOf('c'); + BiFunction g = String::indexOf; + + int j = s.lastIndexOf('c', 8); + TriFunction h = String::lastIndexOf; + + { + List l = new ArrayList<>(); + l.add("pippo"); + l.add("baudo"); + l.add("pluto"); + String s2 = String.join(",", l); + BiFunction, String> k = String::join; + } + Function id = Function.identity(); + Function id2 = (x) -> x; // fun x -> x + } } From 6bf2993df7ab1973ec48bd7351d388ead1ddd32b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 21 Apr 2020 18:52:50 +0200 Subject: [PATCH 026/202] Lezione 17 --- 2020/Lezioni/.idea/workspace.xml | 1 - .../src/it/unive/dais/po2/other/FunctionalPrimitives.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index df35321..2b7aa8e 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -2,7 +2,6 @@ - it, Function f) { + return new Iterator<>() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public B next() { + return f.apply(it.next()); + } + }; + } + + /*1b1*/ + public static class Pair { + public final A a; + public final B b; + + public Pair(A a, B b) { + this.a = a; + this.b = b; + } + } + + /*1b2*/ + public static Iterator> pairIterator(Iterator it) { + return new Iterator<>()/*se non specifico non riesce ad inferire*/ { + A last; + + @Override + public boolean hasNext() { + if (it.hasNext()) + last = it.next(); + return it.hasNext(); + } + + @Override + public Pair next() { + return new Pair<>(last, it.next()); + } + }; + } + + /*1c*/ + public static void main(String[] args) { + ArrayList al = new ArrayList<>(); + Random rnd = new Random(); + for (int i = 0; i < 10; ++i) + al.add(rnd.nextInt(10)); + + Iterator> cateti = pairIterator(al.iterator()); + + Iterator ipotenuse = mapIterator(cateti, p -> Math.sqrt(p.a * p.a + p.b * p.b)); + + while (ipotenuse.hasNext()) + System.out.println("Ipotenusa " + ipotenuse.next()); + } +} + From 0e44b68903d6e92485373d6f892feb4bd8312053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 5 May 2020 17:37:01 +0200 Subject: [PATCH 030/202] Iterable tree --- 2020/Lezioni/.idea/workspace.xml | 45 +++++++----- .../src/esercizi/ScittoGennaio2019__Pool.java | 1 + .../src/esercizi/ScrittoGiugno2019.java | 73 +++++++++++++++++++ 3 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 2020/Lezioni/src/esercizi/ScrittoGiugno2019.java diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index e9dd4d4..ef1f25d 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -2,11 +2,9 @@ - - - + - + - + + + - + - - + + - - + + - + @@ -232,22 +239,22 @@ - + - - + + - - + + - - + + - + diff --git a/2020/Lezioni/src/esercizi/ScittoGennaio2019__Pool.java b/2020/Lezioni/src/esercizi/ScittoGennaio2019__Pool.java index 9751f21..7ec745b 100644 --- a/2020/Lezioni/src/esercizi/ScittoGennaio2019__Pool.java +++ b/2020/Lezioni/src/esercizi/ScittoGennaio2019__Pool.java @@ -46,6 +46,7 @@ public Resource(T x, AutoPool p) { public T get() { return elem; } @Override + @SuppressWarnings("deprecation") public void finalize() { release(); } diff --git a/2020/Lezioni/src/esercizi/ScrittoGiugno2019.java b/2020/Lezioni/src/esercizi/ScrittoGiugno2019.java new file mode 100644 index 0000000..d007bf5 --- /dev/null +++ b/2020/Lezioni/src/esercizi/ScrittoGiugno2019.java @@ -0,0 +1,73 @@ +package esercizi; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.*; + +public class ScrittoGiugno2019 { + + public static class Node implements Iterable { + @NotNull + private final T data; + @Nullable + private final Node left, right; + + public Node(@NotNull T data, @Nullable Node l, @Nullable Node r) { + this.data = data; + left = l; + right = r; + } + + public Node(@NotNull T data) { + this(data, null, null); + } + + public Node(@NotNull T data, @NotNull Node l) { + this(data, l, null); + } + + private void visit(@NotNull Collection c) { + c.add(data); + if (left != null) left.visit(c); + if (right != null) right.visit(c); + } + + @NotNull + @Override + public Iterator iterator() { + Collection r = new ArrayList<>(); + visit(r); + return r.iterator(); + } + + @Override + public String toString() { + return String.format("%s(%s %s)", + data, + left == null ? "_" : left, + right == null ? "_" : right); + } + } + + + public static void main(String[] args) { + + Node l1 = new Node<>(2, new Node<>(4), new Node<>(5)), + r1 = new Node<>(3, + new Node<>(6, + null, + new Node<>(7))), + root = new Node<>(1, l1, r1); + + + for (int i : root) { + System.out.println(i); + } + + System.out.println(root); + + } + + +} From 539f6902e275358a8065d566fc2f111bad397494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 8 May 2020 17:44:49 +0200 Subject: [PATCH 031/202] For each --- 2020/Lezioni/.idea/workspace.xml | 77 ++++++++++++------- .../it/unive/dais/po2/myjdk/MyIterable.java | 11 +++ .../it/unive/dais/po2/other/ForEachTest.java | 38 +++++++++ .../dais/po2/other/FunctionalPrimitives.java | 13 +++- 4 files changed, 110 insertions(+), 29 deletions(-) create mode 100644 2020/Lezioni/src/it/unive/dais/po2/other/ForEachTest.java diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index ef1f25d..1779e6a 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -2,9 +2,10 @@ - + - + + @@ -55,7 +56,7 @@ - + + + + @@ -195,18 +205,18 @@ - + - - + + - - + + - + @@ -239,42 +249,42 @@ - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + @@ -288,6 +298,17 @@ + + + + + file://$PROJECT_DIR$/src/it/unive/dais/po2/other/ForEachTest.java + 12 + + + + l, B zero, BiFunction f) return fold_recur(l.iterator(), zero, f); } + // fold : list A -> B -> (A * B -> B) -> B public static B fold_recur(Iterator it, B zero, BiFunction f) { - return it.hasNext() ? fold_recur(it, f.apply(it.next(), zero), f) : zero; + // caso ricorsivo + if (it.hasNext()) { + return fold_recur(it, f.apply(it.next(), zero), f); + } + // caso base + else { + return zero; + } + // equivalente ma in una solo espressione + //return it.hasNext() ? fold_recur(it, f.apply(it.next(), zero), f) : zero; } public static void f() { // function_f: @@ -98,6 +108,7 @@ interface TriFunction { R apply(A a, B b, C c); } + public static void main(String[] args) { g(10); // st #10, sp From f98157116497b0591f7e24c0f8cc0f1dd8fbfc4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 12 May 2020 18:18:36 +0200 Subject: [PATCH 032/202] Esercizi: cache --- 2020/Lezioni/.idea/workspace.xml | 81 ++++++--- .../Lezioni/META-INF/Lezioni.kotlin_module | Bin 0 -> 16 bytes 2020/Lezioni/src/esercizi/Andrei.java | 154 ++++++++++++++++++ 2020/Lezioni/src/esercizi/EsercizioCache.java | 103 ++++++++++++ .../src/esercizi/ScrittoGennaio2020.java | 6 +- .../it/unive/dais/po2/myjdk/MyArrayList.java | 10 ++ .../src/it/unive/dais/po2/zoo/Main.java | 15 +- 7 files changed, 338 insertions(+), 31 deletions(-) create mode 100644 2020/Lezioni/out/production/Lezioni/META-INF/Lezioni.kotlin_module create mode 100644 2020/Lezioni/src/esercizi/Andrei.java create mode 100644 2020/Lezioni/src/esercizi/EsercizioCache.java diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index 1779e6a..8910a78 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -2,10 +2,12 @@ - + + - - + + + - + + + + + + + @@ -205,22 +225,22 @@ - + - + - + - - + + - + @@ -249,42 +269,46 @@ - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + + + + + @@ -306,6 +330,11 @@ 12 diff --git a/2020/Lezioni/out/production/Lezioni/META-INF/Lezioni.kotlin_module b/2020/Lezioni/out/production/Lezioni/META-INF/Lezioni.kotlin_module new file mode 100644 index 0000000000000000000000000000000000000000..a49347afef10a9b5f95305e1058ba36adec7d6dd GIT binary patch literal 16 RcmZQzU|?ooU|@t|0RRA102TlM literal 0 HcmV?d00001 diff --git a/2020/Lezioni/src/esercizi/Andrei.java b/2020/Lezioni/src/esercizi/Andrei.java new file mode 100644 index 0000000..b438d05 --- /dev/null +++ b/2020/Lezioni/src/esercizi/Andrei.java @@ -0,0 +1,154 @@ +package esercizi; + +import org.jetbrains.annotations.NotNull; + +import java.util.*; +import java.util.function.Function; + +public class Andrei { + + public static class Point { + public final double x, y, z; + + public Point(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + public Point move(double dx, double dy, double dz) { + return new Point(this.x + dx, this.y + dy, this.z + dz); + } + + @Override + public String toString() { + return String.format("(%g, %g, %g)", x, y, z); + } + } + + public interface Solid extends Comparable { + double area(); + + double volume(); + + PositionedSolid at(Point origin); + + static int compareBy(Function f, S s1, S s2) { + return Double.compare(f.apply(s1), f.apply(s2)); + } + + static Comparator comparatorBy(Function f) { + return (s1, s2) -> compareBy(f, s1, s2); + } + + default int compareTo(Solid s) { + return compareBy(Solid::volume, this, s); + } + + } + + public interface Polyhedron extends Solid { + double perimeter(); + + @Override + PositionedPolyhedron at(Point origin); + } + + public interface PositionedSolid { + Point origin(); + } + + public interface PositionedPolyhedron extends PositionedSolid, Iterable { + } + + public static class Cube implements Polyhedron { + private double side; + + public Cube(double side) { + this.side = side; + } + + @Override + public double area() { + return 4 * Math.pow(side, 2); + } + + @Override + public double volume() { + return Math.pow(side, 3); + } + + @Override + public double perimeter() { + return side * 12; + } + + @Override + public PositionedPolyhedron at(Point origin) { + return new PositionedPolyhedron() { + @Override + public Point origin() { + return origin; + } + + @NotNull + @Override + public Iterator iterator() { + Point[] vertici = {origin, origin.move(side, 0, 0), origin.move(0, side, 0), origin.move(0, side, 0), origin.move(side, side, 0), origin.move(side, 0, side)/*ecc*/}; + return Arrays.asList(vertici).iterator(); + } + }; + } + + } + + static class Sphere implements Solid { + private double ray; + + public Sphere(double ray) { + this.ray = ray; + } + + @Override + public double area() { + return 4 * 3.14 * Math.pow(ray, 2); + } + + @Override + public double volume() { + return (4. / 3.) * 3.14 * Math.pow(ray, 3); + } + + @Override + public PositionedSolid at(Point origin) { + return new PositionedSolid() { + @Override + public Point origin() { + return origin; + } + }; + } + } + + public static void main(String[] args) { + + Cube cube1 = new Cube(11.), cube2 = new Cube(23.); + Sphere sphere1 = new Sphere(12.), sphere2 = new Sphere(35.); + List solids = List.of(cube1, cube2, sphere1, sphere2); + List cubes = new ArrayList<>(List.of(cube1, cube2)); + List spheres = List.of(sphere1, sphere2); + List polys = cubes; + + Collections.sort(cubes); + + + Point o = new Point(1., -1.5, 2.); + for (Polyhedron poly : polys) { + for (Point p : poly.at(o)) { + System.out.println(p); //question mark? + } + } + } + + +} diff --git a/2020/Lezioni/src/esercizi/EsercizioCache.java b/2020/Lezioni/src/esercizi/EsercizioCache.java new file mode 100644 index 0000000..93ad77f --- /dev/null +++ b/2020/Lezioni/src/esercizi/EsercizioCache.java @@ -0,0 +1,103 @@ +package esercizi; + +import it.unive.dais.po2.myjdk.Pair; +import org.jetbrains.annotations.NotNull; + +import java.math.BigInteger; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; +import java.util.function.Supplier; + +public class EsercizioCache { + + + public static class Cache { + @NotNull + private final Function f; + @NotNull + private final Map m = new HashMap<>(); + + public Cache(@NotNull Function f) { + this.f = f; + } + + @NotNull + public V lookup(@NotNull K k) { + if (m.containsKey(k)) { + return m.get(k); + } else { + V v = f.apply(k); + m.put(k, v); + return v; + } + } + } + + + public static BigInteger fact(long n) { + return n < 2 ? BigInteger.ONE : fact(n - 1).multiply(new BigInteger(String.valueOf(n))); + } + + + public static long fastFibRecur(long n, Map cache) { + return n < 2 ? 1L : fastFib(n - 1, cache) + fastFib(n - 2, cache); + } + + public static long fastFib(long n, Map cache) { + if (cache.containsKey(n)) return cache.get(n); + else { + long r = fastFibRecur(n, cache); + cache.put(n, r); + return r; + } + } + + public static class CachedFib { + private final Map cache = new HashMap<>(); + + private long fibRecur(long n) { + return n < 2 ? 1L : fib(n - 1) + fib(n - 2); + } + + public long fib(long n) { + if (cache.containsKey(n)) return cache.get(n); + else { + long r = fibRecur(n); + cache.put(n, r); + return r; + } + } + } + + public static Pair cputime(Supplier f) { + long now0 = System.nanoTime(); + T r = f.get(); + long now1 = System.nanoTime(); + return new Pair<>(r, now1 - now0); + } + + public static void main(String[] args) { + + //Cache cache = new Cache<>((n) -> fastFib(n, cache)); + + // versione senza oggetto + Map m = new HashMap<>(); + for (int i = 0; i < 100; ++i) { + long n = i; + Pair r = cputime(() -> fastFib(n, m)); + System.out.println(String.format("fib(%d) = %d [%g s]", n, r.first, ((double) r.second / 1000000000.))); + } + + // versione con oggetto + CachedFib cache = new CachedFib(); + for (int i = 0; i < 100; ++i) { + long n = i; + Pair r = cputime(() -> cache.fib(n)); + System.out.println(String.format("fib(%d) = %d [%g s]", n, r.first, ((double) r.second / 1000000000.))); + } + + + } + +} \ No newline at end of file diff --git a/2020/Lezioni/src/esercizi/ScrittoGennaio2020.java b/2020/Lezioni/src/esercizi/ScrittoGennaio2020.java index 319df94..899ac8d 100644 --- a/2020/Lezioni/src/esercizi/ScrittoGennaio2020.java +++ b/2020/Lezioni/src/esercizi/ScrittoGennaio2020.java @@ -1,12 +1,16 @@ package esercizi; +import org.jetbrains.annotations.NotNull; + import java.util.Comparator; +import java.util.Iterator; +import java.util.List; import java.util.function.Function; import java.util.function.Supplier; public class ScrittoGennaio2020 { - public static class Point { + public static class Point { } diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java index 4bb83e4..be37c1f 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java @@ -1,5 +1,7 @@ package it.unive.dais.po2.myjdk; +import java.util.Collection; + // TODO: togliere il prefisso My da tutti i nomi dei tipi e mostrare la differenza tra tipi omonimi in package diversi public class MyArrayList implements MyList { @@ -11,6 +13,14 @@ public MyArrayList() { this.actualSize = 0; } + public MyArrayList(MyCollection c) { + this(); + MyIterator it = c.iterator(); + while (it.hasNext()) { + add(it.next()); + } + + } @Override public T get(int i) throws OutOfBoundsException { diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java b/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java index 40f7d82..4109fe4 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java +++ b/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java @@ -3,14 +3,21 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; +import java.util.List; import java.util.function.Function; public class Main { - // TODO: provare a mettere a posto in modo che compili -/* Dog fido = new Dog(15, "bruno"); + public static void main(String[] args) { + Dog fido = new Dog(15, "bruno"); Dog baldo = new Dog(20, "bianco"); - Animal jackie = new Dog(2, "nero"); + + List dogs = new ArrayList<>(); + dogs.add(fido); + dogs.add(baldo); + + // TODO: provare a mettere a posto in modo che compili +/* Animal jackie = new Dog(2, "nero"); Dog pluto = new Animal(50); Animal selene = new Cat(4, "grigio"); @@ -31,7 +38,7 @@ public class Main { a.eat(a); // mangiare se stessi si può fare */ - + } } From 06c86b70e8f3cb97b0040ca757e6d96ddd1e6874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 15 May 2020 19:14:38 +0200 Subject: [PATCH 033/202] Cache 2 --- 2020/Lezioni/.idea/workspace.xml | 48 ++++---- 2020/Lezioni/src/esercizi/EsercizioCache.java | 114 ++++++++++++++---- 2 files changed, 115 insertions(+), 47 deletions(-) diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index 8910a78..2683ced 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -2,12 +2,8 @@ - - - - - + diff --git a/2020/Lezioni/src/esercizi/DynamicDispatch.java b/2020/Lezioni/src/esercizi/DynamicDispatch.java new file mode 100644 index 0000000..ebd5a1f --- /dev/null +++ b/2020/Lezioni/src/esercizi/DynamicDispatch.java @@ -0,0 +1,73 @@ +package esercizi; + +public class DynamicDispatch { + + public static class A { + + public void m() { + System.out.println("A::m"); + } + + public void p(int n) {} + public void p(double n) {} + + } + /* + A + SLOT1: m --> A::m + SLOT2: p(int) --> A::p(int) + SLOT3: p(double) --> A::p(double) + + B + SUPER: A + SLOT1: m --> B::m + SLOT2: p(int) --> A::p(int) + SLOT3: p(double) --> A::p(double) + SLOT4: p(String) --> B::p(String) + SLOT5: q(String) --> B::q(String) + */ + + public static class B extends A { + + @Override + public void m() { + System.out.println("B::m"); + } + + public void p(String n) {} + public void q(String n) {} + } + + public static class C { + + public void a() {} + public void p(String n) {} + public void q(String n) {} + public void m() { + System.out.println("C::m"); + } + } + + /* + D + SUPER: B + SLOT1: m --> B::m + SLOT2: p(int) --> A::p(int) + SLOT3: p(double) --> A::p(double) + SLOT4: p(String) --> B::p(String) + SLOT5: q(String) --> D::q(String) + */ + public static class D extends B { + @Override + public void q(String s) {} + } + + public static void main(String[] args) { + A a = new B(); + a.m(); + + B b = new D(); + b.q("ciao"); + } + +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/SortingTest.java b/2020/Lezioni/src/it/unive/dais/po2/other/SortingTest.java index 933d30e..3484862 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/other/SortingTest.java +++ b/2020/Lezioni/src/it/unive/dais/po2/other/SortingTest.java @@ -125,6 +125,7 @@ public String toString() { } + static > void sort__(List list) {} static void sort__(List list, Comparator c) {} diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java b/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java index 4109fe4..b671218 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java +++ b/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java @@ -16,6 +16,30 @@ public static void main(String[] args) { dogs.add(fido); dogs.add(baldo); + List l3 = new ArrayList(); + l3.add(new Cat(10, "idfif")); + + // wildcards + + List l = new ArrayList<>(); + Animal a1 = l.get(0); + + l.set(0, new Dog(10, "fido")); + l.add(new Dog(10, "fido")); + + + List l2 = new ArrayList<>(); + Animal a2 = l2.get(0); + + l2.set(0, new Dog(10, "fido")); + l2.set(0, new Animal(10)); + l2.add(new Dog(10, "fido")); + + Function f1 = (Animal s) -> new Cat(s.weight, "blu"); + Function f = f1; + + + // TODO: provare a mettere a posto in modo che compili /* Animal jackie = new Dog(2, "nero"); Dog pluto = new Animal(50); From fd6c99bcadd52f28469765335e3a9342851426f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 19 May 2020 17:45:11 +0200 Subject: [PATCH 035/202] Esercizi 10 --- 2020/Lezioni/.idea/workspace.xml | 50 ++++++++------- .../Lezioni/src/esercizi/Es_SkipIterator.java | 20 +++++- 2020/Lezioni/src/esercizi/MapIterator.java | 62 +++++++++++++++++++ .../src/it/unive/dais/po2/myjdk/Pair.java | 5 +- .../it/unive/dais/po2/other/SortingTest.java | 3 +- .../it/unive/dais/po2/patterns/Singleton.java | 49 ++++----------- .../dais/po2/patterns/SyncSingleton.java | 53 ++++++++++++++++ .../src/it/unive/dais/po2/zoo/Main.java | 3 +- 8 files changed, 178 insertions(+), 67 deletions(-) create mode 100644 2020/Lezioni/src/esercizi/MapIterator.java create mode 100644 2020/Lezioni/src/it/unive/dais/po2/patterns/SyncSingleton.java diff --git a/2020/Lezioni/.idea/workspace.xml b/2020/Lezioni/.idea/workspace.xml index c738a62..c50ecc7 100644 --- a/2020/Lezioni/.idea/workspace.xml +++ b/2020/Lezioni/.idea/workspace.xml @@ -2,9 +2,13 @@ - + + + + + - + - - + + - + @@ -223,18 +227,18 @@ - + - + - + - + @@ -267,22 +271,22 @@ - + - - + + - - + + - - + + - + diff --git a/2020/Lezioni/src/esercizi/Es_SkipIterator.java b/2020/Lezioni/src/esercizi/Es_SkipIterator.java index ea5f376..103e7f4 100644 --- a/2020/Lezioni/src/esercizi/Es_SkipIterator.java +++ b/2020/Lezioni/src/esercizi/Es_SkipIterator.java @@ -9,7 +9,23 @@ public class Es_SkipIterator { public static class SkipArrayList extends ArrayList { @Override public Iterator iterator() { - Iterator it = super.iterator(); + // senza usare l'iteratore originale + return new Iterator() { + private int pos = 0; + @Override + public boolean hasNext() { + return pos < size(); + } + + @Override + public T next() { + T x = get(pos); + pos += 2; + return x; + } + }; + // usando l'iteratore originale + /*Iterator it = super.iterator(); return new Iterator() { @Override public boolean hasNext() { @@ -22,7 +38,7 @@ public T next() { if (it.hasNext()) it.next(); return x; } - }; + };*/ } } diff --git a/2020/Lezioni/src/esercizi/MapIterator.java b/2020/Lezioni/src/esercizi/MapIterator.java new file mode 100644 index 0000000..d6116f2 --- /dev/null +++ b/2020/Lezioni/src/esercizi/MapIterator.java @@ -0,0 +1,62 @@ +package esercizi; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.function.Function; + +public class MapIterator { + + + + public static Iterator mapIterator(Iterator it, Function f) { + return new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public B next() { + try { + A a = it.next(); + return f.apply(a); + } + catch (RuntimeException e) { + System.err.println(String.format("exception caught: %s", e.getMessage())); + if (hasNext()) return next(); + else throw e; + } + } + }; + } + + + public static void main(String[] args) { + List l = new ArrayList<>(); + for (int i = -10; i < 20; ++i) { + l.add(i); + } + Iterator it = mapIterator(l.iterator(), (n) -> (double) n); + while (it.hasNext()) { + double x = it.next(); + System.out.println(x); + } + + Iterator it2 = mapIterator(l.iterator(), + new Function<>() { + @Override + public Double apply(Integer n) { + if (n > 0) return (double) n; + else throw new RuntimeException(); + } + }); + while (it2.hasNext()) { + double x = it2.next(); + System.out.println(x); + } + + + } + +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java index 2ffe513..5a4411d 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java @@ -2,11 +2,12 @@ // Classe per coppie IMMUTABILI public class Pair { - public A first; - public B second; + public final A first; + public final B second; public Pair(A a, B b) { this.first = a; this.second = b; } + } diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/SortingTest.java b/2020/Lezioni/src/it/unive/dais/po2/other/SortingTest.java index 3484862..2558372 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/other/SortingTest.java +++ b/2020/Lezioni/src/it/unive/dais/po2/other/SortingTest.java @@ -125,8 +125,7 @@ public String toString() { } - + // firme "fake" delle sort static > void sort__(List list) {} - static void sort__(List list, Comparator c) {} } diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java b/2020/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java index 7b8becd..04deade 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java +++ b/2020/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java @@ -3,50 +3,25 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class Singleton { - - public static class Display { - // eventuali campi privati non-statici - - @Nullable - private static Display instance = null; - - private int cnt; +// verione minimal di un singleton, ma non è riutilizzabile - private Display(int n) { - this.cnt = n; - // inizializzazione di tutti i tuoi campi privati non-statici - } - - // metodi che modificano lo stato - public synchronized int getCounter() { - return cnt; - } - - public synchronized void setCounter(int n) { - cnt = n; - } +public class Singleton { - public synchronized void incrementCounter() { - ++cnt; - } + @Nullable + private static Singleton instance = null; - @NotNull - public static synchronized Display getInstance(int cnt) { - if (instance == null) { - instance = new Display(cnt); - } - synchronized (instance) { - instance.cnt *= 3; // esempio di modifica dello stato - } - return instance; + // fa ciò che fa la cache con una entry + @NotNull + public static Singleton getInstance() { + if (instance == null) { + instance = new Singleton(); } - + return instance; } + public static void main(String[] args) { - Display d = Display.getInstance(50); - Display d2 = Display.getInstance(50); + Singleton s = Singleton.getInstance(); } diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/SyncSingleton.java b/2020/Lezioni/src/it/unive/dais/po2/patterns/SyncSingleton.java new file mode 100644 index 0000000..06fac0c --- /dev/null +++ b/2020/Lezioni/src/it/unive/dais/po2/patterns/SyncSingleton.java @@ -0,0 +1,53 @@ +package it.unive.dais.po2.patterns; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class SyncSingleton { + + public static class Display { + // eventuali campi privati non-statici + + @Nullable + private static Display instance = null; + + private int cnt; + + private Display(int n) { + this.cnt = n; + // inizializzazione di tutti i tuoi campi privati non-statici + } + + // metodi che modificano lo stato + public synchronized int getCounter() { + return cnt; + } + + public synchronized void setCounter(int n) { + cnt = n; + } + + public synchronized void incrementCounter() { + ++cnt; + } + + @NotNull + public static synchronized Display getInstance(int cnt) { + if (instance == null) { + instance = new Display(cnt); + } + synchronized (instance) { + instance.cnt *= 3; // esempio di modifica dello stato + } + return instance; + } + + } + + public static void main(String[] args) { + Display d = Display.getInstance(50); + Display d2 = Display.getInstance(50); + } + + +} diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java b/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java index b671218..0377492 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java +++ b/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java @@ -16,6 +16,7 @@ public static void main(String[] args) { dogs.add(fido); dogs.add(baldo); + /* List l3 = new ArrayList(); l3.add(new Cat(10, "idfif")); @@ -36,7 +37,7 @@ public static void main(String[] args) { l2.add(new Dog(10, "fido")); Function f1 = (Animal s) -> new Cat(s.weight, "blu"); - Function f = f1; + Function f = f1;*/ From c5f56ed387be2fb78613208d00d6a4697b18e7b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 22 May 2020 19:47:36 +0200 Subject: [PATCH 036/202] Esercizi 11 --- .../inspectionProfiles/Project_Default.xml | 4 + 2020/Lezioni/.idea/workspace.xml | 36 +++---- .../src/esercizi/camporese/Laureando.java | 32 +++++++ .../src/esercizi/camporese/Persona.java | 32 +++++++ .../src/esercizi/camporese/Studente.java | 50 ++++++++++ .../esercizi/camporese/main/Es_Camporese.java | 93 +++++++++++++++++++ .../src/it/unive/dais/po2/myjdk/Pair.java | 4 +- 7 files changed, 231 insertions(+), 20 deletions(-) create mode 100644 2020/Lezioni/src/esercizi/camporese/Laureando.java create mode 100644 2020/Lezioni/src/esercizi/camporese/Persona.java create mode 100644 2020/Lezioni/src/esercizi/camporese/Studente.java create mode 100644 2020/Lezioni/src/esercizi/camporese/main/Es_Camporese.java diff --git a/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml b/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml index f60e880..6da4be7 100644 --- a/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml +++ b/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml @@ -5,6 +5,10 @@ + + l, boolean ancheBocciati, Function f) { + ArrayList c = new ArrayList<>(); + for (A a : l) + if (ancheBocciati || (((Studente) a).getMediaVoti()) >= 6) + c.add(f.apply(a)); + return c; + } + + public static B fold(Iterable l, B zero, BiFunction f) { + B acc = zero; + for (A a : l) + acc = f.apply(a, acc); + return acc; + } + + public static void printIterable(Iterable l, String str) { + System.out.println(str); + for (A a : l) + System.out.println(a); + } + + + public static void main(String[] args) { + ArrayList persone = new ArrayList<>(); + persone.add(new Persona("Alvise", 43)); + persone.add(new Persona("Federico", 26)); + persone.add(new Persona("Stefano", 35)); + persone.add(new Persona("Matteo", 29)); + persone.add(new Studente("Francesco", 20, 2, 3)); + + /*USA PERSONA::COMPARETO*/ + Collections.sort(persone); + /*USA COMPARE*/ + /* + Collections.sort(persone, (o1, o2) -> o1.età - o2.età); + */ + printIterable(persone, "\nPERSONE ORDINATE PER ETA':\n"); + + ArrayList studenti = new ArrayList<>(); + studenti.add(new Studente("Francesco", 20, 2, 3)); + studenti.add(new Studente("Andrea", 23, 2, 6)); + studenti.add(new Studente("Daniele", 22, 3, 5)); + studenti.add(new Studente("Michele", 21, 2, 7)); + studenti.add(new Studente("Nicola", 20, 1, 10)); + + /* + NON PUOI USARE Collections.sort(studenti); SE NON USI I GENERICS SU PERSONA + perchè useresti il compareTo di Persona e non quello di Studente + siccome Studente estende Persona che implementa Comparable + */ + /*USA STUDENTE::COMPARETO*/ + //Collections.sort(studenti); + /*USA COMPARE*/ + studenti.sort((o1, o2) -> { + int diffAnnoCorso = o1.getClasse() - o2.getClasse(); + return diffAnnoCorso == 0 ? o1.eta - o2.eta : diffAnnoCorso; + }); + printIterable(studenti, "\nSTUDENTI ORDINATI PER (ANNOCORSO, ETA'):\n"); + + ArrayList tuttiVengonoPromossi = mapFilter(studenti, true, (x) -> + { + Studente daRestituire = x.clone(); + daRestituire.prossimoAnno(); + return daRestituire; + }); + printIterable(tuttiVengonoPromossi, "\nSTUDENTI CHE SI SPERA ARRIVINO NELLA NUOVA CLASSE:\n"); + + ArrayList studentiPromossi = mapFilter(studenti, false, (x) -> + { + Studente daRestituire = x.clone(); + daRestituire.prossimoAnno(); + return daRestituire; + }); + printIterable(studentiPromossi, "\nSTUDENTI PROMOSSI VERAMENTE:\n"); + + System.out.println("\nMEDIA VOTI DELLE PERSONE PROMOSSE: " + fold(tuttiVengonoPromossi, 0, (Studente x, Integer acc) -> acc + x.getMediaVoti()) / tuttiVengonoPromossi.size() + "\n"); + } +} + diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java b/2020/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java index 5a4411d..fa7ce6b 100644 --- a/2020/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java +++ b/2020/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java @@ -2,8 +2,8 @@ // Classe per coppie IMMUTABILI public class Pair { - public final A first; - public final B second; + public A first; + public B second; public Pair(A a, B b) { this.first = a; From 588d2001724a387f4875aa0d24de78d2356b7423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Sun, 7 Feb 2021 19:03:31 +0100 Subject: [PATCH 037/202] Dir rename --- {2018 => 2017-18}/pitagora/pitagora.iml | 0 .../src/it/unive/dais/po/pitagora/Lato.java | 0 .../src/it/unive/dais/po/pitagora/Main.java | 0 .../src/it/unive/dais/po/pitagora/Numeric.java | 0 .../tinylib/src/it/unive/dais/po/tinylib/Main.java | 0 .../src/it/unive/dais/po/tinylib/MyIterable.java | 0 .../src/it/unive/dais/po/tinylib/MyIterator.java | 0 .../src/it/unive/dais/po/tinylib/MyList.java | 0 .../src/it/unive/dais/po/tinylib/MyNode.java | 0 .../src/it/unive/dais/po/tinylib/MyNodeList.java | 0 .../unive/dais/po/tinylib/MyNodeListIterator.java | 0 .../src/it/unive/dais/po/tinylib/MySequence.java | 0 .../it/unive/dais/po/tinylib/NotFoundException.java | 0 .../src/it/unive/dais/po/tinylib/OverloadTest.java | 0 {2018 => 2017-18}/tinylib/tinylib.iml | 0 {2019 => 2018-19}/TinyJDK/TinyJDK.iml | 0 .../TinyJDK/META-INF/TinyJDK.kotlin_module | Bin .../TinyJDK/out/production/TinyJDK/TinyJDK.iml | 0 {2019 => 2018-19}/TinyJDK/src/collections/Main.java | 0 .../src/collections/MyAbstractArrayListSet.java | 0 .../TinyJDK/src/collections/MyArrayList.java | 0 .../TinyJDK/src/collections/MyArrayListSet.java | 0 .../src/collections/MyArrayListSortedSet.java | 0 .../TinyJDK/src/collections/MyCollection.java | 0 .../TinyJDK/src/collections/MyIterable.java | 0 .../TinyJDK/src/collections/MyIterator.java | 0 .../TinyJDK/src/collections/MyList.java | 0 .../TinyJDK/src/collections/MyListMap.java | 0 .../TinyJDK/src/collections/MyMap.java | 0 .../TinyJDK/src/collections/MySet.java | 0 .../TinyJDK/src/collections/MySortedSet.java | 0 .../TinyJDK/src/collections/NotFoundException.java | 0 {2019 => 2018-19}/TinyJDK/src/collections/Pair.java | 0 .../TinyJDK/src/misc/AbstractErrors.java | 0 .../TinyJDK/src/misc/ErrorInInvoke.java | 0 {2019 => 2018-19}/TinyJDK/src/misc/GenericSort.java | 0 .../src/patterns/consumer_producer/Main.java | 0 .../TinyJDK/src/patterns/singleton/Main.java | 0 .../src/patterns/singleton/RandomSingleton.java | 0 .../TinyJDK/src/threads/SynchronizedMain.java | 0 .../TinyJDK/src/threads/pool/Main.java | 0 .../TinyJDK/src/threads/pool/ThreadPool.java | 0 {2019 => 2018-19}/lezione18-19/lezione18-19.iml | 0 .../META-INF/lezione18-19.kotlin_module | Bin .../lezione18-19/src/Main__Collections.java | 0 .../lezione18-19/src/Main__Experiments.java | 0 .../lezione18-19/src/Main__Functional.java | 0 .../lezione18-19/src/collections/MyArrayList.java | 0 .../lezione18-19/src/collections/MyCollection.java | 0 .../lezione18-19/src/collections/MyIterator.java | 0 .../lezione18-19/src/collections/Pair.java | 0 {2019 => 2018-19}/lezione18-19/src/zoo/Animale.java | 0 {2019 => 2018-19}/lezione18-19/src/zoo/Cane.java | 0 {2019 => 2018-19}/lezione18-19/src/zoo/Gatto.java | 0 .../lezione18-19/src/zoo/PastoreTedesco.java | 0 .../Lezioni/.idea/codeStyles/codeStyleConfig.xml | 0 {2020 => 2019-20}/Lezioni/.idea/description.html | 0 .../.idea/inspectionProfiles/Project_Default.xml | 0 {2020 => 2019-20}/Lezioni/.idea/misc.xml | 0 {2020 => 2019-20}/Lezioni/.idea/modules.xml | 0 .../Lezioni/.idea/project-template.xml | 0 {2020 => 2019-20}/Lezioni/.idea/uiDesigner.xml | 0 {2020 => 2019-20}/Lezioni/.idea/vcs.xml | 0 {2020 => 2019-20}/Lezioni/.idea/workspace.xml | 0 {2020 => 2019-20}/Lezioni/Lezioni.iml | 0 .../Lezioni/META-INF/Lezioni.kotlin_module | Bin {2020 => 2019-20}/Lezioni/src/esercizi/Andrei.java | 0 .../Lezioni/src/esercizi/DynamicDispatch.java | 0 .../Lezioni/src/esercizi/Es_RandomIterator.java | 0 .../Lezioni/src/esercizi/Es_SkipIterator.java | 0 .../Lezioni/src/esercizi/EsercizioCache.java | 0 .../Lezioni/src/esercizi/MapIterator.java | 0 {2020 => 2019-20}/Lezioni/src/esercizi/PO1.java | 0 .../src/esercizi/ScittoGennaio2019__Pool.java | 0 .../Lezioni/src/esercizi/ScrittoGennaio2019.java | 0 .../Lezioni/src/esercizi/ScrittoGennaio2020.java | 0 .../Lezioni/src/esercizi/ScrittoGiugno2019.java | 0 .../Lezioni/src/esercizi/camporese/Laureando.java | 0 .../Lezioni/src/esercizi/camporese/Persona.java | 0 .../Lezioni/src/esercizi/camporese/Studente.java | 0 .../src/esercizi/camporese/main/Es_Camporese.java | 0 .../src/it/unive/dais/po2/myjdk/MyArrayList.java | 0 .../src/it/unive/dais/po2/myjdk/MyCollection.java | 0 .../src/it/unive/dais/po2/myjdk/MyHashMap.java | 0 .../src/it/unive/dais/po2/myjdk/MyHashSet.java | 0 .../src/it/unive/dais/po2/myjdk/MyIdentityMap.java | 0 .../src/it/unive/dais/po2/myjdk/MyIterable.java | 0 .../src/it/unive/dais/po2/myjdk/MyIterator.java | 0 .../src/it/unive/dais/po2/myjdk/MyLinkedList.java | 0 .../src/it/unive/dais/po2/myjdk/MyLinkedSet.java | 0 .../Lezioni/src/it/unive/dais/po2/myjdk/MyList.java | 0 .../Lezioni/src/it/unive/dais/po2/myjdk/MyMap.java | 0 .../Lezioni/src/it/unive/dais/po2/myjdk/MySet.java | 0 .../it/unive/dais/po2/myjdk/NotFoundException.java | 0 .../unive/dais/po2/myjdk/OutOfBoundsException.java | 0 .../Lezioni/src/it/unive/dais/po2/myjdk/Pair.java | 0 .../src/it/unive/dais/po2/myjdk/TestList.java | 0 .../src/it/unive/dais/po2/myjdk/TestMap.java | 0 .../dais/po2/other/FilosofiACena__Francesco.java | 0 .../src/it/unive/dais/po2/other/ForEachTest.java | 0 .../unive/dais/po2/other/FunctionalPrimitives.java | 0 .../src/it/unive/dais/po2/other/FunctionalTest.java | 0 .../src/it/unive/dais/po2/other/IteratorTest.java | 0 .../unive/dais/po2/other/IteratorTest__Mattia.java | 0 .../src/it/unive/dais/po2/other/RevArrayList.java | 0 .../src/it/unive/dais/po2/other/RevIterator.java | 0 .../src/it/unive/dais/po2/other/RevTest.java | 0 .../src/it/unive/dais/po2/other/SortingTest.java | 0 .../unive/dais/po2/patterns/ConsumerProducer.java | 0 .../src/it/unive/dais/po2/patterns/Singleton.java | 0 .../it/unive/dais/po2/patterns/SyncSingleton.java | 0 .../it/unive/dais/po2/patterns/factory/Circle.java | 0 .../it/unive/dais/po2/patterns/factory/Main.java | 0 .../unive/dais/po2/patterns/factory/Rectangle.java | 0 .../it/unive/dais/po2/patterns/factory/Shape.java | 0 .../dais/po2/patterns/factory/ShapeFactory.java | 0 .../po2/patterns/factory/ShapeFactoryWider.java | 0 .../dais/po2/threading/ThreadCreationMain.java | 0 .../Lezioni/src/it/unive/dais/po2/zoo/Animal.java | 0 .../Lezioni/src/it/unive/dais/po2/zoo/Cat.java | 0 .../src/it/unive/dais/po2/zoo/ColoredAnimal.java | 0 .../Lezioni/src/it/unive/dais/po2/zoo/Creature.java | 0 .../Lezioni/src/it/unive/dais/po2/zoo/Dog.java | 0 .../Lezioni/src/it/unive/dais/po2/zoo/Main.java | 0 .../Lezioni/src/it/unive/dais/po2/zoo/Persian.java | 0 125 files changed, 0 insertions(+), 0 deletions(-) rename {2018 => 2017-18}/pitagora/pitagora.iml (100%) rename {2018 => 2017-18}/pitagora/src/it/unive/dais/po/pitagora/Lato.java (100%) rename {2018 => 2017-18}/pitagora/src/it/unive/dais/po/pitagora/Main.java (100%) rename {2018 => 2017-18}/pitagora/src/it/unive/dais/po/pitagora/Numeric.java (100%) rename {2018 => 2017-18}/tinylib/src/it/unive/dais/po/tinylib/Main.java (100%) rename {2018 => 2017-18}/tinylib/src/it/unive/dais/po/tinylib/MyIterable.java (100%) rename {2018 => 2017-18}/tinylib/src/it/unive/dais/po/tinylib/MyIterator.java (100%) rename {2018 => 2017-18}/tinylib/src/it/unive/dais/po/tinylib/MyList.java (100%) rename {2018 => 2017-18}/tinylib/src/it/unive/dais/po/tinylib/MyNode.java (100%) rename {2018 => 2017-18}/tinylib/src/it/unive/dais/po/tinylib/MyNodeList.java (100%) rename {2018 => 2017-18}/tinylib/src/it/unive/dais/po/tinylib/MyNodeListIterator.java (100%) rename {2018 => 2017-18}/tinylib/src/it/unive/dais/po/tinylib/MySequence.java (100%) rename {2018 => 2017-18}/tinylib/src/it/unive/dais/po/tinylib/NotFoundException.java (100%) rename {2018 => 2017-18}/tinylib/src/it/unive/dais/po/tinylib/OverloadTest.java (100%) rename {2018 => 2017-18}/tinylib/tinylib.iml (100%) rename {2019 => 2018-19}/TinyJDK/TinyJDK.iml (100%) rename {2019 => 2018-19}/TinyJDK/out/production/TinyJDK/META-INF/TinyJDK.kotlin_module (100%) rename {2019 => 2018-19}/TinyJDK/out/production/TinyJDK/TinyJDK.iml (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/Main.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MyAbstractArrayListSet.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MyArrayList.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MyArrayListSet.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MyArrayListSortedSet.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MyCollection.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MyIterable.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MyIterator.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MyList.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MyListMap.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MyMap.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MySet.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/MySortedSet.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/NotFoundException.java (100%) rename {2019 => 2018-19}/TinyJDK/src/collections/Pair.java (100%) rename {2019 => 2018-19}/TinyJDK/src/misc/AbstractErrors.java (100%) rename {2019 => 2018-19}/TinyJDK/src/misc/ErrorInInvoke.java (100%) rename {2019 => 2018-19}/TinyJDK/src/misc/GenericSort.java (100%) rename {2019 => 2018-19}/TinyJDK/src/patterns/consumer_producer/Main.java (100%) rename {2019 => 2018-19}/TinyJDK/src/patterns/singleton/Main.java (100%) rename {2019 => 2018-19}/TinyJDK/src/patterns/singleton/RandomSingleton.java (100%) rename {2019 => 2018-19}/TinyJDK/src/threads/SynchronizedMain.java (100%) rename {2019 => 2018-19}/TinyJDK/src/threads/pool/Main.java (100%) rename {2019 => 2018-19}/TinyJDK/src/threads/pool/ThreadPool.java (100%) rename {2019 => 2018-19}/lezione18-19/lezione18-19.iml (100%) rename {2019 => 2018-19}/lezione18-19/out/production/lezione18-19/META-INF/lezione18-19.kotlin_module (100%) rename {2019 => 2018-19}/lezione18-19/src/Main__Collections.java (100%) rename {2019 => 2018-19}/lezione18-19/src/Main__Experiments.java (100%) rename {2019 => 2018-19}/lezione18-19/src/Main__Functional.java (100%) rename {2019 => 2018-19}/lezione18-19/src/collections/MyArrayList.java (100%) rename {2019 => 2018-19}/lezione18-19/src/collections/MyCollection.java (100%) rename {2019 => 2018-19}/lezione18-19/src/collections/MyIterator.java (100%) rename {2019 => 2018-19}/lezione18-19/src/collections/Pair.java (100%) rename {2019 => 2018-19}/lezione18-19/src/zoo/Animale.java (100%) rename {2019 => 2018-19}/lezione18-19/src/zoo/Cane.java (100%) rename {2019 => 2018-19}/lezione18-19/src/zoo/Gatto.java (100%) rename {2019 => 2018-19}/lezione18-19/src/zoo/PastoreTedesco.java (100%) rename {2020 => 2019-20}/Lezioni/.idea/codeStyles/codeStyleConfig.xml (100%) rename {2020 => 2019-20}/Lezioni/.idea/description.html (100%) rename {2020 => 2019-20}/Lezioni/.idea/inspectionProfiles/Project_Default.xml (100%) rename {2020 => 2019-20}/Lezioni/.idea/misc.xml (100%) rename {2020 => 2019-20}/Lezioni/.idea/modules.xml (100%) rename {2020 => 2019-20}/Lezioni/.idea/project-template.xml (100%) rename {2020 => 2019-20}/Lezioni/.idea/uiDesigner.xml (100%) rename {2020 => 2019-20}/Lezioni/.idea/vcs.xml (100%) rename {2020 => 2019-20}/Lezioni/.idea/workspace.xml (100%) rename {2020 => 2019-20}/Lezioni/Lezioni.iml (100%) rename {2020 => 2019-20}/Lezioni/out/production/Lezioni/META-INF/Lezioni.kotlin_module (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/Andrei.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/DynamicDispatch.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/Es_RandomIterator.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/Es_SkipIterator.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/EsercizioCache.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/MapIterator.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/PO1.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/ScittoGennaio2019__Pool.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/ScrittoGennaio2019.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/ScrittoGennaio2020.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/ScrittoGiugno2019.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/camporese/Laureando.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/camporese/Persona.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/camporese/Studente.java (100%) rename {2020 => 2019-20}/Lezioni/src/esercizi/camporese/main/Es_Camporese.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MyHashSet.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MyIterable.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MyIterator.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedSet.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MyList.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MyMap.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/MySet.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/NotFoundException.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/OutOfBoundsException.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/TestList.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/other/FilosofiACena__Francesco.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/other/ForEachTest.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/other/IteratorTest.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/other/IteratorTest__Mattia.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/other/RevIterator.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/other/RevTest.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/other/SortingTest.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/patterns/ConsumerProducer.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/patterns/SyncSingleton.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/patterns/factory/Circle.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/patterns/factory/Main.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/patterns/factory/Rectangle.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/patterns/factory/Shape.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactory.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactoryWider.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/threading/ThreadCreationMain.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/zoo/Animal.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/zoo/Cat.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/zoo/Creature.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/zoo/Dog.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/zoo/Main.java (100%) rename {2020 => 2019-20}/Lezioni/src/it/unive/dais/po2/zoo/Persian.java (100%) diff --git a/2018/pitagora/pitagora.iml b/2017-18/pitagora/pitagora.iml similarity index 100% rename from 2018/pitagora/pitagora.iml rename to 2017-18/pitagora/pitagora.iml diff --git a/2018/pitagora/src/it/unive/dais/po/pitagora/Lato.java b/2017-18/pitagora/src/it/unive/dais/po/pitagora/Lato.java similarity index 100% rename from 2018/pitagora/src/it/unive/dais/po/pitagora/Lato.java rename to 2017-18/pitagora/src/it/unive/dais/po/pitagora/Lato.java diff --git a/2018/pitagora/src/it/unive/dais/po/pitagora/Main.java b/2017-18/pitagora/src/it/unive/dais/po/pitagora/Main.java similarity index 100% rename from 2018/pitagora/src/it/unive/dais/po/pitagora/Main.java rename to 2017-18/pitagora/src/it/unive/dais/po/pitagora/Main.java diff --git a/2018/pitagora/src/it/unive/dais/po/pitagora/Numeric.java b/2017-18/pitagora/src/it/unive/dais/po/pitagora/Numeric.java similarity index 100% rename from 2018/pitagora/src/it/unive/dais/po/pitagora/Numeric.java rename to 2017-18/pitagora/src/it/unive/dais/po/pitagora/Numeric.java diff --git a/2018/tinylib/src/it/unive/dais/po/tinylib/Main.java b/2017-18/tinylib/src/it/unive/dais/po/tinylib/Main.java similarity index 100% rename from 2018/tinylib/src/it/unive/dais/po/tinylib/Main.java rename to 2017-18/tinylib/src/it/unive/dais/po/tinylib/Main.java diff --git a/2018/tinylib/src/it/unive/dais/po/tinylib/MyIterable.java b/2017-18/tinylib/src/it/unive/dais/po/tinylib/MyIterable.java similarity index 100% rename from 2018/tinylib/src/it/unive/dais/po/tinylib/MyIterable.java rename to 2017-18/tinylib/src/it/unive/dais/po/tinylib/MyIterable.java diff --git a/2018/tinylib/src/it/unive/dais/po/tinylib/MyIterator.java b/2017-18/tinylib/src/it/unive/dais/po/tinylib/MyIterator.java similarity index 100% rename from 2018/tinylib/src/it/unive/dais/po/tinylib/MyIterator.java rename to 2017-18/tinylib/src/it/unive/dais/po/tinylib/MyIterator.java diff --git a/2018/tinylib/src/it/unive/dais/po/tinylib/MyList.java b/2017-18/tinylib/src/it/unive/dais/po/tinylib/MyList.java similarity index 100% rename from 2018/tinylib/src/it/unive/dais/po/tinylib/MyList.java rename to 2017-18/tinylib/src/it/unive/dais/po/tinylib/MyList.java diff --git a/2018/tinylib/src/it/unive/dais/po/tinylib/MyNode.java b/2017-18/tinylib/src/it/unive/dais/po/tinylib/MyNode.java similarity index 100% rename from 2018/tinylib/src/it/unive/dais/po/tinylib/MyNode.java rename to 2017-18/tinylib/src/it/unive/dais/po/tinylib/MyNode.java diff --git a/2018/tinylib/src/it/unive/dais/po/tinylib/MyNodeList.java b/2017-18/tinylib/src/it/unive/dais/po/tinylib/MyNodeList.java similarity index 100% rename from 2018/tinylib/src/it/unive/dais/po/tinylib/MyNodeList.java rename to 2017-18/tinylib/src/it/unive/dais/po/tinylib/MyNodeList.java diff --git a/2018/tinylib/src/it/unive/dais/po/tinylib/MyNodeListIterator.java b/2017-18/tinylib/src/it/unive/dais/po/tinylib/MyNodeListIterator.java similarity index 100% rename from 2018/tinylib/src/it/unive/dais/po/tinylib/MyNodeListIterator.java rename to 2017-18/tinylib/src/it/unive/dais/po/tinylib/MyNodeListIterator.java diff --git a/2018/tinylib/src/it/unive/dais/po/tinylib/MySequence.java b/2017-18/tinylib/src/it/unive/dais/po/tinylib/MySequence.java similarity index 100% rename from 2018/tinylib/src/it/unive/dais/po/tinylib/MySequence.java rename to 2017-18/tinylib/src/it/unive/dais/po/tinylib/MySequence.java diff --git a/2018/tinylib/src/it/unive/dais/po/tinylib/NotFoundException.java b/2017-18/tinylib/src/it/unive/dais/po/tinylib/NotFoundException.java similarity index 100% rename from 2018/tinylib/src/it/unive/dais/po/tinylib/NotFoundException.java rename to 2017-18/tinylib/src/it/unive/dais/po/tinylib/NotFoundException.java diff --git a/2018/tinylib/src/it/unive/dais/po/tinylib/OverloadTest.java b/2017-18/tinylib/src/it/unive/dais/po/tinylib/OverloadTest.java similarity index 100% rename from 2018/tinylib/src/it/unive/dais/po/tinylib/OverloadTest.java rename to 2017-18/tinylib/src/it/unive/dais/po/tinylib/OverloadTest.java diff --git a/2018/tinylib/tinylib.iml b/2017-18/tinylib/tinylib.iml similarity index 100% rename from 2018/tinylib/tinylib.iml rename to 2017-18/tinylib/tinylib.iml diff --git a/2019/TinyJDK/TinyJDK.iml b/2018-19/TinyJDK/TinyJDK.iml similarity index 100% rename from 2019/TinyJDK/TinyJDK.iml rename to 2018-19/TinyJDK/TinyJDK.iml diff --git a/2019/TinyJDK/out/production/TinyJDK/META-INF/TinyJDK.kotlin_module b/2018-19/TinyJDK/out/production/TinyJDK/META-INF/TinyJDK.kotlin_module similarity index 100% rename from 2019/TinyJDK/out/production/TinyJDK/META-INF/TinyJDK.kotlin_module rename to 2018-19/TinyJDK/out/production/TinyJDK/META-INF/TinyJDK.kotlin_module diff --git a/2019/TinyJDK/out/production/TinyJDK/TinyJDK.iml b/2018-19/TinyJDK/out/production/TinyJDK/TinyJDK.iml similarity index 100% rename from 2019/TinyJDK/out/production/TinyJDK/TinyJDK.iml rename to 2018-19/TinyJDK/out/production/TinyJDK/TinyJDK.iml diff --git a/2019/TinyJDK/src/collections/Main.java b/2018-19/TinyJDK/src/collections/Main.java similarity index 100% rename from 2019/TinyJDK/src/collections/Main.java rename to 2018-19/TinyJDK/src/collections/Main.java diff --git a/2019/TinyJDK/src/collections/MyAbstractArrayListSet.java b/2018-19/TinyJDK/src/collections/MyAbstractArrayListSet.java similarity index 100% rename from 2019/TinyJDK/src/collections/MyAbstractArrayListSet.java rename to 2018-19/TinyJDK/src/collections/MyAbstractArrayListSet.java diff --git a/2019/TinyJDK/src/collections/MyArrayList.java b/2018-19/TinyJDK/src/collections/MyArrayList.java similarity index 100% rename from 2019/TinyJDK/src/collections/MyArrayList.java rename to 2018-19/TinyJDK/src/collections/MyArrayList.java diff --git a/2019/TinyJDK/src/collections/MyArrayListSet.java b/2018-19/TinyJDK/src/collections/MyArrayListSet.java similarity index 100% rename from 2019/TinyJDK/src/collections/MyArrayListSet.java rename to 2018-19/TinyJDK/src/collections/MyArrayListSet.java diff --git a/2019/TinyJDK/src/collections/MyArrayListSortedSet.java b/2018-19/TinyJDK/src/collections/MyArrayListSortedSet.java similarity index 100% rename from 2019/TinyJDK/src/collections/MyArrayListSortedSet.java rename to 2018-19/TinyJDK/src/collections/MyArrayListSortedSet.java diff --git a/2019/TinyJDK/src/collections/MyCollection.java b/2018-19/TinyJDK/src/collections/MyCollection.java similarity index 100% rename from 2019/TinyJDK/src/collections/MyCollection.java rename to 2018-19/TinyJDK/src/collections/MyCollection.java diff --git a/2019/TinyJDK/src/collections/MyIterable.java b/2018-19/TinyJDK/src/collections/MyIterable.java similarity index 100% rename from 2019/TinyJDK/src/collections/MyIterable.java rename to 2018-19/TinyJDK/src/collections/MyIterable.java diff --git a/2019/TinyJDK/src/collections/MyIterator.java b/2018-19/TinyJDK/src/collections/MyIterator.java similarity index 100% rename from 2019/TinyJDK/src/collections/MyIterator.java rename to 2018-19/TinyJDK/src/collections/MyIterator.java diff --git a/2019/TinyJDK/src/collections/MyList.java b/2018-19/TinyJDK/src/collections/MyList.java similarity index 100% rename from 2019/TinyJDK/src/collections/MyList.java rename to 2018-19/TinyJDK/src/collections/MyList.java diff --git a/2019/TinyJDK/src/collections/MyListMap.java b/2018-19/TinyJDK/src/collections/MyListMap.java similarity index 100% rename from 2019/TinyJDK/src/collections/MyListMap.java rename to 2018-19/TinyJDK/src/collections/MyListMap.java diff --git a/2019/TinyJDK/src/collections/MyMap.java b/2018-19/TinyJDK/src/collections/MyMap.java similarity index 100% rename from 2019/TinyJDK/src/collections/MyMap.java rename to 2018-19/TinyJDK/src/collections/MyMap.java diff --git a/2019/TinyJDK/src/collections/MySet.java b/2018-19/TinyJDK/src/collections/MySet.java similarity index 100% rename from 2019/TinyJDK/src/collections/MySet.java rename to 2018-19/TinyJDK/src/collections/MySet.java diff --git a/2019/TinyJDK/src/collections/MySortedSet.java b/2018-19/TinyJDK/src/collections/MySortedSet.java similarity index 100% rename from 2019/TinyJDK/src/collections/MySortedSet.java rename to 2018-19/TinyJDK/src/collections/MySortedSet.java diff --git a/2019/TinyJDK/src/collections/NotFoundException.java b/2018-19/TinyJDK/src/collections/NotFoundException.java similarity index 100% rename from 2019/TinyJDK/src/collections/NotFoundException.java rename to 2018-19/TinyJDK/src/collections/NotFoundException.java diff --git a/2019/TinyJDK/src/collections/Pair.java b/2018-19/TinyJDK/src/collections/Pair.java similarity index 100% rename from 2019/TinyJDK/src/collections/Pair.java rename to 2018-19/TinyJDK/src/collections/Pair.java diff --git a/2019/TinyJDK/src/misc/AbstractErrors.java b/2018-19/TinyJDK/src/misc/AbstractErrors.java similarity index 100% rename from 2019/TinyJDK/src/misc/AbstractErrors.java rename to 2018-19/TinyJDK/src/misc/AbstractErrors.java diff --git a/2019/TinyJDK/src/misc/ErrorInInvoke.java b/2018-19/TinyJDK/src/misc/ErrorInInvoke.java similarity index 100% rename from 2019/TinyJDK/src/misc/ErrorInInvoke.java rename to 2018-19/TinyJDK/src/misc/ErrorInInvoke.java diff --git a/2019/TinyJDK/src/misc/GenericSort.java b/2018-19/TinyJDK/src/misc/GenericSort.java similarity index 100% rename from 2019/TinyJDK/src/misc/GenericSort.java rename to 2018-19/TinyJDK/src/misc/GenericSort.java diff --git a/2019/TinyJDK/src/patterns/consumer_producer/Main.java b/2018-19/TinyJDK/src/patterns/consumer_producer/Main.java similarity index 100% rename from 2019/TinyJDK/src/patterns/consumer_producer/Main.java rename to 2018-19/TinyJDK/src/patterns/consumer_producer/Main.java diff --git a/2019/TinyJDK/src/patterns/singleton/Main.java b/2018-19/TinyJDK/src/patterns/singleton/Main.java similarity index 100% rename from 2019/TinyJDK/src/patterns/singleton/Main.java rename to 2018-19/TinyJDK/src/patterns/singleton/Main.java diff --git a/2019/TinyJDK/src/patterns/singleton/RandomSingleton.java b/2018-19/TinyJDK/src/patterns/singleton/RandomSingleton.java similarity index 100% rename from 2019/TinyJDK/src/patterns/singleton/RandomSingleton.java rename to 2018-19/TinyJDK/src/patterns/singleton/RandomSingleton.java diff --git a/2019/TinyJDK/src/threads/SynchronizedMain.java b/2018-19/TinyJDK/src/threads/SynchronizedMain.java similarity index 100% rename from 2019/TinyJDK/src/threads/SynchronizedMain.java rename to 2018-19/TinyJDK/src/threads/SynchronizedMain.java diff --git a/2019/TinyJDK/src/threads/pool/Main.java b/2018-19/TinyJDK/src/threads/pool/Main.java similarity index 100% rename from 2019/TinyJDK/src/threads/pool/Main.java rename to 2018-19/TinyJDK/src/threads/pool/Main.java diff --git a/2019/TinyJDK/src/threads/pool/ThreadPool.java b/2018-19/TinyJDK/src/threads/pool/ThreadPool.java similarity index 100% rename from 2019/TinyJDK/src/threads/pool/ThreadPool.java rename to 2018-19/TinyJDK/src/threads/pool/ThreadPool.java diff --git a/2019/lezione18-19/lezione18-19.iml b/2018-19/lezione18-19/lezione18-19.iml similarity index 100% rename from 2019/lezione18-19/lezione18-19.iml rename to 2018-19/lezione18-19/lezione18-19.iml diff --git a/2019/lezione18-19/out/production/lezione18-19/META-INF/lezione18-19.kotlin_module b/2018-19/lezione18-19/out/production/lezione18-19/META-INF/lezione18-19.kotlin_module similarity index 100% rename from 2019/lezione18-19/out/production/lezione18-19/META-INF/lezione18-19.kotlin_module rename to 2018-19/lezione18-19/out/production/lezione18-19/META-INF/lezione18-19.kotlin_module diff --git a/2019/lezione18-19/src/Main__Collections.java b/2018-19/lezione18-19/src/Main__Collections.java similarity index 100% rename from 2019/lezione18-19/src/Main__Collections.java rename to 2018-19/lezione18-19/src/Main__Collections.java diff --git a/2019/lezione18-19/src/Main__Experiments.java b/2018-19/lezione18-19/src/Main__Experiments.java similarity index 100% rename from 2019/lezione18-19/src/Main__Experiments.java rename to 2018-19/lezione18-19/src/Main__Experiments.java diff --git a/2019/lezione18-19/src/Main__Functional.java b/2018-19/lezione18-19/src/Main__Functional.java similarity index 100% rename from 2019/lezione18-19/src/Main__Functional.java rename to 2018-19/lezione18-19/src/Main__Functional.java diff --git a/2019/lezione18-19/src/collections/MyArrayList.java b/2018-19/lezione18-19/src/collections/MyArrayList.java similarity index 100% rename from 2019/lezione18-19/src/collections/MyArrayList.java rename to 2018-19/lezione18-19/src/collections/MyArrayList.java diff --git a/2019/lezione18-19/src/collections/MyCollection.java b/2018-19/lezione18-19/src/collections/MyCollection.java similarity index 100% rename from 2019/lezione18-19/src/collections/MyCollection.java rename to 2018-19/lezione18-19/src/collections/MyCollection.java diff --git a/2019/lezione18-19/src/collections/MyIterator.java b/2018-19/lezione18-19/src/collections/MyIterator.java similarity index 100% rename from 2019/lezione18-19/src/collections/MyIterator.java rename to 2018-19/lezione18-19/src/collections/MyIterator.java diff --git a/2019/lezione18-19/src/collections/Pair.java b/2018-19/lezione18-19/src/collections/Pair.java similarity index 100% rename from 2019/lezione18-19/src/collections/Pair.java rename to 2018-19/lezione18-19/src/collections/Pair.java diff --git a/2019/lezione18-19/src/zoo/Animale.java b/2018-19/lezione18-19/src/zoo/Animale.java similarity index 100% rename from 2019/lezione18-19/src/zoo/Animale.java rename to 2018-19/lezione18-19/src/zoo/Animale.java diff --git a/2019/lezione18-19/src/zoo/Cane.java b/2018-19/lezione18-19/src/zoo/Cane.java similarity index 100% rename from 2019/lezione18-19/src/zoo/Cane.java rename to 2018-19/lezione18-19/src/zoo/Cane.java diff --git a/2019/lezione18-19/src/zoo/Gatto.java b/2018-19/lezione18-19/src/zoo/Gatto.java similarity index 100% rename from 2019/lezione18-19/src/zoo/Gatto.java rename to 2018-19/lezione18-19/src/zoo/Gatto.java diff --git a/2019/lezione18-19/src/zoo/PastoreTedesco.java b/2018-19/lezione18-19/src/zoo/PastoreTedesco.java similarity index 100% rename from 2019/lezione18-19/src/zoo/PastoreTedesco.java rename to 2018-19/lezione18-19/src/zoo/PastoreTedesco.java diff --git a/2020/Lezioni/.idea/codeStyles/codeStyleConfig.xml b/2019-20/Lezioni/.idea/codeStyles/codeStyleConfig.xml similarity index 100% rename from 2020/Lezioni/.idea/codeStyles/codeStyleConfig.xml rename to 2019-20/Lezioni/.idea/codeStyles/codeStyleConfig.xml diff --git a/2020/Lezioni/.idea/description.html b/2019-20/Lezioni/.idea/description.html similarity index 100% rename from 2020/Lezioni/.idea/description.html rename to 2019-20/Lezioni/.idea/description.html diff --git a/2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml b/2019-20/Lezioni/.idea/inspectionProfiles/Project_Default.xml similarity index 100% rename from 2020/Lezioni/.idea/inspectionProfiles/Project_Default.xml rename to 2019-20/Lezioni/.idea/inspectionProfiles/Project_Default.xml diff --git a/2020/Lezioni/.idea/misc.xml b/2019-20/Lezioni/.idea/misc.xml similarity index 100% rename from 2020/Lezioni/.idea/misc.xml rename to 2019-20/Lezioni/.idea/misc.xml diff --git a/2020/Lezioni/.idea/modules.xml b/2019-20/Lezioni/.idea/modules.xml similarity index 100% rename from 2020/Lezioni/.idea/modules.xml rename to 2019-20/Lezioni/.idea/modules.xml diff --git a/2020/Lezioni/.idea/project-template.xml b/2019-20/Lezioni/.idea/project-template.xml similarity index 100% rename from 2020/Lezioni/.idea/project-template.xml rename to 2019-20/Lezioni/.idea/project-template.xml diff --git a/2020/Lezioni/.idea/uiDesigner.xml b/2019-20/Lezioni/.idea/uiDesigner.xml similarity index 100% rename from 2020/Lezioni/.idea/uiDesigner.xml rename to 2019-20/Lezioni/.idea/uiDesigner.xml diff --git a/2020/Lezioni/.idea/vcs.xml b/2019-20/Lezioni/.idea/vcs.xml similarity index 100% rename from 2020/Lezioni/.idea/vcs.xml rename to 2019-20/Lezioni/.idea/vcs.xml diff --git a/2020/Lezioni/.idea/workspace.xml b/2019-20/Lezioni/.idea/workspace.xml similarity index 100% rename from 2020/Lezioni/.idea/workspace.xml rename to 2019-20/Lezioni/.idea/workspace.xml diff --git a/2020/Lezioni/Lezioni.iml b/2019-20/Lezioni/Lezioni.iml similarity index 100% rename from 2020/Lezioni/Lezioni.iml rename to 2019-20/Lezioni/Lezioni.iml diff --git a/2020/Lezioni/out/production/Lezioni/META-INF/Lezioni.kotlin_module b/2019-20/Lezioni/out/production/Lezioni/META-INF/Lezioni.kotlin_module similarity index 100% rename from 2020/Lezioni/out/production/Lezioni/META-INF/Lezioni.kotlin_module rename to 2019-20/Lezioni/out/production/Lezioni/META-INF/Lezioni.kotlin_module diff --git a/2020/Lezioni/src/esercizi/Andrei.java b/2019-20/Lezioni/src/esercizi/Andrei.java similarity index 100% rename from 2020/Lezioni/src/esercizi/Andrei.java rename to 2019-20/Lezioni/src/esercizi/Andrei.java diff --git a/2020/Lezioni/src/esercizi/DynamicDispatch.java b/2019-20/Lezioni/src/esercizi/DynamicDispatch.java similarity index 100% rename from 2020/Lezioni/src/esercizi/DynamicDispatch.java rename to 2019-20/Lezioni/src/esercizi/DynamicDispatch.java diff --git a/2020/Lezioni/src/esercizi/Es_RandomIterator.java b/2019-20/Lezioni/src/esercizi/Es_RandomIterator.java similarity index 100% rename from 2020/Lezioni/src/esercizi/Es_RandomIterator.java rename to 2019-20/Lezioni/src/esercizi/Es_RandomIterator.java diff --git a/2020/Lezioni/src/esercizi/Es_SkipIterator.java b/2019-20/Lezioni/src/esercizi/Es_SkipIterator.java similarity index 100% rename from 2020/Lezioni/src/esercizi/Es_SkipIterator.java rename to 2019-20/Lezioni/src/esercizi/Es_SkipIterator.java diff --git a/2020/Lezioni/src/esercizi/EsercizioCache.java b/2019-20/Lezioni/src/esercizi/EsercizioCache.java similarity index 100% rename from 2020/Lezioni/src/esercizi/EsercizioCache.java rename to 2019-20/Lezioni/src/esercizi/EsercizioCache.java diff --git a/2020/Lezioni/src/esercizi/MapIterator.java b/2019-20/Lezioni/src/esercizi/MapIterator.java similarity index 100% rename from 2020/Lezioni/src/esercizi/MapIterator.java rename to 2019-20/Lezioni/src/esercizi/MapIterator.java diff --git a/2020/Lezioni/src/esercizi/PO1.java b/2019-20/Lezioni/src/esercizi/PO1.java similarity index 100% rename from 2020/Lezioni/src/esercizi/PO1.java rename to 2019-20/Lezioni/src/esercizi/PO1.java diff --git a/2020/Lezioni/src/esercizi/ScittoGennaio2019__Pool.java b/2019-20/Lezioni/src/esercizi/ScittoGennaio2019__Pool.java similarity index 100% rename from 2020/Lezioni/src/esercizi/ScittoGennaio2019__Pool.java rename to 2019-20/Lezioni/src/esercizi/ScittoGennaio2019__Pool.java diff --git a/2020/Lezioni/src/esercizi/ScrittoGennaio2019.java b/2019-20/Lezioni/src/esercizi/ScrittoGennaio2019.java similarity index 100% rename from 2020/Lezioni/src/esercizi/ScrittoGennaio2019.java rename to 2019-20/Lezioni/src/esercizi/ScrittoGennaio2019.java diff --git a/2020/Lezioni/src/esercizi/ScrittoGennaio2020.java b/2019-20/Lezioni/src/esercizi/ScrittoGennaio2020.java similarity index 100% rename from 2020/Lezioni/src/esercizi/ScrittoGennaio2020.java rename to 2019-20/Lezioni/src/esercizi/ScrittoGennaio2020.java diff --git a/2020/Lezioni/src/esercizi/ScrittoGiugno2019.java b/2019-20/Lezioni/src/esercizi/ScrittoGiugno2019.java similarity index 100% rename from 2020/Lezioni/src/esercizi/ScrittoGiugno2019.java rename to 2019-20/Lezioni/src/esercizi/ScrittoGiugno2019.java diff --git a/2020/Lezioni/src/esercizi/camporese/Laureando.java b/2019-20/Lezioni/src/esercizi/camporese/Laureando.java similarity index 100% rename from 2020/Lezioni/src/esercizi/camporese/Laureando.java rename to 2019-20/Lezioni/src/esercizi/camporese/Laureando.java diff --git a/2020/Lezioni/src/esercizi/camporese/Persona.java b/2019-20/Lezioni/src/esercizi/camporese/Persona.java similarity index 100% rename from 2020/Lezioni/src/esercizi/camporese/Persona.java rename to 2019-20/Lezioni/src/esercizi/camporese/Persona.java diff --git a/2020/Lezioni/src/esercizi/camporese/Studente.java b/2019-20/Lezioni/src/esercizi/camporese/Studente.java similarity index 100% rename from 2020/Lezioni/src/esercizi/camporese/Studente.java rename to 2019-20/Lezioni/src/esercizi/camporese/Studente.java diff --git a/2020/Lezioni/src/esercizi/camporese/main/Es_Camporese.java b/2019-20/Lezioni/src/esercizi/camporese/main/Es_Camporese.java similarity index 100% rename from 2020/Lezioni/src/esercizi/camporese/main/Es_Camporese.java rename to 2019-20/Lezioni/src/esercizi/camporese/main/Es_Camporese.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyArrayList.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyCollection.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyHashMap.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashSet.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyHashSet.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyHashSet.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyHashSet.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyIdentityMap.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIterable.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyIterable.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIterable.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyIterable.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIterator.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyIterator.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyIterator.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyIterator.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedList.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedSet.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedSet.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedSet.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyLinkedSet.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyList.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyList.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyList.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyList.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MyMap.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyMap.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MyMap.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MyMap.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/MySet.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MySet.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/MySet.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/MySet.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/NotFoundException.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/NotFoundException.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/NotFoundException.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/NotFoundException.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/OutOfBoundsException.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/OutOfBoundsException.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/OutOfBoundsException.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/OutOfBoundsException.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/Pair.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestList.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/TestList.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/TestList.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/TestList.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java b/2019-20/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/myjdk/TestMap.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/FilosofiACena__Francesco.java b/2019-20/Lezioni/src/it/unive/dais/po2/other/FilosofiACena__Francesco.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/other/FilosofiACena__Francesco.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/other/FilosofiACena__Francesco.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/ForEachTest.java b/2019-20/Lezioni/src/it/unive/dais/po2/other/ForEachTest.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/other/ForEachTest.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/other/ForEachTest.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java b/2019-20/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/other/FunctionalPrimitives.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java b/2019-20/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/other/FunctionalTest.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/IteratorTest.java b/2019-20/Lezioni/src/it/unive/dais/po2/other/IteratorTest.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/other/IteratorTest.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/other/IteratorTest.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/IteratorTest__Mattia.java b/2019-20/Lezioni/src/it/unive/dais/po2/other/IteratorTest__Mattia.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/other/IteratorTest__Mattia.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/other/IteratorTest__Mattia.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java b/2019-20/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/other/RevArrayList.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/RevIterator.java b/2019-20/Lezioni/src/it/unive/dais/po2/other/RevIterator.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/other/RevIterator.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/other/RevIterator.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/RevTest.java b/2019-20/Lezioni/src/it/unive/dais/po2/other/RevTest.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/other/RevTest.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/other/RevTest.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/other/SortingTest.java b/2019-20/Lezioni/src/it/unive/dais/po2/other/SortingTest.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/other/SortingTest.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/other/SortingTest.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/ConsumerProducer.java b/2019-20/Lezioni/src/it/unive/dais/po2/patterns/ConsumerProducer.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/patterns/ConsumerProducer.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/patterns/ConsumerProducer.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java b/2019-20/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/patterns/Singleton.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/SyncSingleton.java b/2019-20/Lezioni/src/it/unive/dais/po2/patterns/SyncSingleton.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/patterns/SyncSingleton.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/patterns/SyncSingleton.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Circle.java b/2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/Circle.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Circle.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/Circle.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Main.java b/2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/Main.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Main.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/Main.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Rectangle.java b/2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/Rectangle.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Rectangle.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/Rectangle.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Shape.java b/2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/Shape.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/Shape.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/Shape.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactory.java b/2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactory.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactory.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactory.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactoryWider.java b/2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactoryWider.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactoryWider.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/patterns/factory/ShapeFactoryWider.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/threading/ThreadCreationMain.java b/2019-20/Lezioni/src/it/unive/dais/po2/threading/ThreadCreationMain.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/threading/ThreadCreationMain.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/threading/ThreadCreationMain.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Animal.java b/2019-20/Lezioni/src/it/unive/dais/po2/zoo/Animal.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/zoo/Animal.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/zoo/Animal.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Cat.java b/2019-20/Lezioni/src/it/unive/dais/po2/zoo/Cat.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/zoo/Cat.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/zoo/Cat.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java b/2019-20/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/zoo/ColoredAnimal.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Creature.java b/2019-20/Lezioni/src/it/unive/dais/po2/zoo/Creature.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/zoo/Creature.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/zoo/Creature.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Dog.java b/2019-20/Lezioni/src/it/unive/dais/po2/zoo/Dog.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/zoo/Dog.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/zoo/Dog.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java b/2019-20/Lezioni/src/it/unive/dais/po2/zoo/Main.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/zoo/Main.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/zoo/Main.java diff --git a/2020/Lezioni/src/it/unive/dais/po2/zoo/Persian.java b/2019-20/Lezioni/src/it/unive/dais/po2/zoo/Persian.java similarity index 100% rename from 2020/Lezioni/src/it/unive/dais/po2/zoo/Persian.java rename to 2019-20/Lezioni/src/it/unive/dais/po2/zoo/Persian.java From 49ce2d1d6b90df827ea839e4cf76591b21d3175d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 8 Feb 2021 15:39:41 +0100 Subject: [PATCH 038/202] Inizio semestre 2021 --- 2019-20/Lezioni/.idea/workspace.xml | 21 --------------------- 2019-20/Lezioni/Lezioni.iml | 9 +++++++++ 2020-21/Lezioni/.idea/.gitignore | 3 +++ 2020-21/Lezioni/.idea/misc.xml | 6 ++++++ 2020-21/Lezioni/.idea/modules.xml | 8 ++++++++ 2020-21/Lezioni/.idea/vcs.xml | 6 ++++++ 2020-21/Lezioni/Lezioni.iml | 11 +++++++++++ 2020-21/Lezioni/src/Main.java | 5 +++++ 8 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 2020-21/Lezioni/.idea/.gitignore create mode 100644 2020-21/Lezioni/.idea/misc.xml create mode 100644 2020-21/Lezioni/.idea/modules.xml create mode 100644 2020-21/Lezioni/.idea/vcs.xml create mode 100644 2020-21/Lezioni/Lezioni.iml create mode 100644 2020-21/Lezioni/src/Main.java diff --git a/2019-20/Lezioni/.idea/workspace.xml b/2019-20/Lezioni/.idea/workspace.xml index 09c173c..16e9320 100644 --- a/2019-20/Lezioni/.idea/workspace.xml +++ b/2019-20/Lezioni/.idea/workspace.xml @@ -64,7 +64,6 @@ { + void apply(A x); + } + + public interface Supplier { + A get(); + } + + public interface Runnable { + void run(); + } + + public static int f(int n, Function g) { + return g.apply(n); + } + + + public static int increment(int x) { return x + 1; } + + private static class DecrementFun implements Function { + @Override + public Integer apply(Integer x) { + return x - 1; + } + } + + + public static void main(String[] args) { + int z = f(10, new DecrementFun()); // named class + + int y = f(7, TestFun::increment); // method reference + + int z = f(10, new Function() { // anonymous class + @Override + public Integer apply(Integer x) { + return x - 1; + } + }); + + int z2 = f(10, (x) -> x - 1); // lambda + } + +} diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/tinyjdk/Map.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/tinyjdk/Map.java index 4e2210d..ea87878 100644 --- a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/tinyjdk/Map.java +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/tinyjdk/Map.java @@ -14,5 +14,11 @@ public KeyNotFoundException(Object k) { super(String.format("key = %s", k)); } } - } + + + + + + + From e0da6d91b2ca7f9f4606f3149fcb3fc211b80b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 31 Mar 2021 16:24:59 +0200 Subject: [PATCH 055/202] Update TestFun.java --- .../po2/aa2020_21/functional/TestFun.java | 79 ++++++++++++++++--- 1 file changed, 70 insertions(+), 9 deletions(-) diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java index a9ae7ab..737ffe3 100644 --- a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java @@ -1,11 +1,13 @@ package it.unive.dais.po2.aa2020_21.functional; -import javax.lang.model.type.IntersectionType; -import java.util.function.Function; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; public class TestFun { /* - // VERSIONE IN C + // VERSIONE IN C: puntatori a funzione int f(int n, int(*g)(int)) { // f ha tipo: int(*)(int, int(*)(int)) return g(n); } @@ -16,23 +18,24 @@ int main() { int y = f(7, &increment); } - // VERSIONE IN F# + // VERSIONE IN F#: funzioni come valori del primo ordine let f (n, g) = g(n) // f : int * (int -> int) -> int let increment x = x + 1 // increment : int -> int let main () = let y = f(7, increment) - let z = f(10, fun x -> x - 1) + let z = f(10, fun x -> x - 1) // lambda */ - // le 4 forme di lambda + // le 4 forme di lambda (non serve definire le proprio, ci sono nel JDK) + @FunctionalInterface public interface Function { B apply(A x); } public interface Consumer { - void apply(A x); + void accept(A x); } public interface Supplier { @@ -43,13 +46,38 @@ public interface Runnable { void run(); } + // altre functional interface + @FunctionalInterface + public interface BiFunction { + R apply(A x, B y); + } + + public interface TriFunction { + R apply(A x, B b, C c); + } + + public interface QuadFunction { + R apply(A x, B b, C c, D d); + } + + public static int f(int n, Function g) { return g.apply(n); } + private int delta = 4; + public static int increment(int x) { return x + 1; } + public int decrement(int x) { return x - this.delta; } + + public void increaseDelta() { ++this.delta; } + + public int miometodo(int a, String b, ArrayList>> m) { return 0; } + + public int generateNum() { return 23; } + private static class DecrementFun implements Function { @Override public Integer apply(Integer x) { @@ -57,11 +85,43 @@ public Integer apply(Integer x) { } } + /* la map in F# + let rec map f l = // map : ('a -> 'b) -> 'a list -> 'b list + match l with + | [] -> [] + | x :: xs -> f x :: map f xs + */ + + public static List map(Iterable c, Function f) { + List r = new ArrayList<>(); + for(A a : c) r.add(f.apply(a)); + return r; + } + public static void main(String[] args) { - int z = f(10, new DecrementFun()); // named class + int z = f(10, new DecrementFun()); // istanza di classe passata come argomento e subsunta al tipo Function + + TestFun mytestfun = new TestFun(); // istanza di tipo TestFun + + // method reference + Function u = TestFun::increment; // reference ad un metodo statico + BiFunction u2 = TestFun::decrement; // reference ad un metodo non-statico di un oggetto arbitrario di tipo TestFun + Function u3 = mytestfun::decrement; // reference ad un metodo non-statico di un particolare oggetto di tipo TestFun - int y = f(7, TestFun::increment); // method reference + // reference ad un metodo non-statico di un particolare oggetto di tipo TestFun: abbiamo solo i parametri visibili come type argument + TriFunction>>, Integer> u4 = mytestfun::miometodo; + // reference ad un metodo non-statico di un oggetto arbitrario di tipo TestFun: abbiamo un parametro IN PIU' di tipo TestFun + QuadFunction>>, Integer> u5 = TestFun::miometodo; + + Function j1 = TestFun::generateNum; // reference ad un metodo non-statico di un oggetto arbitrario di tipo TestFun + Supplier j2 = mytestfun::generateNum; // reference ad un metodo non-statico di un particolare oggetto di tipo TestFun + + Consumer k1 = TestFun::increaseDelta; // reference ad un metodo non-statico di un oggetto arbitrario di tipo TestFun + Runnable k2 = mytestfun::increaseDelta; // reference ad un metodo non-statico di un particolare oggetto di tipo TestFun + + + int y = f(7, TestFun::increment); // reference ad un metodo statico passato come Function int z = f(10, new Function() { // anonymous class @Override @@ -71,6 +131,7 @@ public Integer apply(Integer x) { }); int z2 = f(10, (x) -> x - 1); // lambda + } } From c1fdf67fc18eb87458b8019bfe35ec3b9e4b100e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 7 Apr 2021 15:44:03 +0200 Subject: [PATCH 056/202] Lezione 7/4/21 --- .../unive/dais/po2/aa2020_21/functional/TestFun.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java index 737ffe3..1829182 100644 --- a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java @@ -92,7 +92,7 @@ public Integer apply(Integer x) { | x :: xs -> f x :: map f xs */ - public static List map(Iterable c, Function f) { + public static List map(Iterable c, Function f) { // POLIMORFISMO PARAMETRICO FIRST-CLASS List r = new ArrayList<>(); for(A a : c) r.add(f.apply(a)); return r; @@ -100,6 +100,14 @@ public static List map(Iterable c, Function f) { public static void main(String[] args) { + + Collection a1 = new ArrayList<>(); + + List l1 = TestFun.map(a1, String::length); + List l2 = TestFun.map(a1, (s) -> s.concat("ciao")); + + + int z = f(10, new DecrementFun()); // istanza di classe passata come argomento e subsunta al tipo Function TestFun mytestfun = new TestFun(); // istanza di tipo TestFun @@ -123,7 +131,7 @@ public static void main(String[] args) { int y = f(7, TestFun::increment); // reference ad un metodo statico passato come Function - int z = f(10, new Function() { // anonymous class + int z1 = f(10, new Function() { // anonymous class @Override public Integer apply(Integer x) { return x - 1; From cdd9613757d972c81879be806087939560c077e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 12 Apr 2021 17:19:20 +0200 Subject: [PATCH 057/202] Lezione 12/4/21: iteratori rovesci e skip --- 2019-20/Lezioni/.idea/workspace.xml | 2 + .../aa2020_21/functional/IteratorTest.java | 134 ++++++++++++++++++ .../po2/aa2020_21/functional/TestFun.java | 2 +- 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/IteratorTest.java diff --git a/2019-20/Lezioni/.idea/workspace.xml b/2019-20/Lezioni/.idea/workspace.xml index 6c92eb0..a3b726d 100644 --- a/2019-20/Lezioni/.idea/workspace.xml +++ b/2019-20/Lezioni/.idea/workspace.xml @@ -2,7 +2,9 @@ + + Iterator skipIterator(Iterator it, int step) { + // TODO per casa + } + + // modo 1.A: rovesciare l'iteratore di input + public static Iterator revIterator(Iterator it) { + // lista dritta, iteratore rovescio + List l = new ArrayList<>(); + while (it.hasNext()) { + l.add(it.next()); + } + return new Iterator() { + private int pos = l.size() - 1; + + @Override + public boolean hasNext() { + return pos >= 0; + } + + @Override + public A next() { + return l.get(pos--); + } + }; + } + + // modo 1.B: rovesciare l'iteratore di input + public static Iterator revIterator2(Iterator it) { + // lista rovescia, iteratore dritto + List l = new ArrayList<>(); + while (it.hasNext()) { + l.add(0, it.next()); + } + return l.iterator(); + } + + // modo 2: rovesciare l'iteratore overridandolo + public static class RevArrayList extends ArrayList { + @Override + public Iterator iterator() { // produce un iteratore che va al contrario + return new Iterator() { + private int pos = RevArrayList.this.size() - 1; + + @Override + public boolean hasNext() { + return pos >= 0; + } + + @Override + public T next() { + return RevArrayList.this.get(pos--); + } + }; + } + } + + // classe che offre iteratori capaci di skippare ("saltare") elementi secondo un valore di step + public static class SkipArrayList extends ArrayList { + private final int step; + + public SkipArrayList(int step) { + this.step = step; + } + + @Override + public Iterator iterator() { // produce un iteratore che va al contrario + return new Iterator() { + private int pos = step < 0 ? SkipArrayList.this.size() - 1 : 0; + + @Override + public boolean hasNext() { + return step < 0 ? (pos >= 0) : (pos < SkipArrayList.this.size()); + } + + @Override + public T next() { + T x = SkipArrayList.this.get(pos); + pos += step; + return x; + } + }; + } + } + + + public static Iterator mapIterator(Iterator it, Function f) { + + return new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public B next() { + return f.apply(it.next()); + } + }; + } + + public static Iterator mapAndRevIterator(Iterator it, Function f) { + return revIterator(mapIterator(it, f)); + } + + + public static void main(String[] args) { + + List l = new SkipArrayList<>(-3); + for (int i = 0; i < 10; ++i) { + char[] a = new char[i]; + for (int j = 0; j < a.length; ++j) + a[j] = 'a'; + l.add(new String(a)); + } + + Iterator it = mapIterator(l.iterator(), (String s) -> s.length()); + while (it.hasNext()) { + int n = it.next(); + System.out.println(n); + } + + } + + +} diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java index 1829182..b53957c 100644 --- a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/TestFun.java @@ -85,7 +85,7 @@ public Integer apply(Integer x) { } } - /* la map in F# + /* la funzione map in F# let rec map f l = // map : ('a -> 'b) -> 'a list -> 'b list match l with | [] -> [] From e297d718a8177a0065d1f03b89421c61362426ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 14 Apr 2021 15:44:47 +0200 Subject: [PATCH 058/202] Lezione 14/4/21 --- 2019-20/Lezioni/.idea/workspace.xml | 2 -- .../aa2020_21/functional/IteratorTest.java | 8 +++-- .../aa2020_21/patterns/ConsumerProducer.java | 35 +++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ConsumerProducer.java diff --git a/2019-20/Lezioni/.idea/workspace.xml b/2019-20/Lezioni/.idea/workspace.xml index a3b726d..6c92eb0 100644 --- a/2019-20/Lezioni/.idea/workspace.xml +++ b/2019-20/Lezioni/.idea/workspace.xml @@ -2,9 +2,7 @@ - - Iterator skipIterator(Iterator it, int step) { - // TODO per casa + @NotNull + public static Iterator skipIterator(@NotNull Iterator it, int step) { + // TODO da implementare come esercizio: deve comportarsi come l'iteratore di SkipArrayList più in basso + throw new RuntimeException("not implemented"); } // modo 1.A: rovesciare l'iteratore di input diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ConsumerProducer.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ConsumerProducer.java new file mode 100644 index 0000000..b291c36 --- /dev/null +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ConsumerProducer.java @@ -0,0 +1,35 @@ +package it.unive.dais.po2.aa2020_21.patterns; + +public class ConsumerProducer { + + public static void loop() { + for (int i = 0; i < 10; ++i) { + System.out.printf("thread#%d: %d\n", Thread.currentThread().getId(), i); + try { + Thread.sleep(300); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + public static class MyThread extends Thread { + + @Override + public void run() { + loop(); + } + + } + + public static void main(String[] args) { + Thread t1 = new MyThread(); + Thread t2 = new MyThread(); + t1.start(); + t2.start(); + + loop(); + + } + +} From 28929fb2616c4d70c14ccd9cba6280688c4262ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 19 Apr 2021 17:27:32 +0200 Subject: [PATCH 059/202] Lezione 19/4/21 --- .../aa2020_21/patterns/ConsumerProducer.java | 55 +++++++++++++------ .../po2/aa2020_21/patterns/ThreadTest.java | 46 ++++++++++++++++ 2 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadTest.java diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ConsumerProducer.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ConsumerProducer.java index b291c36..2d75baf 100644 --- a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ConsumerProducer.java +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ConsumerProducer.java @@ -1,34 +1,57 @@ package it.unive.dais.po2.aa2020_21.patterns; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + public class ConsumerProducer { - public static void loop() { - for (int i = 0; i < 10; ++i) { - System.out.printf("thread#%d: %d\n", Thread.currentThread().getId(), i); - try { - Thread.sleep(300); - } catch (InterruptedException e) { - e.printStackTrace(); + private static final BlockingQueue l = new LinkedBlockingQueue<>(); + + public static class Consumer extends Thread { + + @Override + public void run() { + System.out.println("consumer started"); + while (true) { + try { + int n = l.take(); + System.out.printf("consumer: %d\n", n); + } catch (InterruptedException e) { + e.printStackTrace(); + } } } } - public static class MyThread extends Thread { - + public static class Producer extends Thread { @Override public void run() { - loop(); + System.out.println("producer started"); + Random rnd = new Random(); + while (true) { + l.add(rnd.nextInt() % 100); + } } - } public static void main(String[] args) { - Thread t1 = new MyThread(); - Thread t2 = new MyThread(); - t1.start(); - t2.start(); + try { + + Thread c1 = new Consumer(); + Thread p1 = new Producer(); - loop(); + c1.start(); + p1.start(); + + c1.join(); + p1.join(); + + } catch (Exception e) { + e.printStackTrace(); + } } diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadTest.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadTest.java new file mode 100644 index 0000000..2113d52 --- /dev/null +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadTest.java @@ -0,0 +1,46 @@ +package it.unive.dais.po2.aa2020_21.patterns; + +public class ThreadTest { + + public static void loop() { + for (int i = 0; i < 10; ++i) { + System.out.printf("thread#%d: %d\n", Thread.currentThread().getId(), i); + try { + Thread.sleep(300); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + public static class MyThread extends Thread { + + @Override + public void run() { + loop(); + } + + } + + public static void main(String[] args) { + Thread t1 = new MyThread(); + Thread t2 = new MyThread(); + t1.start(); + t2.start(); + + new Thread(() -> { loop(); }).start(); + + loop(); + + + try { + t1.join(); + t2.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + + } + +} From ba70f0d75f24232383806f429fafaece35f50a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 21 Apr 2021 15:48:39 +0200 Subject: [PATCH 060/202] Lezione 21/4/21 --- .../po2/aa2020_21/patterns/ThreadPool.java | 86 +++++++++++++++++++ .../po2/aa2020_21/patterns/ThreadTest.java | 2 + 2 files changed, 88 insertions(+) create mode 100644 2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadPool.java diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadPool.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadPool.java new file mode 100644 index 0000000..22cc627 --- /dev/null +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadPool.java @@ -0,0 +1,86 @@ +package it.unive.dais.po2.aa2020_21.patterns; + +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public class ThreadPool { + + private final List threads = new ArrayList<>(); + + public ThreadPool() {} + + public ThreadPool(int prespawn) { + // TODO + } + + private class PooledThread extends Thread { + @NotNull + private Runnable r; + + public PooledThread(Runnable r) { + this.r = r; + } + + @Override + public void run() { + while (true) { + try { + r.run(); + } catch (Exception e) { + System.out.printf("%s: exception caught\n", this); + } + threads.add(this); + try { + wait(); // TODO controllare se la notify() può accadere prima della wait() + } catch (InterruptedException e) { + e.printStackTrace(); + throw new RuntimeException(String.format("%s: interrupted. Aborting...", this)); + } + } + } + + @Override + public String toString() { + return String.format("thread#%d[%s]", getId(), getName()); + } + + public void setRunnable(Runnable r) { + this.r = r; + } + } + + @NotNull + public Thread acquire(@NotNull Runnable r) { + PooledThread t; + if (threads.isEmpty()) { + t = new PooledThread(r); + t.start(); + } + else { + // TODO critical section + t = threads.remove(0); + t.setRunnable(r); + t.notify(); + } + return t; + } + + public static void main(String[] args) { + + ThreadPool pool = new ThreadPool(); + + Thread t = pool.acquire(() -> { + for (int i = 0; i < 10; ++i) { + Thread self = Thread.currentThread(); + System.out.printf("%s[%d]: %d", self.getName(), self.getId(), i); + } + }); + + // TODO testare la pool + + + } + +} diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadTest.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadTest.java index 2113d52..c3855aa 100644 --- a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadTest.java +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadTest.java @@ -15,6 +15,8 @@ public static void loop() { public static class MyThread extends Thread { + public MyThread() { super(); } + @Override public void run() { loop(); From 84fc32ea157419512a2ef958d584dc11b659b6da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 28 Apr 2021 15:31:35 +0200 Subject: [PATCH 061/202] Lezione 28/4/2021 --- 2019-20/Lezioni/.idea/workspace.xml | 2 + .../dais/po2/aa2020_21/others/Sorting.java | 84 +++++++++++++++++++ .../po2/aa2020_21/patterns/ThreadPool.java | 61 +++++++++++--- 3 files changed, 137 insertions(+), 10 deletions(-) create mode 100644 2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Sorting.java diff --git a/2019-20/Lezioni/.idea/workspace.xml b/2019-20/Lezioni/.idea/workspace.xml index 6c92eb0..3acc30e 100644 --- a/2019-20/Lezioni/.idea/workspace.xml +++ b/2019-20/Lezioni/.idea/workspace.xml @@ -2,7 +2,9 @@ + + it, Function f) { + return new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public B next() { + return f.apply(it.next()); + } + }; + } + + + + public static class Creatura {} + + + public static class Pianta extends Creatura { public int foglie; } public static class Girasole extends Pianta {} - public static class Animale implements Comparable { + public static class Animale extends Creatura implements Comparable { protected int peso; public Animale(int peso) { this.peso = peso; @@ -43,10 +87,21 @@ public void eat(Animale a) { this.peso += a.peso; } + public Animale mate(Animale a) { + return new Animale((this.peso + a.peso) / 2); + } + @Override public int compareTo(Animale x) { return this.peso - x.peso; } + + public void m() {} + public void m(int x) {} + public double m(double y, int x) { return 0.; } + public int m(Animale a) { return 1; } + public void m(Cane a) {} + //public int m(Cane a) {} // overload invalido } public static class Cane extends Animale { @@ -57,6 +112,16 @@ public Cane(int peso, int peli) { this.peli = peli; } + @Override + public void eat(Animale a) { + this.peso += this.peso / 2; + } + + @Override + public Cane mate(Animale a) { + return new Cane((this.peso + a.peso) / 3, this.peli); + } + @Override public int compareTo(Animale x) { if (x instanceof Cane) { @@ -68,18 +133,25 @@ public int compareTo(Animale x) { } + public static class Dalmata extends Cane { + public int chiazze; - interface __Comparable { - int compareTo(T x); + public Dalmata(int peso, int peli, int chiazze) { + super(peso, peli); + this.chiazze = chiazze; + } } static > void sort(List list) { Collections.sort(list); } - static void sort(List list, Comparator c) { + static void sort(List list, Comparator c) { Collections.sort(list, c); } + + + } From 558a29a15d8878456b448be165cd2b3ab4497252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 6 May 2021 13:45:11 +0200 Subject: [PATCH 064/202] Lezione 5/5/21 --- .../aa2020_21/functional/IteratorTest.java | 3 +- .../dais/po2/aa2020_21/others/Singleton.java | 28 +++++++++++++ .../po2/aa2020_21/others/SingletonMain.java | 16 ++++++++ .../dais/po2/aa2020_21/others/Sorting.java | 40 ++++++++----------- 4 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Singleton.java create mode 100644 2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/SingletonMain.java diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/IteratorTest.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/IteratorTest.java index 6d4b4e0..1c4adf2 100644 --- a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/IteratorTest.java +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/functional/IteratorTest.java @@ -95,8 +95,7 @@ public T next() { } } - - public static Iterator mapIterator(Iterator it, Function f) { + public static Iterator mapIterator(Iterator it, Function f) { return new Iterator() { @Override diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Singleton.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Singleton.java new file mode 100644 index 0000000..cdb9477 --- /dev/null +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Singleton.java @@ -0,0 +1,28 @@ +package it.unive.dais.po2.aa2020_21.others; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +public class Singleton { + + @Nullable + private static Singleton instance = null; + + private int field; + + private Singleton(int x) { + this.field = x; + } + + public int getField() { return field; } + + @NotNull + public static Singleton getInstance(int x) { + if (instance == null || instance.field != x) { + instance = new Singleton(x); + } + return instance; + } +} \ No newline at end of file diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/SingletonMain.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/SingletonMain.java new file mode 100644 index 0000000..3b6991e --- /dev/null +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/SingletonMain.java @@ -0,0 +1,16 @@ +package it.unive.dais.po2.aa2020_21.others; + +public class SingletonMain { + + public static void main(String[] args) { + try { + Singleton s1 = Singleton.getInstance(7); + Singleton s2 = Singleton.getInstance(8); + Singleton s3 = Singleton.getInstance(9); + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Sorting.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Sorting.java index 6b81a16..c646812 100644 --- a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Sorting.java +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Sorting.java @@ -1,11 +1,9 @@ package it.unive.dais.po2.aa2020_21.others; -import it.unive.dais.po2.aa2020_21.functional.TestFun; import it.unive.dais.po2.aa2020_21.generics.Zoo; -import it.unive.dais.po2.aa2020_21.tinyjdk.Pair; - import java.util.*; import java.util.function.Function; +import static it.unive.dais.po2.aa2020_21.functional.IteratorTest.mapIterator; public class Sorting { @@ -42,31 +40,27 @@ public int compare(Animale a, Animale b) { List c3 = new ArrayList<>(); sort(c3, (o1, o2) -> o1.foglie - o2.foglie); - Iterator r = mapIterator(c2.iterator(), new Function() { - @Override - public Integer apply(Animale a) { - return a.peso; - } - }); + Iterator r = mapIterator(c2.iterator(), Sorting::mymapfun); + // altri test coi wildcard - } + List u1 = new ArrayList<>(); + u1.add(new Cane(50, 50)); + u1.add(new Dalmata(50, 50, 50)); + Cane ca1 = u1.get(0); + Animale ca2 = u1.get(0); - static Iterator mapIterator(Iterator it, Function f) { - return new Iterator() { - @Override - public boolean hasNext() { - return it.hasNext(); - } + List u2 = new ArrayList<>(); + u2.add(new Cane(50, 50)); + u2.add(new Dalmata(50, 50, 50)); + Cane ca3 = u2.get(0); + Animale ca4 = u2.get(0); - @Override - public B next() { - return f.apply(it.next()); - } - }; } - + private static Integer mymapfun(Animale a) { + return a.peso; + } public static class Creatura {} @@ -101,7 +95,7 @@ public void m(int x) {} public double m(double y, int x) { return 0.; } public int m(Animale a) { return 1; } public void m(Cane a) {} - //public int m(Cane a) {} // overload invalido + //public int m(Cane a) {} // overload invalido perché ambiguo } public static class Cane extends Animale { From d5581a1c06e1260ee7b4a80964cba01117fa4a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 10 May 2021 18:26:07 +0200 Subject: [PATCH 065/202] Lezione 10/5/21 --- .../src/it/unive/dais/po2/aa2020_21/others/Sorting.java | 8 ++++---- .../it/unive/dais/po2/aa2020_21/patterns/ThreadPool.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Sorting.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Sorting.java index c646812..37ed229 100644 --- a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Sorting.java +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/others/Sorting.java @@ -47,12 +47,12 @@ public int compare(Animale a, Animale b) { List u1 = new ArrayList<>(); u1.add(new Cane(50, 50)); u1.add(new Dalmata(50, 50, 50)); - Cane ca1 = u1.get(0); - Animale ca2 = u1.get(0); + Cane ca1 = u1.get(0); // non compila + Animale ca2 = u1.get(0); // non compila List u2 = new ArrayList<>(); - u2.add(new Cane(50, 50)); - u2.add(new Dalmata(50, 50, 50)); + u2.add(new Cane(50, 50)); // non compila + u2.add(new Dalmata(50, 50, 50)); // non compila Cane ca3 = u2.get(0); Animale ca4 = u2.get(0); diff --git a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadPool.java b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadPool.java index 218ed36..ea4342a 100644 --- a/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadPool.java +++ b/2020-21/Lezioni/src/it/unive/dais/po2/aa2020_21/patterns/ThreadPool.java @@ -28,7 +28,7 @@ public void setTimeoutInMillis(long tm) { } } public static synchronized void m2() { - Class c = ThreadPool.class; + Class c = ThreadPool.class; synchronized (c) { // blocco } From 2480311d0af51ce00031410dc48fe7dc62077efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Sat, 26 Feb 2022 15:10:49 +0100 Subject: [PATCH 066/202] Lezioni2021-22 --- 2021-22/Lezioni21-22/.idea/.gitignore | 3 + 2021-22/Lezioni21-22/.idea/misc.xml | 6 + 2021-22/Lezioni21-22/.idea/modules.xml | 8 ++ 2021-22/Lezioni21-22/Lezioni21-22.iml | 11 ++ 2021-22/Lezioni21-22/src/Collections.java | 106 ++++++++++++++++++ 2021-22/Lezioni21-22/src/Generics.java | 61 ++++++++++ 2021-22/Lezioni21-22/src/Iterators.java | 51 +++++++++ 2021-22/Lezioni21-22/src/Recap.java | 60 ++++++++++ .../Lezioni21-22/src/tinyjdk/ArrayList.java | 70 ++++++++++++ .../Lezioni21-22/src/tinyjdk/Collection.java | 9 ++ .../Lezioni21-22/src/tinyjdk/Iterable.java | 5 + .../Lezioni21-22/src/tinyjdk/Iterator.java | 6 + 2021-22/Lezioni21-22/src/tinyjdk/List.java | 7 ++ .../src/tinyjdk/NotFoundException.java | 5 + 14 files changed, 408 insertions(+) create mode 100644 2021-22/Lezioni21-22/.idea/.gitignore create mode 100644 2021-22/Lezioni21-22/.idea/misc.xml create mode 100644 2021-22/Lezioni21-22/.idea/modules.xml create mode 100644 2021-22/Lezioni21-22/Lezioni21-22.iml create mode 100644 2021-22/Lezioni21-22/src/Collections.java create mode 100644 2021-22/Lezioni21-22/src/Generics.java create mode 100644 2021-22/Lezioni21-22/src/Iterators.java create mode 100644 2021-22/Lezioni21-22/src/Recap.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/Collection.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/Iterable.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/Iterator.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/List.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/NotFoundException.java diff --git a/2021-22/Lezioni21-22/.idea/.gitignore b/2021-22/Lezioni21-22/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/2021-22/Lezioni21-22/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/2021-22/Lezioni21-22/.idea/misc.xml b/2021-22/Lezioni21-22/.idea/misc.xml new file mode 100644 index 0000000..c3dfb30 --- /dev/null +++ b/2021-22/Lezioni21-22/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2021-22/Lezioni21-22/.idea/modules.xml b/2021-22/Lezioni21-22/.idea/modules.xml new file mode 100644 index 0000000..97cb713 --- /dev/null +++ b/2021-22/Lezioni21-22/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/2021-22/Lezioni21-22/Lezioni21-22.iml b/2021-22/Lezioni21-22/Lezioni21-22.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/2021-22/Lezioni21-22/Lezioni21-22.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/2021-22/Lezioni21-22/src/Collections.java b/2021-22/Lezioni21-22/src/Collections.java new file mode 100644 index 0000000..6103459 --- /dev/null +++ b/2021-22/Lezioni21-22/src/Collections.java @@ -0,0 +1,106 @@ +import java.util.*; + +public class Collections { + + public static class RandomSequence implements Iterable { + + private final int sz; + + public RandomSequence(int sz) { + this.sz = sz; + } + + private static class myIterator implements Iterator { + private int cnt = 0; + private Random rnd = new Random(1123); + private RandomSequence that; + + private myIterator(RandomSequence that) { + this.that = that; + } + + @Override + public boolean hasNext() { + return this.cnt < that.sz; + } + + @Override + public Integer next() { + ++cnt; + return rnd.nextInt(); + } + } + + @Override + public Iterator iterator() { + return new myIterator(this); + } + +// @Override + public Iterator iterator__() { + return new Iterator() { + private int cnt = 0; + private Random rnd = new Random(1123); + + @Override + public boolean hasNext() { + return cnt < sz; + } + + @Override + public Integer next() { + ++cnt; + return rnd.nextInt(); + } + }; + } + } + + public interface I { + void m(); + int p(int x); + } + + public static class C implements I { + @Override + public void m() { + System.out.println("ciao"); + } + + @Override + public int p(int x) { + return x * 2; + } + } + + public static void main(String[] args) { + + I y = new C(); + + I x = new I() { + + @Override + public void m() { + System.out.println("ciao"); + } + + @Override + public int p(int x) { + return x * 2; + } + }; + + for (int n : new RandomSequence(80)) { + System.out.println(n); + } + + RandomSequence o = new RandomSequence(80); + Iterator it = o.iterator(); + while (it.hasNext()) { + int n = it.next(); + System.out.println(n); + } + + } + +} diff --git a/2021-22/Lezioni21-22/src/Generics.java b/2021-22/Lezioni21-22/src/Generics.java new file mode 100644 index 0000000..740d178 --- /dev/null +++ b/2021-22/Lezioni21-22/src/Generics.java @@ -0,0 +1,61 @@ +public class Generics { + + + public static class FloatPair { + private float a, b; + + public FloatPair(float a, float b) { + this.a = a; + this.b = b; + } + + public float getFirst() { return a; } + public float getSecond() { return b; } + + public void setFirst(float a) { + this.a = a; + } + public void setSecond(float b) { + this.b = b; + } + + } + + public static class Pair { + private A a; + private B b; + + public Pair(A a, B b) { + this.a = a; + this.b = b; + } + + public A getFirst() { return a; } + public B getSecond() { return b; } + + public void setFirst(A a) { + this.a = a; + } + public void setSecond(B b) { + this.b = b; + } + + } + + public static int f(int x) { + return x + 1; + } + + + public static void main(String[] args) { + FloatPair p1 = new FloatPair(23.11f, 67.69f); + Pair p2 = new Pair<>(23.11f, 67.69f); + Pair p3 = new Pair<>("ciao", new Recap.Dog(10)); + Pair, Pair> p4 = new Pair<>(new Pair<>(1, 2), new Pair<>(5, 6)); + Pair p5 = new Pair<>("ciao", new Recap.Labrador(10)); + + } + + + +} diff --git a/2021-22/Lezioni21-22/src/Iterators.java b/2021-22/Lezioni21-22/src/Iterators.java new file mode 100644 index 0000000..b9f8fc3 --- /dev/null +++ b/2021-22/Lezioni21-22/src/Iterators.java @@ -0,0 +1,51 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +public class Iterators { + + public static class BackwardsArrayList extends ArrayList { + + public Iterator originalIterator() { + return super.iterator(); + } + + @Override + public Iterator iterator() { + return new Iterator() { + private int index = BackwardsArrayList.this.size() - 1; + @Override + public boolean hasNext() { + return index >= 0; + } + + @Override + public T next() { + return BackwardsArrayList.this.get(index--); + } + }; + } + + public static Iterator reverseIterator(Iterator it) { + List buff = new ArrayList<>(); + while (it.hasNext()) buff.add(0, it.next()); + return buff.iterator(); + } + + } + + public static void main(String[] args) { + Collection l = new BackwardsArrayList<>(); + for (int i = 0; i < 20; l.add(i++)); + + Iterator it = l.iterator(); + while (it.hasNext()) + System.out.println(it.next()); + + /*Iterator it2 = l.originalIterator(); + while (it2.hasNext()) + System.out.println(it2.next()); + */ + } +} diff --git a/2021-22/Lezioni21-22/src/Recap.java b/2021-22/Lezioni21-22/src/Recap.java new file mode 100644 index 0000000..9b77b70 --- /dev/null +++ b/2021-22/Lezioni21-22/src/Recap.java @@ -0,0 +1,60 @@ +public class Recap { + + private int x; + private static float y = Recap.z; + public static float z; + + protected void m() { + this.x = 8; + } + public static void n() { + Recap.y = 8; + } + + private class Boo {} + + public static class Animal { + protected int peso; + public Animal(int peso) { + this.peso = peso; + } + public void eat(Animal a) { + this.peso += a.peso; + } + } + + public static class Dog extends Animal { + + public Dog(int peso) { + super(peso); + } + + @Override + public void eat(Animal a) { + this.peso += a.peso / 2; + } + } + + public static class Labrador extends Dog { + + public Labrador(int peso) { + super(peso); + } + + @Override + public void eat(Animal a) { + this.peso += a.peso * 2; + } + + public void drool() {} + + } + + public static void main(String[] args) { + Animal a = new Labrador(15); + a.eat(new Dog(10)); + + } + + +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java b/2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java new file mode 100644 index 0000000..a2b4d27 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java @@ -0,0 +1,70 @@ +package tinyjdk; + +import java.util.Arrays; + +public class ArrayList implements List { + private Object[] a; + private int pos; + private static final int DEFAULT_CAPACITY = 10; + + public ArrayList() { + this(DEFAULT_CAPACITY); + } + + public ArrayList(int capacity) { + assert capacity > 0; + a = new Object[capacity]; + pos = 0; + } + + @Override + public void add(T e) { + if (pos >= a.length) + a = Arrays.copyOf(a, a.length * 2); + a[pos++] = e; + } + + @Override + public int indexOf(T e) throws NotFoundException { + for (int i = 0; i < pos; ++i) + if (a[i].equals(e)) + return i; + throw new NotFoundException(); + } + + @Override + public void remove(T e) throws NotFoundException { + int i = indexOf(e); + // TODO: ricompattare l'array + } + + @Override + public boolean contains(T e) { + return false; + } + + @Override + public void clear() { + a = new Object[pos + 1]; + } + + @Override + public int size() { + return pos; + } + + @Override + public Iterator iterator() { + return null; + } + + @Override + public T get(int i) { + return (T) a[i]; + } + + @Override + public void set(int i, T e) { + a[i] = e; + } +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/Collection.java b/2021-22/Lezioni21-22/src/tinyjdk/Collection.java new file mode 100644 index 0000000..8dd55b0 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/Collection.java @@ -0,0 +1,9 @@ +package tinyjdk; + +public interface Collection extends Iterable { + void add(X e); + void remove(X e) throws NotFoundException; + boolean contains(X e); + void clear(); + int size(); +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/Iterable.java b/2021-22/Lezioni21-22/src/tinyjdk/Iterable.java new file mode 100644 index 0000000..c06c058 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/Iterable.java @@ -0,0 +1,5 @@ +package tinyjdk; + +public interface Iterable { + Iterator iterator(); +} \ No newline at end of file diff --git a/2021-22/Lezioni21-22/src/tinyjdk/Iterator.java b/2021-22/Lezioni21-22/src/tinyjdk/Iterator.java new file mode 100644 index 0000000..3f1efd3 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/Iterator.java @@ -0,0 +1,6 @@ +package tinyjdk; + +public interface Iterator { + boolean hasNext(); + T next(); +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/List.java b/2021-22/Lezioni21-22/src/tinyjdk/List.java new file mode 100644 index 0000000..2a50808 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/List.java @@ -0,0 +1,7 @@ +package tinyjdk; + +public interface List extends Collection { + X get(int pos); + void set(int pos, X e); + int indexOf(X e) throws NotFoundException; +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/NotFoundException.java b/2021-22/Lezioni21-22/src/tinyjdk/NotFoundException.java new file mode 100644 index 0000000..9146323 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/NotFoundException.java @@ -0,0 +1,5 @@ +package tinyjdk; + +public class NotFoundException extends Exception { + +} From 0ec12c678ffeb3d4a61e6aeade1b92c86e99d1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Sat, 26 Feb 2022 15:16:15 +0100 Subject: [PATCH 067/202] .idea updated --- 2021-22/Lezioni21-22/.idea/.name | 1 + 2021-22/Lezioni21-22/.idea/codeStyles/codeStyleConfig.xml | 5 +++++ 2021-22/Lezioni21-22/.idea/vcs.xml | 6 ++++++ 3 files changed, 12 insertions(+) create mode 100644 2021-22/Lezioni21-22/.idea/.name create mode 100644 2021-22/Lezioni21-22/.idea/codeStyles/codeStyleConfig.xml create mode 100644 2021-22/Lezioni21-22/.idea/vcs.xml diff --git a/2021-22/Lezioni21-22/.idea/.name b/2021-22/Lezioni21-22/.idea/.name new file mode 100644 index 0000000..66b3bb8 --- /dev/null +++ b/2021-22/Lezioni21-22/.idea/.name @@ -0,0 +1 @@ +Lezioni21-22.iml \ No newline at end of file diff --git a/2021-22/Lezioni21-22/.idea/codeStyles/codeStyleConfig.xml b/2021-22/Lezioni21-22/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/2021-22/Lezioni21-22/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/2021-22/Lezioni21-22/.idea/vcs.xml b/2021-22/Lezioni21-22/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/2021-22/Lezioni21-22/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From ee289e8b238b84d9a9743f88de82e50e3ca1b6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 7 Mar 2022 17:17:25 +0100 Subject: [PATCH 068/202] LinkedList added --- 2021-22/Lezioni21-22/.idea/vcs.xml | 6 ++ .../Lezioni21-22/src/tinyjdk/ArrayList.java | 39 +++++++++---- .../Lezioni21-22/src/tinyjdk/LinkedList.java | 57 +++++++++++++++++++ 3 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 2021-22/Lezioni21-22/.idea/vcs.xml create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java diff --git a/2021-22/Lezioni21-22/.idea/vcs.xml b/2021-22/Lezioni21-22/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/2021-22/Lezioni21-22/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java b/2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java index a2b4d27..849ca0d 100644 --- a/2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java +++ b/2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java @@ -4,7 +4,7 @@ public class ArrayList implements List { private Object[] a; - private int pos; + private int actualLen; private static final int DEFAULT_CAPACITY = 10; public ArrayList() { @@ -14,19 +14,19 @@ public ArrayList() { public ArrayList(int capacity) { assert capacity > 0; a = new Object[capacity]; - pos = 0; + actualLen = 0; } @Override public void add(T e) { - if (pos >= a.length) + if (actualLen >= a.length) a = Arrays.copyOf(a, a.length * 2); - a[pos++] = e; + a[actualLen++] = e; } @Override public int indexOf(T e) throws NotFoundException { - for (int i = 0; i < pos; ++i) + for (int i = 0; i < actualLen; ++i) if (a[i].equals(e)) return i; throw new NotFoundException(); @@ -35,27 +35,46 @@ public int indexOf(T e) throws NotFoundException { @Override public void remove(T e) throws NotFoundException { int i = indexOf(e); - // TODO: ricompattare l'array + for (int j = i; j < size() - 1; ++j) + a[j] = a[j + 1]; + --actualLen; } @Override public boolean contains(T e) { - return false; + try { + indexOf(e); + return true; + } catch (NotFoundException ex) { + return false; + } } @Override public void clear() { - a = new Object[pos + 1]; + a = new Object[actualLen + 1]; } @Override public int size() { - return pos; + return actualLen; } @Override public Iterator iterator() { - return null; + return new Iterator() { + private int pos = 0; + + @Override + public boolean hasNext() { + return pos < actualLen; + } + + @Override + public T next() { + return (T) a[pos++]; + } + }; } @Override diff --git a/2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java b/2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java new file mode 100644 index 0000000..6ae2ed9 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java @@ -0,0 +1,57 @@ +package tinyjdk; + +public class LinkedList implements List { + + private Node head; + + // TODO: farla non statica + private static class Node { + public X data; + public Node next; + } + + @Override + public void add(T e) { + + } + + @Override + public void remove(T e) throws NotFoundException { + + } + + @Override + public boolean contains(T e) { + return false; + } + + @Override + public void clear() { + + } + + @Override + public int size() { + return 0; + } + + @Override + public Iterator iterator() { + return null; + } + + @Override + public T get(int pos) { + return null; + } + + @Override + public void set(int pos, T e) { + + } + + @Override + public int indexOf(T e) throws NotFoundException { + return 0; + } +} From 1cb2f8906ed505170ea4df3c814eb931660a486d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 8 Mar 2022 17:20:07 +0100 Subject: [PATCH 069/202] Update LinkedList.java --- .../Lezioni21-22/src/tinyjdk/LinkedList.java | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java b/2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java index 6ae2ed9..94da00e 100644 --- a/2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java +++ b/2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java @@ -2,17 +2,28 @@ public class LinkedList implements List { - private Node head; + protected Node head = null, tail = null; + protected int len = 0; - // TODO: farla non statica - private static class Node { + protected static class Node { public X data; public Node next; + protected Node(X data, Node next) { + this.data = data; + this.next = next; + } } @Override public void add(T e) { - + Node n = new Node<>(e, null); + if (tail != null) { + tail.next = n; + tail = n; + } + else + head = tail = n; + ++len; } @Override @@ -27,12 +38,13 @@ public boolean contains(T e) { @Override public void clear() { - + head = tail = null; + len = 0; } @Override public int size() { - return 0; + return len; } @Override @@ -40,14 +52,22 @@ public Iterator iterator() { return null; } + protected Node nodeAt(int pos) { + assert (pos < size()); + Node n = head; + for (; pos > 0; --pos) + n = n.next; + return n; + } + @Override public T get(int pos) { - return null; + return nodeAt(pos).data; } @Override public void set(int pos, T e) { - + nodeAt(pos).data = e; } @Override From 06f22d25818d72f5ed776325c510ae0df9ce5168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 14 Mar 2022 17:15:19 +0100 Subject: [PATCH 070/202] Map --- .../Lezioni21-22/src/tinyjdk/ArrayList.java | 4 +- .../Lezioni21-22/src/tinyjdk/LinkedList.java | 48 +++++++++++++++---- 2021-22/Lezioni21-22/src/tinyjdk/List.java | 10 ++++ 2021-22/Lezioni21-22/src/tinyjdk/Map.java | 10 ++++ 2021-22/Lezioni21-22/src/tinyjdk/Pair.java | 24 ++++++++++ 5 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/Map.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/Pair.java diff --git a/2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java b/2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java index 849ca0d..7715b06 100644 --- a/2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java +++ b/2021-22/Lezioni21-22/src/tinyjdk/ArrayList.java @@ -40,7 +40,7 @@ public void remove(T e) throws NotFoundException { --actualLen; } - @Override +/* @Override public boolean contains(T e) { try { indexOf(e); @@ -48,7 +48,7 @@ public boolean contains(T e) { } catch (NotFoundException ex) { return false; } - } + }*/ @Override public void clear() { diff --git a/2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java b/2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java index 94da00e..c77933d 100644 --- a/2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java +++ b/2021-22/Lezioni21-22/src/tinyjdk/LinkedList.java @@ -8,6 +8,7 @@ public class LinkedList implements List { protected static class Node { public X data; public Node next; + protected Node(X data, Node next) { this.data = data; this.next = next; @@ -26,14 +27,24 @@ public void add(T e) { ++len; } + // TODO: da testare @Override public void remove(T e) throws NotFoundException { - - } - - @Override - public boolean contains(T e) { - return false; + Node prev = null, n = head; + while (n != null) { + if (n.data.equals(e)) { + if (prev != null) + prev.next = n.next; + else + head = n.next; + if (n.next == null) + tail = prev; + return; + } + prev = n; + n = n.next; + } + throw new NotFoundException(); } @Override @@ -49,7 +60,21 @@ public int size() { @Override public Iterator iterator() { - return null; + return new Iterator() { + private Node n = head; + + @Override + public boolean hasNext() { + return n.next != null; + } + + @Override + public T next() { + T r = n.data; + n = n.next; + return r; + } + }; } protected Node nodeAt(int pos) { @@ -72,6 +97,13 @@ public void set(int pos, T e) { @Override public int indexOf(T e) throws NotFoundException { - return 0; + Node n = head; + for (int pos = 0; n != null; ++pos) { + if (n.data.equals(e)) { + return pos; + } + n = n.next; + } + throw new NotFoundException(); } } diff --git a/2021-22/Lezioni21-22/src/tinyjdk/List.java b/2021-22/Lezioni21-22/src/tinyjdk/List.java index 2a50808..0f03707 100644 --- a/2021-22/Lezioni21-22/src/tinyjdk/List.java +++ b/2021-22/Lezioni21-22/src/tinyjdk/List.java @@ -4,4 +4,14 @@ public interface List extends Collection { X get(int pos); void set(int pos, X e); int indexOf(X e) throws NotFoundException; + + default boolean contains(X e) { + try { + indexOf(e); + return true; + } catch (NotFoundException ex) { + return false; + } + } } + diff --git a/2021-22/Lezioni21-22/src/tinyjdk/Map.java b/2021-22/Lezioni21-22/src/tinyjdk/Map.java new file mode 100644 index 0000000..565066b --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/Map.java @@ -0,0 +1,10 @@ +package tinyjdk; + +public interface Map extends Iterable> { + V get(K key); + void put(K key, V value); + void clear(); + boolean containsKey(K key); + boolean containsValue(V value); + void remove(K key); +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/Pair.java b/2021-22/Lezioni21-22/src/tinyjdk/Pair.java new file mode 100644 index 0000000..3340009 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/Pair.java @@ -0,0 +1,24 @@ +package tinyjdk; + +public class Pair { + private A a; + private B b; + + public Pair(A a, B b) { + this.a = a; + this.b = b; + } + + public A getFirst() { return a; } + public B getSecond() { return b; } + + public void setFirst(A a) { + this.a = a; + } + public void setSecond(B b) { + this.b = b; + } + +} + + From 9564b8ac2f6abb94a32ac603211a140d06c70a2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 15 Mar 2022 17:16:28 +0100 Subject: [PATCH 071/202] Set --- 2021-22/Lezioni21-22/src/tinyjdk/Map.java | 4 +- 2021-22/Lezioni21-22/src/tinyjdk/PairMap.java | 69 +++++++++++++++++++ .../src/tinyjdk/SimpleSortedSet.java | 43 ++++++++++++ .../Lezioni21-22/src/tinyjdk/SortedSet.java | 5 ++ 2021-22/Lezioni21-22/src/tinyjdk/Test.java | 12 ++++ 5 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/PairMap.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/SimpleSortedSet.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/SortedSet.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/Test.java diff --git a/2021-22/Lezioni21-22/src/tinyjdk/Map.java b/2021-22/Lezioni21-22/src/tinyjdk/Map.java index 565066b..8fb0da4 100644 --- a/2021-22/Lezioni21-22/src/tinyjdk/Map.java +++ b/2021-22/Lezioni21-22/src/tinyjdk/Map.java @@ -2,9 +2,9 @@ public interface Map extends Iterable> { V get(K key); - void put(K key, V value); + V put(K key, V value); void clear(); boolean containsKey(K key); boolean containsValue(V value); - void remove(K key); + void remove(K key) throws NotFoundException; } diff --git a/2021-22/Lezioni21-22/src/tinyjdk/PairMap.java b/2021-22/Lezioni21-22/src/tinyjdk/PairMap.java new file mode 100644 index 0000000..d8321d3 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/PairMap.java @@ -0,0 +1,69 @@ +package tinyjdk; + +public class PairMap implements Map { + private List> l; + + public PairMap() { + l = new ArrayList<>(); + } + + @Override + public Iterator> iterator() { + return l.iterator(); + } + + @Override + public V get(K key) { + Pair p = find(key); + if (p != null) return p.getSecond(); + return null; + } + + private Pair find(K key) { + Iterator> it = this.iterator(); + while (it.hasNext()) { + Pair p = it.next(); + if (p.getFirst().equals(key)) + return p; + } + return null; + } + + @Override + public V put(K key, V value) { + Pair p = find(key); + if (p != null) { + V r = p.getSecond(); + p.setSecond(value); + return r; + } + l.add(new Pair<>(key, value)); + return null; + } + + @Override + public void clear() { + l.clear(); + } + + @Override + public boolean containsKey(K key) { + return find(key) != null; + } + + @Override + public boolean containsValue(V value) { + Iterator> it = this.iterator(); + while (it.hasNext()) { + Pair p = it.next(); + if (p.getSecond().equals(value)) + return true; + } + return false; + } + + @Override + public void remove(K key) throws NotFoundException { + l.remove(find(key)); + } +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/SimpleSortedSet.java b/2021-22/Lezioni21-22/src/tinyjdk/SimpleSortedSet.java new file mode 100644 index 0000000..62809c0 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/SimpleSortedSet.java @@ -0,0 +1,43 @@ +package tinyjdk; + +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; + + +public class SimpleSortedSet> implements SortedSet { + private List l = new ArrayList<>(); + + @Override + public void add(T e) { + if (!contains(e)) { + l.add(e); + Collections.sort(l); + } + } + + @Override + public void remove(T e) throws NotFoundException { + + } + + @Override + public boolean contains(T e) { + return false; + } + + @Override + public void clear() { + + } + + @Override + public int size() { + return 0; + } + + @Override + public Iterator iterator() { + return null; + } +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/SortedSet.java b/2021-22/Lezioni21-22/src/tinyjdk/SortedSet.java new file mode 100644 index 0000000..5c922bd --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/SortedSet.java @@ -0,0 +1,5 @@ +package tinyjdk; + +public interface SortedSet> extends Collection { + +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/Test.java b/2021-22/Lezioni21-22/src/tinyjdk/Test.java new file mode 100644 index 0000000..e3a88bd --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/Test.java @@ -0,0 +1,12 @@ +package tinyjdk; + + +public class Test { + + public static void main(String[] args) { + for (Integer n : new ArrayList()) { + + } + } + +} From 5da50f1b10dc6849e863ec65bb99308ebe9fea74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 21 Mar 2022 17:19:55 +0100 Subject: [PATCH 072/202] Sorting --- 2021-22/Lezioni21-22/.idea/uiDesigner.xml | 124 ++++++++++++++++++ ...Collections.java => CollectionSample.java} | 2 +- 2021-22/Lezioni21-22/src/Sorting.java | 60 +++++++++ 2021-22/Lezioni21-22/src/tinyjdk/IntPair.java | 15 +++ .../src/tinyjdk/SimpleSortedSet.java | 6 +- 2021-22/Lezioni21-22/src/tinyjdk/Test.java | 12 -- 6 files changed, 204 insertions(+), 15 deletions(-) create mode 100644 2021-22/Lezioni21-22/.idea/uiDesigner.xml rename 2021-22/Lezioni21-22/src/{Collections.java => CollectionSample.java} (98%) create mode 100644 2021-22/Lezioni21-22/src/Sorting.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/IntPair.java delete mode 100644 2021-22/Lezioni21-22/src/tinyjdk/Test.java diff --git a/2021-22/Lezioni21-22/.idea/uiDesigner.xml b/2021-22/Lezioni21-22/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/2021-22/Lezioni21-22/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/2021-22/Lezioni21-22/src/Collections.java b/2021-22/Lezioni21-22/src/CollectionSample.java similarity index 98% rename from 2021-22/Lezioni21-22/src/Collections.java rename to 2021-22/Lezioni21-22/src/CollectionSample.java index 6103459..182dc73 100644 --- a/2021-22/Lezioni21-22/src/Collections.java +++ b/2021-22/Lezioni21-22/src/CollectionSample.java @@ -1,6 +1,6 @@ import java.util.*; -public class Collections { +public class CollectionSample { public static class RandomSequence implements Iterable { diff --git a/2021-22/Lezioni21-22/src/Sorting.java b/2021-22/Lezioni21-22/src/Sorting.java new file mode 100644 index 0000000..1e3682f --- /dev/null +++ b/2021-22/Lezioni21-22/src/Sorting.java @@ -0,0 +1,60 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +public class Sorting { + + public static class Humanoid implements Comparable { + protected int health; + + public Humanoid(int h) { + health = h; + } + + @Override + public int compareTo(Humanoid o) { + return -(health - o.health); + } + + @Override + public String toString() { + return String.format("Humanoid[health=%d]", health); + } + } + + public static class Elf extends Humanoid { + private int mana; + + public Elf(int health, int mana) { + super(health); + this.mana = mana; + } + + @Override + public String toString() { + return String.format("Elf[health=%d;mana=%d]", health, mana); + } + + } + + + public static void main(String[] args) { + Random rand = new Random(); + List l = new ArrayList<>(); + for (int i = 0; i < 10; ++i) { + //if (rand.nextBoolean()) + // l.add(new Elf(rand.nextInt(100), rand.nextInt(50))); + //else + l.add(new Humanoid(rand.nextInt(100))); + } + + System.out.println(l); + sort(l); + System.out.println(l); + } + + public static > void sort(List l) { + Collections.sort(l); + } +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/IntPair.java b/2021-22/Lezioni21-22/src/tinyjdk/IntPair.java new file mode 100644 index 0000000..47e007c --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/IntPair.java @@ -0,0 +1,15 @@ +package tinyjdk; + +public class IntPair implements Comparable { + public final int first, second; + + public IntPair(int a, int b) { + first = a; + second = b; + } + + @Override + public int compareTo(IntPair o) { + return first * o.first - second * o.second; + } +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/SimpleSortedSet.java b/2021-22/Lezioni21-22/src/tinyjdk/SimpleSortedSet.java index 62809c0..231b784 100644 --- a/2021-22/Lezioni21-22/src/tinyjdk/SimpleSortedSet.java +++ b/2021-22/Lezioni21-22/src/tinyjdk/SimpleSortedSet.java @@ -16,6 +16,8 @@ public void add(T e) { } } + // TODO: implementare questi metodi + @Override public void remove(T e) throws NotFoundException { @@ -33,11 +35,11 @@ public void clear() { @Override public int size() { - return 0; + return l.size(); } @Override public Iterator iterator() { - return null; + throw new RuntimeException("not implemented"); } } diff --git a/2021-22/Lezioni21-22/src/tinyjdk/Test.java b/2021-22/Lezioni21-22/src/tinyjdk/Test.java deleted file mode 100644 index e3a88bd..0000000 --- a/2021-22/Lezioni21-22/src/tinyjdk/Test.java +++ /dev/null @@ -1,12 +0,0 @@ -package tinyjdk; - - -public class Test { - - public static void main(String[] args) { - for (Integer n : new ArrayList()) { - - } - } - -} From d37eb26a3e27de1a27a98f530eb1fa24a1d004e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 22 Mar 2022 17:18:12 +0100 Subject: [PATCH 073/202] Update Sorting.java --- 2021-22/Lezioni21-22/src/Sorting.java | 89 ++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 14 deletions(-) diff --git a/2021-22/Lezioni21-22/src/Sorting.java b/2021-22/Lezioni21-22/src/Sorting.java index 1e3682f..7549b94 100644 --- a/2021-22/Lezioni21-22/src/Sorting.java +++ b/2021-22/Lezioni21-22/src/Sorting.java @@ -1,7 +1,4 @@ -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Random; +import java.util.*; public class Sorting { @@ -14,7 +11,7 @@ public Humanoid(int h) { @Override public int compareTo(Humanoid o) { - return -(health - o.health); + return health - o.health; } @Override @@ -31,6 +28,15 @@ public Elf(int health, int mana) { this.mana = mana; } + @Override + public int compareTo(Humanoid o) { + if (o instanceof Elf) { + Elf e = (Elf) o; + return mana - e.mana; + } + return super.compareTo(o); + } + @Override public String toString() { return String.format("Elf[health=%d;mana=%d]", health, mana); @@ -38,20 +44,75 @@ public String toString() { } + public static class Creature { + } + + public static class Animal extends Creature { + protected int weight; + + public Animal(int w) { + weight = w; + } + + public void eat(Animal a) { + } + + public Animal spawn() { + return new Animal(weight / 3); + } + } + + public static class Dog extends Animal { + private int tail; + + public Dog(int w, int tail) { + super(w); + this.tail = tail; + } + + public void bark() { + } + + @Override + public void eat(Animal a) { + } + + @Override + public Dog spawn() { + return new Dog(weight / 2, 1); + } + } public static void main(String[] args) { Random rand = new Random(); - List l = new ArrayList<>(); - for (int i = 0; i < 10; ++i) { - //if (rand.nextBoolean()) - // l.add(new Elf(rand.nextInt(100), rand.nextInt(50))); - //else - l.add(new Humanoid(rand.nextInt(100))); + { + List a = new ArrayList<>(); + for (int i = 0; i < 10; ++i) + a.add(new Dog(i * 10, i)); + + + Collections.sort(a, (Animal o1, Animal o2) -> o1.weight - o2.weight); + Collections.sort(a, new Comparator() { + @Override + public int compare(Animal o1, Animal o2) { + return o1.weight - o2.weight; + } + }); } - System.out.println(l); - sort(l); - System.out.println(l); + { + List l = new ArrayList<>(); + for (int i = 0; i < 10; ++i) { +// if (rand.nextBoolean()) + l.add(new Elf(rand.nextInt(100), rand.nextInt(50))); + // else + // l.add(new Humanoid(rand.nextInt(100))); + } + + System.out.println(l); + sort(l); + System.out.println(l); + } } public static > void sort(List l) { From b7aabf0305571de33be2e3afa054cb0dc58e312f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 29 Mar 2022 17:15:36 +0200 Subject: [PATCH 074/202] Lambda --- 2021-22/Lezioni21-22/src/Functional.java | 87 ++++++++++++++++++++++++ 2021-22/Lezioni21-22/src/Sorting.java | 21 +++++- 2 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 2021-22/Lezioni21-22/src/Functional.java diff --git a/2021-22/Lezioni21-22/src/Functional.java b/2021-22/Lezioni21-22/src/Functional.java new file mode 100644 index 0000000..869e966 --- /dev/null +++ b/2021-22/Lezioni21-22/src/Functional.java @@ -0,0 +1,87 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Random; + +public class Functional { + + public interface Function { + B apply(A x); + } + + public interface Consumer { + void apply(T x); + } + + public interface Supplier { + T apply(); + } + + public static Collection map(Collection c, Function f) { + Collection r = new ArrayList<>(); + for (A x : c) + r.add(f.apply(x)); + return r; + } + + public static void foreach(Collection c, Consumer f) { + for (T x : c) + f.apply(x); + } + + public static Collection generate(int len, Supplier f) { + Collection r = new ArrayList<>(); + for (; len > 0; --len) + r.add(f.apply()); + return r; + } + + public static void main(String[] args) { + List l = List.of("ciao", "come", "ti", "chiami?"); + + // test map + Collection r = map(l, (String s) -> s.length()); + Collection r2 = map(l, new Function() { + @Override + public Integer apply(String x) { + return x.length(); + } + }); + System.out.println(r); + System.out.println(r2); + + // test foreach + foreach(l, (String s) -> System.out.println(s)); + + // test generate + Collection g1 = generate(10, () -> 7); + System.out.println(g1); + Random rand = new Random(); + Collection g2 = generate(10, () -> rand.nextInt(100)); + Collection g3 = generate(10, new Supplier() { + @Override + public Integer apply() { + return rand.nextInt(100); + } + }); + Collection g4 = generate(10, new MySupplier(rand)); + System.out.println(g2); + System.out.println(g3); + System.out.println(g4); + } + + static class MySupplier implements Supplier { + + private final Random rand; + + public MySupplier(Random rand) { + this.rand = rand; + } + + @Override + public Integer apply() { + return rand.nextInt(100); + } + } + +} diff --git a/2021-22/Lezioni21-22/src/Sorting.java b/2021-22/Lezioni21-22/src/Sorting.java index 7549b94..b241e78 100644 --- a/2021-22/Lezioni21-22/src/Sorting.java +++ b/2021-22/Lezioni21-22/src/Sorting.java @@ -1,4 +1,5 @@ import java.util.*; +import java.util.function.Function; public class Sorting { @@ -83,6 +84,13 @@ public Dog spawn() { } } + static class MyComparator implements Comparator { + @Override + public int compare(Animal o1, Animal o2) { + return o1.weight - o2.weight; + } + } + public static void main(String[] args) { Random rand = new Random(); { @@ -90,14 +98,17 @@ public static void main(String[] args) { for (int i = 0; i < 10; ++i) a.add(new Dog(i * 10, i)); - - Collections.sort(a, (Animal o1, Animal o2) -> o1.weight - o2.weight); - Collections.sort(a, new Comparator() { + // non-anonymous class + sort(a, new MyComparator()); + // anonymous class + sort(a, new Comparator() { @Override public int compare(Animal o1, Animal o2) { return o1.weight - o2.weight; } }); + // lambda che si converte in una anonymous class + sort(a, (o1, o2) -> o1.weight - o2.weight); } { @@ -118,4 +129,8 @@ public int compare(Animal o1, Animal o2) { public static > void sort(List l) { Collections.sort(l); } + + public static void sort(List l, Comparator c) { + Collections.sort(l, c); + } } From d123f8bfd38f426c55cd593ddab6c9aa2af7903d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 4 Apr 2022 17:20:57 +0200 Subject: [PATCH 075/202] Threading --- 2021-22/Lezioni21-22/src/Functional.java | 25 +++++++++++---- 2021-22/Lezioni21-22/src/Threading.java | 39 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 2021-22/Lezioni21-22/src/Threading.java diff --git a/2021-22/Lezioni21-22/src/Functional.java b/2021-22/Lezioni21-22/src/Functional.java index 869e966..b64d30c 100644 --- a/2021-22/Lezioni21-22/src/Functional.java +++ b/2021-22/Lezioni21-22/src/Functional.java @@ -10,11 +10,15 @@ public interface Function { } public interface Consumer { - void apply(T x); + void accept(T x); } public interface Supplier { - T apply(); + T get(); + } + + public interface Runnable { + void run(); } public static Collection map(Collection c, Function f) { @@ -26,13 +30,13 @@ public static Collection map(Collection c, Function f) { public static void foreach(Collection c, Consumer f) { for (T x : c) - f.apply(x); + f.accept(x); } public static Collection generate(int len, Supplier f) { Collection r = new ArrayList<>(); for (; len > 0; --len) - r.add(f.apply()); + r.add(f.get()); return r; } @@ -60,7 +64,7 @@ public Integer apply(String x) { Collection g2 = generate(10, () -> rand.nextInt(100)); Collection g3 = generate(10, new Supplier() { @Override - public Integer apply() { + public Integer get() { return rand.nextInt(100); } }); @@ -68,6 +72,15 @@ public Integer apply() { System.out.println(g2); System.out.println(g3); System.out.println(g4); + + // runnable + Runnable run1 = () -> System.out.println("ciao"); + Runnable run2 = new Runnable() { + @Override + public void run() { + System.out.println("ciao"); + } + }; } static class MySupplier implements Supplier { @@ -79,7 +92,7 @@ public MySupplier(Random rand) { } @Override - public Integer apply() { + public Integer get() { return rand.nextInt(100); } } diff --git a/2021-22/Lezioni21-22/src/Threading.java b/2021-22/Lezioni21-22/src/Threading.java new file mode 100644 index 0000000..ff44159 --- /dev/null +++ b/2021-22/Lezioni21-22/src/Threading.java @@ -0,0 +1,39 @@ +import java.util.Random; + +public class Threading { + + private static Random rand = new Random(); + + private static void doSomething() { + long ms = rand.nextLong(500L); + Thread self = Thread.currentThread(); + for (int i = 0; i < 20; ++i) { + System.out.printf("%s[%d] #%d\n", self.getName(), self.getId(), i); + try { + Thread.sleep(ms); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + + public static void main(String[] args) { + + Thread t1 = new Thread(() -> { + doSomething(); + }); + t1.start(); + + doSomething(); + + try { + t1.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + +} From 915208f807db2da174eda6cb86768e87847acf28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 5 Apr 2022 17:19:44 +0200 Subject: [PATCH 076/202] Create ConsumerProducer.java --- .../Lezioni21-22/src/ConsumerProducer.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 2021-22/Lezioni21-22/src/ConsumerProducer.java diff --git a/2021-22/Lezioni21-22/src/ConsumerProducer.java b/2021-22/Lezioni21-22/src/ConsumerProducer.java new file mode 100644 index 0000000..3334b8f --- /dev/null +++ b/2021-22/Lezioni21-22/src/ConsumerProducer.java @@ -0,0 +1,59 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class ConsumerProducer { + + public static Random rand = new Random(); + + public static List q = new ArrayList<>(); + + public static class Producer extends Thread { + @Override + public void run() { + while (true) { + try { + Thread.sleep(rand.nextLong(100L)); + synchronized (q) { + q.add(rand.nextInt(100)); + q.notify(); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + + public static class Consumer extends Thread { + @Override + public void run() { + while (true) { + synchronized (q) { + if (q.isEmpty()) { + try { + q.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + int n = q.remove(0); + System.out.printf("consumed: %d (size = %d)\n", n, q.size()); + } + } + } + } + + public static void main(String[] args) { + Producer p = new Producer(); + Consumer c = new Consumer(); + p.start(); + c.start(); + + try { + p.join(); + c.join(); + } catch (InterruptedException e) {} + } + +} From 43c6647b6d370be477131c4ef3dd10324582860b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 12 Apr 2022 17:16:20 +0200 Subject: [PATCH 077/202] Update ConsumerProducer.java --- .../Lezioni21-22/src/ConsumerProducer.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/2021-22/Lezioni21-22/src/ConsumerProducer.java b/2021-22/Lezioni21-22/src/ConsumerProducer.java index 3334b8f..2dc2cbc 100644 --- a/2021-22/Lezioni21-22/src/ConsumerProducer.java +++ b/2021-22/Lezioni21-22/src/ConsumerProducer.java @@ -9,15 +9,17 @@ public class ConsumerProducer { public static List q = new ArrayList<>(); public static class Producer extends Thread { + @Override public void run() { while (true) { try { Thread.sleep(rand.nextLong(100L)); - synchronized (q) { + + synchronized (q) { // LOCK q.add(rand.nextInt(100)); q.notify(); - } + } // UNLOCK } catch (InterruptedException e) { e.printStackTrace(); } @@ -29,7 +31,7 @@ public static class Consumer extends Thread { @Override public void run() { while (true) { - synchronized (q) { + synchronized (q) { // LOCK if (q.isEmpty()) { try { q.wait(); @@ -39,20 +41,23 @@ public void run() { } int n = q.remove(0); System.out.printf("consumed: %d (size = %d)\n", n, q.size()); - } + } // UNLOCK } } } public static void main(String[] args) { - Producer p = new Producer(); - Consumer c = new Consumer(); - p.start(); - c.start(); + Producer p1 = new Producer(); + Producer p2 = new Producer(); + Consumer c1 = new Consumer(); + Consumer c2 = new Consumer(); + p1.start(); + p2.start(); + c1.start(); + c2.start(); try { - p.join(); - c.join(); + p1.join(); } catch (InterruptedException e) {} } From 74e79a235a31bc5677c10a470bec0ec4d58325d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 19 Apr 2022 13:53:31 +0200 Subject: [PATCH 078/202] VSCPP project and solution created --- 2021-22/LezioniCPP/.gitignore | 438 ++++++++++++++++++ 2021-22/LezioniCPP/LezioniCPP.cpp | 6 + 2021-22/LezioniCPP/LezioniCPP.sln | 31 ++ 2021-22/LezioniCPP/LezioniCPP.vcxproj | 147 ++++++ 2021-22/LezioniCPP/LezioniCPP.vcxproj.filters | 22 + 5 files changed, 644 insertions(+) create mode 100644 2021-22/LezioniCPP/.gitignore create mode 100644 2021-22/LezioniCPP/LezioniCPP.cpp create mode 100644 2021-22/LezioniCPP/LezioniCPP.sln create mode 100644 2021-22/LezioniCPP/LezioniCPP.vcxproj create mode 100644 2021-22/LezioniCPP/LezioniCPP.vcxproj.filters diff --git a/2021-22/LezioniCPP/.gitignore b/2021-22/LezioniCPP/.gitignore new file mode 100644 index 0000000..6879d2e --- /dev/null +++ b/2021-22/LezioniCPP/.gitignore @@ -0,0 +1,438 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/visualstudio,c++ +# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudio,c++ + +### C++ ### +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +### VisualStudio ### +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.iobj +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) +*.vbp + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio 6 technical files + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# Visual Studio History (VSHistory) files +.vshistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +*.sln.iml + +### VisualStudio Patch ### +# Additional files built by Visual Studio + +# End of https://www.toptal.com/developers/gitignore/api/visualstudio,c++ diff --git a/2021-22/LezioniCPP/LezioniCPP.cpp b/2021-22/LezioniCPP/LezioniCPP.cpp new file mode 100644 index 0000000..54e7785 --- /dev/null +++ b/2021-22/LezioniCPP/LezioniCPP.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + std::cout << "Hello World!\n"; +} diff --git a/2021-22/LezioniCPP/LezioniCPP.sln b/2021-22/LezioniCPP/LezioniCPP.sln new file mode 100644 index 0000000..f4c9044 --- /dev/null +++ b/2021-22/LezioniCPP/LezioniCPP.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31729.503 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LezioniCPP", "LezioniCPP.vcxproj", "{CE9ED7C4-E77D-4122-952D-8206CBD38868}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CE9ED7C4-E77D-4122-952D-8206CBD38868}.Debug|x64.ActiveCfg = Debug|x64 + {CE9ED7C4-E77D-4122-952D-8206CBD38868}.Debug|x64.Build.0 = Debug|x64 + {CE9ED7C4-E77D-4122-952D-8206CBD38868}.Debug|x86.ActiveCfg = Debug|Win32 + {CE9ED7C4-E77D-4122-952D-8206CBD38868}.Debug|x86.Build.0 = Debug|Win32 + {CE9ED7C4-E77D-4122-952D-8206CBD38868}.Release|x64.ActiveCfg = Release|x64 + {CE9ED7C4-E77D-4122-952D-8206CBD38868}.Release|x64.Build.0 = Release|x64 + {CE9ED7C4-E77D-4122-952D-8206CBD38868}.Release|x86.ActiveCfg = Release|Win32 + {CE9ED7C4-E77D-4122-952D-8206CBD38868}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F5305459-C861-4D73-8AF9-786E2D511A45} + EndGlobalSection +EndGlobal diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj new file mode 100644 index 0000000..c235e93 --- /dev/null +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {ce9ed7c4-e77d-4122-952d-8206cbd38868} + LezioniCPP + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters new file mode 100644 index 0000000..d80ed55 --- /dev/null +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file From a89a35d2ca2c3b5878e91b3448aeed925d9a87cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 19 Apr 2022 15:14:20 +0200 Subject: [PATCH 079/202] Example --- 2021-22/LezioniCPP/LezioniCPP.cpp | 6 --- 2021-22/LezioniCPP/LezioniCPP.vcxproj | 11 ++--- 2021-22/LezioniCPP/LezioniCPP.vcxproj.filters | 2 +- 2021-22/LezioniCPP/main.cpp | 42 +++++++++++++++++++ 4 files changed, 49 insertions(+), 12 deletions(-) delete mode 100644 2021-22/LezioniCPP/LezioniCPP.cpp create mode 100644 2021-22/LezioniCPP/main.cpp diff --git a/2021-22/LezioniCPP/LezioniCPP.cpp b/2021-22/LezioniCPP/LezioniCPP.cpp deleted file mode 100644 index 54e7785..0000000 --- a/2021-22/LezioniCPP/LezioniCPP.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main() -{ - std::cout << "Hello World!\n"; -} diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj index c235e93..ba61716 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -29,26 +29,26 @@ Application true - v142 + v143 Unicode Application false - v142 + v143 true Unicode Application true - v142 + v143 Unicode Application false - v142 + v143 true Unicode @@ -116,6 +116,7 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + stdcpp20 Console @@ -139,7 +140,7 @@ - + diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters index d80ed55..ce0c35c 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters @@ -15,7 +15,7 @@ - + Source Files diff --git a/2021-22/LezioniCPP/main.cpp b/2021-22/LezioniCPP/main.cpp new file mode 100644 index 0000000..08567a9 --- /dev/null +++ b/2021-22/LezioniCPP/main.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + +template +iterator_traits::value_type sum(InputIterator first, InputIterator last, BinaryFunction f) +{ + typename iterator_traits::value_type z = *first++; + for (; first != last; ++first) + { + z = f(z, *first); + } + return z; +} + +template +auto sum(InputIterator first, InputIterator last) -> decltype(*first) +{ + decltype(*first) z = *first++; + for (; first != last; ++first) + { + z += *first; + + } + return z; +} + + +int main() +{ + vector v = { 1, 2, 3, 4 }; + for_each(begin(v), end(v), [](const auto& x) { cout << x << ", "; }); + + cout << "sum1 = " << sum(v.begin(), v.end()) << endl; + cout << "sum2 = " << sum(v.begin(), v.end(), [](const auto& a, const auto& b) { return a * b; }) << endl; +} From 4c15faf4ca732f30bfc7f0f97f55ba6dbbec49cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 26 Apr 2022 15:43:18 +0200 Subject: [PATCH 080/202] Functional --- 2021-22/Lezioni21-22/src/Functional.java | 23 ++++++++-- 2021-22/LezioniCPP/Classes.cpp | 54 ++++++++++++++++++++++++ 2021-22/LezioniCPP/LezioniCPP.vcxproj | 1 + 3 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 2021-22/LezioniCPP/Classes.cpp diff --git a/2021-22/Lezioni21-22/src/Functional.java b/2021-22/Lezioni21-22/src/Functional.java index b64d30c..b933b78 100644 --- a/2021-22/Lezioni21-22/src/Functional.java +++ b/2021-22/Lezioni21-22/src/Functional.java @@ -1,7 +1,5 @@ -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Random; +import java.util.*; +import java.util.function.BiFunction; public class Functional { @@ -21,6 +19,23 @@ public interface Runnable { void run(); } + + + + + public static T sum(Iterator it, BiFunction f) { + T z = it.next(); + while (it.hasNext()) { + z = f.apply(z, it.next()); + } + return z; + } + + + + + + public static Collection map(Collection c, Function f) { Collection r = new ArrayList<>(); for (A x : c) diff --git a/2021-22/LezioniCPP/Classes.cpp b/2021-22/LezioniCPP/Classes.cpp new file mode 100644 index 0000000..c49b1e6 --- /dev/null +++ b/2021-22/LezioniCPP/Classes.cpp @@ -0,0 +1,54 @@ + +#include + +using namespace std; + +template +class pair +{ +private: + A first; + B second; + +public: + pair(const A& a, const B& b) : first(a), second(b) + {} + + pair(const pair& p) : first(p.first), second(p.second) + {} + + template + pair(const pair& p) : first(p.first), second(p.second) + {} + + const A& get_first() const + { + return first; + } + + B get_second() const + { + return second; + } + +}; + +int main() +{ + int x; + int y(3); + x = y; + + pair p1(4, 5); + pair p2(p1); + + pair p3("ciao", true); + pair p4(p3); + + const int& u(p4.get_first()); + + return 0; +} + + + diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj index ba61716..e4c5fdf 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -88,6 +88,7 @@ true WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + stdcpp20 Console From 162340c70cb624bf668185ae1bdaf429dbf9cdfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 26 Apr 2022 17:24:22 +0200 Subject: [PATCH 081/202] Zoo --- 2021-22/Lezioni21-22/src/Functional.java | 7 --- 2021-22/Lezioni21-22/src/Generics.java | 4 +- .../Lezioni21-22/src/{Recap.java => Zoo.java} | 6 +-- 2021-22/LezioniCPP/Classes.cpp | 49 +++++++++++++++++-- 2021-22/LezioniCPP/LezioniCPP.vcxproj | 1 + 2021-22/LezioniCPP/LezioniCPP.vcxproj.filters | 3 ++ 2021-22/LezioniCPP/Zoo.cpp | 41 ++++++++++++++++ 7 files changed, 95 insertions(+), 16 deletions(-) rename 2021-22/Lezioni21-22/src/{Recap.java => Zoo.java} (92%) create mode 100644 2021-22/LezioniCPP/Zoo.cpp diff --git a/2021-22/Lezioni21-22/src/Functional.java b/2021-22/Lezioni21-22/src/Functional.java index b933b78..25226fb 100644 --- a/2021-22/Lezioni21-22/src/Functional.java +++ b/2021-22/Lezioni21-22/src/Functional.java @@ -19,10 +19,6 @@ public interface Runnable { void run(); } - - - - public static T sum(Iterator it, BiFunction f) { T z = it.next(); while (it.hasNext()) { @@ -33,9 +29,6 @@ public static T sum(Iterator it, BiFunction f) { - - - public static Collection map(Collection c, Function f) { Collection r = new ArrayList<>(); for (A x : c) diff --git a/2021-22/Lezioni21-22/src/Generics.java b/2021-22/Lezioni21-22/src/Generics.java index 740d178..116b06c 100644 --- a/2021-22/Lezioni21-22/src/Generics.java +++ b/2021-22/Lezioni21-22/src/Generics.java @@ -50,9 +50,9 @@ public static int f(int x) { public static void main(String[] args) { FloatPair p1 = new FloatPair(23.11f, 67.69f); Pair p2 = new Pair<>(23.11f, 67.69f); - Pair p3 = new Pair<>("ciao", new Recap.Dog(10)); + Pair p3 = new Pair<>("ciao", new Zoo.Dog(10)); Pair, Pair> p4 = new Pair<>(new Pair<>(1, 2), new Pair<>(5, 6)); - Pair p5 = new Pair<>("ciao", new Recap.Labrador(10)); + Pair p5 = new Pair<>("ciao", new Zoo.Labrador(10)); } diff --git a/2021-22/Lezioni21-22/src/Recap.java b/2021-22/Lezioni21-22/src/Zoo.java similarity index 92% rename from 2021-22/Lezioni21-22/src/Recap.java rename to 2021-22/Lezioni21-22/src/Zoo.java index 9b77b70..383ffcb 100644 --- a/2021-22/Lezioni21-22/src/Recap.java +++ b/2021-22/Lezioni21-22/src/Zoo.java @@ -1,14 +1,14 @@ -public class Recap { +public class Zoo { private int x; - private static float y = Recap.z; + private static float y = Zoo.z; public static float z; protected void m() { this.x = 8; } public static void n() { - Recap.y = 8; + Zoo.y = 8; } private class Boo {} diff --git a/2021-22/LezioniCPP/Classes.cpp b/2021-22/LezioniCPP/Classes.cpp index c49b1e6..233b3c3 100644 --- a/2021-22/LezioniCPP/Classes.cpp +++ b/2021-22/LezioniCPP/Classes.cpp @@ -21,12 +21,50 @@ class pair pair(const pair& p) : first(p.first), second(p.second) {} - const A& get_first() const + pair& operator=(const pair& p) + { + first = p.first; + second = p.second; + return *this; + } + + pair operator++() + { + pair tmp(*this); + first++; + second++; + return tmp; + } + + pair& operator++(int) + { + ++first; + ++second; + return *this; + } + + pair operator+(const pair& a) const + { + return pair(first + a.first, second + a.second); + } + + + const A& first() const + { + return first; + } + + A& first() { return first; } - B get_second() const + const B& second() const + { + return second; + } + + B& second() { return second; } @@ -39,13 +77,16 @@ int main() int y(3); x = y; - pair p1(4, 5); + pair p1 = { 4, 5 }; pair p2(p1); pair p3("ciao", true); pair p4(p3); - const int& u(p4.get_first()); + p1 = p2; + + int n = p1.first(); + p1.first() = 8; return 0; } diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj index e4c5fdf..0042c05 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -141,6 +141,7 @@ + diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters index ce0c35c..0f58473 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters @@ -18,5 +18,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/2021-22/LezioniCPP/Zoo.cpp b/2021-22/LezioniCPP/Zoo.cpp new file mode 100644 index 0000000..015c8ec --- /dev/null +++ b/2021-22/LezioniCPP/Zoo.cpp @@ -0,0 +1,41 @@ + +namespace zoo +{ + + class animal + { + protected: + int weight; + + public: + explicit animal(int w) : weight(w) {} + + animal(const animal& a) : weight(a.weight) {} + + virtual void eat(const animal& a) + { + weight += a.weight; + } + }; + + class dog : public animal + { + public: + void eat(const animal& a) override + { + weight += a.weight * 2; + } + }; + + + int main() + { + animal fido(50); + animal* pluto = new animal(40); + + fido.eat(*pluto); + + return 0; + } +} + From 1406461999547e64696509a5b4935027951f438a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 27 Apr 2022 16:00:51 +0200 Subject: [PATCH 082/202] Lezione 27/4/22 fixata --- 2021-22/LezioniCPP/Classes.cpp | 95 --------------- 2021-22/LezioniCPP/LezioniCPP.vcxproj | 4 +- 2021-22/LezioniCPP/LezioniCPP.vcxproj.filters | 8 +- 2021-22/LezioniCPP/Zoo.cpp | 41 ------- 2021-22/LezioniCPP/main.cpp | 42 ++----- 2021-22/LezioniCPP/pairs.ixx | 115 ++++++++++++++++++ 2021-22/LezioniCPP/sums.ixx | 46 +++++++ 2021-22/LezioniCPP/zoo.ixx | 64 ++++++++++ 8 files changed, 243 insertions(+), 172 deletions(-) delete mode 100644 2021-22/LezioniCPP/Classes.cpp delete mode 100644 2021-22/LezioniCPP/Zoo.cpp create mode 100644 2021-22/LezioniCPP/pairs.ixx create mode 100644 2021-22/LezioniCPP/sums.ixx create mode 100644 2021-22/LezioniCPP/zoo.ixx diff --git a/2021-22/LezioniCPP/Classes.cpp b/2021-22/LezioniCPP/Classes.cpp deleted file mode 100644 index 233b3c3..0000000 --- a/2021-22/LezioniCPP/Classes.cpp +++ /dev/null @@ -1,95 +0,0 @@ - -#include - -using namespace std; - -template -class pair -{ -private: - A first; - B second; - -public: - pair(const A& a, const B& b) : first(a), second(b) - {} - - pair(const pair& p) : first(p.first), second(p.second) - {} - - template - pair(const pair& p) : first(p.first), second(p.second) - {} - - pair& operator=(const pair& p) - { - first = p.first; - second = p.second; - return *this; - } - - pair operator++() - { - pair tmp(*this); - first++; - second++; - return tmp; - } - - pair& operator++(int) - { - ++first; - ++second; - return *this; - } - - pair operator+(const pair& a) const - { - return pair(first + a.first, second + a.second); - } - - - const A& first() const - { - return first; - } - - A& first() - { - return first; - } - - const B& second() const - { - return second; - } - - B& second() - { - return second; - } - -}; - -int main() -{ - int x; - int y(3); - x = y; - - pair p1 = { 4, 5 }; - pair p2(p1); - - pair p3("ciao", true); - pair p4(p3); - - p1 = p2; - - int n = p1.first(); - p1.first() = 8; - - return 0; -} - - - diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj index 0042c05..7ad97e5 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -141,8 +141,10 @@ - + + + diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters index 0f58473..4ad9526 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters @@ -18,7 +18,13 @@ Source Files - + + Source Files + + + Source Files + + Source Files diff --git a/2021-22/LezioniCPP/Zoo.cpp b/2021-22/LezioniCPP/Zoo.cpp deleted file mode 100644 index 015c8ec..0000000 --- a/2021-22/LezioniCPP/Zoo.cpp +++ /dev/null @@ -1,41 +0,0 @@ - -namespace zoo -{ - - class animal - { - protected: - int weight; - - public: - explicit animal(int w) : weight(w) {} - - animal(const animal& a) : weight(a.weight) {} - - virtual void eat(const animal& a) - { - weight += a.weight; - } - }; - - class dog : public animal - { - public: - void eat(const animal& a) override - { - weight += a.weight * 2; - } - }; - - - int main() - { - animal fido(50); - animal* pluto = new animal(40); - - fido.eat(*pluto); - - return 0; - } -} - diff --git a/2021-22/LezioniCPP/main.cpp b/2021-22/LezioniCPP/main.cpp index 08567a9..ceaf737 100644 --- a/2021-22/LezioniCPP/main.cpp +++ b/2021-22/LezioniCPP/main.cpp @@ -1,42 +1,16 @@ #include -#include -#include -#include -#include -#include - -using namespace std; - -template -iterator_traits::value_type sum(InputIterator first, InputIterator last, BinaryFunction f) -{ - typename iterator_traits::value_type z = *first++; - for (; first != last; ++first) - { - z = f(z, *first); - } - return z; -} - -template -auto sum(InputIterator first, InputIterator last) -> decltype(*first) -{ - decltype(*first) z = *first++; - for (; first != last; ++first) - { - z += *first; - - } - return z; -} +import pairs; +import sums; +import zoo; int main() { - vector v = { 1, 2, 3, 4 }; - for_each(begin(v), end(v), [](const auto& x) { cout << x << ", "; }); + sums::test(); + zoo::test(); + pairs::test(); - cout << "sum1 = " << sum(v.begin(), v.end()) << endl; - cout << "sum2 = " << sum(v.begin(), v.end(), [](const auto& a, const auto& b) { return a * b; }) << endl; + return 0; } + diff --git a/2021-22/LezioniCPP/pairs.ixx b/2021-22/LezioniCPP/pairs.ixx new file mode 100644 index 0000000..7cf625d --- /dev/null +++ b/2021-22/LezioniCPP/pairs.ixx @@ -0,0 +1,115 @@ +export module pairs; + +import ; + +export namespace pairs +{ + + template + class mypair + { + template friend class mypair; // necessario per il conversion copy constructor + + private: + A first; + B second; + + public: + // default constructor + mypair() : first(), second() {} + + // main constructor + mypair(const A& a, const B& b) : first(a), second(b) + {} + + // copy constructor + mypair(const mypair& p) : first(p.first), second(p.second) + {} + + // conversion copy constructor + template + mypair(const mypair& p) : first(p.first), second(p.second) + {} + + // assignment operator + mypair& operator=(const mypair& p) + { + first = p.first; + second = p.second; + return *this; + } + + // altri operatori + + mypair operator++() + { + mypair tmp(*this); + first++; + second++; + return tmp; + } + + mypair& operator++(int) + { + ++first; + ++second; + return *this; + } + + mypair operator+(const mypair& p) const + { + return mypair(first + p.first, second + p.second); + } + + mypair& operator+=(const mypair& p) + { + return *this = *this + p; // implementazione che dipende dall'assegnamento e dalla + + } + + // TODO STUDENTI: implementare altri operatori aritmetici che hanno senso + + // metodi di accesso read/write ai campi + + const A& fst() const + { + return first; + } + + A& fst() + { + return first; + } + + const B& snd() const + { + return second; + } + + B& snd() + { + return second; + } + + }; + + + using std::string; + + export void test() + { + mypair p1(4, 5); + mypair p2(p1); + + mypair p3("ciao", true); + mypair p4(p1); + + p1 = p2; + + int n = p1.fst(); + p1.snd() = p1.snd() * 3; + p4 += p1; // converte implicitamente il RV in un pair tramite in conversion copy-constructor templatizzato + } + +} + + diff --git a/2021-22/LezioniCPP/sums.ixx b/2021-22/LezioniCPP/sums.ixx new file mode 100644 index 0000000..33961ed --- /dev/null +++ b/2021-22/LezioniCPP/sums.ixx @@ -0,0 +1,46 @@ +export module sums; + +import ; +import ; +import ; +import ; +import ; +import ; + +using namespace std; + +export namespace sums +{ + template + iterator_traits::value_type sum(InputIterator first, InputIterator last, BinaryFunction f) + { + typename iterator_traits::value_type z = *first++; + for (; first != last; ++first) + { + z = f(z, *first); + } + return z; + } + + template + auto sum(InputIterator first, InputIterator last) -> decltype(*first) + { + decltype(*first) z = *first++; + for (; first != last; ++first) + { + z += *first; + + } + return z; + } + + export void test() + { + vector v = { 1, 2, 3, 4 }; + for_each(begin(v), end(v), [](const auto& x) { cout << x << ", "; }); + + cout << "sum1 = " << sum(v.begin(), v.end()) << endl; + cout << "sum2 = " << sum(v.begin(), v.end(), [](const auto& a, const auto& b) { return a * b; }) << endl; + } + +} \ No newline at end of file diff --git a/2021-22/LezioniCPP/zoo.ixx b/2021-22/LezioniCPP/zoo.ixx new file mode 100644 index 0000000..3a61b0a --- /dev/null +++ b/2021-22/LezioniCPP/zoo.ixx @@ -0,0 +1,64 @@ + +export module zoo; + +namespace zoo +{ + + class animal + { + protected: + int weight_; + + public: + explicit animal(int w) : weight_(w) {} + + animal(const animal& a) : weight_(a.weight_) {} + + virtual void eat(const animal* a) + { + weight_ += a->weight_; + } + + const int& weight() const { return weight_; } + int& weight() { return weight_; } + }; + + class dog : public animal + { + public: + void eat(const animal* a) override + { + weight() = a->weight() * 2; + } + }; + + class cat : public animal + { + public: + void eat(const animal* a) override + { + weight() = a->weight() / 3; + } + }; + + class labrador : public dog + { + public: + void eat(const animal* a) override + { + weight() = a->weight() * 2; + } + }; + + export void test() + { + animal fido(50); + animal* pluto = new animal(40); + + fido.eat(pluto); + pluto->eat(&fido); + + } +} + + From 939c93db25f5953f431e9527f458c6b22258995c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 2 May 2022 14:47:36 +0200 Subject: [PATCH 083/202] Lezione di oggi --- 2021-22/LezioniCPP/pairs.ixx | 55 ++++++++++++++++++++++++++++++++++-- 2021-22/LezioniCPP/zoo.ixx | 8 +++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/2021-22/LezioniCPP/pairs.ixx b/2021-22/LezioniCPP/pairs.ixx index 7cf625d..7aca4b0 100644 --- a/2021-22/LezioniCPP/pairs.ixx +++ b/2021-22/LezioniCPP/pairs.ixx @@ -1,10 +1,34 @@ export module pairs; import ; +import ; export namespace pairs { + // TODO LEZIONE: mostrare questo + + template + inline This& poly_op_assign(This* this_, const T& x, const Op& op) + { + return *this_ = op(*this_, x); + } + +#define OP_BIN(op) \ + mypair& operator op (const mypair& x) const \ + { \ + return mypair(first op x.first, second op x.second); \ + } + +#define OP_ASSIGN(op) \ + auto& operator op ## =(const auto& x) \ + { \ + return *this = *this op x; \ + } + + + + template class mypair { @@ -14,6 +38,18 @@ export namespace pairs A first; B second; + template + mypair op_bin(const mypair& x, const Op& op) const + { + return mypair(op(this->first, x.first), op(this->second, x.second)); + } + + template + mypair& op_assign(const mypair& x, const Op& op) + { + return poly_op_assign(this, x, op); + } + public: // default constructor mypair() : first(), second() {} @@ -61,11 +97,23 @@ export namespace pairs return mypair(first + p.first, second + p.second); } + mypair operator-(const mypair& p) const + { + return op_bin(p, std::minus>()); + } + + /*OP_BIN(*) + OP_BIN(/)*/ + mypair& operator+=(const mypair& p) { - return *this = *this + p; // implementazione che dipende dall'assegnamento e dalla + + return op_assign(p, std::plus>()); } + /*OP_ASSIGN(-) + OP_ASSIGN(*) + OP_ASSIGN(/)*/ + // TODO STUDENTI: implementare altri operatori aritmetici che hanno senso // metodi di accesso read/write ai campi @@ -107,7 +155,10 @@ export namespace pairs int n = p1.fst(); p1.snd() = p1.snd() * 3; - p4 += p1; // converte implicitamente il RV in un pair tramite in conversion copy-constructor templatizzato + p4 += p1; // converte implicitamente il RV in un pair tramite un conversion copy-constructor templatizzato + + //p4 -= p1; + } } diff --git a/2021-22/LezioniCPP/zoo.ixx b/2021-22/LezioniCPP/zoo.ixx index 3a61b0a..0f8cbc3 100644 --- a/2021-22/LezioniCPP/zoo.ixx +++ b/2021-22/LezioniCPP/zoo.ixx @@ -26,6 +26,8 @@ namespace zoo class dog : public animal { public: + explicit dog(int w) : animal(w) {} + void eat(const animal* a) override { weight() = a->weight() * 2; @@ -35,6 +37,8 @@ namespace zoo class cat : public animal { public: + explicit cat(int w) : animal(w) {} + void eat(const animal* a) override { weight() = a->weight() / 3; @@ -44,6 +48,8 @@ namespace zoo class labrador : public dog { public: + explicit labrador(int w) : dog(w) {} + void eat(const animal* a) override { weight() = a->weight() * 2; @@ -53,7 +59,7 @@ namespace zoo export void test() { animal fido(50); - animal* pluto = new animal(40); + animal* pluto = new dog(40); fido.eat(pluto); pluto->eat(&fido); From dff2129c54009fbc0b802f7dea3f1a87c5eb7306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 2 May 2022 17:21:10 +0200 Subject: [PATCH 084/202] smart pointers --- 2021-22/LezioniCPP/LezioniCPP.vcxproj | 1 + 2021-22/LezioniCPP/LezioniCPP.vcxproj.filters | 3 ++ 2021-22/LezioniCPP/smart_ptr.ixx | 33 +++++++++++++++++++ 2021-22/LezioniCPP/zoo.ixx | 26 ++++++++++++--- 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 2021-22/LezioniCPP/smart_ptr.ixx diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj index 7ad97e5..870fae6 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -144,6 +144,7 @@ + diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters index 4ad9526..0ce961b 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters @@ -27,5 +27,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/2021-22/LezioniCPP/smart_ptr.ixx b/2021-22/LezioniCPP/smart_ptr.ixx new file mode 100644 index 0000000..0046275 --- /dev/null +++ b/2021-22/LezioniCPP/smart_ptr.ixx @@ -0,0 +1,33 @@ +export module smart_ptr; + +export +template +class smart_ptr +{ +private: + T* pt; + +// typedef unsigned int counter_t; + using counter_t = unsigned int; + + counter_t* cnt; + +public: + smart_ptr(const T*& pt_) : pt(pt_), cnt(new int(0)) {} + + smart_ptr(const smart_ptr& p) : pt(p.pt), cnt(p.cnt) + { + ++(*cnt); + } + + ~smart_ptr() + { + if (--(*cnt) == 0) + { + delete pt; + delete cnt; + } + } + + +}; \ No newline at end of file diff --git a/2021-22/LezioniCPP/zoo.ixx b/2021-22/LezioniCPP/zoo.ixx index 0f8cbc3..a0571ab 100644 --- a/2021-22/LezioniCPP/zoo.ixx +++ b/2021-22/LezioniCPP/zoo.ixx @@ -1,16 +1,19 @@ export module zoo; -namespace zoo +import ; + +export namespace zoo { - class animal + export class animal { protected: int weight_; public: explicit animal(int w) : weight_(w) {} + virtual ~animal() {} animal(const animal& a) : weight_(a.weight_) {} @@ -25,8 +28,16 @@ namespace zoo class dog : public animal { + private: + int* a; + public: - explicit dog(int w) : animal(w) {} + explicit dog(int w) : animal(w), a(new int[10]) {} + + ~dog() override + { + delete[] a; + } void eat(const animal* a) override { @@ -52,7 +63,7 @@ namespace zoo void eat(const animal* a) override { - weight() = a->weight() * 2; + weight() = a->weight() * 5; } }; @@ -64,7 +75,14 @@ namespace zoo fido.eat(pluto); pluto->eat(&fido); + animal pluto2(dog(40)); + pluto2.eat(pluto); + + delete pluto; } + + + } From e3831a02f766d6ca459918e92abfa95e13d74f62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 2 May 2022 17:49:12 +0200 Subject: [PATCH 085/202] VS project fixed --- 2021-22/LezioniCPP/LezioniCPP.vcxproj | 2 +- 2021-22/LezioniCPP/LezioniCPP.vcxproj.filters | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj index 870fae6..757637b 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -143,8 +143,8 @@ + - diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters index 0ce961b..92f9527 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters @@ -27,7 +27,7 @@ Source Files - + Source Files From 8a4af7b4e9891ca20f4c4e71604adbfb7c04de8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 3 May 2022 15:07:44 +0200 Subject: [PATCH 086/202] pairs updated --- 2021-22/LezioniCPP/pairs.ixx | 111 ++++++++++++++++++++----------- 2021-22/LezioniCPP/smart_ptr.ixx | 10 ++- 2 files changed, 79 insertions(+), 42 deletions(-) diff --git a/2021-22/LezioniCPP/pairs.ixx b/2021-22/LezioniCPP/pairs.ixx index 7aca4b0..97fde7d 100644 --- a/2021-22/LezioniCPP/pairs.ixx +++ b/2021-22/LezioniCPP/pairs.ixx @@ -6,68 +6,84 @@ import ; export namespace pairs { - // TODO LEZIONE: mostrare questo - - template - inline This& poly_op_assign(This* this_, const T& x, const Op& op) + // utility generale per definire operatori aritmetici di assegnamento in-place (es: +=, -= ecc) tramite una funzione di ordine superiore + // questa funzione non è specifica per mypair ma può definire operatori aritmetici in-place dato un operatore binario qualunque + // in altre parole, essa non ha bisogno di sapere com'è fatta la classe su cui opera: gli è sufficiente conoscere l'operatore artimetico binario e necessita dell'esistenza dell'assegnamento + template + inline This& gen_op_assign(This* this_, const This& x, const Op& op) { - return *this_ = op(*this_, x); - } - -#define OP_BIN(op) \ - mypair& operator op (const mypair& x) const \ - { \ - return mypair(first op x.first, second op x.second); \ - } - -#define OP_ASSIGN(op) \ - auto& operator op ## =(const auto& x) \ - { \ - return *this = *this op x; \ + return *this_ = op(*this_, x); // richiede sia definito definito operator=() } - + // classe templatizzata mypair + // template class mypair { template friend class mypair; // necessario per il conversion copy constructor + public: + // main constructor: inizializza i campi privati first e second copiando gli argomenti a e b; ovvero invoca i copy constructor di A e B + mypair(const A& a, const B& b) : first(a), second(b) + {} + private: A first; B second; - template - mypair op_bin(const mypair& x, const Op& op) const + // metodo privato di utility per definire operatori aritmetici tramite funzioni di ordine superiore + // occorre passare i due operatori per i tipi A e B come argomento + template + inline mypair op_bin(const mypair& x, const Op1& op_first, const Op2& op_second) const { - return mypair(op(this->first, x.first), op(this->second, x.second)); + return mypair(op_first(first, x.first), op_second(second, x.second)); } + // metodo privato di utility per definire operatori aritmetici con assegnamento in-place stubbando poly_op_assign() con this come primo argomento template - mypair& op_assign(const mypair& x, const Op& op) + inline mypair& op_assign(const mypair& x, const Op& op) { - return poly_op_assign(this, x, op); + return gen_op_assign(this, x, op); } + // macro per definire operatori aritmetici facilmente: essa chiama a sua volta l'operatore op sui campi privati della mypair + // questa macro fa una cosa simile a ciò che fa la funzione op_bin(), ma lo fa con una macro anziché con una funzione di ordine superiore + // NOTA: definire una macro dentro una classe non ha un vero motivo: le macro sono entità gestite dal pre-processore e non sono soggette a scoping + // questa macro è definita qui solamente perché a livello logico opera con mypair +# define OP_BIN(op) \ + mypair operator op (const mypair& x) const \ + { \ + return mypair(first op x.first, second op x.second); \ + } + + // macro per definire operatori aritmetici facilmente: essa chiama a sua volta l'operatore op tra this e l'argomento x + // questa macro fa una cosa simile a ciò che fa global_op_assign(), ma lo fa con una macro anziché con una funzione di ordine superiore + // è interessante notare che anche questa macro, come global_op_assign(), non menziona i campi privati +# define OP_ASSIGN(op) \ + mypair& operator op ## =(const mypair& x) \ + { \ + return *this = *this op x; \ + } + + public: - // default constructor + // default constructor: chiama i default constructor dei campi privati, quindi deve esistere un default construtor per i tipi A e B mypair() : first(), second() {} - // main constructor - mypair(const A& a, const B& b) : first(a), second(b) - {} + - // copy constructor + // copy constructor: invoca i copy constructor di A e B mypair(const mypair& p) : first(p.first), second(p.second) {} - // conversion copy constructor + // conversion copy constructor: richiede l'esistenza di un costruttore di A tramite un argomento di tipo C, e di B tramite un argomento di tipo D template mypair(const mypair& p) : first(p.first), second(p.second) {} - // assignment operator + // assignment operator: richiede che sia definito operator=() per A e B mypair& operator=(const mypair& p) { first = p.first; @@ -75,9 +91,8 @@ export namespace pairs return *this; } - // altri operatori - - mypair operator++() + // post-incremento: richiede che sia definito l'operatore di post-incremento per A e B + mypair operator++() // ritorna una NUOVA mypair per copia { mypair tmp(*this); first++; @@ -85,38 +100,54 @@ export namespace pairs return tmp; } - mypair& operator++(int) + // pre-incremento: richiede che sia definito l'operatore di pre-incremento per A e B + mypair& operator++(int) // ritorna un reference alla mypair appena incrementata { ++first; ++second; return *this; } + // seguono gli operatori aritmetici in-place e non implementati in vari modi: direttamente, usando le funzioni di utility e con le macro + // + + // operator+: richiede l'esistenza dell'operator+() per A e B mypair operator+(const mypair& p) const { return mypair(first + p.first, second + p.second); } + // operator-: usando op_bin() è sufficiente passare le 2 operazioni di sottrazione binaria come argomenti mypair operator-(const mypair& p) const { - return op_bin(p, std::minus>()); + return op_bin(p, std::minus(), std::minus()); // passiamo DUE VOLTE l'operazione di sottrazione perché occorre passare quella per A e quella per B } - /*OP_BIN(*) - OP_BIN(/)*/ + // operator* e operator/ definiti usando la macro + OP_BIN(*) + OP_BIN(/) + // operator+: usando op_assign() è sufficiente passare l'operazione di somma binaria come argomento mypair& operator+=(const mypair& p) { return op_assign(p, std::plus>()); } - /*OP_ASSIGN(-) + // operator-: richiede solamente l'esistenza dell'operator=() e dell'operator-() + // questa implementazione non menziona i campi ed è praticamente uguale a quello che fa global_op_assign(), con la differenza che quest'ultima lo fa in modo generale per qualunque operatore binario + mypair& operator-=(const mypair& p) + { + return *this = *this - p; + } + + // operator* e operator/ definiti usando la macro OP_ASSIGN(*) - OP_ASSIGN(/)*/ + OP_ASSIGN(/) // TODO STUDENTI: implementare altri operatori aritmetici che hanno senso // metodi di accesso read/write ai campi + // const A& fst() const { @@ -157,7 +188,7 @@ export namespace pairs p1.snd() = p1.snd() * 3; p4 += p1; // converte implicitamente il RV in un pair tramite un conversion copy-constructor templatizzato - //p4 -= p1; + p4 -= p1; } diff --git a/2021-22/LezioniCPP/smart_ptr.ixx b/2021-22/LezioniCPP/smart_ptr.ixx index 0046275..7e320a2 100644 --- a/2021-22/LezioniCPP/smart_ptr.ixx +++ b/2021-22/LezioniCPP/smart_ptr.ixx @@ -17,7 +17,7 @@ public: smart_ptr(const smart_ptr& p) : pt(p.pt), cnt(p.cnt) { - ++(*cnt); + ++*cnt; } ~smart_ptr() @@ -30,4 +30,10 @@ public: } -}; \ No newline at end of file +}; + + +void test_smart_ptr() +{ + +} From 510bcce2a1f1a498d1422f55e85b2fd414734dae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 3 May 2022 15:08:08 +0200 Subject: [PATCH 087/202] Update pairs.ixx --- 2021-22/LezioniCPP/pairs.ixx | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/2021-22/LezioniCPP/pairs.ixx b/2021-22/LezioniCPP/pairs.ixx index 97fde7d..38bd3b6 100644 --- a/2021-22/LezioniCPP/pairs.ixx +++ b/2021-22/LezioniCPP/pairs.ixx @@ -24,11 +24,6 @@ export namespace pairs { template friend class mypair; // necessario per il conversion copy constructor - public: - // main constructor: inizializza i campi privati first e second copiando gli argomenti a e b; ovvero invoca i copy constructor di A e B - mypair(const A& a, const B& b) : first(a), second(b) - {} - private: A first; B second; @@ -72,7 +67,9 @@ export namespace pairs // default constructor: chiama i default constructor dei campi privati, quindi deve esistere un default construtor per i tipi A e B mypair() : first(), second() {} - + // main constructor: inizializza i campi privati first e second copiando gli argomenti a e b; ovvero invoca i copy constructor di A e B + mypair(const A& a, const B& b) : first(a), second(b) + {} // copy constructor: invoca i copy constructor di A e B mypair(const mypair& p) : first(p.first), second(p.second) From 99d53cb7318572b139d09d3414990735e2f6b8d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 3 May 2022 15:12:58 +0200 Subject: [PATCH 088/202] Update pairs.ixx --- 2021-22/LezioniCPP/pairs.ixx | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/2021-22/LezioniCPP/pairs.ixx b/2021-22/LezioniCPP/pairs.ixx index 38bd3b6..bc814dc 100644 --- a/2021-22/LezioniCPP/pairs.ixx +++ b/2021-22/LezioniCPP/pairs.ixx @@ -64,6 +64,10 @@ export namespace pairs public: + /////////////////////////////////////////////////////////// + // costruttori + // + // default constructor: chiama i default constructor dei campi privati, quindi deve esistere un default construtor per i tipi A e B mypair() : first(), second() {} @@ -80,7 +84,11 @@ export namespace pairs mypair(const mypair& p) : first(p.first), second(p.second) {} - // assignment operator: richiede che sia definito operator=() per A e B + /////////////////////////////////////////////////////////// + // operatori principali + // + + // assegnamento: richiede che sia definito operator=() per A e B mypair& operator=(const mypair& p) { first = p.first; @@ -105,32 +113,38 @@ export namespace pairs return *this; } - // seguono gli operatori aritmetici in-place e non implementati in vari modi: direttamente, usando le funzioni di utility e con le macro + /////////////////////////////////////////////////////////// + // operatori aritmetici binari implementati in vari modi: direttamente, usando le funzioni di utility e con le macro // - // operator+: richiede l'esistenza dell'operator+() per A e B + // somma in-place implemetato direttamente: richiede l'esistenza dell'operator+() per A e B mypair operator+(const mypair& p) const { return mypair(first + p.first, second + p.second); } - // operator-: usando op_bin() è sufficiente passare le 2 operazioni di sottrazione binaria come argomenti + // sottrazione in-place usando op_bin(): è sufficiente passare le 2 operazioni di sottrazione binaria come argomenti mypair operator-(const mypair& p) const { return op_bin(p, std::minus(), std::minus()); // passiamo DUE VOLTE l'operazione di sottrazione perché occorre passare quella per A e quella per B } - // operator* e operator/ definiti usando la macro + // operator* e operator/ implementati usando la macro OP_BIN(*) - OP_BIN(/) + OP_BIN(/ ) - // operator+: usando op_assign() è sufficiente passare l'operazione di somma binaria come argomento + + /////////////////////////////////////////////////////////// + // operatori aritmetici in-place implementati in vari modi: direttamente, usando le funzioni di utility e con le macro + // + + // somma usando op_assign(): è sufficiente passare l'operazione di somma binaria come argomento mypair& operator+=(const mypair& p) { return op_assign(p, std::plus>()); } - // operator-: richiede solamente l'esistenza dell'operator=() e dell'operator-() + // sottrazione implementata direttamente: richiede solamente l'esistenza dell'operator=() e dell'operator-() // questa implementazione non menziona i campi ed è praticamente uguale a quello che fa global_op_assign(), con la differenza che quest'ultima lo fa in modo generale per qualunque operatore binario mypair& operator-=(const mypair& p) { @@ -141,8 +155,11 @@ export namespace pairs OP_ASSIGN(*) OP_ASSIGN(/) - // TODO STUDENTI: implementare altri operatori aritmetici che hanno senso + + + + /////////////////////////////////////////////////////////// // metodi di accesso read/write ai campi // From bedb8a034c68191e51206b28e8414d2986368787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 3 May 2022 15:21:38 +0200 Subject: [PATCH 089/202] Update pairs.ixx --- 2021-22/LezioniCPP/pairs.ixx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/2021-22/LezioniCPP/pairs.ixx b/2021-22/LezioniCPP/pairs.ixx index bc814dc..667f547 100644 --- a/2021-22/LezioniCPP/pairs.ixx +++ b/2021-22/LezioniCPP/pairs.ixx @@ -114,22 +114,22 @@ export namespace pairs } /////////////////////////////////////////////////////////// - // operatori aritmetici binari implementati in vari modi: direttamente, usando le funzioni di utility e con le macro + // operatori aritmetici implementati in vari modi: direttamente, usando le funzioni di utility e con le macro // - // somma in-place implemetato direttamente: richiede l'esistenza dell'operator+() per A e B + // somma implemetata direttamente: richiede l'esistenza dell'operator+() per A e B mypair operator+(const mypair& p) const { return mypair(first + p.first, second + p.second); } - // sottrazione in-place usando op_bin(): è sufficiente passare le 2 operazioni di sottrazione binaria come argomenti + // sottrazione usando op_bin(): è sufficiente passare le 2 operazioni di sottrazione binaria come argomenti mypair operator-(const mypair& p) const { return op_bin(p, std::minus(), std::minus()); // passiamo DUE VOLTE l'operazione di sottrazione perché occorre passare quella per A e quella per B } - // operator* e operator/ implementati usando la macro + // operator*() e operator/() implementati usando la macro OP_BIN(*) OP_BIN(/ ) @@ -138,20 +138,20 @@ export namespace pairs // operatori aritmetici in-place implementati in vari modi: direttamente, usando le funzioni di utility e con le macro // - // somma usando op_assign(): è sufficiente passare l'operazione di somma binaria come argomento + // somma in-place usando op_assign(): è sufficiente passare l'operazione di somma binaria come argomento mypair& operator+=(const mypair& p) { return op_assign(p, std::plus>()); } - // sottrazione implementata direttamente: richiede solamente l'esistenza dell'operator=() e dell'operator-() + // sottrazione in-place implementata direttamente: richiede solamente l'esistenza dell'operator=() e dell'operator-() // questa implementazione non menziona i campi ed è praticamente uguale a quello che fa global_op_assign(), con la differenza che quest'ultima lo fa in modo generale per qualunque operatore binario mypair& operator-=(const mypair& p) { return *this = *this - p; } - // operator* e operator/ definiti usando la macro + // operator*=() e operator/=() definiti usando la macro OP_ASSIGN(*) OP_ASSIGN(/) From 5713faf4dd6b8514113b1bb6e7f26585a1df3178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 3 May 2022 17:21:15 +0200 Subject: [PATCH 090/202] smart pointers update --- 2021-22/LezioniCPP/main.cpp | 12 ++++ 2021-22/LezioniCPP/smart_ptr.ixx | 106 +++++++++++++++++++++++++++++-- 2 files changed, 112 insertions(+), 6 deletions(-) diff --git a/2021-22/LezioniCPP/main.cpp b/2021-22/LezioniCPP/main.cpp index ceaf737..e8f2a02 100644 --- a/2021-22/LezioniCPP/main.cpp +++ b/2021-22/LezioniCPP/main.cpp @@ -5,12 +5,24 @@ import sums; import zoo; + +template +int f(const Gigi& gigi) +{ + return gianni(&gigi); +} + + +int gianni(int x) { return x + 1; } + int main() { sums::test(); zoo::test(); pairs::test(); + f(3); + return 0; } diff --git a/2021-22/LezioniCPP/smart_ptr.ixx b/2021-22/LezioniCPP/smart_ptr.ixx index 7e320a2..f7a57a8 100644 --- a/2021-22/LezioniCPP/smart_ptr.ixx +++ b/2021-22/LezioniCPP/smart_ptr.ixx @@ -1,19 +1,39 @@ export module smart_ptr; +import ; + +using std::string; + export template class smart_ptr { private: T* pt; + size_t len; // typedef unsigned int counter_t; using counter_t = unsigned int; counter_t* cnt; + void destroy() + { + if (--(*cnt) == 0) + { + if (len == 1) delete pt; + else delete[] pt; + delete cnt; + } + } + public: - smart_ptr(const T*& pt_) : pt(pt_), cnt(new int(0)) {} + // typedef T value_type; + using value_type = T; + + smart_ptr(const T*& pt_, size_t len_ = 1) : pt(pt_), cnt(new int(1)), len(len_) {} + + smart_ptr(size_t len_) : pt(new T[len_]), cnt(new int(1)), len(len_) {} smart_ptr(const smart_ptr& p) : pt(p.pt), cnt(p.cnt) { @@ -22,18 +42,92 @@ public: ~smart_ptr() { - if (--(*cnt) == 0) - { - delete pt; - delete cnt; - } + destroy(); + } + + smart_ptr& operator=(const smart_ptr& p) + { + ++(*p.cnt); + destroy(); + pt = p.pt; + cnt = p.cnt; + return *this; + } + + T& operator*() + { + return (*this)[0]; + } + + const T& operator*() const + { + return *pt; + } + + T& operator[](size_t i) + { + return pt[i]; } + const T& operator[](size_t i) const + { + return pt[i]; + } + + T* operator->() + { + return pt; + } + + const T* operator->() const + { + return pt; + } + + // TODO STUDENTI: implementare operatori +, ++, +=, -, --, -= + +}; + +class foo +{ +public: + smart_ptr pt; + foo(smart_ptr pt_) : pt(pt_) {} }; +template +void swap(Pointer& p1, Pointer& p2) +{ + Pointer::value_type& tmp = *p1; + *p1 = *p2; + *p2 = tmp; +} + + +void f(smart_ptr p) +{ + foo oo(p); +} + void test_smart_ptr() { + int* a = new int[100]; + smart_ptr a2(new int[100], 100); + smart_ptr x(600); + string* s = new string("ciao"); + smart_ptr s2 = new string("ciao"); + + s2 = s2; + + f(a2); + // ... + + delete[] a; + delete s; } + + + From e3904cc6ed7c0941b95e69d95c34ba94293aa380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 3 May 2022 18:27:03 +0200 Subject: [PATCH 091/202] updated --- 2021-22/LezioniCPP/main.cpp | 17 ++++++++--------- 2021-22/LezioniCPP/smart_ptr.ixx | 16 ++++++++-------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/2021-22/LezioniCPP/main.cpp b/2021-22/LezioniCPP/main.cpp index e8f2a02..0b4e119 100644 --- a/2021-22/LezioniCPP/main.cpp +++ b/2021-22/LezioniCPP/main.cpp @@ -4,24 +4,23 @@ import pairs; import sums; import zoo; +// per C++20 ALMENO una definizione di gianni() deve esistere PRIMA della funzione templatizzata che la usa (questo progetto compila in C++20) +// in C++ vanilla poteva non esisterne neanche una +int gianni(int x) { return x + 1; } -template -int f(const Gigi& gigi) +template +Gigi f(const Gigi& gigi) { - return gianni(&gigi); + return gianni(gigi); // da C++20 i template sono type-checkati e una funzione di nome gianni deve esistere ALMENO di un tipo } -int gianni(int x) { return x + 1; } - int main() { - sums::test(); - zoo::test(); - pairs::test(); - f(3); + int x = f(3); + //char* s = f("ciao"); // questa NON compila perché non esiste un overload di gianni() che prenda char* return 0; } diff --git a/2021-22/LezioniCPP/smart_ptr.ixx b/2021-22/LezioniCPP/smart_ptr.ixx index f7a57a8..ba04a1a 100644 --- a/2021-22/LezioniCPP/smart_ptr.ixx +++ b/2021-22/LezioniCPP/smart_ptr.ixx @@ -10,7 +10,7 @@ class smart_ptr { private: T* pt; - size_t len; + bool is_array; // typedef unsigned int counter_t; using counter_t = unsigned int; @@ -21,7 +21,7 @@ private: { if (--(*cnt) == 0) { - if (len == 1) delete pt; + if (is_array) delete pt; else delete[] pt; delete cnt; } @@ -31,13 +31,13 @@ public: // typedef T value_type; using value_type = T; - smart_ptr(const T*& pt_, size_t len_ = 1) : pt(pt_), cnt(new int(1)), len(len_) {} + smart_ptr(T* pt_, bool is_array_ = false) : pt(pt_), cnt(new unsigned int(1)), is_array(is_array_) {} - smart_ptr(size_t len_) : pt(new T[len_]), cnt(new int(1)), len(len_) {} + smart_ptr(size_t len) : smart_ptr(new T[len], len > 1) {} - smart_ptr(const smart_ptr& p) : pt(p.pt), cnt(p.cnt) + smart_ptr(const smart_ptr& p) : pt(p.pt), cnt(p.cnt), is_array(p.is_array) { - ++*cnt; + ++(*cnt); } ~smart_ptr() @@ -100,7 +100,7 @@ public: template void swap(Pointer& p1, Pointer& p2) { - Pointer::value_type& tmp = *p1; + typename Pointer::value_type& tmp = *p1; *p1 = *p2; *p2 = tmp; } @@ -114,7 +114,7 @@ void f(smart_ptr p) void test_smart_ptr() { int* a = new int[100]; - smart_ptr a2(new int[100], 100); + smart_ptr a2(new int[100], true); smart_ptr x(600); string* s = new string("ciao"); From 51b020f36cea2c601e97060e9ad6dc31ce0c55fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 3 May 2022 18:29:08 +0200 Subject: [PATCH 092/202] Update main.cpp --- 2021-22/LezioniCPP/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/2021-22/LezioniCPP/main.cpp b/2021-22/LezioniCPP/main.cpp index 0b4e119..ec9d561 100644 --- a/2021-22/LezioniCPP/main.cpp +++ b/2021-22/LezioniCPP/main.cpp @@ -4,7 +4,7 @@ import pairs; import sums; import zoo; -// per C++20 ALMENO una definizione di gianni() deve esistere PRIMA della funzione templatizzata che la usa (questo progetto compila in C++20) +// da C++11 ALMENO una definizione di gianni() deve esistere PRIMA della funzione templatizzata che la usa (questo progetto compila in C++20) // in C++ vanilla poteva non esisterne neanche una int gianni(int x) { return x + 1; } @@ -12,7 +12,7 @@ int gianni(int x) { return x + 1; } template Gigi f(const Gigi& gigi) { - return gianni(gigi); // da C++20 i template sono type-checkati e una funzione di nome gianni deve esistere ALMENO di un tipo + return gianni(gigi); // da C++11 i template sono type-checkati e una funzione di nome gianni deve esistere ALMENO di un tipo } From 4bdb5a266166dbe9761d3ccff8430cc6df196dca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 4 May 2022 10:34:36 +0200 Subject: [PATCH 093/202] Update main.cpp --- 2021-22/LezioniCPP/main.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/2021-22/LezioniCPP/main.cpp b/2021-22/LezioniCPP/main.cpp index ec9d561..067d3a0 100644 --- a/2021-22/LezioniCPP/main.cpp +++ b/2021-22/LezioniCPP/main.cpp @@ -9,10 +9,11 @@ import zoo; int gianni(int x) { return x + 1; } -template +template Gigi f(const Gigi& gigi) { - return gianni(gigi); // da C++11 i template sono type-checkati e una funzione di nome gianni deve esistere ALMENO di un tipo + return gianni(gigi); // da C++11 i template sono type-checkati e una funzione gianni() deve esistere ALMENO di un tipo + // ma nemmeno C++20 può sapere con quale template argument verrà usata f() e quindi } From 9aea98c6965bf1d7eb785f745c546d3e12f237a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 10 May 2022 12:12:39 +0200 Subject: [PATCH 094/202] Create member_types.ixx --- 2021-22/LezioniCPP/member_types.ixx | 68 +++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 2021-22/LezioniCPP/member_types.ixx diff --git a/2021-22/LezioniCPP/member_types.ixx b/2021-22/LezioniCPP/member_types.ixx new file mode 100644 index 0000000..7102cc0 --- /dev/null +++ b/2021-22/LezioniCPP/member_types.ixx @@ -0,0 +1,68 @@ + +export module member_types; + +import ; +import ; +import ; +import ; + +using namespace std; + +class miononno +{ +private: + int x; + +public: + int& operator*() { return x; } + + miononno& operator++() + { + x++; + return *this; + } +}; + + +template +void increment_all(Container& v) +{ + for (Container::iterator it = v.begin(); it != v.end(); ++it) + { + *it = *it + 2; + } +} + + +void test_vector_iter() +{ + + vector v{ 1, 2, 3, 4 }; + + increment_all(v); + + list l{ 1, 2, 3, 5, 6 }; + increment_all(l); + + + + + + for (size_t i = 0; i < v.size(); ++i) + { + int n = v[i]; + // ... + } + + for (vector::iterator it = v.begin(); it != v.end(); ++it) + { + ++* it; + } + + for (vector::const_iterator it = v.begin(); it != v.end(); ++it) + { + cout << *it << "\n"; + } + + +} \ No newline at end of file From 55ab0317f74dff858f6ede3c06787d35faf6b2fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 10 May 2022 12:17:11 +0200 Subject: [PATCH 095/202] Added new file --- 2021-22/LezioniCPP/LezioniCPP.vcxproj | 1 + 2021-22/LezioniCPP/LezioniCPP.vcxproj.filters | 3 +++ 2 files changed, 4 insertions(+) diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj index 757637b..297a2a9 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -141,6 +141,7 @@ + diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters index 92f9527..c3734e2 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters @@ -30,5 +30,8 @@ Source Files + + Source Files + \ No newline at end of file From ed3caad520335fc64109eb17971a183d591bacf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 10 May 2022 15:11:47 +0200 Subject: [PATCH 096/202] Lezione 9/5/22 commentata --- 2021-22/LezioniCPP/LezioniCPP.vcxproj | 3 +- 2021-22/LezioniCPP/LezioniCPP.vcxproj.filters | 5 +- 2021-22/LezioniCPP/ident.ixx | 32 ++++++++ 2021-22/LezioniCPP/iterators.ixx | 75 +++++++++++++++++++ 2021-22/LezioniCPP/main.cpp | 30 ++++---- 2021-22/LezioniCPP/member_types.ixx | 68 ----------------- 2021-22/LezioniCPP/pairs.ixx | 20 ++--- 2021-22/LezioniCPP/smart_ptr.ixx | 59 +++++++-------- 2021-22/LezioniCPP/sums.ixx | 4 +- 9 files changed, 164 insertions(+), 132 deletions(-) create mode 100644 2021-22/LezioniCPP/ident.ixx create mode 100644 2021-22/LezioniCPP/iterators.ixx delete mode 100644 2021-22/LezioniCPP/member_types.ixx diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj index 297a2a9..191ac6d 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -141,7 +141,8 @@ - + + diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters index c3734e2..ef999c1 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters @@ -30,7 +30,10 @@ Source Files - + + Source Files + + Source Files diff --git a/2021-22/LezioniCPP/ident.ixx b/2021-22/LezioniCPP/ident.ixx new file mode 100644 index 0000000..7c2aa67 --- /dev/null +++ b/2021-22/LezioniCPP/ident.ixx @@ -0,0 +1,32 @@ +#include + +import pairs; +import sums; +import zoo; + +// da C++11 ALMENO una definizione di ident() deve esistere PRIMA della funzione templatizzata che la usa (questo progetto compila in C++20) +// in C++ vanilla poteva non esisterne neanche una +int ident(int x) { return x; } + + +template +T dual_ident(const T& a) +{ + return ident(ident(a)); // da C++11 i template sono type-checkati debolmente: viene controllato solamente che esista ALMENO una definizione per le funzioni usate internamente + // ma il compilatore non può sapere con quale template argument verrà invocata questa funzione, quindi sospende la compilazione +} + +export int test_ident() +{ + + // ogni singola chiamata ad una funzione templatizzata genera una nuova istanza della funzione in cui viene sostituito il template parameter con il tipo + // concreto usato in quel caso; la funzione viene poi compilata con i tipo completo. + int x = dual_ident(3); // questa invocazione genera una istanza della funzione dual_ident() con int come template argument + // tale istanza compila perché esiste una definizione di ident() che prende e ritorna int + + //char* s = dual_ident("ciao"); // questa invocazione genera una istanza della funzione dual_ident() con char* come template argument + // ma questa istanza NON compila perché non esiste un overload di ident() che prenda char* + + return 0; +} + diff --git a/2021-22/LezioniCPP/iterators.ixx b/2021-22/LezioniCPP/iterators.ixx new file mode 100644 index 0000000..9e5e002 --- /dev/null +++ b/2021-22/LezioniCPP/iterators.ixx @@ -0,0 +1,75 @@ + +export module iterators; + +import ; +import ; +import ; +import ; +import pairs; +import smart_ptr; + +using namespace std; + + +// questa funzione è templatizzata su un tipo Container QUALUNQUE +// non c'è alcun genere di specifica dei requisiti che questo tipo Container deve rispettare: il nome stesso è totalmente arbitrario +// l'implementazione fa emergere la necessità che questo tipo Container risponda a certi requisiti +template +void increment_all(Container& v) +{ + // la keyword typename indica al compilatore che l'identificatore che segue è un TIPO e non un membro statico + // senza typename il compilatore potrebbe confondere Container::iterator con un campo statico di nome iterator dentro la classe Container + for (typename Container::iterator it = v.begin(); it != v.end(); ++it) // richiede l'esistenza di un member type di nome iterator e dei metodi begin() ed end() + { // inoltre richiede l'operatore di post-incremento sul tipo dell'iteratore + typename Container::reference v = *it; // richiede l'esistenza di un member type di nome reference (di solito è equivalente a value_type&) + ++v; // richiede l'esistenza dell'operatore di pre-incremento per il tipo dell'elemento del container + } +} + +template +void print_all(Container& v) +{ + cout << typeid(Container).name() << "["; + for (typename Container::const_iterator it = v.begin(); it != v.end(); ++it) // usiamo il const_iterator perché non dobbiamo modificare gli elementi del container + { + cout << *it << ", "; // richiede l'operatore << sul tipo dell'elemento del container + } + cout << "\b\b]" << endl; +} + +using pairs::mypair; + +// definiamo gli operatori di output stream per smart_ptr e mypair +template +ostream& operator<<(ostream& os, const mypair& p) +{ + return os << "(" << p.fst() << ", " << p.snd() << ")"; +} + +// ridefinisce l'operatore GLOBALE di streaming per double* +ostream& operator<<(ostream& os, const double*& p) +{ + os << "&" << static_cast(p); // casta a constvoid* per stampare l'indirizzo numerico (senza cast sarebbe una RICORSIONE!!!!) + if (p != nullptr) os << "[" << *p << "]"; // se non è nullo printa anche il dereference + return os; +} + +export void test_iterators() +{ + vector v{ 1, 2, 3, 4 }; + print_all(v); // questa chiamata genera una istanza della funzione print_all() con vector sostituo al template parameter Container + increment_all(v); // questa chiamata genera una istanza della funzione print_all() con vector sostituo al template parameter Container + print_all(v); // questa NON rigenera un'altra istanza della funzione print_all() per vector: per ogni tipo diverso ne viene generata UNA SOLA + + list l1{ "ciao", "sono", "io" }; + print_all(l1); // questa chiamata genera una istanza di print_all() per list + //increment_all(l1); // NON COMPILA: questa chiamata genera una istanza di increment_all() per list, ma il pre-incremento non esiste per string + + double arr[] = {11.23, 35.53}; + list> l2{ {1u, arr}, {2u, arr}, {3u, arr} }; + print_all(l2); // questa genera una istanza della print_all() per list> + // essa compila perché abbiamo definito l'operator<< per mypair, altrimenti non compilerebbe + increment_all(l2); // questa compila perché mypair ha l'operatore di pre-incremento, il quale invoca a sua volta il pre-incremento sui due campi della coppia + // pertanto la chiamata compila perché sia unsigned int che double* supportano il pre-incremento + print_all(l2); +} \ No newline at end of file diff --git a/2021-22/LezioniCPP/main.cpp b/2021-22/LezioniCPP/main.cpp index 067d3a0..ce4bd27 100644 --- a/2021-22/LezioniCPP/main.cpp +++ b/2021-22/LezioniCPP/main.cpp @@ -1,28 +1,24 @@ #include +#include import pairs; import sums; import zoo; +import iterators; -// da C++11 ALMENO una definizione di gianni() deve esistere PRIMA della funzione templatizzata che la usa (questo progetto compila in C++20) -// in C++ vanilla poteva non esisterne neanche una -int gianni(int x) { return x + 1; } - - -template -Gigi f(const Gigi& gigi) -{ - return gianni(gigi); // da C++11 i template sono type-checkati e una funzione gianni() deve esistere ALMENO di un tipo - // ma nemmeno C++20 può sapere con quale template argument verrà usata f() e quindi -} - +using namespace std; int main() { - - int x = f(3); - //char* s = f("ciao"); // questa NON compila perché non esiste un overload di gianni() che prenda char* - - return 0; + try + { + test_iterators(); + return 0; + } + catch (exception& e) + { + cerr << "exception caught: " << e.what() << endl; + return 1; + } } diff --git a/2021-22/LezioniCPP/member_types.ixx b/2021-22/LezioniCPP/member_types.ixx deleted file mode 100644 index 7102cc0..0000000 --- a/2021-22/LezioniCPP/member_types.ixx +++ /dev/null @@ -1,68 +0,0 @@ - -export module member_types; - -import ; -import ; -import ; -import ; - -using namespace std; - -class miononno -{ -private: - int x; - -public: - int& operator*() { return x; } - - miononno& operator++() - { - x++; - return *this; - } -}; - - -template -void increment_all(Container& v) -{ - for (Container::iterator it = v.begin(); it != v.end(); ++it) - { - *it = *it + 2; - } -} - - -void test_vector_iter() -{ - - vector v{ 1, 2, 3, 4 }; - - increment_all(v); - - list l{ 1, 2, 3, 5, 6 }; - increment_all(l); - - - - - - for (size_t i = 0; i < v.size(); ++i) - { - int n = v[i]; - // ... - } - - for (vector::iterator it = v.begin(); it != v.end(); ++it) - { - ++* it; - } - - for (vector::const_iterator it = v.begin(); it != v.end(); ++it) - { - cout << *it << "\n"; - } - - -} \ No newline at end of file diff --git a/2021-22/LezioniCPP/pairs.ixx b/2021-22/LezioniCPP/pairs.ixx index 667f547..faa23da 100644 --- a/2021-22/LezioniCPP/pairs.ixx +++ b/2021-22/LezioniCPP/pairs.ixx @@ -3,13 +3,14 @@ export module pairs; import ; import ; +// questo namespace ha lo stesso nome del modulo: in C++20 i moduli NON SONO NAMESPACE, quindi i namespace possono essere definiti e posso avere lo stesso nome dei moduli export namespace pairs { // utility generale per definire operatori aritmetici di assegnamento in-place (es: +=, -= ecc) tramite una funzione di ordine superiore // questa funzione non è specifica per mypair ma può definire operatori aritmetici in-place dato un operatore binario qualunque // in altre parole, essa non ha bisogno di sapere com'è fatta la classe su cui opera: gli è sufficiente conoscere l'operatore artimetico binario e necessita dell'esistenza dell'assegnamento - template + template inline This& gen_op_assign(This* this_, const This& x, const Op& op) { return *this_ = op(*this_, x); // richiede sia definito definito operator=() @@ -19,10 +20,11 @@ export namespace pairs // classe templatizzata mypair // - template + export + template class mypair { - template friend class mypair; // necessario per il conversion copy constructor + template friend class mypair; // necessario per il conversion copy constructor private: A first; @@ -30,14 +32,14 @@ export namespace pairs // metodo privato di utility per definire operatori aritmetici tramite funzioni di ordine superiore // occorre passare i due operatori per i tipi A e B come argomento - template + template inline mypair op_bin(const mypair& x, const Op1& op_first, const Op2& op_second) const { return mypair(op_first(first, x.first), op_second(second, x.second)); } // metodo privato di utility per definire operatori aritmetici con assegnamento in-place stubbando poly_op_assign() con this come primo argomento - template + template inline mypair& op_assign(const mypair& x, const Op& op) { return gen_op_assign(this, x, op); @@ -80,7 +82,7 @@ export namespace pairs {} // conversion copy constructor: richiede l'esistenza di un costruttore di A tramite un argomento di tipo C, e di B tramite un argomento di tipo D - template + template mypair(const mypair& p) : first(p.first), second(p.second) {} @@ -156,9 +158,6 @@ export namespace pairs OP_ASSIGN(/) - - - /////////////////////////////////////////////////////////// // metodi di accesso read/write ai campi // @@ -188,6 +187,9 @@ export namespace pairs using std::string; + // questa funzione viene esportata quindi gli altri moduli la vedranno; viene chiamata solamente test() e non test_pairs() perché è dentro il namespace pairs + // perciò coloro che la chiameranno da altri moduli dovranno chiamarla pairs::test() + // ATTENZIONE: i moduli non creano namespace automaticamente export void test() { mypair p1(4, 5); diff --git a/2021-22/LezioniCPP/smart_ptr.ixx b/2021-22/LezioniCPP/smart_ptr.ixx index ba04a1a..01f551b 100644 --- a/2021-22/LezioniCPP/smart_ptr.ixx +++ b/2021-22/LezioniCPP/smart_ptr.ixx @@ -1,19 +1,19 @@ export module smart_ptr; import ; +import ; -using std::string; +using namespace std; export -template +template class smart_ptr { private: T* pt; bool is_array; -// typedef unsigned int counter_t; - using counter_t = unsigned int; + using counter_t = unsigned int; // equivalente a scrivere: typedef unsigned int counter_t; counter_t* cnt; @@ -28,8 +28,7 @@ private: } public: - // typedef T value_type; - using value_type = T; + using value_type = T; // equivalente a scrivere: typedef T value_type smart_ptr(T* pt_, bool is_array_ = false) : pt(pt_), cnt(new unsigned int(1)), is_array(is_array_) {} @@ -85,48 +84,40 @@ public: } // TODO STUDENTI: implementare operatori +, ++, +=, -, --, -= - -}; - -class foo -{ -public: - smart_ptr pt; - - foo(smart_ptr pt_) : pt(pt_) {} + // attenzione: postare il pointer interno quando si incrementa/decrementa/ecc non è sufficiente, altrimenti la delete verrà invocata su un indirizzo sbagliato + // consiglio1: l'aritmetica dei puntatori ha senso supportarla solamente quando is_array è true + // consiglio2: tenere una copia del pointer ORIGINALE passato al costruttore che poi useremo per la delete; e spostare in avanti/indietro l'altro }; -template -void swap(Pointer& p1, Pointer& p2) +// questa funzione templatizzata swappa cose qualunque, a patto che siano de-referenziabili e che il tipo del valore puntato sia assegnabile +template +void swap_any_pointer(Pointer& p1, Pointer& p2) { - typename Pointer::value_type& tmp = *p1; + typename Pointer::value_type& tmp = *p1; // richiede anche l'esistenza di un member type di nome value_type che indichi il tipo del valore puntato *p1 = *p2; *p2 = tmp; } - -void f(smart_ptr p) -{ - foo oo(p); -} - -void test_smart_ptr() +export void test_smart_ptr() { int* a = new int[100]; - smart_ptr a2(new int[100], true); - smart_ptr x(600); + smart_ptr a2(a, true); // ora l'array puntato da a è di proprietà dello smart_ptr, quindi non servirà fare 'delete[] a' + smart_ptr b(600); // utilizza il costruttore che costruisce più di un istanza dello heap + b[2] = a[3]; // supportano l'operatore di subscript - string* s = new string("ciao"); - smart_ptr s2 = new string("ciao"); + string* s1 = new string("ciao"); + smart_ptr s2 = s1; + s2 = s1; // questo assegnamento converte automaticamente il right value di tipo string* in un temporary object di tipo smart_ptr + s2 = s2; // supporta anche l'assegnamento di sé stesso - s2 = s2; + smart_ptr> v1(new vector(10, 1.)); + smart_ptr> v2(new vector(20, 2.)); + swap_any_pointer(v1, v2); - f(a2); - // ... + int u = 3, w = 4; + //swap_any_pointer(&u, &w); // NON COMPILA perché int* non ha un member type di nome value_type come richiesto dalla funzione templatizzata - delete[] a; - delete s; } diff --git a/2021-22/LezioniCPP/sums.ixx b/2021-22/LezioniCPP/sums.ixx index 33961ed..840abc7 100644 --- a/2021-22/LezioniCPP/sums.ixx +++ b/2021-22/LezioniCPP/sums.ixx @@ -11,7 +11,7 @@ using namespace std; export namespace sums { - template + template iterator_traits::value_type sum(InputIterator first, InputIterator last, BinaryFunction f) { typename iterator_traits::value_type z = *first++; @@ -22,7 +22,7 @@ export namespace sums return z; } - template + template auto sum(InputIterator first, InputIterator last) -> decltype(*first) { decltype(*first) z = *first++; From 685d2aef41fae51d3e8602351786b6d7eeac1cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 10 May 2022 17:16:37 +0200 Subject: [PATCH 097/202] Update iterators.ixx --- 2021-22/LezioniCPP/iterators.ixx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/2021-22/LezioniCPP/iterators.ixx b/2021-22/LezioniCPP/iterators.ixx index 9e5e002..0c7e5a2 100644 --- a/2021-22/LezioniCPP/iterators.ixx +++ b/2021-22/LezioniCPP/iterators.ixx @@ -27,7 +27,7 @@ void increment_all(Container& v) } template -void print_all(Container& v) +void print_all(const Container& v) { cout << typeid(Container).name() << "["; for (typename Container::const_iterator it = v.begin(); it != v.end(); ++it) // usiamo il const_iterator perché non dobbiamo modificare gli elementi del container @@ -72,4 +72,5 @@ export void test_iterators() increment_all(l2); // questa compila perché mypair ha l'operatore di pre-incremento, il quale invoca a sua volta il pre-incremento sui due campi della coppia // pertanto la chiamata compila perché sia unsigned int che double* supportano il pre-incremento print_all(l2); -} \ No newline at end of file +} + From f8217671f90381c1f34843b79270f5859ffab4b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 11 May 2022 14:14:33 +0200 Subject: [PATCH 098/202] Update iterators.ixx --- 2021-22/LezioniCPP/iterators.ixx | 40 ++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/2021-22/LezioniCPP/iterators.ixx b/2021-22/LezioniCPP/iterators.ixx index 0c7e5a2..80d003a 100644 --- a/2021-22/LezioniCPP/iterators.ixx +++ b/2021-22/LezioniCPP/iterators.ixx @@ -46,11 +46,41 @@ ostream& operator<<(ostream& os, const mypair& p) return os << "(" << p.fst() << ", " << p.snd() << ")"; } -// ridefinisce l'operatore GLOBALE di streaming per double* -ostream& operator<<(ostream& os, const double*& p) + +// ridefinisce l'operatore GLOBALE di streaming per il tipo double* stampando sia l'indirizzo che il double puntato +// ATTENZIONE: la sintassi col 'const&' DOPO il tipo è una sintassi alternativa: scrivere 'const MioTipo&' e 'MioTipo const&' è esattamente EQUIVALENTE ed indica un const-reference di tipo MioTipo. +// C++ supporta dal vanilla sia la sintassi PREFISSA con il const PRIMA del tipo, sia quella SUFFISSA con il const DOPO il tipo. +// Nel nostro caso però MioTipo è un pointer a double e qui emerge un problema sottile: il const PREFISSO si riferisce al tipo PUNTATO, non al puntatore; ma come posso specificare che voglio prendere il PUNTATOTORE per const-reference? +// In altre parole, voglio un const-reference ad un puntatore che punta ad un const double: per farlo devo mescolare la sintassi prefissa con quella suffissa e scrivere 'const double* const&'. +// Il motivo per cui voglio che sia il puntatore sia il puntato siano const è perché sto ridefinendo l'operator<< GLOBALE per i double*, quindi devo riprodurre la firma ESATTA originale per assicurarmi di non fare un overload per sbaglio. +// La firma esatta originale è: +// ostream& operator<<(ostream& os, double* p) +// +// Quanti e quali altri modi di scrivere il tipo del parametro p esistono e sono equivalenti ad un semplice pointer a double per valore? +// double* const& prendo per const-reference (suffisso) un pointer ad un double non-const +// const double* const& prendo per const-reference (suffisso) un pointer ad un double const (prefisso) +// double* const prendo per copia un pointer const (suffisso) ad un double non-const +// const double* const prendo per copia un pointer const (suffisso) ad un double const (prefisso) +// double* prendo per copia un pointer non-const ad un double non-const +// const double* prendo per copia un pointer non-const ad un double const (prefisso) +// +// E ora facciamoci la domanda inversa: quali tipi sono invece diversi e farebbero un overload anziché uno shadowing? +// const double*& prendo per reference un pointer non-const ad un double const (prefisso) +// double*& prendo per reference un pointer non-const ad un double non-const +// +// Per essere equivalenti alla firma originale (double*) non importa se il double è const oppure no, quello che importa è che il pointer sia const se lo prendiamo per reference, perché se non è una copia non deve essere modificabile. +// Il significato di tutto questo è: prendere un argomento per valore (cioè per copia) è compatibile, dal punto di vista dei tipi del compilatore, con il prendere per const-reference. +// Non perché sia la stessa cosa (perché sappiamo che sono cose diverse) ma perché dal punto di vista della soundness è uguale: un argomento passato per copia garantisce la non modificabilità dell'originale, esattamente come lo garantisce il prenderlo per const-reference. +// +// Nel nostro caso usiamo il tipo più sicuro tra quelli equivalenti a double*, cioè const double* const& +// +// ESPERIMENTO: provate a cambiare il tipo del parametro p in const double*& ad esempio, e lanciate il programma di nuovo: vedrete che il printing dei double* sarà quello originale e non quello definito da noi, perché quella qui sotto diventa un overload, non uno shadowing di quella globale. +ostream& operator<<(ostream& os, const double* const& p) { - os << "&" << static_cast(p); // casta a constvoid* per stampare l'indirizzo numerico (senza cast sarebbe una RICORSIONE!!!!) - if (p != nullptr) os << "[" << *p << "]"; // se non è nullo printa anche il dereference + os << "&" << static_cast(p); // castiamo a const void* per stampare l'indirizzo numerico invocando l'operator<< sui void* definito globalmente dalla standard library + // lo static_cast non ha il potere di rimuovere un const, quindi non si può castare a void* semplicemente + // ATTENZIONE: senza cast diventerebbe una chiamata RICORSIVA all'operator<< che noi stessi stiamo definendo + if (p != nullptr) os << "[" << *p << "]"; // se il pointer non è nullo printa anche il dereference return os; } @@ -65,7 +95,7 @@ export void test_iterators() print_all(l1); // questa chiamata genera una istanza di print_all() per list //increment_all(l1); // NON COMPILA: questa chiamata genera una istanza di increment_all() per list, ma il pre-incremento non esiste per string - double arr[] = {11.23, 35.53}; + double arr[] = { 11.23, 35.53 }; list> l2{ {1u, arr}, {2u, arr}, {3u, arr} }; print_all(l2); // questa genera una istanza della print_all() per list> // essa compila perché abbiamo definito l'operator<< per mypair, altrimenti non compilerebbe From 8ef3e73a5f6dd46f8c83b8eec962f805c9ea61c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 11 May 2022 14:29:28 +0200 Subject: [PATCH 099/202] Update iterators.ixx --- 2021-22/LezioniCPP/iterators.ixx | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/2021-22/LezioniCPP/iterators.ixx b/2021-22/LezioniCPP/iterators.ixx index 80d003a..cd00c00 100644 --- a/2021-22/LezioniCPP/iterators.ixx +++ b/2021-22/LezioniCPP/iterators.ixx @@ -48,15 +48,15 @@ ostream& operator<<(ostream& os, const mypair& p) // ridefinisce l'operatore GLOBALE di streaming per il tipo double* stampando sia l'indirizzo che il double puntato -// ATTENZIONE: la sintassi col 'const&' DOPO il tipo è una sintassi alternativa: scrivere 'const MioTipo&' e 'MioTipo const&' è esattamente EQUIVALENTE ed indica un const-reference di tipo MioTipo. +// ATTENZIONE: la sintassi SUFFISSA con il 'const&' DOPO il tipo è una sintassi alternativa: scrivere 'const MioTipo&' e 'MioTipo const&' è esattamente EQUIVALENTE ed indica un const-reference di tipo MioTipo. // C++ supporta dal vanilla sia la sintassi PREFISSA con il const PRIMA del tipo, sia quella SUFFISSA con il const DOPO il tipo. -// Nel nostro caso però MioTipo è un pointer a double e qui emerge un problema sottile: il const PREFISSO si riferisce al tipo PUNTATO, non al puntatore; ma come posso specificare che voglio prendere il PUNTATOTORE per const-reference? -// In altre parole, voglio un const-reference ad un puntatore che punta ad un const double: per farlo devo mescolare la sintassi prefissa con quella suffissa e scrivere 'const double* const&'. -// Il motivo per cui voglio che sia il puntatore sia il puntato siano const è perché sto ridefinendo l'operator<< GLOBALE per i double*, quindi devo riprodurre la firma ESATTA originale per assicurarmi di non fare un overload per sbaglio. -// La firma esatta originale è: +// Nel nostro caso però MioTipo è un POINTER e qui emerge un problema sottile: quando il const è PREFISSO esso si riferisce al tipo PUNTATO (cioè double), ma come posso specificare che voglio il PUNTATORE per const? +// In altre parole, voglio prendere per const-reference un puntatore che punta ad un const double: per farlo devo mescolare la sintassi prefissa con quella suffissa e scrivere 'const double* const&'. +// Il motivo per cui voglio questo è perché voglio ridefinire l'operator<< GLOBALE per i double*, quindi devo riprodurre la firma ESATTA originale per assicurarmi che sia una shadowing e non un overload. +// La firma esatta dell'operator<< originale definito dalla standard library è: // ostream& operator<<(ostream& os, double* p) // -// Quanti e quali altri modi di scrivere il tipo del parametro p esistono e sono equivalenti ad un semplice pointer a double per valore? +// Quanti e quali altri modi esistono di scrivere una firma compatibile a quella originale? Tutti i seguenti sono COMPATIBILI quindi sono considerati shadowing: // double* const& prendo per const-reference (suffisso) un pointer ad un double non-const // const double* const& prendo per const-reference (suffisso) un pointer ad un double const (prefisso) // double* const prendo per copia un pointer const (suffisso) ad un double non-const @@ -64,13 +64,14 @@ ostream& operator<<(ostream& os, const mypair& p) // double* prendo per copia un pointer non-const ad un double non-const // const double* prendo per copia un pointer non-const ad un double const (prefisso) // -// E ora facciamoci la domanda inversa: quali tipi sono invece diversi e farebbero un overload anziché uno shadowing? +// E quali tipi invece sono NON compatibili e sono considerati overload anziché uno shadowing dal compilatore? // const double*& prendo per reference un pointer non-const ad un double const (prefisso) // double*& prendo per reference un pointer non-const ad un double non-const // -// Per essere equivalenti alla firma originale (double*) non importa se il double è const oppure no, quello che importa è che il pointer sia const se lo prendiamo per reference, perché se non è una copia non deve essere modificabile. +// Per essere compatibili con la firma originale (double*) non importa se il double è const oppure no, quello che importa è che il pointer sia const SE lo prendiamo per reference, poiché se non è una copia esso allora non deve essere modificabile. // Il significato di tutto questo è: prendere un argomento per valore (cioè per copia) è compatibile, dal punto di vista dei tipi del compilatore, con il prendere per const-reference. -// Non perché sia la stessa cosa (perché sappiamo che sono cose diverse) ma perché dal punto di vista della soundness è uguale: un argomento passato per copia garantisce la non modificabilità dell'originale, esattamente come lo garantisce il prenderlo per const-reference. +// Non perché siano equivalenti (perché sappiamo che sono cose diverse) ma perché dal punto di vista della const-correctness imposta dal compilatore sono compatibili. +// Il punto è che un argomento passato per copia garantisce la NON MODIFICABILITA' dell'oggetto passato dal chiamante, esattamente come lo garantisce il passarlo per const-reference. // // Nel nostro caso usiamo il tipo più sicuro tra quelli equivalenti a double*, cioè const double* const& // From cb9f024d1557ea16fa537cd8ac820adb380ff4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 11 May 2022 14:33:33 +0200 Subject: [PATCH 100/202] Update smart_ptr.ixx --- 2021-22/LezioniCPP/smart_ptr.ixx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/2021-22/LezioniCPP/smart_ptr.ixx b/2021-22/LezioniCPP/smart_ptr.ixx index 01f551b..4c285d7 100644 --- a/2021-22/LezioniCPP/smart_ptr.ixx +++ b/2021-22/LezioniCPP/smart_ptr.ixx @@ -84,9 +84,9 @@ public: } // TODO STUDENTI: implementare operatori +, ++, +=, -, --, -= - // attenzione: postare il pointer interno quando si incrementa/decrementa/ecc non è sufficiente, altrimenti la delete verrà invocata su un indirizzo sbagliato - // consiglio1: l'aritmetica dei puntatori ha senso supportarla solamente quando is_array è true - // consiglio2: tenere una copia del pointer ORIGINALE passato al costruttore che poi useremo per la delete; e spostare in avanti/indietro l'altro + // ATTENZIONE: spostare il pointer interno quando si incrementa/decrementa/ecc non è sufficiente, altrimenti la delete verrà invocata su un indirizzo sbagliato. + // consiglio1: l'aritmetica dei puntatori ha senso supportarla solamente quando is_array è true. + // consiglio2: tenere una copia del pointer ORIGINALE passato al costruttore che poi useremo per la delete; e spostare in avanti/indietro una copia messa in un altro campo. }; From c9d9c7127302031fa5e4d6c4b89f0ed5a1c8794c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 11 May 2022 16:08:36 +0200 Subject: [PATCH 101/202] Sums fixed --- 2021-22/LezioniCPP/LezioniCPP.vcxproj | 2 +- 2021-22/LezioniCPP/main.cpp | 2 +- 2021-22/LezioniCPP/sums.ixx | 15 +++++++++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj index 191ac6d..3a67155 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -113,7 +113,7 @@ - Level3 + Level4 true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true diff --git a/2021-22/LezioniCPP/main.cpp b/2021-22/LezioniCPP/main.cpp index ce4bd27..d2d42ef 100644 --- a/2021-22/LezioniCPP/main.cpp +++ b/2021-22/LezioniCPP/main.cpp @@ -12,7 +12,7 @@ int main() { try { - test_iterators(); + sums::test(); return 0; } catch (exception& e) diff --git a/2021-22/LezioniCPP/sums.ixx b/2021-22/LezioniCPP/sums.ixx index 840abc7..25c32a0 100644 --- a/2021-22/LezioniCPP/sums.ixx +++ b/2021-22/LezioniCPP/sums.ixx @@ -22,10 +22,11 @@ export namespace sums return z; } + template - auto sum(InputIterator first, InputIterator last) -> decltype(*first) + auto sum(InputIterator first, InputIterator last) -> remove_reference::type { - decltype(*first) z = *first++; + auto z = *first++; for (; first != last; ++first) { z += *first; @@ -38,9 +39,15 @@ export namespace sums { vector v = { 1, 2, 3, 4 }; for_each(begin(v), end(v), [](const auto& x) { cout << x << ", "; }); + cout << endl; + + double arr[5] = { 1.1, 2.2, 3.3, 4.4 }; + - cout << "sum1 = " << sum(v.begin(), v.end()) << endl; - cout << "sum2 = " << sum(v.begin(), v.end(), [](const auto& a, const auto& b) { return a * b; }) << endl; + cout << "sum vector = " << sum(v.begin(), v.end()) << endl; + cout << "sum vector = " << sum(v.begin(), v.end()) << endl; + cout << "sum array = " << sum(arr, arr + size(arr)) << endl; + cout << "sum con lambda = " << sum(v.begin(), v.end(), [](const auto& a, const auto& b) { return a + b; }) << endl; } } \ No newline at end of file From f1ff7dd816426e60f1a8200af3d59e88430869d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 16 May 2022 10:41:01 +0200 Subject: [PATCH 102/202] Update sums.ixx --- 2021-22/LezioniCPP/sums.ixx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/2021-22/LezioniCPP/sums.ixx b/2021-22/LezioniCPP/sums.ixx index 25c32a0..ee122b8 100644 --- a/2021-22/LezioniCPP/sums.ixx +++ b/2021-22/LezioniCPP/sums.ixx @@ -24,13 +24,12 @@ export namespace sums template - auto sum(InputIterator first, InputIterator last) -> remove_reference::type + auto sum(InputIterator first, InputIterator last) -> remove_reference::type { auto z = *first++; for (; first != last; ++first) { z += *first; - } return z; } @@ -44,7 +43,6 @@ export namespace sums double arr[5] = { 1.1, 2.2, 3.3, 4.4 }; - cout << "sum vector = " << sum(v.begin(), v.end()) << endl; cout << "sum vector = " << sum(v.begin(), v.end()) << endl; cout << "sum array = " << sum(arr, arr + size(arr)) << endl; cout << "sum con lambda = " << sum(v.begin(), v.end(), [](const auto& a, const auto& b) { return a + b; }) << endl; From 5c48689eef870d7a66eb07cbe1b08c7fa4f9373c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 16 May 2022 12:37:32 +0200 Subject: [PATCH 103/202] Sums updated --- 2021-22/LezioniCPP/ident.ixx | 2 +- 2021-22/LezioniCPP/sums.ixx | 52 +++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/2021-22/LezioniCPP/ident.ixx b/2021-22/LezioniCPP/ident.ixx index 7c2aa67..5b8d852 100644 --- a/2021-22/LezioniCPP/ident.ixx +++ b/2021-22/LezioniCPP/ident.ixx @@ -21,7 +21,7 @@ export int test_ident() // ogni singola chiamata ad una funzione templatizzata genera una nuova istanza della funzione in cui viene sostituito il template parameter con il tipo // concreto usato in quel caso; la funzione viene poi compilata con i tipo completo. - int x = dual_ident(3); // questa invocazione genera una istanza della funzione dual_ident() con int come template argument + [[maybe_unused]] int x = dual_ident(3); // questa invocazione genera una istanza della funzione dual_ident() con int come template argument // tale istanza compila perché esiste una definizione di ident() che prende e ritorna int //char* s = dual_ident("ciao"); // questa invocazione genera una istanza della funzione dual_ident() con char* come template argument diff --git a/2021-22/LezioniCPP/sums.ixx b/2021-22/LezioniCPP/sums.ixx index ee122b8..eb8e494 100644 --- a/2021-22/LezioniCPP/sums.ixx +++ b/2021-22/LezioniCPP/sums.ixx @@ -6,46 +6,68 @@ import ; import ; import ; import ; +import ; + + +template +struct my_remove_reference; + +template +struct my_remove_reference +{ + using type = T; +}; + +template +struct my_remove_reference +{ + using type = T; +}; + +template +using my_remove_reference_t = typename my_remove_reference::type; + -using namespace std; export namespace sums { template - iterator_traits::value_type sum(InputIterator first, InputIterator last, BinaryFunction f) + std::iterator_traits::value_type sum(InputIterator first, InputIterator last, BinaryFunction f) { - typename iterator_traits::value_type z = *first++; - for (; first != last; ++first) + typename std::iterator_traits::value_type z = *first++; + while (first != last) { - z = f(z, *first); + z = f(z, *first++); } return z; } template - auto sum(InputIterator first, InputIterator last) -> remove_reference::type + auto sum(InputIterator first, InputIterator last) -> my_remove_reference_t { + static_assert(std::is_same_v, std::remove_reference_t>); auto z = *first++; - for (; first != last; ++first) + static_assert(std::is_same_v, decltype(z)>); + while (first != last) { - z += *first; + z += *first++; } return z; } + export void test() { - vector v = { 1, 2, 3, 4 }; - for_each(begin(v), end(v), [](const auto& x) { cout << x << ", "; }); - cout << endl; + std::vector v = { 1, 2, 3, 4 }; + std::for_each(std::begin(v), std::end(v), [](const auto& x) { std::cout << x << ", "; }); + std::cout << std::endl; double arr[5] = { 1.1, 2.2, 3.3, 4.4 }; - - cout << "sum vector = " << sum(v.begin(), v.end()) << endl; - cout << "sum array = " << sum(arr, arr + size(arr)) << endl; - cout << "sum con lambda = " << sum(v.begin(), v.end(), [](const auto& a, const auto& b) { return a + b; }) << endl; + std::cout << "sum vector = " << sum(v.begin(), v.end()) << std::endl; + std::cout << "sum array = " << sum(arr, arr + std::size(arr)) << std::endl; + std::cout << "sum con lambda = " << sum(v.begin(), v.end(), [](const auto& a, const auto& b) { return a + b; }) << std::endl; } } \ No newline at end of file From 7f53d711c6517cf7188bfe6cf86200e166c5de74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 16 May 2022 15:24:28 +0200 Subject: [PATCH 104/202] Update sums.ixx --- 2021-22/LezioniCPP/sums.ixx | 50 ++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/2021-22/LezioniCPP/sums.ixx b/2021-22/LezioniCPP/sums.ixx index eb8e494..27fd68f 100644 --- a/2021-22/LezioniCPP/sums.ixx +++ b/2021-22/LezioniCPP/sums.ixx @@ -7,31 +7,13 @@ import ; import ; import ; import ; - - -template -struct my_remove_reference; - -template -struct my_remove_reference -{ - using type = T; -}; - -template -struct my_remove_reference -{ - using type = T; -}; - -template -using my_remove_reference_t = typename my_remove_reference::type; - +import ; export namespace sums { - template + // sum con operazione di somma binaria passata come argomento + template std::iterator_traits::value_type sum(InputIterator first, InputIterator last, BinaryFunction f) { typename std::iterator_traits::value_type z = *first++; @@ -42,13 +24,12 @@ export namespace sums return z; } - + // sum che richiede l'operatore += implementata tramite template classici template - auto sum(InputIterator first, InputIterator last) -> my_remove_reference_t + auto sum(InputIterator first, InputIterator last) -> std::remove_reference_t { - static_assert(std::is_same_v, std::remove_reference_t>); auto z = *first++; - static_assert(std::is_same_v, decltype(z)>); + static_assert(std::is_same_v, decltype(z)>); while (first != last) { z += *first++; @@ -56,6 +37,19 @@ export namespace sums return z; } + // sum con concept + // + + template + concept MyAddable = requires (T x, T y) { x += y; }; + + template + requires MyAddable::value_type> + auto sum_with_concepts(I first, I last) + { + return sum(first, last); + } + export void test() { @@ -65,9 +59,13 @@ export namespace sums double arr[5] = { 1.1, 2.2, 3.3, 4.4 }; + std::cout << "sum con lambda = " << sum(v.begin(), v.end(), [](const auto& a, const auto& b) { return a + b; }) << std::endl; + std::cout << "sum vector = " << sum(v.begin(), v.end()) << std::endl; std::cout << "sum array = " << sum(arr, arr + std::size(arr)) << std::endl; - std::cout << "sum con lambda = " << sum(v.begin(), v.end(), [](const auto& a, const auto& b) { return a + b; }) << std::endl; + + std::cout << "sum_with_concepts vector = " << sum_with_concepts(v.begin(), v.end()) << std::endl; + std::cout << "sum_with_concepts array = " << sum_with_concepts(arr, arr + std::size(arr)) << std::endl; } } \ No newline at end of file From 0a8c06d08e7c18542c1651a3cc3236d674f001b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 17 May 2022 17:13:17 +0200 Subject: [PATCH 105/202] Thread pool --- 2021-22/Lezioni21-22/Lezioni21-22.iml | 9 +++ 2021-22/Lezioni21-22/src/tinyjdk/Mapping.java | 73 +++++++++++++++++++ .../Lezioni21-22/src/tinyjdk/ThreadPool.java | 70 ++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/Mapping.java create mode 100644 2021-22/Lezioni21-22/src/tinyjdk/ThreadPool.java diff --git a/2021-22/Lezioni21-22/Lezioni21-22.iml b/2021-22/Lezioni21-22/Lezioni21-22.iml index c90834f..0181b11 100644 --- a/2021-22/Lezioni21-22/Lezioni21-22.iml +++ b/2021-22/Lezioni21-22/Lezioni21-22.iml @@ -7,5 +7,14 @@ + + + + + + + + + \ No newline at end of file diff --git a/2021-22/Lezioni21-22/src/tinyjdk/Mapping.java b/2021-22/Lezioni21-22/src/tinyjdk/Mapping.java new file mode 100644 index 0000000..7dd5207 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/Mapping.java @@ -0,0 +1,73 @@ +package tinyjdk; + +import java.util.function.Function; +import java.util.List; +import java.util.Iterator; +import java.util.function.Supplier; + +public class Mapping { + + + public static Iterator mapIterator(Iterator it, Function f) { + return new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public B next() { + return f.apply(it.next()); + } + }; + } + + public static Iterator> mapIterator__mt(Iterator it, Function f) { + return new Iterator<>() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public Supplier next() { + A a = it.next(); + final B[] b = (B[]) new Object[1]; + Thread t = new Thread(() -> { + b[0] = f.apply(a); + }); + t.start(); + return () -> { + try { + t.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return b[0]; + }; + } + }; + } + + + public static void main(String[] args) { + List l = List.of(1, 2, 3, 4, 5); + Iterator it1 = mapIterator(l.iterator(), (n) -> String.format("%d", n)); + + Iterator> it2 = mapIterator__mt(l.iterator(), (n) -> { + try { + Thread.sleep(1000L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return String.format("%d", n); + }); + + while(it2.hasNext()) { + Supplier sup = it2.next(); + System.out.println(sup.get()); + } + } + + +} diff --git a/2021-22/Lezioni21-22/src/tinyjdk/ThreadPool.java b/2021-22/Lezioni21-22/src/tinyjdk/ThreadPool.java new file mode 100644 index 0000000..092b257 --- /dev/null +++ b/2021-22/Lezioni21-22/src/tinyjdk/ThreadPool.java @@ -0,0 +1,70 @@ +package tinyjdk; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Queue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +public class ThreadPool { + + private BlockingQueue q = new LinkedBlockingQueue<>(); + + public class PooledThread extends Thread { + + @Nullable + private Runnable myRunnable; + + @Override + public void run() { + while (true) { + try { + wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if (myRunnable != null) { + myRunnable.run(); + q.add(this); + } + } + } + + private void setRunnable(@NotNull Runnable r) { + myRunnable = r; + notify(); + } + } + + public ThreadPool(int capacity) { + for (int i = 0; i < capacity; ++i) { + PooledThread t = new PooledThread(); + t.start(); + q.add(t); + } + } + + public PooledThread acquire(Runnable r) throws InterruptedException { + PooledThread t = q.take(); + // TODO: spawnare thread nuovi se la coda è vuota + t.setRunnable(r); + return t; + // TODO: scorrere la pool e buttare via thread in eccesso se non usati + } + + + + + public static void main(String[] args) { + try { + ThreadPool pool = new ThreadPool(20); + PooledThread t1 = pool.acquire(() -> { for (int i = 0; i < 10; ++i) System.out.println(i); }); + + PooledThread t2 = pool.acquire(() -> { for (int i = 0; i < 100; ++i) System.out.println(i); }); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} From d12ecb3c44346e3526b722bc52e3f4311c816b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 13 Jun 2022 11:29:57 +0200 Subject: [PATCH 106/202] Soluzioni appello scritto del 3/6/22 --- .gitignore | 2 + 2021-22/LezioniCPP/main.cpp | 1 + .../cpp/Appello_3_6_22/Appello_3_6_22.cpp | 83 +++++++ .../cpp/Appello_3_6_22/Appello_3_6_22.sln | 31 +++ .../cpp/Appello_3_6_22/Appello_3_6_22.vcxproj | 135 +++++++++++ .../Appello_3_6_22.vcxproj.filters | 22 ++ .../Appello_3_6_22.vcxproj.user | 4 + .../Scritto PO2 3 6 22/java/.idea/.gitignore | 3 + .../Scritto PO2 3 6 22/java/.idea/.name | 1 + .../java/.idea/codeStyles/codeStyleConfig.xml | 5 + .../inspectionProfiles/Project_Default.xml | 30 +++ .../Scritto PO2 3 6 22/java/.idea/misc.xml | 6 + .../Scritto PO2 3 6 22/java/.idea/modules.xml | 8 + .../java/.idea/uiDesigner.xml | 124 ++++++++++ .../Scritto PO2 3 6 22/java/.idea/vcs.xml | 6 + .../java/Appello_3_6_22.iml | 11 + .../java/src/Appello_3_6_22.java | 221 ++++++++++++++++++ 17 files changed, 693 insertions(+) create mode 100644 soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.cpp create mode 100644 soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.sln create mode 100644 soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj create mode 100644 soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.filters create mode 100644 soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.user create mode 100644 soluzione appelli/Scritto PO2 3 6 22/java/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 3 6 22/java/.idea/.name create mode 100644 soluzione appelli/Scritto PO2 3 6 22/java/.idea/codeStyles/codeStyleConfig.xml create mode 100644 soluzione appelli/Scritto PO2 3 6 22/java/.idea/inspectionProfiles/Project_Default.xml create mode 100644 soluzione appelli/Scritto PO2 3 6 22/java/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 3 6 22/java/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 3 6 22/java/.idea/uiDesigner.xml create mode 100644 soluzione appelli/Scritto PO2 3 6 22/java/.idea/vcs.xml create mode 100644 soluzione appelli/Scritto PO2 3 6 22/java/Appello_3_6_22.iml create mode 100644 soluzione appelli/Scritto PO2 3 6 22/java/src/Appello_3_6_22.java diff --git a/.gitignore b/.gitignore index 03af692..071a54d 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ hs_err_pid* #.idea/* #**/.idea/* +.vs/* +x64/* diff --git a/2021-22/LezioniCPP/main.cpp b/2021-22/LezioniCPP/main.cpp index d2d42ef..094b0f8 100644 --- a/2021-22/LezioniCPP/main.cpp +++ b/2021-22/LezioniCPP/main.cpp @@ -5,6 +5,7 @@ import pairs; import sums; import zoo; import iterators; +import ; using namespace std; diff --git a/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.cpp b/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.cpp new file mode 100644 index 0000000..b8a2c83 --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.cpp @@ -0,0 +1,83 @@ + +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 3/6/2022 per ciò che riguarda il quesito 6, ovvero la domanda che coinvolge C++. +// I quesiti 1-5 riguardanti Java sono in un progetto IntelliJ a parte, non qui. +// Il codice C++ qui esposto è standard C++ vanilla (a.k.a. C++03), sebbene il progetto VS sia configurato con il compilatore di default C++14 + +#include +#include + +using namespace std; + +// 6 +template +class matrix +{ +private: + size_t cols; + vector v; + +public: + matrix() : cols(0), v() {} + matrix(size_t rows, size_t cols_) : cols(cols_), v(rows * cols) {} + matrix(size_t rows, size_t cols_, const T& v) : cols(cols_), v(rows* cols, v) {} + matrix(const matrix& m) : cols(m.cols), v(m.v) {} + + typedef T value_type; + typedef typename vector::iterator iterator; + typedef typename vector::const_iterator const_iterator; + + matrix& operator=(const matrix& m) + { + v = m.v; + return *this; + } + + T& operator()(size_t i, size_t j) + { + return v[i * cols + j]; + } + + const T& operator()(size_t i, size_t j) const + { + return (*this)(i, j); + } + + iterator begin() + { + return v.begin(); + } + + iterator end() + { + return v.end(); + } + + const_iterator begin() const + { + return begin(); + } + + const_iterator end() const + { + return end(); + } +}; + + +int main() +{ + matrix m1; // non inizializzata + matrix m2(10, 20); // 10*20 inizializzata col default constructor di double + matrix m3(m2); // costruita per copia + m1 = m2; // assegnamento + m3(3, 1) = 11.23; // operatore di accesso come left-value + + for (typename matrix::iterator it = m1.begin(); it != m1.end(); ++it) { + typename matrix::value_type& x = *it; // de-reference non-const + x = m2(0, 2); // operatore di accesso come right-value + } + + matrix ms(5, 4, "ciao"); // 5*4 inizializzata col la stringa passata come terzo argomento + for (typename matrix::const_iterator it = ms.begin(); it != ms.end(); ++it) + cout << *it; // de-reference const +} diff --git a/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.sln b/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.sln new file mode 100644 index 0000000..160d856 --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32526.322 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Appello_3_6_22", "Appello_3_6_22.vcxproj", "{134AAFA0-F15F-4F91-B364-962161EC5D7F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x64.ActiveCfg = Debug|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x64.Build.0 = Debug|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x86.ActiveCfg = Debug|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x86.Build.0 = Debug|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x64.ActiveCfg = Release|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x64.Build.0 = Release|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x86.ActiveCfg = Release|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {49DE568A-D54C-485E-A095-5948A84AED91} + EndGlobalSection +EndGlobal diff --git a/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj b/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj new file mode 100644 index 0000000..04e7e58 --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {134aafa0-f15f-4f91-b364-962161ec5d7f} + Appello3622 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level4 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.filters b/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.filters new file mode 100644 index 0000000..7676feb --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.user b/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/java/.idea/.gitignore b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/soluzione appelli/Scritto PO2 3 6 22/java/.idea/.name b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/.name new file mode 100644 index 0000000..8ba47ca --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/.name @@ -0,0 +1 @@ +Appello_3_6_22.iml \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/java/.idea/codeStyles/codeStyleConfig.xml b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/java/.idea/inspectionProfiles/Project_Default.xml b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..a8d3774 --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,30 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/java/.idea/misc.xml b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/misc.xml new file mode 100644 index 0000000..09e91bf --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/java/.idea/modules.xml b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/modules.xml new file mode 100644 index 0000000..82465c0 --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/java/.idea/uiDesigner.xml b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/java/.idea/vcs.xml b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/java/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/java/Appello_3_6_22.iml b/soluzione appelli/Scritto PO2 3 6 22/java/Appello_3_6_22.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/java/Appello_3_6_22.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/java/src/Appello_3_6_22.java b/soluzione appelli/Scritto PO2 3 6 22/java/src/Appello_3_6_22.java new file mode 100644 index 0000000..caf344a --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/java/src/Appello_3_6_22.java @@ -0,0 +1,221 @@ +import java.util.*; +import java.util.function.BiFunction; +import java.util.function.Function; + +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 3/6/2022 per ciò che riguarda i quesiti 1-5, ovvero le domande che coinvolgono Java. +// Il quesito 6 riguardante C++ è in una Solution per Visual Studio a parte, non qui. + +public class Appello_3_6_22 { + + public static State fold(Iterable i, final State st0, BiFunction f) { + State st = st0; + for (final T e : i) { + st = f.apply(st, e); + } + return st; + } + + // 1.a + public static double sumBy(Iterable i, Function f) { + return fold(i, 0., (r, x) -> r + f.apply(x)); + } + + // 1.b + public static int compareBy(T s1, T s2, Function f) { + return Double.compare(f.apply(s1), f.apply(s2)); + } + + public static class Edge implements Comparable { + private final double len; + + public Edge(double len) { + this.len = len; + } + + public double length() { + return len; + } + + // 2.a + @Override + public int compareTo(Edge s) { + return compareBy(this, s, Edge::length); + } + } + + public interface Surface extends Comparable { + double area(); + + double perimiter(); + + // 2.b + @Override + default int compareTo(Surface s) { + return compareBy(this, s, Surface::area); + } + } + + public interface Polygon extends Surface, Iterable { + // 2.c + @Override + default double perimiter() { + return sumBy(this, Edge::length); + } + } + + public interface Solid extends Comparable { + double outerArea(); + + double volume(); + + // 2.d + default int compareTo(Solid s) { + return compareBy(this, s, Solid::volume); + } + } + + public interface Polyhedron

extends Solid, Iterable

{ + // 2.e + @Override + default double outerArea() { + return sumBy(this, P::area); + } + } + + // 3.a + public static class Sphere implements Solid { + private final double radius; + + public Sphere(double radius) { + this.radius = radius; + } + + @Override + public double outerArea() { + return 4. * Math.PI * radius * radius; + } + + @Override + public double volume() { + return 4. / 3. * Math.PI * radius * radius * radius; + } + } + + // 3.b + public static class Cilinder implements Solid { + private final double radius, height; + + public Cilinder(double radius, double height) { + this.radius = radius; + this.height = height; + } + + @Override + public double outerArea() { + double b = Math.PI * radius * radius, l = 2. * Math.PI * radius * height; // hanno capito solo laterale + return 2. * b + l; + } + + @Override + public double volume() { + return Math.PI * radius * radius * height; + } + } + + // 3.c + public static class Rectangle implements Polygon { + private final double width, height; + + public Rectangle(double width, double height) { + this.width = width; + this.height = height; + } + + @Override + public double area() { + return width * height; + } + + @Override + public Iterator iterator() { + Edge w = new Edge(width), h = new Edge(height); + return List.of(w, h, w, h).iterator(); + } + } + + // 3.d + public static class Square extends Rectangle { + public Square(double side) { + super(side, side); + } + } + + public static class Parallelepiped implements Polyhedron { + protected double width, height, depth; + + public Parallelepiped(double width, double height, double depth) { + this.width = width; + this.height = height; + this.depth = depth; + } + + // 4.a + @Override + public double volume() { + return width * height * depth; + } + + @Override + public Iterator iterator() { + Rectangle r1 = new Rectangle(width, height), r2 = new Rectangle(width, depth), r3 = new Rectangle(height, depth); + return List.of(r1, r2, r3, r1, r2, r3).iterator(); + } + } + + // 4.b + public static class Cube extends Parallelepiped { + public Cube(double side) { + super(side, side, side); + } + } + + + public static void main(String[] args) { + // 4.d + { + int facet_cnt = 1; + // questo foreach non compila perché Cube è sottotipo di Iterable, non di Iterable + // si badi che NON è possibile co-variare il tipo di ritorno del metodo iterator() di Cube in modo che si specializzi in Iterator + // perché è sound co-variare il tipo più esterno di un tipo parametrico, ma non il type argument + // for (Square sq : new Cube(10.)) { + for (Rectangle sq : new Cube(10.)) { // così invece compilerebbe + int side_cnt = 1; + for (Edge e : sq) { + System.out.printf("side #%d/%d = %f\n", side_cnt++, facet_cnt, e.length()); + } + ++facet_cnt; + } + } + + // 5 + { + Cube c1 = new Cube(1.), c2 = new Cube(2.); + Parallelepiped p1 = new Parallelepiped(1., 2., 3.), p2 = new Parallelepiped(2., 3., 4.); + List> polys = new ArrayList<>(List.of(c1, c2, p1, p2)); + + // per testare rapidamente i risultati di queste sort, si usi debugger + Collections.sort(polys); // c1 + Collections.sort(polys, (x, y) -> compareBy(x, y, Polyhedron::outerArea)); // c1 + Collections.sort(polys, (x, y) -> compareBy(x, y, (p) -> p.outerArea())); // c1 +// Collections.sort(polys, (x, y) -> compareBy(x, y, (r) -> r.perimiter())); // non compila + Collections.sort(polys, (x, y) -> compareBy(x, y, new Function<>() { // c1 + @Override + public Double apply(Polyhedron r) { + return r.volume(); + } + })); +// Collections.sort(polys, (x, y) -> Double.compare(sumBy(x, Square::perimiter), // non compila +// sumBy(y, Rectangle::perimiter))); + } + } +} From 4e5073d20a0476cf80efae0a275071de4251de9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 12 Jul 2022 15:59:05 +0200 Subject: [PATCH 107/202] Soluzione appello scritto 1 7 22 --- .gitignore | 4 +- .../cpp/Appello_1_7_22/Appello_1_7_22.cpp | 134 ++++++++++++++++ .../cpp/Appello_1_7_22/Appello_1_7_22.sln | 31 ++++ .../cpp/Appello_1_7_22/Appello_1_7_22.vcxproj | 135 ++++++++++++++++ .../Appello_1_7_22.vcxproj.user | 4 + .../Appello_3_6_22.vcxproj.filters | 22 +++ .../Appello_3_6_22.vcxproj.user | 4 + .../java/Appello_1_7_22/.idea/.gitignore | 3 + .../java/Appello_1_7_22/.idea/misc.xml | 6 + .../java/Appello_1_7_22/.idea/modules.xml | 8 + .../java/Appello_1_7_22/.idea/uiDesigner.xml | 124 +++++++++++++++ .../java/Appello_1_7_22/.idea/vcs.xml | 6 + .../java/Appello_1_7_22/Appello_1_7_22.iml | 20 +++ .../java/Appello_1_7_22/src/Es1.java | 50 ++++++ .../java/Appello_1_7_22/src/Es2.java | 145 ++++++++++++++++++ .../java/.idea/workspace.xml | 104 +++++++++++++ 16 files changed, 798 insertions(+), 2 deletions(-) create mode 100644 soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.cpp create mode 100644 soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.sln create mode 100644 soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj create mode 100644 soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj.user create mode 100644 soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_3_6_22.vcxproj.filters create mode 100644 soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_3_6_22.vcxproj.user create mode 100644 soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/uiDesigner.xml create mode 100644 soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/vcs.xml create mode 100644 soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/Appello_1_7_22.iml create mode 100644 soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es1.java create mode 100644 soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es2.java create mode 100644 soluzione appelli/Scritto PO2 7 1 22/java/.idea/workspace.xml diff --git a/.gitignore b/.gitignore index 071a54d..342b181 100644 --- a/.gitignore +++ b/.gitignore @@ -25,5 +25,5 @@ hs_err_pid* #.idea/* #**/.idea/* -.vs/* -x64/* +**/.vs/* +**/x64/* diff --git a/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.cpp b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.cpp new file mode 100644 index 0000000..e5dcd35 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.cpp @@ -0,0 +1,134 @@ + +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 1/7/2022 per ciò che riguarda il quesito 3, ovvero la domanda che coinvolge C++. +// I quesiti 1-2 riguardanti Java sono in un progetto IntelliJ a parte, non qui. +// Il codice C++ qui esposto è standard C++14 solamente perché usa nullptr e using per i type member; tutto il resto è vanilla. + +#include +#include + +using namespace std; + +template +class smart_ptr +{ +private: + T* pt; + ptrdiff_t offset; + bool is_array; + + using counter_t = unsigned short; + + counter_t* cnt; + + void destroy() + { + if (cnt != nullptr && --(*cnt) == 0) + { + if (pt != nullptr) + { + if (is_array) delete pt; + else delete[] pt; + } + delete cnt; + } + } + + +public: + using value_type = T; + + smart_ptr() : pt(nullptr), offset(0), is_array(false), cnt(nullptr) {} + + explicit smart_ptr(T* pt_, bool is_array_ = false) : pt(pt_), offset(0), is_array(is_array_), cnt(new counter_t(1)) {} + + smart_ptr(const smart_ptr& p) : pt(p.pt), offset(p.offset), is_array(p.is_array), cnt(p.cnt) + { + if (cnt != nullptr) ++(*cnt); + } + + ~smart_ptr() + { + destroy(); + } + + smart_ptr& operator=(const smart_ptr& p) + { + if (cnt != nullptr) ++(*p.cnt); + destroy(); + pt = p.pt; + cnt = p.cnt; + return *this; + } + + // de-reference + T& operator*() + { + return *pt; + } + const T& operator*() const + { + return *pt; + } + + // subscript + T& operator[](size_t i) + { + return pt[i]; + } + const T& operator[](size_t i) const + { + return pt[i]; + } + + // field access + T* operator->() + { + return pt; + } + const T* operator->() const + { + return pt; + } + + // plus + smart_ptr& operator+=(ptrdiff_t off) + { + offset += off; + return *this; + } + smart_ptr operator+(ptrdiff_t off) const + { + return smart_ptr(*this) += off; // ritorna il risultato di operator+=(ptrdiff_t) invocato sulla copia + } + + // minus + smart_ptr& operator-=(ptrdiff_t off) + { + offset -= off; + return *this; + } + smart_ptr operator-(ptrdiff_t off) + { + return smart_ptr(*this) -= off; + } +}; + + +int main() +{ + int* a = new int[100]; + smart_ptr a1(a, true); + smart_ptr a2(a, true); + a1 = a2 + 10; + a1 -= 3; + + string* sp = new string("ciao"); + smart_ptr s1(sp), s2; + s2 = s1; + + smart_ptr> v1(new vector(10, 1.)); + smart_ptr> v2(new vector(20, 2.)); + v1 = v2 + 3; + v1 -= 2; + +} diff --git a/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.sln b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.sln new file mode 100644 index 0000000..a7d5c81 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32526.322 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Appello_1_7_22", "Appello_1_7_22.vcxproj", "{134AAFA0-F15F-4F91-B364-962161EC5D7F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x64.ActiveCfg = Debug|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x64.Build.0 = Debug|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x86.ActiveCfg = Debug|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x86.Build.0 = Debug|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x64.ActiveCfg = Release|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x64.Build.0 = Release|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x86.ActiveCfg = Release|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {49DE568A-D54C-485E-A095-5948A84AED91} + EndGlobalSection +EndGlobal diff --git a/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj new file mode 100644 index 0000000..f391faa --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {134aafa0-f15f-4f91-b364-962161ec5d7f} + Appello3622 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level4 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj.user b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_3_6_22.vcxproj.filters b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_3_6_22.vcxproj.filters new file mode 100644 index 0000000..7676feb --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_3_6_22.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_3_6_22.vcxproj.user b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_3_6_22.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_3_6_22.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/.gitignore b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/misc.xml b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/misc.xml new file mode 100644 index 0000000..3e5ccef --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/modules.xml b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/modules.xml new file mode 100644 index 0000000..d98a9cf --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/uiDesigner.xml b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/vcs.xml b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/vcs.xml new file mode 100644 index 0000000..4fce1d8 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/Appello_1_7_22.iml b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/Appello_1_7_22.iml new file mode 100644 index 0000000..0181b11 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/Appello_1_7_22.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es1.java b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es1.java new file mode 100644 index 0000000..650b7e3 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es1.java @@ -0,0 +1,50 @@ + +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 1/7/2022 per ciò che riguarda i quesiti 1-2, ovvero le domande che coinvolgono Java. +// Il quesito 3 riguardante C++ è in un progetto Visual Studio a parte, non qui. +// Il codice qui esposto è Java 8+. + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.function.Function; + +public class Es1 { + + // 1.a + public interface Predicate extends Function {} + + // 1.b + public interface Either { + T onSuccess(T x); + void onFailure(T x) throws Exception; + } + + // 1.c + public static class SkippableArrayList extends ArrayList { + public Iterator iterator(Predicate p, Either f) { + final Iterator it = super.iterator(); // può anche essere un campo privato della anonymous class + return new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public E next() { + E x = it.next(); + if (p.apply(x)) + return f.onSuccess(x); + else { + try { + f.onFailure(x); + } + catch (Exception e) { + e.printStackTrace(); // si può anche non fare niente dentro il catch, è indifferente + } + return x; + } + } + }; + } + } + +} diff --git a/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es2.java b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es2.java new file mode 100644 index 0000000..112b899 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es2.java @@ -0,0 +1,145 @@ +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + +public class Es2 { + + // 2.a + public static class FiboSequence implements Iterable { + + private final int len; + + public FiboSequence(int len) { + this.len = len; + } + + // questo metodo potrebbe essere static perché non usa nessun membro dell'oggetto + // tuttavia lo definiamo non-static così possiamo overridarlo nella sottoclasse + protected int fib(int n) { + return n < 2 ? 1 : fib(n - 1) + fib(n - 2); + } + + @Override + public Iterator iterator() { + return new Iterator<>() { + private int i = 0; + + @Override + public boolean hasNext() { + return i < len; + } + + @Override + public Integer next() { + return fib(i++); + } + }; + } + } + + // 2.b + // anziché definire una classe a sé stante, facciamo una cosa più elegante (sebbene non richiesta dal testo d'esame) + // definiamo una sottoclasse che overrida solamente il metodo fib() e reimplementa fibonacci con la cache + // l'iteratore senza saperlo invocherà questo override quando chiama fib() + public static class CachedFiboSequence extends FiboSequence { + + @NotNull + private final Map cache; + + // questo costruttore serve alle sottoclasso per passare una certa istanza di cache + protected CachedFiboSequence(@NotNull Map cache, int len) { + super(len); + this.cache = cache; + } + + // questo costruttore costruisce una cache nuova per ogni istanza + public CachedFiboSequence(int len) { + this(new HashMap<>(), len); + } + + // questo override calcola fibonacci usando la cache ricorsivamente + @Override + protected int fib(int n) { + if (n < 2) return 1; // non è necessario fare caching anche dei 2 casi base + else { + Integer x = cache.get(n); + if (x != null) return x; + else { + int r = fib(n - 1) + fib(n - 2); // questa è una ricorsione in dynamic dispatch + cache.put(n, r); + return r; + } + } + } + } + + // 2.c + public static class GlobalCachedFiboSequence extends CachedFiboSequence { + + // inizializziamo questa cache statica; poi passiamo al supercostruttore sempre questo campo + // così tutte le istanze useranno sempre la stessa hashmap senza saperlo + private final static Map globalCache = new HashMap<>(); + + public GlobalCachedFiboSequence(int max) { + super(globalCache, max); + } + } + + + /* + * funzioni di test + */ + + // questo codice non fa parte del tema d'esame, ma per coloro che desiderano testare le 3 differenti + // versioni di FiboSequence, possono curiosare qui + private static void execTimed(String text, Runnable r) { + long t0 = System.nanoTime(); + System.out.printf("[START] %s...\n", text); + r.run(); + System.out.printf("[DONE] %.3f ms\n", ((double)(System.nanoTime() - t0)) / 1000000.); + } + + private static void testFibo(FiboSequence seq, int len, int run) { + execTimed(String.format("%s(%d) #%d", seq.getClass().getName(), len, run), + () -> { + for (int n : seq) + System.out.printf("%d ", n); + System.out.println(); + }); + } + + private static void testFibo(Function cons) { + // parametri modificabili di test + final List LENS = List.of(10, 20, 35, 44); // lunghezze delle sequenze + final int REPEATS = 3; // numero di run con la stessa istanza + + // per ogni lunghezza in LENS fa un ciclo di REPEATS ripetizioni + for (int len : LENS) { + Seq seq = cons.apply(len); + // le ripetizioni usano la stessa istanza per verificare la performance della versione cached non-global + for (int i = 1; i <= REPEATS; ++i) + testFibo(seq, len, i); + } + } + + // si può lanciare questo main e leggere i risultati dei test + public static void main(String[] args) { + // i tempi sono lunghi senza cache: più alto è il numero di fibonacci da calcolare, più tempo ci mette + testFibo(FiboSequence::new); + + // con una cache per ogni istanza i tempi sono enormemente più veloci e non dipende da quanto alto è il numero + // di fibonacci da calcolare; tuttavia la prima run di ogni ripetizione ci mette leggermente più tempo + // perché deve popolare la cache per la prima volta + testFibo(CachedFiboSequence::new); + + // con la cache condivisa raggiungiamo la massima velocità: solamente i numeri di fibonacci mai calcolati finora + // da qualunque istanza vengono davvero calcolati; quindi è veloce anche la prima run di ogni ciclo + testFibo(GlobalCachedFiboSequence::new); + } + + +} diff --git a/soluzione appelli/Scritto PO2 7 1 22/java/.idea/workspace.xml b/soluzione appelli/Scritto PO2 7 1 22/java/.idea/workspace.xml new file mode 100644 index 0000000..73b8a42 --- /dev/null +++ b/soluzione appelli/Scritto PO2 7 1 22/java/.idea/workspace.xml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1651049919437 + + + + + + + + + + + + + file://$PROJECT_DIR$/src/Appello_1_7_22.java + 9 + + + + + \ No newline at end of file From ed9b22bc00c34737c516c7a74b2302b743ce98d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 13 Jul 2022 15:05:43 +0200 Subject: [PATCH 108/202] Some updates --- .../cpp/Appello_1_7_22/Appello_1_7_22.cpp | 134 ------------- .../cpp/Appello_1_7_22/Appello_1_7_22.vcxproj | 3 +- .../cpp/Appello_1_7_22/Es3.cpp | 186 ++++++++++++++++++ .../java/Appello_1_7_22/src/Es2.java | 10 +- 4 files changed, 193 insertions(+), 140 deletions(-) delete mode 100644 soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.cpp create mode 100644 soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Es3.cpp diff --git a/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.cpp b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.cpp deleted file mode 100644 index e5dcd35..0000000 --- a/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.cpp +++ /dev/null @@ -1,134 +0,0 @@ - -// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 1/7/2022 per ciò che riguarda il quesito 3, ovvero la domanda che coinvolge C++. -// I quesiti 1-2 riguardanti Java sono in un progetto IntelliJ a parte, non qui. -// Il codice C++ qui esposto è standard C++14 solamente perché usa nullptr e using per i type member; tutto il resto è vanilla. - -#include -#include - -using namespace std; - -template -class smart_ptr -{ -private: - T* pt; - ptrdiff_t offset; - bool is_array; - - using counter_t = unsigned short; - - counter_t* cnt; - - void destroy() - { - if (cnt != nullptr && --(*cnt) == 0) - { - if (pt != nullptr) - { - if (is_array) delete pt; - else delete[] pt; - } - delete cnt; - } - } - - -public: - using value_type = T; - - smart_ptr() : pt(nullptr), offset(0), is_array(false), cnt(nullptr) {} - - explicit smart_ptr(T* pt_, bool is_array_ = false) : pt(pt_), offset(0), is_array(is_array_), cnt(new counter_t(1)) {} - - smart_ptr(const smart_ptr& p) : pt(p.pt), offset(p.offset), is_array(p.is_array), cnt(p.cnt) - { - if (cnt != nullptr) ++(*cnt); - } - - ~smart_ptr() - { - destroy(); - } - - smart_ptr& operator=(const smart_ptr& p) - { - if (cnt != nullptr) ++(*p.cnt); - destroy(); - pt = p.pt; - cnt = p.cnt; - return *this; - } - - // de-reference - T& operator*() - { - return *pt; - } - const T& operator*() const - { - return *pt; - } - - // subscript - T& operator[](size_t i) - { - return pt[i]; - } - const T& operator[](size_t i) const - { - return pt[i]; - } - - // field access - T* operator->() - { - return pt; - } - const T* operator->() const - { - return pt; - } - - // plus - smart_ptr& operator+=(ptrdiff_t off) - { - offset += off; - return *this; - } - smart_ptr operator+(ptrdiff_t off) const - { - return smart_ptr(*this) += off; // ritorna il risultato di operator+=(ptrdiff_t) invocato sulla copia - } - - // minus - smart_ptr& operator-=(ptrdiff_t off) - { - offset -= off; - return *this; - } - smart_ptr operator-(ptrdiff_t off) - { - return smart_ptr(*this) -= off; - } -}; - - -int main() -{ - int* a = new int[100]; - smart_ptr a1(a, true); - smart_ptr a2(a, true); - a1 = a2 + 10; - a1 -= 3; - - string* sp = new string("ciao"); - smart_ptr s1(sp), s2; - s2 = s1; - - smart_ptr> v1(new vector(10, 1.)); - smart_ptr> v2(new vector(20, 2.)); - v1 = v2 + 3; - v1 -= 2; - -} diff --git a/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj index f391faa..e1e2235 100644 --- a/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj +++ b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Appello_1_7_22.vcxproj @@ -104,6 +104,7 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + stdcpp14 Console @@ -127,7 +128,7 @@ - + diff --git a/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Es3.cpp b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Es3.cpp new file mode 100644 index 0000000..f5f1be8 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/cpp/Appello_1_7_22/Es3.cpp @@ -0,0 +1,186 @@ + +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 1/7/2022 per ciò che riguarda il quesito 3, ovvero la domanda che coinvolge C++. +// I quesiti 1-2 riguardanti Java sono in un progetto IntelliJ a parte, non qui. +// Il codice qui esposto è C++14 per qualche piccolo particolare, ma in gran parte è essenzialmente vanilla. + +#include +#include + +template +class smart_ptr +{ +private: + T* pt; + ptrdiff_t offset; + bool is_array; + + using counter_t = unsigned short; + + counter_t* cnt; + + void dec() + { + if (cnt != nullptr && --(*cnt) == 0) + { + if (pt != nullptr) // se non punta a nulla non c'è niente da liberare + { + if (is_array) delete pt; + else delete [] pt; + } + delete cnt; + } + } + + void inc() + { + if (cnt != nullptr) ++(*cnt); + } + +public: + using value_type = T; + + smart_ptr() : pt(nullptr), offset(0), is_array(false), cnt(nullptr) {} + + explicit smart_ptr(T* pt_, bool is_array_ = false) : pt(pt_), offset(0), is_array(is_array_), cnt(new counter_t(1)) {} + + smart_ptr(const smart_ptr& p) : pt(p.pt), offset(p.offset), is_array(p.is_array), cnt(p.cnt) + { + inc(); + } + + ~smart_ptr() + { + dec(); + } + + smart_ptr& operator=(const smart_ptr& p) + { + dec(); + pt = p.pt; + cnt = p.cnt; + offset = p.offset; + is_array = p.is_array; + inc(); + return *this; + } + + // subscript + // l'operatore di subscript è l'unica implementazione reale; tutti gli altri operatori sono implementati in funzione di questo + // questo approccio è poco error-prone perché concentra solamente qui il calcolo esatto dell'indirizzo usando l'offset + // in altre parole, l'operatore di subscript funge da API interna a basso livello; tutto il resto è costruito sopra di essa + const T& operator[](size_t i) const + { + return pt[offset + i]; + } + T& operator[](size_t i) + { + // capita spesso che l'implementazione const e l'implementazione non-const siano identiche + // in questi casi è necessario duplicare il codice, che è una pratica inelegante ed error-prone + // questo trucco sfrutta un giro di const cast per rimandare questa implementazione a quella const appena sopra, che è l'unica che implementiamo davvero + // ATTENZIONE: tutto questo NON è richiesto dal tema d'esame, lo mostriamo solamente a scopo didattico per insegnare una tecnica avanzata di non-duplicazione del codice + return const_cast(const_cast&>(*this).operator[](i)); + } + + // de-reference + const T& operator*() const + { + // usa l'operatore di subscript + return pt[0]; + } + T& operator*() + { + // stessa tecnica per non duplicare: chiamiamo la versione const di questo operatore definita qui sopra, che è l'unica che implementiamo davvero + return const_cast(const_cast&>(*this).operator*()); + } + + // field access + const T* operator->() const + { + // anche questa implementazione sfrutta altri operatori scritti sopra: in particolare usa de-reference che a sua volta usa il subscript, in questo modo non richiede manutenzione + return &*(*this); + // ^ + // si faccia attenzione a un particolare: solo l'operatore * indicato dalla freccetta invoca l'overload definito da noi + // il de-reference di this dentro le parentesi tonde e l'operatore & più esterno invocano gli operatori nativi di C++, non i nostri overload + } + T* operator->() + { + // altro uso della tecnica avanzata per non duplicare l'implementazione non-const + // cerchiamo di capirla: lo scopo è chiamare l'implementazione const di questo operatore senza duplicare il codice + // 1) trasformiamo *this (che in questo scope è di tipo smart_ptr&) in un const smart_ptr& + // 2) ora che *this è castato a const, invochiamo l'operatore o il metodo che ci interessa: la risoluzione dell'overload risolverà l'implementazione const, non farà una ricorsione! + // 3) siccome stiamo invocando la versione const, il risultato è const: ma il nostro tipo di ritorno deve essere un T* non-const, pertanto bisogna const-castare per TOGLIERE il const + return const_cast(const_cast&>(*this).operator->()); + } + + // plus + smart_ptr& operator+=(ptrdiff_t off) + { + offset += off; + return *this; + } + smart_ptr operator+(ptrdiff_t off) const + { + // questa implementazione usa il copy-constructor e l'operator+= definito qui sopra + return smart_ptr(*this) += off; + } + + // minus + smart_ptr& operator-=(ptrdiff_t off) + { + offset -= off; // analogo al += + return *this; + } + smart_ptr operator-(ptrdiff_t off) + { + return smart_ptr(*this) -= off; + } + + // pre + smart_ptr& operator++() + { + // questa implementazione usa l'operator+= e basta + return *this += 1; + } + smart_ptr& operator--() + { + return *this -= 1; // analogo al ++ ma usa il -= + } + + // post + smart_ptr operator++(int) + { + smart_ptr r(*this); // copia + ++(*this); // pre-incrementa this: usiamo il pre-incremento affinché questa implementazione dipenda totalmente dall'implementazione di operator++ + return r; // ritorna la copia + } + smart_ptr operator--(int) + { + smart_ptr r(*this); + --(*this); // analogo a ++ + return r; + } + +}; + +using std::string; + +int main() +{ + int* a = new int[5]; + smart_ptr a1(a, true), a2; + a2 = a1 + 10; + a1 -= 3; + a1 = ++a1 - *a2-- + a1[3]; + + smart_ptr b(new double[10], true); + for (unsigned int i = 0; i < 10; ++i) + b++; + + string* sp = new string("ciao"); + smart_ptr s1(sp), s2; + s2 = s1; + size_t i = s2->find('a', 0); + + + +} diff --git a/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es2.java b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es2.java index 112b899..f2a7461 100644 --- a/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es2.java +++ b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/src/Es2.java @@ -66,13 +66,12 @@ public CachedFiboSequence(int len) { protected int fib(int n) { if (n < 2) return 1; // non è necessario fare caching anche dei 2 casi base else { - Integer x = cache.get(n); - if (x != null) return x; - else { - int r = fib(n - 1) + fib(n - 2); // questa è una ricorsione in dynamic dispatch + Integer r = cache.get(n); + if (r == null) { + r = fib(n - 1) + fib(n - 2); // questa è una ricorsione in dynamic dispatch cache.put(n, r); - return r; } + return r; } } } @@ -82,6 +81,7 @@ public static class GlobalCachedFiboSequence extends CachedFiboSequence { // inizializziamo questa cache statica; poi passiamo al supercostruttore sempre questo campo // così tutte le istanze useranno sempre la stessa hashmap senza saperlo + @NotNull private final static Map globalCache = new HashMap<>(); public GlobalCachedFiboSequence(int max) { From 0ad0c8e3cbe4905866789d7b26731931913647d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 9 Sep 2022 11:48:24 +0200 Subject: [PATCH 109/202] Create .name --- .../Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/.name | 1 + 1 file changed, 1 insertion(+) create mode 100644 soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/.name diff --git a/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/.name b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/.name new file mode 100644 index 0000000..f5d0f17 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/java/Appello_1_7_22/.idea/.name @@ -0,0 +1 @@ +Es1.java \ No newline at end of file From c5482e668e67bcdb736f7ecc851caaaad5309680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 14 Sep 2022 18:08:53 +0200 Subject: [PATCH 110/202] Soluzioni appello scritto 13/9/22 --- .../Scritto PO2 1 7 22/.idea/.gitignore | 3 + .../Scritto PO2 1 7 22/.idea/misc.xml | 6 + .../Scritto PO2 1 7 22/.idea/modules.xml | 8 + .../Scritto PO2 1 7 22/.idea/vcs.xml | 6 + .../Scritto PO2 1 7 22/Scritto PO2 1 7 22.iml | 11 + .../cpp/Appello_13_9_22/Appello_13_9_22.cpp | 290 ++++++++++++++++++ .../cpp/Appello_13_9_22/Appello_13_9_22.sln | 31 ++ .../Appello_13_9_22/Appello_13_9_22.vcxproj | 136 ++++++++ .../Appello_13_9_22.vcxproj.filters | 22 ++ .../Appello_13_9_22.vcxproj.user | 4 + .../Scritto PO2 13 9 22/java/.idea/.gitignore | 3 + .../Scritto PO2 13 9 22/java/.idea/.name | 1 + .../java/.idea/codeStyles/codeStyleConfig.xml | 5 + .../Scritto PO2 13 9 22/java/.idea/misc.xml | 6 + .../java/.idea/modules.xml | 8 + .../Scritto PO2 13 9 22/java/.idea/vcs.xml | 6 + .../java/Appello_13_9_22.iml | 20 ++ .../Scritto PO2 13 9 22/java/src/Es1.java | 158 ++++++++++ 18 files changed, 724 insertions(+) create mode 100644 soluzione appelli/Scritto PO2 1 7 22/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 1 7 22/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 1 7 22/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 1 7 22/.idea/vcs.xml create mode 100644 soluzione appelli/Scritto PO2 1 7 22/Scritto PO2 1 7 22.iml create mode 100644 soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.cpp create mode 100644 soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.sln create mode 100644 soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj create mode 100644 soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj.filters create mode 100644 soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj.user create mode 100644 soluzione appelli/Scritto PO2 13 9 22/java/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 13 9 22/java/.idea/.name create mode 100644 soluzione appelli/Scritto PO2 13 9 22/java/.idea/codeStyles/codeStyleConfig.xml create mode 100644 soluzione appelli/Scritto PO2 13 9 22/java/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 13 9 22/java/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 13 9 22/java/.idea/vcs.xml create mode 100644 soluzione appelli/Scritto PO2 13 9 22/java/Appello_13_9_22.iml create mode 100644 soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java diff --git a/soluzione appelli/Scritto PO2 1 7 22/.idea/.gitignore b/soluzione appelli/Scritto PO2 1 7 22/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/soluzione appelli/Scritto PO2 1 7 22/.idea/misc.xml b/soluzione appelli/Scritto PO2 1 7 22/.idea/misc.xml new file mode 100644 index 0000000..09e91bf --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/.idea/modules.xml b/soluzione appelli/Scritto PO2 1 7 22/.idea/modules.xml new file mode 100644 index 0000000..b8e82d1 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/.idea/vcs.xml b/soluzione appelli/Scritto PO2 1 7 22/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 1 7 22/Scritto PO2 1 7 22.iml b/soluzione appelli/Scritto PO2 1 7 22/Scritto PO2 1 7 22.iml new file mode 100644 index 0000000..2c505e4 --- /dev/null +++ b/soluzione appelli/Scritto PO2 1 7 22/Scritto PO2 1 7 22.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.cpp b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.cpp new file mode 100644 index 0000000..4909ab3 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.cpp @@ -0,0 +1,290 @@ + +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 1/7/2022 per ciò che riguarda il quesito 2, ovvero l'esercizio di C++. +// Il quesito 1 riguardante Java è in un progetto IntelliJ a parte, non qui. +// Il codice qui esposto è C++14. +// ATTENZIONE: il codice qui fornito è ricco di dettagli e complessità, allo scopo di fornire materiale di studio. + +#include +#include +#include + +template +class tree_node +{ +public: + T data; + tree_node* left, * right; + +private: + tree_node* parent; + + static bool are_equal(const tree_node* a, const tree_node* b) + { + return a == b || (a != nullptr && b != nullptr && *a == *b); + } + + static tree_node* get_next_node(const tree_node* n) { + if (n->left != nullptr) + return n->left; + else if (n->right != nullptr) + return n->right; + else { + while (n->parent != nullptr) { + const tree_node* last = n; + n = n->parent; + if (n->right != nullptr && n->right != last) + return n->right; + } + return nullptr; + } + } + +public: + // 2.c + + tree_node() = default; + tree_node(const tree_node& t) = default; + tree_node& operator=(const tree_node& t) = default; + + ~tree_node() + { + if (left != nullptr) + { + delete left; + left = nullptr; + } + if (right != nullptr) + { + delete right; + right = nullptr; + } + } + + tree_node(const T& v, tree_node* l, tree_node* r) : data(v), left(l), right(r), parent(nullptr) + { + if (left != nullptr) left->parent = this; + if (right != nullptr) right->parent = this; + } + + // 2.b + + bool operator==(const tree_node& t) const + { + return data == t.data && are_equal(left, t.left) && are_equal(right, t.right); + } + + // 2.a + + // iteratore non-const + class my_iterator + { + friend class my_const_iterator; // questo serve perché altrimenti my_const_iterator non può accedere al campo current di my_iterator + + private: + tree_node* current; + + public: + using iterator_category = std::forward_iterator_tag; // questo member type indica che si tratta di un ForwardIterator, si veda la doc di STL per i dettagli + using difference_type = std::ptrdiff_t; + using value_type = T; + using pointer = T*; + using reference = T&; + + my_iterator() = default; + my_iterator(const my_iterator& i) = default; + my_iterator& operator=(const my_iterator& i) = default; + + my_iterator(tree_node* t) : current(t) {} + + reference operator*() { return current->data; } + pointer operator->() { return ¤t->data; } + + my_iterator& operator++() + { + current = get_next_node(current); + return *this; + } + + my_iterator operator++(int) + { + my_iterator r(*this); + current = get_next_node(); + return r; + } + + bool operator==(const my_iterator& i) const { return current == i.current; } + bool operator!=(const my_iterator& i) const { return !(*this == i); } + + }; + + // interatore const + // si noti come sono praticamente uguali a parte il fatto che gesticono un nodo const oppure no, con conseguente impatto in tutti i tipi di ritorno dei vari operatori + // questa replicazione di codice sarebbe evitabile solamente tramite un complesso uso dei template, troppo complesso per questo corso + // chi è interessato a sapere come evitare questa duplicazione di codice dovuta a const vs. non-const può approfondire qui: https://stackoverflow.com/questions/765148/how-to-remove-constness-of-const-iterator + class my_const_iterator + { + private: + const tree_node* current; + + public: + using iterator_category = std::forward_iterator_tag; + using difference_type = std::ptrdiff_t; + using value_type = const T; + using pointer = const T*; + using reference = const T&; + + my_const_iterator() = default; + my_const_iterator(const my_const_iterator& i) = default; + my_const_iterator& operator=(const my_const_iterator& i) = default; + + // questo costruttore è molto interessante: permette di costruire un my_const_iterator dato un my_iterator: in altre parole possiamo convertire un iteratore non-const in uno const + // il motivo per cui è necessario è se chiamiamo begin() su un tree_node non-const ma vogliamo un const_iterator perché lo leggiamo soltanto + my_const_iterator(const my_iterator& i) : current(i.current) {} + + my_const_iterator(const tree_node* t) : current(t) {} + + reference operator*() const { return current->data; } + pointer operator->() const { return ¤t->data; } + + my_const_iterator& operator++() + { + current = get_next_node(current); + return *this; + } + + my_const_iterator operator++(int) + { + my_const_iterator r(*this); + current = get_next_node(); + return r; + } + + bool operator==(const my_const_iterator& i) const { return current == i.current; } + bool operator!=(const my_const_iterator& i) const { return !(*this == i); } + + }; + + // definiamo questi member type perché sono quelli che i Container STL solitamente definiscono + using const_iterator = my_const_iterator; // rebinding della nested class my_const_iterator definita sopra + using iterator = my_iterator; // rebinding della nested class my_iterator definita sopra + using value_type = T; + + const_iterator begin() const + { + return const_iterator(this); + } + + const_iterator end() const + { + return const_iterator(nullptr); + } + + iterator begin() + { + return iterator(this); + } + + iterator end() + { + return iterator(nullptr); + } + +}; + +// 2.c +// pseudo-costruttori +// invece di fare metodi statici facciamo funzioni templatizzate globali, così il template argument è inferito e diventano più comode da usare + +template +tree_node* lr(const T& v, tree_node* l, tree_node* r) +{ + return new tree_node(v, l, r); +} + +template +tree_node* l(const T& v, tree_node* n) +{ + return new tree_node(v, n, nullptr); +} + +template +tree_node* r(const T& v, tree_node* n) +{ + return new tree_node(v, nullptr, n); +} + +template +tree_node* v(const T& v) +{ + return new tree_node(v, nullptr, nullptr); +} + +// 2.e + +template +std::ostream& operator<<(std::ostream& os, const tree_node& t) +{ + os << t.data; + if (t.left != nullptr) os << "(" << *t.left << ")"; + if (t.right != nullptr) os << "[" << *t.right << "]"; + return os; +} + +using namespace std; + +// 2.d + +int main() +{ + auto t1 = + shared_ptr>( // usiamo gli shared_ptr per non doverci ricordare di fare delete + lr(1, + lr(2, // con le funzioni globali è comodissimo costruire un albero + v(3), + v(4)), + r(5, + lr(6, + v(7), + v(8))))); + + auto t2 = + shared_ptr>( + lr(1, + r(5, + lr(6, // il sottoalbero destro è uguale al sinistro + v(7), + v(8))), + lr(2, + v(3), + v(4)))); + + // test dell'operatore di stream (<<) + cout << "pretty printer: " << endl + << "t1: " << *t1 << endl // dereferenziamo per stampare perché il nostro operator<< non vuole un pointer ma un reference + << "t2: " << *t2 << endl; + + // test dell'operatore di uguaglianza (==) + cout << "equality: " << (*t1 == *t2) << ", " << (*t1->left == *t2->right) << endl; // dereferenziamo gli operandi sinistro e destro del nostro operator== perchè non accetta pointer ma reference + + // test dell'iteratore non-const + cout << "iterator: "; + for (tree_node::iterator it = t1->begin(); it != t1->end(); ++it) + { + int& n = *it; // dereferenziando l'iteratore abbiamo accesso non-const al dato dentro il nodo + n *= 2; // il campo data in ogni nodo può quindi essere modificato + cout << n << " "; + } + cout << endl << "t1 modificato: " << *t1 << endl; // ristampiamo t1 dopo le modifiche + + // test dell'iteratore const + cout << "const iterator: "; + for (tree_node::const_iterator it = t1->begin(); it != t1->end(); ++it) // t1->begin() ritorna un iterator, che viene convertito in un const_iterator dal costruttore alla linea 142 + { + const int& n = *it; // dereferenziando l'iteratore abbiamo accesso const al dato dentro il nodo, quindi non possiamo modificarlo ma solo leggerlo + cout << n << " "; + } + cout << endl; + + + +} diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.sln b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.sln new file mode 100644 index 0000000..a6f521c --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32630.192 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Appello_13_9_22", "Appello_13_9_22.vcxproj", "{E8ACD1DF-24EF-429E-84EF-4EB4C65986A8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E8ACD1DF-24EF-429E-84EF-4EB4C65986A8}.Debug|x64.ActiveCfg = Debug|x64 + {E8ACD1DF-24EF-429E-84EF-4EB4C65986A8}.Debug|x64.Build.0 = Debug|x64 + {E8ACD1DF-24EF-429E-84EF-4EB4C65986A8}.Debug|x86.ActiveCfg = Debug|Win32 + {E8ACD1DF-24EF-429E-84EF-4EB4C65986A8}.Debug|x86.Build.0 = Debug|Win32 + {E8ACD1DF-24EF-429E-84EF-4EB4C65986A8}.Release|x64.ActiveCfg = Release|x64 + {E8ACD1DF-24EF-429E-84EF-4EB4C65986A8}.Release|x64.Build.0 = Release|x64 + {E8ACD1DF-24EF-429E-84EF-4EB4C65986A8}.Release|x86.ActiveCfg = Release|Win32 + {E8ACD1DF-24EF-429E-84EF-4EB4C65986A8}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {67D120F8-8926-4693-8B0B-591C7F23EAE0} + EndGlobalSection +EndGlobal diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj new file mode 100644 index 0000000..73f7552 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj @@ -0,0 +1,136 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {e8acd1df-24ef-429e-84ef-4eb4c65986a8} + Appello13922 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Default + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj.filters b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj.filters new file mode 100644 index 0000000..09a4bf5 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj.user b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/java/.idea/.gitignore b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/soluzione appelli/Scritto PO2 13 9 22/java/.idea/.name b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/.name new file mode 100644 index 0000000..fd55a0e --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/.name @@ -0,0 +1 @@ +Appello_13_9_22.iml \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/java/.idea/codeStyles/codeStyleConfig.xml b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/java/.idea/misc.xml b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/misc.xml new file mode 100644 index 0000000..d15472f --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/java/.idea/modules.xml b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/modules.xml new file mode 100644 index 0000000..eefc65a --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/java/.idea/vcs.xml b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/java/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/java/Appello_13_9_22.iml b/soluzione appelli/Scritto PO2 13 9 22/java/Appello_13_9_22.iml new file mode 100644 index 0000000..0181b11 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/java/Appello_13_9_22.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java b/soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java new file mode 100644 index 0000000..18139c1 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java @@ -0,0 +1,158 @@ +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 13/9/2022 per ciò che riguarda il quesito 1, ovvero l'esercizio in Java. +// Il quesito 2 riguardante C++ è in una solution per Visual Studio a parte. + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.util.Iterator; + +public class Es1 { + + // 1.d + + public static void main(String[] args) { + TreeNode t1 = + TreeNode.lr(1, + TreeNode.lr(2, + TreeNode.v(3), + TreeNode.v(4)), + TreeNode.r(5, + TreeNode.lr(6, + TreeNode.v(7), + TreeNode.v(8)))); + + TreeNode t2 = + TreeNode.lr(1, + TreeNode.r(5, + TreeNode.lr(6, + TreeNode.v(7), + TreeNode.v(8))), + TreeNode.lr(2, + TreeNode.v(3), + TreeNode.v(4))); + + + // test pretty printer + System.out.println("pretty printer:"); + System.out.println("t1: " + t1); + System.out.println("t2: " + t2); + + // test equality + System.out.print("equality: "); + System.out.println(t1.equals(t2) + ", " + t1.left.equals(t2.right)); + + // test iterator + System.out.print("iterator: "); + for (Integer n : t1) { + System.out.printf("%d ", n); + } + System.out.println(); + + } + + public static class TreeNode implements Iterable { + + @NotNull + private final T data; + @Nullable + private final TreeNode left, right; + @Nullable + private TreeNode parent = null; + + // 1.c + + public TreeNode(@NotNull T data, @Nullable TreeNode left, @Nullable TreeNode right) { + this.data = data; + this.left = left; + this.right = right; + if (left != null) left.parent = this; + if (right != null) right.parent = this; + } + + // i seguenti pseudo-costruttori aiutano a costruire alberi in modo più succinto e controllato rispetto all'innestamento dei costruttore + + // solo ramo sinistro + public static TreeNode l(@NotNull T data, @NotNull TreeNode left) { + return new TreeNode<>(data, left, null); + } + + // solo ramo destro + public static TreeNode r(@NotNull T data, @NotNull TreeNode right) { + return new TreeNode<>(data, null, right); + } + + // ramo sinistro e destro + public static TreeNode lr(@NotNull T data, @NotNull TreeNode left, @NotNull TreeNode right) { + return new TreeNode<>(data, left, right); + } + + // foglia + public static TreeNode v(@NotNull T data) { + return new TreeNode<>(data, null, null); + } + + // 1.b + + @Override + public boolean equals(@Nullable Object o) { + if (o instanceof TreeNode) { + TreeNode t = (TreeNode) o; + return areEqual(data, t.data) && areEqual(left, t.left) && areEqual(right, t.right); + } + return false; + } + + // questo metodo ausiliaro non serve, basterebbe usare questo metodo del JDK: https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object- + private static boolean areEqual(@Nullable Object a, @Nullable Object b) { + return a == b || (a != null && a.equals(b)); + //return Objects.equals(a, b); + } + + // 1.e + + @Override + @NotNull + public String toString() { + return String.format("%s%s%s", data, left != null ? String.format("(%s)", left) : "", right != null ? String.format("[%s]", right) : ""); + } + + // 1.a + + @Override + public Iterator iterator() { + return new Iterator() { + @Nullable + private TreeNode current = TreeNode.this; + + @Override + public boolean hasNext() { + return current != null; + } + + @Nullable + private static TreeNode getNextNode(@NotNull TreeNode n) { + if (n.left != null) + return n.left; + else if (n.right != null) + return n.right; + else { + while (n.parent != null) { + final TreeNode last = n; + n = n.parent; + if (n.right != null && n.right != last) + return n.right; + } + return null; + } + } + + @Override + @NotNull + public T next() { + T r = current.data; + current = getNextNode(current); + return r; + } + }; + } + } +} From 62abe8a03c13f8e078acc3ad79d9382cfc9d24fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 23 Sep 2022 15:37:33 +0200 Subject: [PATCH 111/202] Soluzioni appello 13 9 22 complete --- .../cpp/Appello_13_9_22/Appello_13_9_22.cpp | 122 ++++++++++++++---- .../Scritto PO2 13 9 22/java/src/Es1.java | 44 +++++-- 2 files changed, 133 insertions(+), 33 deletions(-) diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.cpp b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.cpp index 4909ab3..85a0b9f 100644 --- a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.cpp +++ b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_9_22/Appello_13_9_22.cpp @@ -2,11 +2,16 @@ // Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 1/7/2022 per ciò che riguarda il quesito 2, ovvero l'esercizio di C++. // Il quesito 1 riguardante Java è in un progetto IntelliJ a parte, non qui. // Il codice qui esposto è C++14. -// ATTENZIONE: il codice qui fornito è ricco di dettagli e complessità, allo scopo di fornire materiale di studio. +// ATTENZIONE: il codice qui fornito è ricco di dettagli e complessità, allo scopo di fornire materiale di studio. La versione richiesta all'esame è molto più semplice. #include #include #include +#include + +using std::vector; + +#define EASY_ITERATOR // commentare questa macro per compilare la versione ottimizzata degli iteratori template class tree_node @@ -15,30 +20,11 @@ class tree_node T data; tree_node* left, * right; -private: - tree_node* parent; - static bool are_equal(const tree_node* a, const tree_node* b) { return a == b || (a != nullptr && b != nullptr && *a == *b); } - static tree_node* get_next_node(const tree_node* n) { - if (n->left != nullptr) - return n->left; - else if (n->right != nullptr) - return n->right; - else { - while (n->parent != nullptr) { - const tree_node* last = n; - n = n->parent; - if (n->right != nullptr && n->right != last) - return n->right; - } - return nullptr; - } - } - public: // 2.c @@ -60,10 +46,14 @@ class tree_node } } - tree_node(const T& v, tree_node* l, tree_node* r) : data(v), left(l), right(r), parent(nullptr) + tree_node(const T& v, tree_node* l, tree_node* r) : data(v), left(l), right(r) { +# ifdef EASY_ITERATOR + prepopulate(); +# else if (left != nullptr) left->parent = this; if (right != nullptr) right->parent = this; +# endif } // 2.b @@ -75,6 +65,87 @@ class tree_node // 2.a + using value_type = T; + + + // implementazione facile degli iteratori, suggerita pubblicamente dal docente durante l'appello del 13/9/22 + // + +# ifdef EASY_ITERATOR + + using const_iterator = typename vector::const_iterator; + using iterator = typename vector::iterator; + +private: + // per motivi di semplicità ogni nodo ha un campo di tipo vector che viene popolato al volo per essere iterator + // si faccia attenzione ad un particolare: per poter ritornare begin() ed end() dello stesso vector, bisogna conservarlo come membro del nodo + vector children; + + void dfs(vector& v) + { + // perché non popoliamo direttamente il campo children? il motivo è molto sottile: ogni nodo ha un suo campo children, ma noi non vogliamo che ogni nodo popoli il proprio vector; noi vogliamo + // che quando decidiamo di popolare il campo di children di un certo nodo, allora viene popolato il vector di QUEL nodo + // ogni nodo quindi ha un suo campo children che viene popolato con i SUOI sottorami: tutto ciò è uno spreco di spazio, certo, però permette di fare iteratori a partire da QUALUNQUE nodo + // in maniera semplice e immutabile + v.push_back(data); + if (left != nullptr) left->dfs(v); + if (right != nullptr) right->dfs(v); + } + + // questa viene chiamata dal costruttore: ogni nodo prepopola il proprio campo children + void prepopulate() + { + dfs(children); + } + +public: + + const_iterator begin() const + { + return children.begin(); + } + + const_iterator end() const + { + return children.end(); + } + + iterator begin() + { + return children.begin(); + } + + iterator end() + { + return children.end(); + } + + + // implementazione ottimizzata degli iteratori + // + +# else + +private: + tree_node* parent; + + static tree_node* get_next_node(const tree_node* n) { + if (n->left != nullptr) + return n->left; + else if (n->right != nullptr) + return n->right; + else { + while (n->parent != nullptr) { + const tree_node* last = n; + n = n->parent; + if (n->right != nullptr && n->right != last) + return n->right; + } + return nullptr; + } + } + +public: // iteratore non-const class my_iterator { @@ -189,6 +260,8 @@ class tree_node return iterator(nullptr); } +# endif + }; // 2.c @@ -238,8 +311,9 @@ int main() { auto t1 = shared_ptr>( // usiamo gli shared_ptr per non doverci ricordare di fare delete + // con i pseudo-costruttori globali è comodissimo costruire un albero, basta innestare le chiamate lr(1, - lr(2, // con le funzioni globali è comodissimo costruire un albero + lr(2, v(3), v(4)), r(5, @@ -250,8 +324,8 @@ int main() auto t2 = shared_ptr>( lr(1, - r(5, - lr(6, // il sottoalbero destro è uguale al sinistro + r(5, // il sottoalbero destro di t2 è uguale al sinistro di t1 e viceversa + lr(6, v(7), v(8))), lr(2, diff --git a/soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java b/soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java index 18139c1..a587c1e 100644 --- a/soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java +++ b/soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java @@ -1,8 +1,12 @@ -// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 13/9/2022 per ciò che riguarda il quesito 1, ovvero l'esercizio in Java. -// Il quesito 2 riguardante C++ è in una solution per Visual Studio a parte. +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 13/9/2022 per ciò che riguarda il quesito 1, +// ovvero l'esercizio in Java. +// Il quesito 2 riguardante C++ è in una Solution per Visual Studio a parte. import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; public class Es1 { @@ -38,7 +42,7 @@ public static void main(String[] args) { // test equality System.out.print("equality: "); - System.out.println(t1.equals(t2) + ", " + t1.left.equals(t2.right)); + System.out.println(t1.equals(t2) + ", " + (t1.left != null ? t1.left.equals(t2.right) : "")); // test iterator System.out.print("iterator: "); @@ -68,7 +72,8 @@ public TreeNode(@NotNull T data, @Nullable TreeNode left, @Nullable TreeNode< if (right != null) right.parent = this; } - // i seguenti pseudo-costruttori aiutano a costruire alberi in modo più succinto e controllato rispetto all'innestamento dei costruttore + // i seguenti pseudo-costruttori aiutano a costruire alberi in modo più succinto e controllato rispetto + // all'innestamento dei costruttori // solo ramo sinistro public static TreeNode l(@NotNull T data, @NotNull TreeNode left) { @@ -101,10 +106,9 @@ public boolean equals(@Nullable Object o) { return false; } - // questo metodo ausiliaro non serve, basterebbe usare questo metodo del JDK: https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object- - private static boolean areEqual(@Nullable Object a, @Nullable Object b) { + private static boolean areEqual(@Nullable Object a, @Nullable Object b) { return a == b || (a != null && a.equals(b)); - //return Objects.equals(a, b); + //return Objects.equals(a, b); // alternativamente si può usare questo metodo del JDK } // 1.e @@ -112,14 +116,34 @@ private static boolean areEqual(@Nullable Object a, @Nullable Object b) { @Override @NotNull public String toString() { - return String.format("%s%s%s", data, left != null ? String.format("(%s)", left) : "", right != null ? String.format("[%s]", right) : ""); + return String.format("%s%s%s", data, left != null ? String.format("(%s)", left) : "", right != null ? + String.format("[%s]", right) : ""); } // 1.a @Override public Iterator iterator() { - return new Iterator() { + return iterator_easy(); // stub ad una delle due implementazione; cambiare lo stub per testare l'altra + } + + + // questo è l'implementazione facile, suggerita pubblicamente dal docente in classe duranto l'appello del 13/9/22 + private Iterator iterator_easy() { + Collection r = new ArrayList<>(); + dfs(r); + return r.iterator(); + } + + private void dfs(Collection c) { + c.add(data); + if (left != null) left.dfs(c); + if (right != null) right.dfs(c); + } + + // questo è l'implementazione ottimizzata, che attraversa l'albero senza liste d'appoggio + private Iterator iterator_opt() { + return new Iterator<>() { @Nullable private TreeNode current = TreeNode.this; @@ -154,5 +178,7 @@ public T next() { } }; } + } + } From 710e7f3173ae653a75ea13671b693ff13d411613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 24 Jan 2023 14:09:48 +0100 Subject: [PATCH 112/202] Soluzioni appello 13 1 23 --- .../Appello_13_1_2023/.idea/.gitignore | 3 + .../Appello_13_1_2023/.idea/misc.xml | 6 + .../Appello_13_1_2023/.idea/modules.xml | 8 + .../Appello_13_1_2023/.idea/vcs.xml | 6 + .../Appello_13_1_2023/Appello_13_1_2023.iml | 11 ++ .../Appello_13_1_2023/Es1.java | 93 ++++++++++++ .../cpp/Appello_13_/Appello_13_.vcxproj | 137 ++++++++++++++++++ .../Appello_13_/Appello_13_.vcxproj.filters | 22 +++ .../cpp/Appello_13_/Appello_13_.vcxproj.user | 4 + .../cpp/Appello_13_/Appello_13_1_2023.sln | 31 ++++ .../cpp/Appello_13_/curve.cpp | 137 ++++++++++++++++++ .../Scritto PO2 13 9 22/java/src/Es1.java | 3 + .../Scritto PO2 22 1 19/.idea/vcs.xml | 6 + .../Scritto PO2 22 1 19/.idea/workspace.xml | 69 +++------ 14 files changed, 485 insertions(+), 51 deletions(-) create mode 100644 soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/vcs.xml create mode 100644 soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Appello_13_1_2023.iml create mode 100644 soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Es1.java create mode 100644 soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj create mode 100644 soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.filters create mode 100644 soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.user create mode 100644 soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_1_2023.sln create mode 100644 soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/curve.cpp create mode 100644 soluzione appelli/Scritto PO2 22 1 19/.idea/vcs.xml diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/.gitignore b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/misc.xml b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/misc.xml new file mode 100644 index 0000000..d15472f --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/modules.xml b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/modules.xml new file mode 100644 index 0000000..01b0bca --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/vcs.xml b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Appello_13_1_2023.iml b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Appello_13_1_2023.iml new file mode 100644 index 0000000..8b2ade9 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Appello_13_1_2023.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Es1.java b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Es1.java new file mode 100644 index 0000000..1739420 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Es1.java @@ -0,0 +1,93 @@ +import java.util.Random; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingDeque; + +public class Es1 { + + public interface Pool { + R acquire() throws InterruptedException; + void release(R x); + } + + // 2.b + public static class SimplePool implements Pool { + + private final BlockingQueue q = new LinkedBlockingDeque<>(); + + @Override + public T acquire() throws InterruptedException { + return q.take(); + } + + @Override + public void release(T x) { + q.add(x); + } + } + + // 2.c + 2.d + + public interface Resource { + T get(); + void release(); + } + + public static class AutoPool implements Pool> { + + private final BlockingQueue q = new LinkedBlockingDeque<>(); + + public void add(T x) { + q.add(x); + } + + public Resource acquire() throws InterruptedException { + T r = q.take(); + return new Resource<>() { + + @Override + public T get() { + System.out.printf("acquired: %s%n", r); + return r; + } + + @Override + public void release() { + System.out.printf("released: %s%n", r); + add(r); + } + + @SuppressWarnings("deprecation") + @Override + protected void finalize() { + release(); + } + }; + } + + @Override + public void release(Resource x) { + x.release(); + } + } + + public static void main(String[] args) { + AutoPool pool = new AutoPool<>(); + Random rnd = new Random(); + for (int i = 0; i < 5; ++i) { + pool.add(i); + } + + try { + while (true) { + Resource r = pool.acquire(); + System.out.println("using " + r.get()); + Thread.sleep(Math.abs(rnd.nextInt() % 1000)); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + +} diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj new file mode 100644 index 0000000..a94f4fc --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj @@ -0,0 +1,137 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {3c05ab7e-1582-4bd1-866b-5da21b3a6778} + Appello13 + 10.0 + Appello_13_1_2023 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Default + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.filters b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.filters new file mode 100644 index 0000000..ed2698d --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.user b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_1_2023.sln b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_1_2023.sln new file mode 100644 index 0000000..76133ad --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_1_2023.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32901.215 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Appello_13_", "Appello_13_.vcxproj", "{3C05AB7E-1582-4BD1-866B-5DA21B3A6778}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3C05AB7E-1582-4BD1-866B-5DA21B3A6778}.Debug|x64.ActiveCfg = Debug|x64 + {3C05AB7E-1582-4BD1-866B-5DA21B3A6778}.Debug|x64.Build.0 = Debug|x64 + {3C05AB7E-1582-4BD1-866B-5DA21B3A6778}.Debug|x86.ActiveCfg = Debug|Win32 + {3C05AB7E-1582-4BD1-866B-5DA21B3A6778}.Debug|x86.Build.0 = Debug|Win32 + {3C05AB7E-1582-4BD1-866B-5DA21B3A6778}.Release|x64.ActiveCfg = Release|x64 + {3C05AB7E-1582-4BD1-866B-5DA21B3A6778}.Release|x64.Build.0 = Release|x64 + {3C05AB7E-1582-4BD1-866B-5DA21B3A6778}.Release|x86.ActiveCfg = Release|Win32 + {3C05AB7E-1582-4BD1-866B-5DA21B3A6778}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {66CDDA76-E424-4DD8-87CF-E87E03399139} + EndGlobalSection +EndGlobal diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/curve.cpp b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/curve.cpp new file mode 100644 index 0000000..46fd892 --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/curve.cpp @@ -0,0 +1,137 @@ + +#include +#include +#include +#include + +using namespace std; + + +using real = double; +using unary_fun = function; + +#define RESOLUTION (10) + + +class curve +{ + +private: + real a, b; + unary_fun f; + +public: + curve(const real& a_, const real& b_, const unary_fun& f_) : f(f_), a(a_), b(b_) {} + + real get_dx() const { return (b - a) / RESOLUTION; } + + pair interval() const { return pair(a, b); } + + curve derivative() const + { + return curve(a, b, [=](const real& x) { + real dx = get_dx(), dy = f(x + dx) - f(x); + return dy / dx; + }); + } + + curve primitive() const + { + return curve(a, b, [=](const real& x) { + real dx = get_dx(), y = f(x); + return y * dx; + }); + } + + real integral() const + { + real dx = get_dx(); + real r = 0.; + const unary_fun& F = primitive(); + for (real x = a; x <= b; x += dx) + { + r += F(x); + } + return r; + } + + real operator()(const real& x) const + { + return f(x); + } + + class iterator + { + private: + const curve& c; + real x; + + public: + iterator(const curve& c_, const real& x_) : c(c_), x(x_) {} + + pair operator*() const + { + return pair(x, c.f(x)); + } + + iterator operator++() + { + x += c.get_dx(); + return iterator(*this); + } + + iterator operator++(int) + { + auto r = *this; + x += c.get_dx(); + return r; + } + + bool operator!=(const iterator& it) const + { + return x < it.x; // scorretto ma funziona + } + }; + + iterator begin() const + { + return iterator(*this, a); + } + + iterator end() const + { + return iterator(*this, b + get_dx()); + } + +}; + + +ostream& operator<<(ostream& os, const curve& c) +{ + os << "dom = [" << c.interval().first << ", " << c.interval().second << "] dx = " << c.get_dx() << ": " << endl; + /*for (const pair& p : c) + { + const real& x = p.first, & y = p.second; + os << "\tf(" << x << ") = " << y << endl; + }*/ + for (curve::iterator it = c.begin(); it != c.end(); ++it) + { + const pair& p = *it; + const real& x = p.first, & y = p.second; + os << "\tf(" << x << ") = " << y << endl; + } + return os << endl; +} + +int main() +{ + curve f(-1., 1., [](const real& x) { return x * x; }); + + cout << f << endl + << f.derivative() << endl + << f.primitive() << endl + << f.primitive().derivative() << endl + << f.derivative().primitive() << endl + ; + return 0; +} \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java b/soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java index a587c1e..8c42583 100644 --- a/soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java +++ b/soluzione appelli/Scritto PO2 13 9 22/java/src/Es1.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; +import java.util.concurrent.BlockingQueue; public class Es1 { @@ -100,6 +101,8 @@ public static TreeNode v(@NotNull T data) { @Override public boolean equals(@Nullable Object o) { if (o instanceof TreeNode) { + BlockingQueue b; + TreeNode t = (TreeNode) o; return areEqual(data, t.data) && areEqual(left, t.left) && areEqual(right, t.right); } diff --git a/soluzione appelli/Scritto PO2 22 1 19/.idea/vcs.xml b/soluzione appelli/Scritto PO2 22 1 19/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/soluzione appelli/Scritto PO2 22 1 19/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 22 1 19/.idea/workspace.xml b/soluzione appelli/Scritto PO2 22 1 19/.idea/workspace.xml index bd71a55..aa0b220 100644 --- a/soluzione appelli/Scritto PO2 22 1 19/.idea/workspace.xml +++ b/soluzione appelli/Scritto PO2 22 1 19/.idea/workspace.xml @@ -7,11 +7,9 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + @@ -103,14 +80,15 @@ - - - - - - - + + + @@ -119,18 +97,6 @@ - - - + @@ -208,9 +175,9 @@ - - - + + + From 0de1606a7e431f1ea8c2eb250dc02dd1218f38eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 24 Jan 2023 14:14:28 +0100 Subject: [PATCH 113/202] fix --- .../Appello_13_ => Scritto PO2 13 1 2023/cpp}/Appello_13_.vcxproj | 0 .../cpp}/Appello_13_.vcxproj.filters | 0 .../cpp}/Appello_13_.vcxproj.user | 0 .../cpp}/Appello_13_1_2023.sln | 0 .../cpp/Appello_13_ => Scritto PO2 13 1 2023/cpp}/curve.cpp | 0 .../{Appello_13_1_2023 => java}/.idea/.gitignore | 0 .../{Appello_13_1_2023 => java}/.idea/misc.xml | 0 .../{Appello_13_1_2023 => java}/.idea/modules.xml | 0 .../{Appello_13_1_2023 => java}/.idea/vcs.xml | 0 .../{Appello_13_1_2023 => java}/Appello_13_1_2023.iml | 0 .../Scritto PO2 13 1 2023/{Appello_13_1_2023 => java}/Es1.java | 0 11 files changed, 0 insertions(+), 0 deletions(-) rename soluzione appelli/{Scritto PO2 13 9 22/cpp/Appello_13_ => Scritto PO2 13 1 2023/cpp}/Appello_13_.vcxproj (100%) rename soluzione appelli/{Scritto PO2 13 9 22/cpp/Appello_13_ => Scritto PO2 13 1 2023/cpp}/Appello_13_.vcxproj.filters (100%) rename soluzione appelli/{Scritto PO2 13 9 22/cpp/Appello_13_ => Scritto PO2 13 1 2023/cpp}/Appello_13_.vcxproj.user (100%) rename soluzione appelli/{Scritto PO2 13 9 22/cpp/Appello_13_ => Scritto PO2 13 1 2023/cpp}/Appello_13_1_2023.sln (100%) rename soluzione appelli/{Scritto PO2 13 9 22/cpp/Appello_13_ => Scritto PO2 13 1 2023/cpp}/curve.cpp (100%) rename soluzione appelli/Scritto PO2 13 1 2023/{Appello_13_1_2023 => java}/.idea/.gitignore (100%) rename soluzione appelli/Scritto PO2 13 1 2023/{Appello_13_1_2023 => java}/.idea/misc.xml (100%) rename soluzione appelli/Scritto PO2 13 1 2023/{Appello_13_1_2023 => java}/.idea/modules.xml (100%) rename soluzione appelli/Scritto PO2 13 1 2023/{Appello_13_1_2023 => java}/.idea/vcs.xml (100%) rename soluzione appelli/Scritto PO2 13 1 2023/{Appello_13_1_2023 => java}/Appello_13_1_2023.iml (100%) rename soluzione appelli/Scritto PO2 13 1 2023/{Appello_13_1_2023 => java}/Es1.java (100%) diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj b/soluzione appelli/Scritto PO2 13 1 2023/cpp/Appello_13_.vcxproj similarity index 100% rename from soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj rename to soluzione appelli/Scritto PO2 13 1 2023/cpp/Appello_13_.vcxproj diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.filters b/soluzione appelli/Scritto PO2 13 1 2023/cpp/Appello_13_.vcxproj.filters similarity index 100% rename from soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.filters rename to soluzione appelli/Scritto PO2 13 1 2023/cpp/Appello_13_.vcxproj.filters diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.user b/soluzione appelli/Scritto PO2 13 1 2023/cpp/Appello_13_.vcxproj.user similarity index 100% rename from soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_.vcxproj.user rename to soluzione appelli/Scritto PO2 13 1 2023/cpp/Appello_13_.vcxproj.user diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_1_2023.sln b/soluzione appelli/Scritto PO2 13 1 2023/cpp/Appello_13_1_2023.sln similarity index 100% rename from soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/Appello_13_1_2023.sln rename to soluzione appelli/Scritto PO2 13 1 2023/cpp/Appello_13_1_2023.sln diff --git a/soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/curve.cpp b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp similarity index 100% rename from soluzione appelli/Scritto PO2 13 9 22/cpp/Appello_13_/curve.cpp rename to soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/.gitignore b/soluzione appelli/Scritto PO2 13 1 2023/java/.idea/.gitignore similarity index 100% rename from soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/.gitignore rename to soluzione appelli/Scritto PO2 13 1 2023/java/.idea/.gitignore diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/misc.xml b/soluzione appelli/Scritto PO2 13 1 2023/java/.idea/misc.xml similarity index 100% rename from soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/misc.xml rename to soluzione appelli/Scritto PO2 13 1 2023/java/.idea/misc.xml diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/modules.xml b/soluzione appelli/Scritto PO2 13 1 2023/java/.idea/modules.xml similarity index 100% rename from soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/modules.xml rename to soluzione appelli/Scritto PO2 13 1 2023/java/.idea/modules.xml diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/vcs.xml b/soluzione appelli/Scritto PO2 13 1 2023/java/.idea/vcs.xml similarity index 100% rename from soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/.idea/vcs.xml rename to soluzione appelli/Scritto PO2 13 1 2023/java/.idea/vcs.xml diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Appello_13_1_2023.iml b/soluzione appelli/Scritto PO2 13 1 2023/java/Appello_13_1_2023.iml similarity index 100% rename from soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Appello_13_1_2023.iml rename to soluzione appelli/Scritto PO2 13 1 2023/java/Appello_13_1_2023.iml diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Es1.java b/soluzione appelli/Scritto PO2 13 1 2023/java/Es1.java similarity index 100% rename from soluzione appelli/Scritto PO2 13 1 2023/Appello_13_1_2023/Es1.java rename to soluzione appelli/Scritto PO2 13 1 2023/java/Es1.java From fb6e991e200a2d51a11cf2e8117d9a28997aeeac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 24 Jan 2023 15:03:26 +0100 Subject: [PATCH 114/202] Update curve.cpp --- .../Scritto PO2 13 1 2023/cpp/curve.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp index 46fd892..01e86e9 100644 --- a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp +++ b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp @@ -74,22 +74,22 @@ class curve return pair(x, c.f(x)); } - iterator operator++() + iterator& operator++() // il pre-incremento modifica sè stesso e non ritorna una copia { x += c.get_dx(); - return iterator(*this); + return *this; } - iterator operator++(int) + iterator operator++(int) // il post-incremento fa una copia, modifica sé stesso e ritorna la copia { - auto r = *this; + auto r(*this); x += c.get_dx(); return r; } bool operator!=(const iterator& it) const { - return x < it.x; // scorretto ma funziona + return fabs(x - it.x) > c.get_dx(); // non si confrontano mai i float direttamente con l'operatore di uguaglianza o disuguaglianza } }; @@ -117,6 +117,8 @@ ostream& operator<<(ostream& os, const curve& c) for (curve::iterator it = c.begin(); it != c.end(); ++it) { const pair& p = *it; + auto it2 = it++; + auto it3 = ++it; const real& x = p.first, & y = p.second; os << "\tf(" << x << ") = " << y << endl; } From 73bf52d4caa55f9af57ad39289547352a1d8c434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 25 Jan 2023 16:31:18 +0100 Subject: [PATCH 115/202] Update curve.cpp --- soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp index 01e86e9..48cae76 100644 --- a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp +++ b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp @@ -50,7 +50,8 @@ class curve const unary_fun& F = primitive(); for (real x = a; x <= b; x += dx) { - r += F(x); + \ + r += F(x); } return r; } @@ -117,8 +118,6 @@ ostream& operator<<(ostream& os, const curve& c) for (curve::iterator it = c.begin(); it != c.end(); ++it) { const pair& p = *it; - auto it2 = it++; - auto it3 = ++it; const real& x = p.first, & y = p.second; os << "\tf(" << x << ") = " << y << endl; } From 5fe8206143da8f3497864d6fde22a3e79c38024f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 26 Jan 2023 11:38:42 +0100 Subject: [PATCH 116/202] Update curve.cpp --- .../Scritto PO2 13 1 2023/cpp/curve.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp index 48cae76..7317c8d 100644 --- a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp +++ b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp @@ -29,30 +29,27 @@ class curve curve derivative() const { - return curve(a, b, [=](const real& x) { - real dx = get_dx(), dy = f(x + dx) - f(x); + return curve(a, b, [&, dx = get_dx()](const real& x) { // anche la capture [=] sarebbe stata sufficiente + const real dy = f(x + dx) - f(x); return dy / dx; }); } curve primitive() const { - return curve(a, b, [=](const real& x) { - real dx = get_dx(), y = f(x); + return curve(a, b, [&, dx = get_dx()](const real& x) { // oppure la [&] + const real y = f(x); return y * dx; }); } real integral() const { - real dx = get_dx(); - real r = 0.; + const real dx = get_dx(); const unary_fun& F = primitive(); + real r = 0.; for (real x = a; x <= b; x += dx) - { - \ - r += F(x); - } + r += F(x); return r; } From 2bef7a98ccdc28278e9af4543747ffe07fb45be2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 26 Jan 2023 11:45:18 +0100 Subject: [PATCH 117/202] Update curve.cpp --- soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp index 7317c8d..ff9f73e 100644 --- a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp +++ b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp @@ -81,7 +81,7 @@ class curve iterator operator++(int) // il post-incremento fa una copia, modifica sé stesso e ritorna la copia { auto r(*this); - x += c.get_dx(); + ++(*this); return r; } From cbbf3656d87df09bd989bf334f18a09e1e1ad6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 26 Jan 2023 15:51:23 +0100 Subject: [PATCH 118/202] Update curve.cpp --- .../Scritto PO2 13 1 2023/cpp/curve.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp index ff9f73e..ac3040c 100644 --- a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp +++ b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp @@ -45,12 +45,13 @@ class curve real integral() const { - const real dx = get_dx(); const unary_fun& F = primitive(); - real r = 0.; - for (real x = a; x <= b; x += dx) - r += F(x); - return r; + return F(b) - F(a); + //const real dx = get_dx(); + //real r = 0.; + //for (real x = a; x <= b; x += dx) + // r += F(x); + //return r; } real operator()(const real& x) const @@ -87,7 +88,7 @@ class curve bool operator!=(const iterator& it) const { - return fabs(x - it.x) > c.get_dx(); // non si confrontano mai i float direttamente con l'operatore di uguaglianza o disuguaglianza + return fabs(x - it.x) >= c.get_dx(); // non si confrontano mai i float direttamente con l'operatore di uguaglianza o disuguaglianza } }; From 90dc21dd537d25c0831be58140b97210ecca423c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 30 Jan 2023 10:56:17 +0100 Subject: [PATCH 119/202] Update curve.cpp --- soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp index ac3040c..2032bdd 100644 --- a/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp +++ b/soluzione appelli/Scritto PO2 13 1 2023/cpp/curve.cpp @@ -47,11 +47,6 @@ class curve { const unary_fun& F = primitive(); return F(b) - F(a); - //const real dx = get_dx(); - //real r = 0.; - //for (real x = a; x <= b; x += dx) - // r += F(x); - //return r; } real operator()(const real& x) const From 5eee376a889938aa1502bac03857fcc797ac0b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 17 Feb 2023 15:19:50 +0100 Subject: [PATCH 120/202] Lezioni Java 2022-23 First Push --- 2022-23/LezioniJava22-23/.idea/.gitignore | 3 + 2022-23/LezioniJava22-23/.idea/misc.xml | 6 ++ 2022-23/LezioniJava22-23/.idea/modules.xml | 8 ++ 2022-23/LezioniJava22-23/LezioniJava22-23.iml | 11 +++ 2022-23/LezioniJava22-23/src/Generics.java | 88 +++++++++++++++++++ 2022-23/LezioniJava22-23/src/Zoo.java | 42 +++++++++ 6 files changed, 158 insertions(+) create mode 100644 2022-23/LezioniJava22-23/.idea/.gitignore create mode 100644 2022-23/LezioniJava22-23/.idea/misc.xml create mode 100644 2022-23/LezioniJava22-23/.idea/modules.xml create mode 100644 2022-23/LezioniJava22-23/LezioniJava22-23.iml create mode 100644 2022-23/LezioniJava22-23/src/Generics.java create mode 100644 2022-23/LezioniJava22-23/src/Zoo.java diff --git a/2022-23/LezioniJava22-23/.idea/.gitignore b/2022-23/LezioniJava22-23/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/2022-23/LezioniJava22-23/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/2022-23/LezioniJava22-23/.idea/misc.xml b/2022-23/LezioniJava22-23/.idea/misc.xml new file mode 100644 index 0000000..1ce6e7f --- /dev/null +++ b/2022-23/LezioniJava22-23/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/.idea/modules.xml b/2022-23/LezioniJava22-23/.idea/modules.xml new file mode 100644 index 0000000..76da50f --- /dev/null +++ b/2022-23/LezioniJava22-23/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/LezioniJava22-23.iml b/2022-23/LezioniJava22-23/LezioniJava22-23.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/2022-23/LezioniJava22-23/LezioniJava22-23.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/src/Generics.java b/2022-23/LezioniJava22-23/src/Generics.java new file mode 100644 index 0000000..c401d1e --- /dev/null +++ b/2022-23/LezioniJava22-23/src/Generics.java @@ -0,0 +1,88 @@ +public class Generics { + + // JDK 1.0 (1994) + + public interface Iterator { + boolean hasNext(); + Object next(); + } + + public interface Iterable { + Iterator iterator(); + } + + public interface Collection extends Iterable { + int size(); + void add(Object x); + void remove(Object x); + boolean contains(Object x); + } + + public interface List extends Collection { + Object get(int index); + void set(int index, Object x); + } + + public static class ArrayList implements List { + + private Object[] a; + private int sz; + + public ArrayList(int capacity) { + a = new Object[capacity]; + sz = 0; + } + + public ArrayList() { + this(10); + } + + @Override + public int size() { + return sz; + } + + @Override + public void add(Object x) { + if (sz >= a.length) { + Object[] old = a; + a = new Object[old.length * 2]; + for (int i = 0; i < old.length; ++i) + a[i] = old[i]; + } + a[sz++] = x; + } + + @Override + public void remove(Object x) { + + } + + @Override + public boolean contains(Object x) { + return false; + } + + @Override + public Iterator iterator() { + return null; + } + + + @Override + public Object get(int index) { + return a[index]; + } + + @Override + public void set(int index, Object x) { + a[index] = x; + } + } + + + public static void main(String[] args) { + + } + +} diff --git a/2022-23/LezioniJava22-23/src/Zoo.java b/2022-23/LezioniJava22-23/src/Zoo.java new file mode 100644 index 0000000..eae7cda --- /dev/null +++ b/2022-23/LezioniJava22-23/src/Zoo.java @@ -0,0 +1,42 @@ +public class Zoo { + + + public static class Animal { + protected int weight; + + public Animal(int w) { + this.weight = w; + } + + public void eat(Animal a) { + this.weight += a.weight; + } + } + + public static class Dog extends Animal { + private String hairColor; + + public Dog(int w, String hairColor) { + super(w); + this.hairColor = hairColor; + } + + @Override + public void eat(Animal a) { + this.weight = a.weight * 2; + } + } + + public static void main(String[] args) { + + Animal fido = new Dog(50, "Red"); + Dog pluto = new Dog(20, "Dotted"); + Animal ciccio = pluto; + Dog bobby = pluto; + ciccio = bobby; + + ciccio.eat(new Dog(10, "White")); + + } + +} From 6d2de0e0e5b614c5574a6470177b79f00c8dbdd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 17 Feb 2023 15:30:31 +0100 Subject: [PATCH 121/202] Updated --- 2022-23/LezioniJava22-23/.idea/.gitignore | 3 - 2022-23/LezioniJava22-23/.idea/misc.xml | 2 +- 2022-23/LezioniJava22-23/.idea/modules.xml | 2 +- 2022-23/LezioniJava22-23/.idea/uiDesigner.xml | 124 ++++++++++++++++++ 2022-23/LezioniJava22-23/.idea/vcs.xml | 6 + 2022-23/LezioniJava22-23/.idea/workspace.xml | 89 +++++++++++++ .../LezioniJava22-23/{src => }/Generics.java | 1 + 2022-23/LezioniJava22-23/LezioniJava22-23.iml | 6 +- 2022-23/LezioniJava22-23/{src => }/Zoo.java | 1 + .../LezioniJava22-23/.idea/misc.xml | 6 + .../LezioniJava22-23/.idea/modules.xml | 8 ++ .../LezioniJava22-23/.idea/uiDesigner.xml | 124 ++++++++++++++++++ .../production/LezioniJava22-23/.idea/vcs.xml | 6 + .../LezioniJava22-23/.idea/workspace.xml | 77 +++++++++++ .../LezioniJava22-23/LezioniJava22-23.iml | 11 ++ 15 files changed, 458 insertions(+), 8 deletions(-) delete mode 100644 2022-23/LezioniJava22-23/.idea/.gitignore create mode 100644 2022-23/LezioniJava22-23/.idea/uiDesigner.xml create mode 100644 2022-23/LezioniJava22-23/.idea/vcs.xml create mode 100644 2022-23/LezioniJava22-23/.idea/workspace.xml rename 2022-23/LezioniJava22-23/{src => }/Generics.java (99%) rename 2022-23/LezioniJava22-23/{src => }/Zoo.java (99%) create mode 100644 2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/misc.xml create mode 100644 2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/modules.xml create mode 100644 2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/uiDesigner.xml create mode 100644 2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/vcs.xml create mode 100644 2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml create mode 100644 2022-23/LezioniJava22-23/out/production/LezioniJava22-23/LezioniJava22-23.iml diff --git a/2022-23/LezioniJava22-23/.idea/.gitignore b/2022-23/LezioniJava22-23/.idea/.gitignore deleted file mode 100644 index 26d3352..0000000 --- a/2022-23/LezioniJava22-23/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml diff --git a/2022-23/LezioniJava22-23/.idea/misc.xml b/2022-23/LezioniJava22-23/.idea/misc.xml index 1ce6e7f..639900d 100644 --- a/2022-23/LezioniJava22-23/.idea/misc.xml +++ b/2022-23/LezioniJava22-23/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/.idea/modules.xml b/2022-23/LezioniJava22-23/.idea/modules.xml index 76da50f..ef8c266 100644 --- a/2022-23/LezioniJava22-23/.idea/modules.xml +++ b/2022-23/LezioniJava22-23/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/.idea/uiDesigner.xml b/2022-23/LezioniJava22-23/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/2022-23/LezioniJava22-23/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/.idea/vcs.xml b/2022-23/LezioniJava22-23/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/2022-23/LezioniJava22-23/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/.idea/workspace.xml b/2022-23/LezioniJava22-23/.idea/workspace.xml new file mode 100644 index 0000000..01b1154 --- /dev/null +++ b/2022-23/LezioniJava22-23/.idea/workspace.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1676643953687 + + + + + + + + + + + + + file://$PROJECT_DIR$/Zoo.java + 32 + + + + + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/src/Generics.java b/2022-23/LezioniJava22-23/Generics.java similarity index 99% rename from 2022-23/LezioniJava22-23/src/Generics.java rename to 2022-23/LezioniJava22-23/Generics.java index c401d1e..4dc1b15 100644 --- a/2022-23/LezioniJava22-23/src/Generics.java +++ b/2022-23/LezioniJava22-23/Generics.java @@ -83,6 +83,7 @@ public void set(int index, Object x) { public static void main(String[] args) { + } } diff --git a/2022-23/LezioniJava22-23/LezioniJava22-23.iml b/2022-23/LezioniJava22-23/LezioniJava22-23.iml index c90834f..6abc57f 100644 --- a/2022-23/LezioniJava22-23/LezioniJava22-23.iml +++ b/2022-23/LezioniJava22-23/LezioniJava22-23.iml @@ -1,11 +1,11 @@ - + - + - + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/src/Zoo.java b/2022-23/LezioniJava22-23/Zoo.java similarity index 99% rename from 2022-23/LezioniJava22-23/src/Zoo.java rename to 2022-23/LezioniJava22-23/Zoo.java index eae7cda..27e2e55 100644 --- a/2022-23/LezioniJava22-23/src/Zoo.java +++ b/2022-23/LezioniJava22-23/Zoo.java @@ -1,3 +1,4 @@ + public class Zoo { diff --git a/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/misc.xml b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/modules.xml b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/modules.xml new file mode 100644 index 0000000..ef8c266 --- /dev/null +++ b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/uiDesigner.xml b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/vcs.xml b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml new file mode 100644 index 0000000..7dc96a4 --- /dev/null +++ b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1676643953687 + + + + + + + + + + + + + file://$PROJECT_DIR$/Zoo.java + 32 + + + + + \ No newline at end of file diff --git a/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/LezioniJava22-23.iml b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/LezioniJava22-23.iml new file mode 100644 index 0000000..6abc57f --- /dev/null +++ b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/LezioniJava22-23.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file From e455e073625fd683ea1d146dfd6b7e1e23171344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 20 Feb 2023 17:17:52 +0100 Subject: [PATCH 122/202] TinyJDK 1 --- 2022-23/LezioniJava22-23/.idea/workspace.xml | 24 ++--- .../it/unive/dais/po2/tinyjdk/Generics.java | 101 ++++++++++++++++++ .../unive/dais/po2/tinyjdk/NoGenerics.java} | 12 ++- 3 files changed, 120 insertions(+), 17 deletions(-) create mode 100644 2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Generics.java rename 2022-23/LezioniJava22-23/{Generics.java => it/unive/dais/po2/tinyjdk/NoGenerics.java} (85%) diff --git a/2022-23/LezioniJava22-23/.idea/workspace.xml b/2022-23/LezioniJava22-23/.idea/workspace.xml index 01b1154..73285c5 100644 --- a/2022-23/LezioniJava22-23/.idea/workspace.xml +++ b/2022-23/LezioniJava22-23/.idea/workspace.xml @@ -2,14 +2,9 @@ - - - - - - - - + + + - + + + + + diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Generics.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Generics.java new file mode 100644 index 0000000..8446e68 --- /dev/null +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Generics.java @@ -0,0 +1,101 @@ +package it.unive.dais.po2.tinyjdk; + +public class Generics { + + // JDK 1.5 (2004) + + public interface Iterator { + boolean hasNext(); + T next(); + } + + public interface Iterable { + Iterator iterator(); + } + + public interface Collection extends Iterable { + int size(); + void add(T x); + void remove(T x); + boolean contains(T x); + } + + public interface List extends Collection { + T get(int index); + + void set(int index, T x); + } + + public static class ArrayList implements List { + + private T[] a; + private int sz; + + public ArrayList(int capacity) { + a = new T[capacity]; + sz = 0; + } + + public ArrayList() { + this(10); + } + + @Override + public int size() { + return sz; + } + + @Override + public void add(T x) { + if (sz >= a.length) { + T[] old = a; + a = new T[old.length * 2]; + for (int i = 0; i < old.length; ++i) + a[i] = old[i]; + } + a[sz++] = x; + } + + @Override + public void remove(T x) { + + } + + @Override + public boolean contains(T x) { + return false; + } + + @Override + public Iterator iterator() { + return null; + } + + + @Override + public T get(int index) { + return a[index]; + } + + @Override + public void set(int index, T x) { + a[index] = x; + } + } + + + public static void main(String[] args) { + List c = new ArrayList(); + //c.add("ciao"); + //c.add(8); + c.add(new Zoo.Dog(20, "Red")); + + for (int i = 0; i < c.size(); ++i) { + Zoo.Animal a = c.get(i); + + } + + } + +} + diff --git a/2022-23/LezioniJava22-23/Generics.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/NoGenerics.java similarity index 85% rename from 2022-23/LezioniJava22-23/Generics.java rename to 2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/NoGenerics.java index 4dc1b15..6cf821c 100644 --- a/2022-23/LezioniJava22-23/Generics.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/NoGenerics.java @@ -1,4 +1,6 @@ -public class Generics { +package it.unive.dais.po2.tinyjdk; + +public class NoGenerics { // JDK 1.0 (1994) @@ -82,7 +84,15 @@ public void set(int index, Object x) { public static void main(String[] args) { + List c = new ArrayList(); + c.add("ciao"); + c.add(8); + c.add(new Zoo.Dog(20, "Red")); + + for (int i = 0; i < c.size(); ++i) { + String s = (String) c.get(i); + } } From e9cbc01c7aa09ac580522b62c5b420d4255a91fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 21 Feb 2023 17:16:54 +0100 Subject: [PATCH 123/202] Iterators --- 2022-23/LezioniJava22-23/.idea/workspace.xml | 3 +- .../it/unive/dais/po2/tinyjdk/Generics.java | 60 ++++++++++++++++--- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/2022-23/LezioniJava22-23/.idea/workspace.xml b/2022-23/LezioniJava22-23/.idea/workspace.xml index 73285c5..a430a63 100644 --- a/2022-23/LezioniJava22-23/.idea/workspace.xml +++ b/2022-23/LezioniJava22-23/.idea/workspace.xml @@ -2,9 +2,8 @@ - - + diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/ArrayList.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/ArrayList.java new file mode 100644 index 0000000..47cbb13 --- /dev/null +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/ArrayList.java @@ -0,0 +1,105 @@ +package it.unive.dais.po2.tinyjdk; + +public class ArrayList implements List { + + private Object[] a; + private int sz; + + public ArrayList(int capacity) { + a = new Object[capacity]; + sz = 0; + } + + public ArrayList() { + this(10); + } + + @Override + public int size() { + return sz; + } + + @Override + public void add(T x) { + if (sz >= a.length) { + Object[] old = a; + a = new Object[old.length * 2]; + for (int i = 0; i < old.length; ++i) + a[i] = old[i]; + } + a[sz++] = x; + } + + @Override + public void remove(T x) { + + } + + @Override + public boolean contains(T x) { + return false; + } + + private class MyIterator implements Iterator { + private int pos = 0; + + @Override + public boolean hasNext() { + return pos < size(); + } + + @Override + public T next() { + return get(pos++); + } + } + + private static class ListIterator implements Iterator { + private int pos = 0; + private List l; + + public ListIterator(List l) { + this.l = l; + } + + @Override + public boolean hasNext() { + return pos < l.size(); + } + + @Override + public E next() { + return l.get(pos++); + } + } + + @Override + public Iterator iterator() { + //return new MyIterator(); + //return new ListIterator(this); + return new Iterator() { + private int pos = 0; + + @Override + public boolean hasNext() { + return pos < size(); + } + + @Override + public T next() { + return get(pos++); + } + }; + } + + + @Override + public T get(int index) { + return (T) a[index]; + } + + @Override + public void set(int index, T x) { + a[index] = x; + } +} diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Collection.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Collection.java new file mode 100644 index 0000000..0aa9772 --- /dev/null +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Collection.java @@ -0,0 +1,11 @@ +package it.unive.dais.po2.tinyjdk; + +public interface Collection extends Iterable { + int size(); + + void add(T x); + + void remove(T x); + + boolean contains(T x); +} diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Generics.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Generics.java deleted file mode 100644 index d028970..0000000 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Generics.java +++ /dev/null @@ -1,147 +0,0 @@ -package it.unive.dais.po2.tinyjdk; - -public class Generics { - - // JDK 1.5 (2004) - - public interface Iterator { - boolean hasNext(); - T next(); - } - - public interface Iterable { - Iterator iterator(); - } - - public interface Collection extends Iterable { - int size(); - void add(T x); - void remove(T x); - boolean contains(T x); - } - - public interface List extends Collection { - T get(int index); - void set(int index, T x); - } - - public static class ArrayList implements List { - - private Object[] a; - private int sz; - - public ArrayList(int capacity) { - a = new Object[capacity]; - sz = 0; - } - - public ArrayList() { - this(10); - } - - @Override - public int size() { - return sz; - } - - @Override - public void add(T x) { - if (sz >= a.length) { - Object[] old = a; - a = new Object[old.length * 2]; - for (int i = 0; i < old.length; ++i) - a[i] = old[i]; - } - a[sz++] = x; - } - - @Override - public void remove(T x) { - - } - - @Override - public boolean contains(T x) { - return false; - } - - private class MyIterator implements Iterator { - private int pos = 0; - - @Override - public boolean hasNext() { - return pos < size(); - } - - @Override - public T next() { - return get(pos++); - } - } - - private static class MyStaticIterator implements Iterator { - private int pos = 0; - private ArrayList l; - - public MyStaticIterator(ArrayList l) { - this.l = l; - } - - @Override - public boolean hasNext() { - return pos < l.size(); - } - - @Override - public E next() { - return l.get(pos++); - } - } - - @Override - public Iterator iterator() { - //return new MyIterator(); - return new MyStaticIterator<>(this); - // TODO usare una anonymous class - } - -// public static void main() { -// List l = new ArrayList(); -// Iterator it = l.iterator(); -// while (it.hasNext()) { -// Integer n = it.next(); -// System.out.println(n); -// } -// } - - - - - - @Override - public T get(int index) { - return (T) a[index]; - } - - @Override - public void set(int index, T x) { - a[index] = x; - } - } - - - public static void main(String[] args) { - List c = new ArrayList(); - //c.add("ciao"); - //c.add(8); - c.add(new Zoo.Dog(20, "Red")); - - for (int i = 0; i < c.size(); ++i) { - Zoo.Animal a = c.get(i); - - } - - } - -} - diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Iterable.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Iterable.java new file mode 100644 index 0000000..8a384cc --- /dev/null +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Iterable.java @@ -0,0 +1,5 @@ +package it.unive.dais.po2.tinyjdk; + +public interface Iterable { + Iterator iterator(); +} diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Iterator.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Iterator.java new file mode 100644 index 0000000..565282a --- /dev/null +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Iterator.java @@ -0,0 +1,6 @@ +package it.unive.dais.po2.tinyjdk; + +public interface Iterator { + boolean hasNext(); + T next(); +} diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/List.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/List.java new file mode 100644 index 0000000..87121c4 --- /dev/null +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/List.java @@ -0,0 +1,7 @@ +package it.unive.dais.po2.tinyjdk; + +public interface List extends Collection { + T get(int index); + + void set(int index, T x); +} diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Set.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Set.java new file mode 100644 index 0000000..a3749b2 --- /dev/null +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Set.java @@ -0,0 +1,4 @@ +package it.unive.dais.po2.tinyjdk; + +public interface Set extends Collection { +} diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/NoGenerics.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/nogenerics/NoGenerics.java similarity index 97% rename from 2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/NoGenerics.java rename to 2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/nogenerics/NoGenerics.java index 6cf821c..437a745 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/NoGenerics.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/nogenerics/NoGenerics.java @@ -1,4 +1,4 @@ -package it.unive.dais.po2.tinyjdk; +package it.unive.dais.po2.tinyjdk.nogenerics; public class NoGenerics { From 7ccd5400f9878fbb217b6bee3395e0c3dda0fd2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 28 Feb 2023 17:18:08 +0100 Subject: [PATCH 125/202] Queue --- 2022-23/LezioniJava22-23/.idea/workspace.xml | 17 ++++--- .../it/unive/dais/po2/tinyjdk/BasicQueue.java | 45 +++++++++++++++++++ .../it/unive/dais/po2/tinyjdk/Collection.java | 2 + .../it/unive/dais/po2/tinyjdk/HashSet.java | 40 +++++++++++++++++ .../it/unive/dais/po2/tinyjdk/List.java | 2 + .../po2/tinyjdk/NotImplementedException.java | 4 ++ .../it/unive/dais/po2/tinyjdk/Queue.java | 7 +++ 7 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/BasicQueue.java create mode 100644 2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/HashSet.java create mode 100644 2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/NotImplementedException.java create mode 100644 2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Queue.java diff --git a/2022-23/LezioniJava22-23/.idea/workspace.xml b/2022-23/LezioniJava22-23/.idea/workspace.xml index fdb71a1..2b3fa30 100644 --- a/2022-23/LezioniJava22-23/.idea/workspace.xml +++ b/2022-23/LezioniJava22-23/.idea/workspace.xml @@ -2,14 +2,13 @@ - - - - - - - - + + + + + + + c, + Function f) { + Collection r = new ArrayList<>(); + for (A x : c) { + r.add(f.apply(x)); + } + return r; + } + + public static Iterator mapIterator(Iterator it, + Function f) { + return new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public B next() { + return f.apply(it.next()); + } + }; + } + + public static Collection filter__pure(Collection c, + Predicate p) { + Collection r = new ArrayList<>(); + for (T x : c) { + if (p.test(x)) + r.add(x); + } + return r; + } + + public static void filter__impure(Collection c, + Predicate p) { + Iterator it = c.iterator(); + while (it.hasNext()) { + if (!p.test(it.next())) + it.remove(); + } + } + + public static void iter(Collection c, Consumer f) { + for (T x : c) { + f.accept(x); + } + } + + public static T sum(Collection c, T zero, BiFunction f) { + /*T z = zero; + for (T x : c) { + z = f.apply(z, x); + } + return z;*/ + return foldLeft(c, zero, f); + } + + public static B foldLeft(Collection c, + B zero, + BiFunction f) { + B z = zero; + for (A x : c) { + z = f.apply(x, z); + } + return z; + } + + // TODO scrivere la foldLeft ricorsivamente + + public static void main2(String[] args) { + List l = new ArrayList<>(); + l.add("ciao"); + l.add("mi"); + l.add("chiamo"); + l.add("pippo"); + String s = sum(l, "", (x, y) -> x + y); + + } +} diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/ArrayList.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/ArrayList.java index fc9b8d3..f50967a 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/ArrayList.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/ArrayList.java @@ -1,6 +1,8 @@ package it.unive.dais.po2.tinyjdk; import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Predicate; public class ArrayList implements List { @@ -50,7 +52,7 @@ public void remove(T x) { private static class MyPredicate implements Predicate { @Override - public Boolean apply(Integer x) { + public boolean test(Integer x) { return x % 3 == 0; } } @@ -65,10 +67,13 @@ public static void main(String[] args) { c.removeIf(new Predicate() { @Override - public Boolean apply(Integer x) { + public boolean test(Integer x) { return x < 0; } }); + + Function y = x -> x > 80; + c.removeIf(new MyPredicate()); c.removeIf(x -> x > 80); } @@ -95,7 +100,7 @@ public boolean contains(T x) { public boolean contains(Predicate p) { Iterator it = iterator(); while (it.hasNext()) { - if (p.apply(it.next())) + if (p.test(it.next())) return true; } return false; diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/BasicQueue.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/BasicQueue.java index 73c829a..b7a501c 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/BasicQueue.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/BasicQueue.java @@ -1,5 +1,7 @@ package it.unive.dais.po2.tinyjdk; +import java.util.function.Predicate; + public class BasicQueue implements Queue { private List l = new ArrayList<>(); @@ -24,6 +26,12 @@ public boolean contains(T x) { return false; } + @Override + public boolean contains(Predicate p) { + // TODO per casa + return false; + } + @Override public Iterator iterator() { return null; diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Collection.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Collection.java index aaa8583..78215a2 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Collection.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Collection.java @@ -1,5 +1,7 @@ package it.unive.dais.po2.tinyjdk; +import java.util.function.Predicate; + public interface Collection extends Iterable { int size(); diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Function.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Function.java deleted file mode 100644 index fac99e6..0000000 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Function.java +++ /dev/null @@ -1,6 +0,0 @@ -package it.unive.dais.po2.tinyjdk; - -@FunctionalInterface -public interface Function { - B apply(A x); -} diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/HashSet.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/HashSet.java index e18f470..42bc410 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/HashSet.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/HashSet.java @@ -1,5 +1,7 @@ package it.unive.dais.po2.tinyjdk; +import java.util.function.Predicate; + public class HashSet implements Set { private List l = new ArrayList<>(); @@ -33,6 +35,12 @@ public boolean contains(T x) { return false; } + @Override + public boolean contains(Predicate p) { + // TODO per casa + return false; + } + @Override public Iterator iterator() { return l.iterator(); diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/List.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/List.java index b60479b..5c1d9bc 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/List.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/List.java @@ -1,4 +1,5 @@ package it.unive.dais.po2.tinyjdk; +import java.util.function.Predicate; public interface List extends Collection { T get(int index); @@ -10,7 +11,7 @@ public interface List extends Collection { default T removeIf(Predicate p) { for (int i = 0; i < size(); ++i) { T x = get(i); - if (p.apply(x)) { + if (p.test(x)) { removeAt(i); return x; } diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Predicate.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Predicate.java deleted file mode 100644 index 0550cd0..0000000 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/Predicate.java +++ /dev/null @@ -1,4 +0,0 @@ -package it.unive.dais.po2.tinyjdk; - -public interface Predicate extends Function { -} diff --git a/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml index 7dc96a4..3a4be51 100644 --- a/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml +++ b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml @@ -2,21 +2,21 @@ - - - - - - - - - + + + + @@ -31,13 +31,27 @@ c, + Function f) { + Collection r = new ArrayList<>(); + for (A x : c) { + r.add(f.apply(x)); + } + return r; + } + + + public static void main(String[] args) { + List l = List.of(1, 2, 3); + Collection u = map(l, (Number x) -> 1); + + } + + public static class Animale { + public Animale accoppia(Animale a) {} + } + + public static class Gatto extends Animale { + + @Override + public Gatto accoppia(Object a) { /* implementazione diversa */ } + } + +} diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/sorting/Sorting.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/sorting/Sorting.java index d0b975d..bb120f5 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/sorting/Sorting.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/sorting/Sorting.java @@ -76,12 +76,15 @@ public static void main(String[] args) { Collections.sort(l2, (Shape o1, Shape o2) -> (int) (o1.perimeter() - o2.perimeter())); } -// static > void sort(List l) + //static > void sort(List l) - static void sort(List l, Comparator cmp) { - Collections.sort(l, cmp); - } + //static void sort(List l, Comparator cmp) { + // Collections.sort(l, cmp); + //} // void qsort(void* a, size_t len, int(*f)(void*, void*)) + + + } From 94e9f25d11b136df5727e618cb9096c6c953ce81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 4 May 2023 12:05:10 +0200 Subject: [PATCH 136/202] Fixata soluzione appello 30_5_19 esercizio 3 --- soluzione appelli/.idea/.gitignore | 3 +++ .../.idea/codeStyles/codeStyleConfig.xml | 5 +++++ soluzione appelli/.idea/misc.xml | 6 ++++++ soluzione appelli/.idea/modules.xml | 20 +++++++++++++++++++ soluzione appelli/.idea/soluzione appelli.iml | 9 +++++++++ soluzione appelli/.idea/vcs.xml | 6 ++++++ .../Scritto PO2 13 1 2023.iml | 11 ++++++++++ .../Scritto PO2 13 9 22.iml | 11 ++++++++++ .../Scritto PO2 20 6 19.iml | 9 --------- .../Scritto PO2 3 6 22/Scritto PO2 3 6 22.iml | 11 ++++++++++ .../Scritto PO2 30 5 19/.idea/.gitignore | 3 +++ .../.idea/Scritto PO2 30 5 19.iml | 9 +++++++++ .../.idea/codeStyles/codeStyleConfig.xml | 5 +++++ .../Scritto PO2 30 5 19/.idea/misc.xml | 4 ++++ .../Scritto PO2 30 5 19/.idea/modules.xml | 8 ++++++++ .../Scritto PO2 30 5 19/.idea/vcs.xml | 6 ++++++ .../Scritto PO2 30 5 19/src/Es3.java | 13 +++++++++++- .../Scritto PO2 4 9 18/Scritto PO2 4 9 18.iml | 9 --------- .../Scritto PO2 6 9 19/Scritto PO2 6 9 19.iml | 9 --------- 19 files changed, 129 insertions(+), 28 deletions(-) create mode 100644 soluzione appelli/.idea/.gitignore create mode 100644 soluzione appelli/.idea/codeStyles/codeStyleConfig.xml create mode 100644 soluzione appelli/.idea/misc.xml create mode 100644 soluzione appelli/.idea/modules.xml create mode 100644 soluzione appelli/.idea/soluzione appelli.iml create mode 100644 soluzione appelli/.idea/vcs.xml create mode 100644 soluzione appelli/Scritto PO2 13 1 2023/Scritto PO2 13 1 2023.iml create mode 100644 soluzione appelli/Scritto PO2 13 9 22/Scritto PO2 13 9 22.iml create mode 100644 soluzione appelli/Scritto PO2 3 6 22/Scritto PO2 3 6 22.iml create mode 100644 soluzione appelli/Scritto PO2 30 5 19/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 30 5 19/.idea/Scritto PO2 30 5 19.iml create mode 100644 soluzione appelli/Scritto PO2 30 5 19/.idea/codeStyles/codeStyleConfig.xml create mode 100644 soluzione appelli/Scritto PO2 30 5 19/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 30 5 19/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 30 5 19/.idea/vcs.xml diff --git a/soluzione appelli/.idea/.gitignore b/soluzione appelli/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/soluzione appelli/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/soluzione appelli/.idea/codeStyles/codeStyleConfig.xml b/soluzione appelli/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/soluzione appelli/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/.idea/misc.xml b/soluzione appelli/.idea/misc.xml new file mode 100644 index 0000000..09e91bf --- /dev/null +++ b/soluzione appelli/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/.idea/modules.xml b/soluzione appelli/.idea/modules.xml new file mode 100644 index 0000000..ea9d9fc --- /dev/null +++ b/soluzione appelli/.idea/modules.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/.idea/soluzione appelli.iml b/soluzione appelli/.idea/soluzione appelli.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/soluzione appelli/.idea/soluzione appelli.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/.idea/vcs.xml b/soluzione appelli/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/soluzione appelli/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 1 2023/Scritto PO2 13 1 2023.iml b/soluzione appelli/Scritto PO2 13 1 2023/Scritto PO2 13 1 2023.iml new file mode 100644 index 0000000..908ad4f --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 1 2023/Scritto PO2 13 1 2023.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 13 9 22/Scritto PO2 13 9 22.iml b/soluzione appelli/Scritto PO2 13 9 22/Scritto PO2 13 9 22.iml new file mode 100644 index 0000000..e13cb1e --- /dev/null +++ b/soluzione appelli/Scritto PO2 13 9 22/Scritto PO2 13 9 22.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 20 6 19/Scritto PO2 20 6 19.iml b/soluzione appelli/Scritto PO2 20 6 19/Scritto PO2 20 6 19.iml index 1a7f0f7..c90834f 100644 --- a/soluzione appelli/Scritto PO2 20 6 19/Scritto PO2 20 6 19.iml +++ b/soluzione appelli/Scritto PO2 20 6 19/Scritto PO2 20 6 19.iml @@ -7,14 +7,5 @@ - - - - - - - - - \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 3 6 22/Scritto PO2 3 6 22.iml b/soluzione appelli/Scritto PO2 3 6 22/Scritto PO2 3 6 22.iml new file mode 100644 index 0000000..e13cb1e --- /dev/null +++ b/soluzione appelli/Scritto PO2 3 6 22/Scritto PO2 3 6 22.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 30 5 19/.idea/.gitignore b/soluzione appelli/Scritto PO2 30 5 19/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/soluzione appelli/Scritto PO2 30 5 19/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/soluzione appelli/Scritto PO2 30 5 19/.idea/Scritto PO2 30 5 19.iml b/soluzione appelli/Scritto PO2 30 5 19/.idea/Scritto PO2 30 5 19.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/soluzione appelli/Scritto PO2 30 5 19/.idea/Scritto PO2 30 5 19.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 30 5 19/.idea/codeStyles/codeStyleConfig.xml b/soluzione appelli/Scritto PO2 30 5 19/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/soluzione appelli/Scritto PO2 30 5 19/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 30 5 19/.idea/misc.xml b/soluzione appelli/Scritto PO2 30 5 19/.idea/misc.xml new file mode 100644 index 0000000..ddb2beb --- /dev/null +++ b/soluzione appelli/Scritto PO2 30 5 19/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 30 5 19/.idea/modules.xml b/soluzione appelli/Scritto PO2 30 5 19/.idea/modules.xml new file mode 100644 index 0000000..c20025c --- /dev/null +++ b/soluzione appelli/Scritto PO2 30 5 19/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 30 5 19/.idea/vcs.xml b/soluzione appelli/Scritto PO2 30 5 19/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/soluzione appelli/Scritto PO2 30 5 19/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 30 5 19/src/Es3.java b/soluzione appelli/Scritto PO2 30 5 19/src/Es3.java index 03811f9..cd89349 100644 --- a/soluzione appelli/Scritto PO2 30 5 19/src/Es3.java +++ b/soluzione appelli/Scritto PO2 30 5 19/src/Es3.java @@ -13,6 +13,15 @@ public FactThread(int n) { this.n = n; } + private static int fact(int n) { + return n < 2 ? 1 : n * fact(n - 1); + } + + @Override + public void run() { + result = fact(n); + } + public int getResult() { return result; } @@ -23,7 +32,9 @@ public int getResult() { public static Collection multiFact(Collection ns) { Collection r = new ArrayList<>(); for (int n : ns) { - r.add(new FactThread(n)); + FactThread t = new FactThread(n); + t.start(); + r.add(t); } return r; } diff --git a/soluzione appelli/Scritto PO2 4 9 18/Scritto PO2 4 9 18.iml b/soluzione appelli/Scritto PO2 4 9 18/Scritto PO2 4 9 18.iml index 1a7f0f7..c90834f 100644 --- a/soluzione appelli/Scritto PO2 4 9 18/Scritto PO2 4 9 18.iml +++ b/soluzione appelli/Scritto PO2 4 9 18/Scritto PO2 4 9 18.iml @@ -7,14 +7,5 @@ - - - - - - - - - \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 6 9 19/Scritto PO2 6 9 19.iml b/soluzione appelli/Scritto PO2 6 9 19/Scritto PO2 6 9 19.iml index 1a7f0f7..c90834f 100644 --- a/soluzione appelli/Scritto PO2 6 9 19/Scritto PO2 6 9 19.iml +++ b/soluzione appelli/Scritto PO2 6 9 19/Scritto PO2 6 9 19.iml @@ -7,14 +7,5 @@ - - - - - - - - - \ No newline at end of file From 7ac5944beeb9bae23aa374093e0bbf3781bea98b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 8 May 2023 15:45:18 +0200 Subject: [PATCH 137/202] C++ --- 2021-22/LezioniCPP/LezioniCPP.vcxproj | 4 ++ 2021-22/LezioniCPP/LezioniCPP.vcxproj.filters | 8 +++ 2021-22/LezioniCPP/gp.ixx | 34 ++++++++++++ 2021-22/LezioniCPP/pizza.h | 10 ++++ 2021-22/LezioniCPP/preprocessor.cpp | 52 +++++++++++++++++++ 2021-22/LezioniCPP/zoo.ixx | 30 ++++++++++- .../it/unive/dais/po2/lambdas/Wildcards.java | 9 ---- 7 files changed, 137 insertions(+), 10 deletions(-) create mode 100644 2021-22/LezioniCPP/gp.ixx create mode 100644 2021-22/LezioniCPP/pizza.h create mode 100644 2021-22/LezioniCPP/preprocessor.cpp diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj index 3a67155..581d358 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -145,10 +145,14 @@ + + + + diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters index ef999c1..7cef7b1 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters @@ -36,5 +36,13 @@ Source Files + + Source Files + + + + + Header Files + \ No newline at end of file diff --git a/2021-22/LezioniCPP/gp.ixx b/2021-22/LezioniCPP/gp.ixx new file mode 100644 index 0000000..4b75ac3 --- /dev/null +++ b/2021-22/LezioniCPP/gp.ixx @@ -0,0 +1,34 @@ +export module gp; + +import ; +import ; + +template +T sum2(T a, T b) { + return a + b; +} + +template +C::value_type sum(const C& v) +{ + if (v.size() > 0) + { + C::value_type r(v[0]); + for (size_t i = 0; i < v.size(); ++i) + { + C::reference x(v[i]); + r += x; + } + return r; + } + return C::value_type(); + +} + +int main() +{ + std::vector v1{ 1, 2, 3 }; + int x = sum(v1); + +} + diff --git a/2021-22/LezioniCPP/pizza.h b/2021-22/LezioniCPP/pizza.h new file mode 100644 index 0000000..3f4fe77 --- /dev/null +++ b/2021-22/LezioniCPP/pizza.h @@ -0,0 +1,10 @@ + +#ifndef __PIZZA_H__ +#define __PIZZA_H__ + +void f(); +void g(); +void h(); + +#endif + diff --git a/2021-22/LezioniCPP/preprocessor.cpp b/2021-22/LezioniCPP/preprocessor.cpp new file mode 100644 index 0000000..84e41cf --- /dev/null +++ b/2021-22/LezioniCPP/preprocessor.cpp @@ -0,0 +1,52 @@ + +#include +#include + + +#define MYMACRO ciao ragazzi sono io + +#define MYMACRO2(a, b, c) (a + b + c) + +#ifdef MACRO +.... + +#else + +#endif + +#pragma + + +void swap_int(int* a, int* b) { + int tmp = *a; + *a = *b; + *b = tmp; +} + +void swap_double(double* a, double* b) { + double tmp = *a; + *a = *b; + *b = tmp; +} + +#define SWAP(T) \\ +void swap_T(T* a, T* b) { \\ + T tmp = *a; \\ + *a = *b; \\ + *b = tmp; \\ +} + +SWAP(int) +SWAP(double) +SWAP(char) +SWAP(unsigned long) +SWAP(mio nonno) + + + + + + + + + diff --git a/2021-22/LezioniCPP/zoo.ixx b/2021-22/LezioniCPP/zoo.ixx index a0571ab..a2cff84 100644 --- a/2021-22/LezioniCPP/zoo.ixx +++ b/2021-22/LezioniCPP/zoo.ixx @@ -12,7 +12,7 @@ export namespace zoo int weight_; public: - explicit animal(int w) : weight_(w) {} + animal(int w) : weight_(w) {} virtual ~animal() {} animal(const animal& a) : weight_(a.weight_) {} @@ -26,6 +26,34 @@ export namespace zoo int& weight() { return weight_; } }; + class dog : public animal + { + protected: + bool has_pedigree; + + public: + dog(int w, bool has_ped) : animal(w), has_pedigree(has_ped) {} + + }; + + animal f(animal a) { return a; } + + void main() + { + animal a(4); + animal* b(new animal(4)); + + animal c(a); + animal d(f(c)); + + animal u(a); + animal& w = a; + f(5); + } + + + + class dog : public animal { private: diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/lambdas/Wildcards.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/lambdas/Wildcards.java index dcf9551..2691f5a 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/lambdas/Wildcards.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/lambdas/Wildcards.java @@ -66,14 +66,5 @@ public static void main(String[] args) { } - public static class Animale { - public Animale accoppia(Animale a) {} - } - - public static class Gatto extends Animale { - - @Override - public Gatto accoppia(Object a) { /* implementazione diversa */ } - } } From 6e4d99d45e88d442ae40f8b443dbf06e689f0b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 8 May 2023 17:16:47 +0200 Subject: [PATCH 138/202] Sums --- 2021-22/LezioniCPP/LezioniCPP.vcxproj | 1 + 2021-22/LezioniCPP/LezioniCPP.vcxproj.filters | 3 ++ 2021-22/LezioniCPP/gp.ixx | 44 +++++++++++++++++-- 2021-22/LezioniCPP/sums.ixx | 4 +- 2021-22/LezioniCPP/zoo.ixx | 11 +---- 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj b/2021-22/LezioniCPP/LezioniCPP.vcxproj index 581d358..39dcd13 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj @@ -141,6 +141,7 @@ + diff --git a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters index 7cef7b1..4876e96 100644 --- a/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters +++ b/2021-22/LezioniCPP/LezioniCPP.vcxproj.filters @@ -39,6 +39,9 @@ Source Files + + Source Files + diff --git a/2021-22/LezioniCPP/gp.ixx b/2021-22/LezioniCPP/gp.ixx index 4b75ac3..46031a9 100644 --- a/2021-22/LezioniCPP/gp.ixx +++ b/2021-22/LezioniCPP/gp.ixx @@ -9,26 +9,62 @@ T sum2(T a, T b) { } template -C::value_type sum(const C& v) +typename C::value_type sum(const C& v) { if (v.size() > 0) { - C::value_type r(v[0]); + typename C::value_type r(v[0]); for (size_t i = 0; i < v.size(); ++i) { - C::reference x(v[i]); + typename C::reference x(v[i]); r += x; } return r; } return C::value_type(); +} + +template +typename std::iterator_traits::value_type +sum(InputIterator a, InputIterator b) +{ + typename std::iterator_traits::value_type r; + while (a != b) + { + r += *a++; + } + return r; +} + +template +auto sum(InputIterator a, InputIterator b) -> decltype(*a) +{ + auto r; + while (a != b) + { + r += *a++; + } + return r; } + + + + + int main() { std::vector v1{ 1, 2, 3 }; - int x = sum(v1); + int n = sum(v1.begin(), v1.end()); + char a[10]; + int m = sum(a, a + 10); + + int x = sum(v1); + std::vector v2{ 234, 456,4567, 7 }; + int y = sum(v2); + std::vector v3{ 0.0 }; + int y = sum(v3); } diff --git a/2021-22/LezioniCPP/sums.ixx b/2021-22/LezioniCPP/sums.ixx index 27fd68f..a4f4b24 100644 --- a/2021-22/LezioniCPP/sums.ixx +++ b/2021-22/LezioniCPP/sums.ixx @@ -14,7 +14,9 @@ export namespace sums { // sum con operazione di somma binaria passata come argomento template - std::iterator_traits::value_type sum(InputIterator first, InputIterator last, BinaryFunction f) + std::iterator_traits::value_type sum(InputIterator first, + InputIterator last, + BinaryFunction f) { typename std::iterator_traits::value_type z = *first++; while (first != last) diff --git a/2021-22/LezioniCPP/zoo.ixx b/2021-22/LezioniCPP/zoo.ixx index a2cff84..871a380 100644 --- a/2021-22/LezioniCPP/zoo.ixx +++ b/2021-22/LezioniCPP/zoo.ixx @@ -12,7 +12,7 @@ export namespace zoo int weight_; public: - animal(int w) : weight_(w) {} + explicit animal(int w) : weight_(w) {} virtual ~animal() {} animal(const animal& a) : weight_(a.weight_) {} @@ -26,15 +26,6 @@ export namespace zoo int& weight() { return weight_; } }; - class dog : public animal - { - protected: - bool has_pedigree; - - public: - dog(int w, bool has_ped) : animal(w), has_pedigree(has_ped) {} - - }; animal f(animal a) { return a; } From 2b053144d47349d52d48eb35ed5e2017c9d0d9b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 8 May 2023 17:54:16 +0200 Subject: [PATCH 139/202] Java material updated and cleaned up --- 2022-23/LezioniJava22-23/.idea/workspace.xml | 36 ++++++---- .../it/unive/dais/po2/lambdas/Wildcards.java | 70 ------------------- .../HIgherOrderFunctions.java} | 8 +-- .../dais/po2/{sorting => misc}/Sorting.java | 27 ++++--- .../it/unive/dais/po2/misc/Wildcards.java | 61 ++++++++++++++++ .../it/unive/dais/po2/{ => misc}/Zoo.java | 2 +- .../dais/po2/threading/ConsumerProducer.java | 2 +- .../threading/{Sample.java => Threads.java} | 12 ++-- .../po2/tinyjdk/nogenerics/NoGenerics.java | 4 +- .../LezioniJava22-23/.idea/workspace.xml | 6 +- 10 files changed, 112 insertions(+), 116 deletions(-) delete mode 100644 2022-23/LezioniJava22-23/it/unive/dais/po2/lambdas/Wildcards.java rename 2022-23/LezioniJava22-23/it/unive/dais/po2/{lambdas/Lambdas.java => misc/HIgherOrderFunctions.java} (95%) rename 2022-23/LezioniJava22-23/it/unive/dais/po2/{sorting => misc}/Sorting.java (72%) create mode 100644 2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Wildcards.java rename 2022-23/LezioniJava22-23/it/unive/dais/po2/{ => misc}/Zoo.java (96%) rename 2022-23/LezioniJava22-23/it/unive/dais/po2/threading/{Sample.java => Threads.java} (79%) diff --git a/2022-23/LezioniJava22-23/.idea/workspace.xml b/2022-23/LezioniJava22-23/.idea/workspace.xml index e4a0c0b..b0eb135 100644 --- a/2022-23/LezioniJava22-23/.idea/workspace.xml +++ b/2022-23/LezioniJava22-23/.idea/workspace.xml @@ -1,7 +1,20 @@ + + - + + + + + + + + + + + - - - - - - - + @@ -72,8 +84,8 @@ @@ -121,7 +133,7 @@ - file://$PROJECT_DIR$/it/unive/dais/po2/Zoo.java + file://$PROJECT_DIR$/it/unive/dais/po2/misc/Zoo.java 33 diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/lambdas/Wildcards.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/lambdas/Wildcards.java deleted file mode 100644 index 2691f5a..0000000 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/lambdas/Wildcards.java +++ /dev/null @@ -1,70 +0,0 @@ -package it.unive.dais.po2.lambdas; - -import it.unive.dais.po2.Zoo; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.function.Function; - -public class Wildcards { - - public static T identity(T x) { - return x; - } - - public static Object identity2(Object x) { - return x; - } - - public static Collection w1(Collection x, - Collection y) { - - } - - public static Collection w2(Collection x, - Collection y) { - - } - - public static Collection w3(Collection x, - Collection y) { - - } - - public static void main1(String[] args) { - List l = new ArrayList<>(); - Zoo.Dog fido = new Zoo.Dog(30, "red"); - l.add(fido); - Zoo.Animal pluto = l.get(0); - - List l2 = new ArrayList<>(); - l2.add(fido); - Object rex = l2.get(0); - } - - public static Integer f(Integer x) { - return x + 1; - } - - public static Integer f2(Number x) { - } - - public static Collection map(Collection c, - Function f) { - Collection r = new ArrayList<>(); - for (A x : c) { - r.add(f.apply(x)); - } - return r; - } - - - public static void main(String[] args) { - List l = List.of(1, 2, 3); - Collection u = map(l, (Number x) -> 1); - - } - - -} diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/lambdas/Lambdas.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/HIgherOrderFunctions.java similarity index 95% rename from 2022-23/LezioniJava22-23/it/unive/dais/po2/lambdas/Lambdas.java rename to 2022-23/LezioniJava22-23/it/unive/dais/po2/misc/HIgherOrderFunctions.java index fe9ae90..c6f447f 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/lambdas/Lambdas.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/HIgherOrderFunctions.java @@ -1,6 +1,4 @@ -package it.unive.dais.po2.lambdas; - -import it.unive.dais.po2.tinyjdk.Pair; +package it.unive.dais.po2.misc; import java.util.ArrayList; import java.util.Collection; @@ -8,7 +6,9 @@ import java.util.List; import java.util.function.*; -public class Lambdas { +public class HIgherOrderFunctions { + + // non serve riscrivere queste interfacce funzionali, ma le riportiamo per facilitare la consultazione /*interface Function { B apply(A x); diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/sorting/Sorting.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Sorting.java similarity index 72% rename from 2022-23/LezioniJava22-23/it/unive/dais/po2/sorting/Sorting.java rename to 2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Sorting.java index bb120f5..8093ebf 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/sorting/Sorting.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Sorting.java @@ -1,19 +1,18 @@ -package it.unive.dais.po2.sorting; +package it.unive.dais.po2.misc; import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; import java.util.List; public class Sorting { - public interface Shape /*extends Comparable*/ { + public interface Shape extends Comparable { double area(); double perimeter(); -/* default int compareTo(Shape o) { + default int compareTo(Shape o) { return (int) (this.area() - o.area()); - }*/ + } } public static class Circle implements Shape { @@ -23,7 +22,7 @@ public Circle(double r) { this.ray = r; } -/* @Override + @Override public int compareTo(Shape s) { if (s instanceof Circle) { Circle c = (Circle) s; @@ -32,7 +31,7 @@ public int compareTo(Shape s) { else { return (int) (area() - s.area()); } - }*/ + } @Override public double area() { @@ -66,23 +65,23 @@ public double perimeter() { public static void main(String[] args) { List l = List.of(4, 1, 38, -4, -567); - Collections.sort(l); + Collections.sort(l); // questa sort() sfrutta il fatto che gli Integer implementano Comparable List l2 = new ArrayList<>(); l2.add(new Circle(4.)); l2.add(new Circle(40.)); l2.add(new Circle(56.)); + Collections.sort(l2); // questa sort() funziona perché Circle implementa Shape che implementa Comparable + + // queste due sort() ignora il fatto che Cirlce implementi Comparable e usa il Comparator passato + // come secondo argomento: si badi che il Comparator vince sul Comparable Collections.sort(l2, (o1, o2) -> (int) (o1.ray - o2.ray)); Collections.sort(l2, (Shape o1, Shape o2) -> (int) (o1.perimeter() - o2.perimeter())); } + // le due Collections.sort hanno le seguenti firme (vedi doc del JDK) //static > void sort(List l) - - //static void sort(List l, Comparator cmp) { - // Collections.sort(l, cmp); - //} - - // void qsort(void* a, size_t len, int(*f)(void*, void*)) + //static void sort(List l, Comparator cmp) diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Wildcards.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Wildcards.java new file mode 100644 index 0000000..e63e768 --- /dev/null +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Wildcards.java @@ -0,0 +1,61 @@ +package it.unive.dais.po2.misc; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.function.Function; + +public class Wildcards { + + // esempi di funzione identica + public static T identity(T x) { + return x; + } // polimorfa tramite generics + public static Object identity2(Object x) { + return x; + } // polimorfa tramite subtyping + + // esempi di wildcard + public static Collection w1(Collection x) { + return x; // ? extends Number è sottotipo di ? + } + + public static Collection w2(Collection x) { + return x; // X è un Number, quindi è sottotipo di ? + } + + public static Collection w3(Collection x) { + return x; // + } + + // riscriviamo ora la funzione di ordine superiore map() che avevamo scritto nel file HigherOrderFunctions.java + // usiamo i wildcard per ampliare le potenzialità della funzione passata come secondo parametro + // permettendo la controvarianza del dominio e la co-varianza del co-dominio + public static Collection map(Collection c, + Function f) { + Collection r = new ArrayList<>(); + for (A x : c) { + r.add(f.apply(x)); + } + return r; + } + + + public static void main(String[] args) { + // esempio di chiamata della nuova versione della map() + List l = List.of(1, 2, 3); + Collection u = map(l, (Number x) -> 1); // passiamo una lambda che ha tipo Function nonostante i generic A = Integer e B = Number + + // altri esempi con i wildcard: cosa è possibile fare e non fare + List l1 = new ArrayList<>(); + Zoo.Dog fido = new Zoo.Dog(30, "red"); + //l.add(fido); // non si può chiamare add() perché Dog non è sottotipo di ? extends Animal + Zoo.Animal pluto = l1.get(0); + + List l2 = new ArrayList<>(); + l2.add(fido); + //Zoo.Dog rex = l2.get(0); // non si può gettare perché Dog non è supertipo di ? super Dog + } + + +} diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/Zoo.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Zoo.java similarity index 96% rename from 2022-23/LezioniJava22-23/it/unive/dais/po2/Zoo.java rename to 2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Zoo.java index dfe6c29..9d1bd22 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/Zoo.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Zoo.java @@ -1,4 +1,4 @@ -package it.unive.dais.po2; +package it.unive.dais.po2.misc; public class Zoo { diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/threading/ConsumerProducer.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/threading/ConsumerProducer.java index 4ca8ba5..5e1547d 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/threading/ConsumerProducer.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/threading/ConsumerProducer.java @@ -7,7 +7,7 @@ public class ConsumerProducer { - private static List l = new ArrayList<>(); + private static final List l = new ArrayList<>(); public static class Producer extends Thread { @Override diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/threading/Sample.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/threading/Threads.java similarity index 79% rename from 2022-23/LezioniJava22-23/it/unive/dais/po2/threading/Sample.java rename to 2022-23/LezioniJava22-23/it/unive/dais/po2/threading/Threads.java index 75ed83d..dabc622 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/threading/Sample.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/threading/Threads.java @@ -2,11 +2,9 @@ import java.util.Random; -public class Sample { +public class Threads { - private static Random rnd = new Random(); - - Object myMutex = new Object(); + private static final Random rnd = new Random(); private static int rand(int a, int b) { synchronized (rnd) { @@ -22,11 +20,9 @@ private static void loop() { } catch (InterruptedException e) {} System.out.println("ciao sono "); - System.out.println("il thread #" - + Thread.currentThread().getId()); + System.out.println("il thread #" + Thread.currentThread().getId()); } } - } public static class MyThread extends Thread { @@ -40,7 +36,7 @@ public static void main(String[] args) { Thread t1 = new MyThread(); t1.start(); - new Thread(Sample::loop).start(); + new Thread(Threads::loop).start(); new MyThread().start(); new MyThread().start(); new MyThread().start(); diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/nogenerics/NoGenerics.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/nogenerics/NoGenerics.java index 3bd56cb..50aef27 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/nogenerics/NoGenerics.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/tinyjdk/nogenerics/NoGenerics.java @@ -1,6 +1,8 @@ package it.unive.dais.po2.tinyjdk.nogenerics; -import it.unive.dais.po2.Zoo; +import it.unive.dais.po2.misc.Zoo; + +// in questo file mostriamo come si faceva una volta, prima che Java 5 introducesse i generics public class NoGenerics { diff --git a/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml index 11ee13c..e4a0c0b 100644 --- a/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml +++ b/2022-23/LezioniJava22-23/out/production/LezioniJava22-23/.idea/workspace.xml @@ -1,11 +1,7 @@ - - - - - + - - - - - - - - it, Function f) { + return new Iterator<>() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + private class Future implements Supplier { + @Nullable + private B r; + @NotNull + private final Thread t; + + public Future(Supplier f) { + t = new Thread(() -> { r = f.get(); }); + } + + @Override + @NotNull + public B get() { + try { + t.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return r; + } + } + + @Override + public Supplier next() { + A a = it.next(); + return new Future(() -> f.apply(a)); + } + }; + } + + // 1.b + static > void sortLists(Iterable> c) { + Iterator>> it = asyncIterator(c.iterator(), (l) -> { Collections.sort(l); return l; }); + while (it.hasNext()) { + Supplier> f = it.next(); + System.out.println(f.get()); // questo non è davvero necessario, ma + } + } + +} \ No newline at end of file From 140bd2477f6a85ae87608447a978ab4bc619b9a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 29 Sep 2023 16:22:15 +0200 Subject: [PATCH 157/202] Update Main.java --- soluzione appelli/Scritto PO2 5 9 23/src/Main.java | 1 + 1 file changed, 1 insertion(+) diff --git a/soluzione appelli/Scritto PO2 5 9 23/src/Main.java b/soluzione appelli/Scritto PO2 5 9 23/src/Main.java index 2dd162b..ddaf336 100644 --- a/soluzione appelli/Scritto PO2 5 9 23/src/Main.java +++ b/soluzione appelli/Scritto PO2 5 9 23/src/Main.java @@ -25,6 +25,7 @@ private class Future implements Supplier { public Future(Supplier f) { t = new Thread(() -> { r = f.get(); }); + t.start(); } @Override From 494da6c2d7eda01854d92320bc3b75d58409ceb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 11 Jan 2024 12:41:39 +0100 Subject: [PATCH 158/202] Soluzione appello 10/1/24 --- .../Scritto PO2 10 1 24/.gitignore | 29 ++++ .../Scritto PO2 10 1 24/.idea/.gitignore | 3 + .../Scritto PO2 10 1 24/.idea/misc.xml | 6 + .../Scritto PO2 10 1 24/.idea/modules.xml | 8 ++ .../Scritto PO2 10 1 24/.idea/uiDesigner.xml | 124 ++++++++++++++++ .../Scritto PO2 10 1 24.iml | 11 ++ .../Scritto PO2 10 1 24/cpp/Es2.cpp | 83 +++++++++++ .../cpp/Scritto PO2 10 9 24.sln | 31 ++++ .../cpp/Scritto PO2 10 9 24.vcxproj | 135 ++++++++++++++++++ .../cpp/Scritto PO2 10 9 24.vcxproj.filters | 22 +++ .../cpp/Scritto PO2 10 9 24.vcxproj.user | 4 + .../Scritto PO2 10 1 24/java/.idea/.gitignore | 3 + .../java/.idea/codeStyles/Project.xml | 7 + .../java/.idea/codeStyles/codeStyleConfig.xml | 5 + .../Scritto PO2 10 1 24/java/.idea/misc.xml | 6 + .../java/.idea/modules.xml | 8 ++ .../Scritto PO2 10 1 24/java/src/Es1.java | 71 +++++++++ 17 files changed, 556 insertions(+) create mode 100644 soluzione appelli/Scritto PO2 10 1 24/.gitignore create mode 100644 soluzione appelli/Scritto PO2 10 1 24/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 10 1 24/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 10 1 24/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 10 1 24/.idea/uiDesigner.xml create mode 100644 soluzione appelli/Scritto PO2 10 1 24/Scritto PO2 10 1 24.iml create mode 100644 soluzione appelli/Scritto PO2 10 1 24/cpp/Es2.cpp create mode 100644 soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.sln create mode 100644 soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj create mode 100644 soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj.filters create mode 100644 soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj.user create mode 100644 soluzione appelli/Scritto PO2 10 1 24/java/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 10 1 24/java/.idea/codeStyles/Project.xml create mode 100644 soluzione appelli/Scritto PO2 10 1 24/java/.idea/codeStyles/codeStyleConfig.xml create mode 100644 soluzione appelli/Scritto PO2 10 1 24/java/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 10 1 24/java/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 10 1 24/java/src/Es1.java diff --git a/soluzione appelli/Scritto PO2 10 1 24/.gitignore b/soluzione appelli/Scritto PO2 10 1 24/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/.idea/.gitignore b/soluzione appelli/Scritto PO2 10 1 24/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/soluzione appelli/Scritto PO2 10 1 24/.idea/misc.xml b/soluzione appelli/Scritto PO2 10 1 24/.idea/misc.xml new file mode 100644 index 0000000..09e91bf --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/.idea/modules.xml b/soluzione appelli/Scritto PO2 10 1 24/.idea/modules.xml new file mode 100644 index 0000000..e660639 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/.idea/uiDesigner.xml b/soluzione appelli/Scritto PO2 10 1 24/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/Scritto PO2 10 1 24.iml b/soluzione appelli/Scritto PO2 10 1 24/Scritto PO2 10 1 24.iml new file mode 100644 index 0000000..e13cb1e --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/Scritto PO2 10 1 24.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/cpp/Es2.cpp b/soluzione appelli/Scritto PO2 10 1 24/cpp/Es2.cpp new file mode 100644 index 0000000..67f5fa3 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/cpp/Es2.cpp @@ -0,0 +1,83 @@ +// Scritto PO2 10 9 24.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include +#include +#include +#include + +using namespace std; + +// 2.a +template +void map(InputIterator from, InputIterator to, OutputIterator out, function f) +{ + while (from != to) + *out++ = f(*from++); +} + +// 2.b +template +vector map(const vector& v, function f) +{ + vector r(v.size()); + map(v.begin(), v.end(), r.begin(), f); + return r; +} + +// 2.c +namespace cpp03 { + + // 2.c.i + template + vector map(const vector& v, F f) + { + vector r(v.size()); + for (int i = 0; i < v.size(); ++i) + r[i] = f(v[i]); + return r; + } + + // 2.c.ii + // no, non possono coesistere, perché sarebbero overload ambigui. Per questo motivo ho dovuto metterli in un namespace a parte. + + // 2.c.iii + // bisogna usare i function object, cioè oggetti per cui è definito l'operatore di applicazione operator() + // esempio: + class myfunction { + public: + bool operator()(int n) { return n > 2; } + }; + + void test() + { + vector v1{ 1, 2, 3, 4, 5 }; + vector v2 = map(v1, myfunction()); // senza le annotazioni esplicite dei template argument non compila (questo non è richiesto nell'esame perché è una cosa molto sottile) + } +} + +// main for testing +// + +template +ostream& operator<<(ostream& os, const vector& v) +{ + os << "[ "; + for (auto it = v.begin(); it != v.end(); ++it) + os << *it << ", "; + os << "\b\b ]"; + return os; +} + +int main() +{ + vector v1{ 1, 2, 3, 4, 5 }; + vector v2(v1.size()); + map(v1.begin(), v1.end(), v2.begin(), [](int n) { return n > 2; }); + + v2 = map(v1, [](int n) { return n > 2; }); // anche qui bisogna mettere i template argument espliciti + + cout << v1 << endl << v2 << endl; + + return 0; +} \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.sln b/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.sln new file mode 100644 index 0000000..ecf3557 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33829.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Scritto PO2 10 9 24", "Scritto PO2 10 9 24.vcxproj", "{03C631EE-30AC-4D4D-A5BA-A31FD0C40886}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {03C631EE-30AC-4D4D-A5BA-A31FD0C40886}.Debug|x64.ActiveCfg = Debug|x64 + {03C631EE-30AC-4D4D-A5BA-A31FD0C40886}.Debug|x64.Build.0 = Debug|x64 + {03C631EE-30AC-4D4D-A5BA-A31FD0C40886}.Debug|x86.ActiveCfg = Debug|Win32 + {03C631EE-30AC-4D4D-A5BA-A31FD0C40886}.Debug|x86.Build.0 = Debug|Win32 + {03C631EE-30AC-4D4D-A5BA-A31FD0C40886}.Release|x64.ActiveCfg = Release|x64 + {03C631EE-30AC-4D4D-A5BA-A31FD0C40886}.Release|x64.Build.0 = Release|x64 + {03C631EE-30AC-4D4D-A5BA-A31FD0C40886}.Release|x86.ActiveCfg = Release|Win32 + {03C631EE-30AC-4D4D-A5BA-A31FD0C40886}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {64686522-43E3-4D48-A3A3-15FF5D93AE3F} + EndGlobalSection +EndGlobal diff --git a/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj b/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj new file mode 100644 index 0000000..8a992d4 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {03c631ee-30ac-4d4d-a5ba-a31fd0c40886} + ScrittoPO210924 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj.filters b/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj.filters new file mode 100644 index 0000000..2833605 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj.user b/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/cpp/Scritto PO2 10 9 24.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/java/.idea/.gitignore b/soluzione appelli/Scritto PO2 10 1 24/java/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/java/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/soluzione appelli/Scritto PO2 10 1 24/java/.idea/codeStyles/Project.xml b/soluzione appelli/Scritto PO2 10 1 24/java/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..919ce1f --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/java/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/java/.idea/codeStyles/codeStyleConfig.xml b/soluzione appelli/Scritto PO2 10 1 24/java/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/java/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/java/.idea/misc.xml b/soluzione appelli/Scritto PO2 10 1 24/java/.idea/misc.xml new file mode 100644 index 0000000..d15472f --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/java/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/java/.idea/modules.xml b/soluzione appelli/Scritto PO2 10 1 24/java/.idea/modules.xml new file mode 100644 index 0000000..e660639 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/java/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 1 24/java/src/Es1.java b/soluzione appelli/Scritto PO2 10 1 24/java/src/Es1.java new file mode 100644 index 0000000..2d4319a --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 1 24/java/src/Es1.java @@ -0,0 +1,71 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.function.Function; + +public class Es1 { + + // 1.a + public static class FactorialThread extends Thread { + private final int n; + private long res; + + public FactorialThread(int n) { + this.n = n; + } + + @Override + public void run() { + res = fact(n); + } + + public long getResult() { + try { + join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return res; + } + + public int getN() { + return n; + } + + private static long fact(int n) { + if (n <= 1) return 1; + return n * fact(n - 1); + } + + } + + // 1.b + 1.c + public static List parallelFactorial(Iterable c) { + List r = new ArrayList<>(); + for (int n : c) { + FactorialThread t = new FactorialThread(n); + t.start(); + r.add(t); + } + return r; + } + + // 1.d + public static void main(String[] args) { + for (FactorialThread t : parallelFactorial(List.of(0, 1, 2, 3, 11, 12, 23, 35))) // chiamare anche parallelFactorial2() per provarla + System.out.printf("fact(%d) = %d\n", t.getN(), t.getResult()); + } + + // 1.e.i + public static List map(Iterable i, Function f) { + List r = new ArrayList<>(); + for (A a : i) + r.add(f.apply(a)); + return r; + } + + // 1.e.ii + public static Collection parallelFactorial2(Collection c) { + return map(c, (n) -> { FactorialThread t = new FactorialThread(n); t.start(); return t; }); + } +} \ No newline at end of file From 3501a2215b2e74dc4bc28fadf577a3a153743b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 22 Jan 2024 12:17:07 +0100 Subject: [PATCH 159/202] Update Es2.cpp --- soluzione appelli/Scritto PO2 10 1 24/cpp/Es2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/soluzione appelli/Scritto PO2 10 1 24/cpp/Es2.cpp b/soluzione appelli/Scritto PO2 10 1 24/cpp/Es2.cpp index 67f5fa3..5f31a09 100644 --- a/soluzione appelli/Scritto PO2 10 1 24/cpp/Es2.cpp +++ b/soluzione appelli/Scritto PO2 10 1 24/cpp/Es2.cpp @@ -39,7 +39,7 @@ namespace cpp03 { } // 2.c.ii - // no, non possono coesistere, perché sarebbero overload ambigui. Per questo motivo ho dovuto metterli in un namespace a parte. + // no, non possono coesistere, perché sarebbero overload ambigui. Infatti ho dovuto metterli in un sotto-namespace a parte per far compilare questo sorgente. // 2.c.iii // bisogna usare i function object, cioè oggetti per cui è definito l'operatore di applicazione operator() From 2f3d5c6c6293104e0685496014eb8675444d75e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 21 Feb 2024 14:03:15 +0100 Subject: [PATCH 160/202] Lezioni --- 2022-23/LezioniCPP22-23/smart_ptr.ixx | 77 +++++------ 2022-23/LezioniJava22-23/.idea/workspace.xml | 2 +- .../it/unive/dais/po2/misc/Zoo.java | 2 - 2023-24/LezioniJava23-24/.gitignore | 29 ++++ 2023-24/LezioniJava23-24/.idea/.gitignore | 3 + 2023-24/LezioniJava23-24/.idea/misc.xml | 6 + 2023-24/LezioniJava23-24/.idea/modules.xml | 8 ++ 2023-24/LezioniJava23-24/.idea/uiDesigner.xml | 124 ++++++++++++++++++ 2023-24/LezioniJava23-24/.idea/vcs.xml | 6 + 2023-24/LezioniJava23-24/LezioniJava23-24.iml | 11 ++ 2023-24/LezioniJava23-24/src/Misc1.java | 40 ++++++ 2023-24/LezioniJava23-24/src/ProveJDK1.java | 26 ++++ 2023-24/LezioniJava23-24/src/Zoo.java | 45 +++++++ .../src/tinyjdk/ArrayList.java | 72 ++++++++++ .../src/tinyjdk/Collection.java | 17 +++ .../src/tinyjdk/Iterable.java | 6 + .../src/tinyjdk/Iterator.java | 6 + .../LezioniJava23-24/src/tinyjdk/List.java | 8 ++ 18 files changed, 444 insertions(+), 44 deletions(-) create mode 100644 2023-24/LezioniJava23-24/.gitignore create mode 100644 2023-24/LezioniJava23-24/.idea/.gitignore create mode 100644 2023-24/LezioniJava23-24/.idea/misc.xml create mode 100644 2023-24/LezioniJava23-24/.idea/modules.xml create mode 100644 2023-24/LezioniJava23-24/.idea/uiDesigner.xml create mode 100644 2023-24/LezioniJava23-24/.idea/vcs.xml create mode 100644 2023-24/LezioniJava23-24/LezioniJava23-24.iml create mode 100644 2023-24/LezioniJava23-24/src/Misc1.java create mode 100644 2023-24/LezioniJava23-24/src/ProveJDK1.java create mode 100644 2023-24/LezioniJava23-24/src/Zoo.java create mode 100644 2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java create mode 100644 2023-24/LezioniJava23-24/src/tinyjdk/Collection.java create mode 100644 2023-24/LezioniJava23-24/src/tinyjdk/Iterable.java create mode 100644 2023-24/LezioniJava23-24/src/tinyjdk/Iterator.java create mode 100644 2023-24/LezioniJava23-24/src/tinyjdk/List.java diff --git a/2022-23/LezioniCPP22-23/smart_ptr.ixx b/2022-23/LezioniCPP22-23/smart_ptr.ixx index 7156d4d..424b3aa 100644 --- a/2022-23/LezioniCPP22-23/smart_ptr.ixx +++ b/2022-23/LezioniCPP22-23/smart_ptr.ixx @@ -1,22 +1,27 @@ export module smart_ptr; import ; +import ; +import ; -template +template ? std::extent_v : 1)> class smart_ptr { private: + using T = std::remove_extent_t; + T* pt; ptrdiff_t offset; size_t* cnt; - bool is_array; + + using self = smart_ptr; void dec() { --(*cnt); if (*cnt == 0) { - if (is_array) delete[] pt; + if constexpr (Len >= 1) delete[] pt; else delete pt; delete cnt; } @@ -27,18 +32,16 @@ private: ++(*cnt); } - smart_ptr(T* pt_, ptrdiff_t offset_, size_t* cnt_, bool is_array_) - : pt(pt_), offset(offset_), cnt(cnt_), is_array(is_array_) { + smart_ptr(T* pt_, ptrdiff_t offset_, size_t* cnt_) + : pt(pt_), offset(offset_), cnt(cnt_) { inc(); } public: - smart_ptr(T* p, bool is_array_) - : pt(p), offset(0), cnt(new size_t(1)), is_array(is_array_) {} + explicit smart_ptr(T* p) + : pt(p), offset(0), cnt(new size_t(1)) {} - explicit smart_ptr(T* p) : smart_ptr(p, false) {} - - smart_ptr(const smart_ptr& p) + smart_ptr(const self& p) : pt(p.pt), cnt(p.cnt), offset(p.offset) { inc(); @@ -49,7 +52,7 @@ public: dec(); } - smart_ptr& operator=(const smart_ptr& p) + self& operator=(const self& p) { if (pt != p.pt) { @@ -57,7 +60,6 @@ public: pt = p.pt; cnt = p.cnt; offset = p.offset; - is_array = p.is_array; inc(); } return *this; @@ -65,7 +67,7 @@ public: T& operator*() { - return pt[offset]; + return const_cast(*std::as_const(*this)); } const T& operator*() const @@ -73,84 +75,85 @@ public: return pt[offset]; } - bool operator==(const smart_ptr& p) const + bool operator==(const self& p) const { return pt == p.pt && offset == p.offset; } - bool operator!=(const smart_ptr& p) const + bool operator!=(const self& p) const { return !(*this == p); } operator T*() { - return pt + offset; + return const_cast(std::as_const(*this).operator const T*()); } - operator const T* () const + operator const T*() const { return pt + offset; } - smart_ptr operator+(ptrdiff_t d) const + self operator+(ptrdiff_t d) const { return smart_ptr(pt, offset + d, cnt, is_array); } - smart_ptr operator-(ptrdiff_t d) const + self operator-(ptrdiff_t d) const { return *this + (-d); } - smart_ptr& operator+=(ptrdiff_t d) + self& operator+=(ptrdiff_t d) { offset += d; return *this; } - smart_ptr& operator-=(ptrdiff_t d) + self& operator-=(ptrdiff_t d) { return *this += -d; } - smart_ptr& operator++() + self& operator++() { return *this += 1; } - smart_ptr operator++(int) + self operator++(int) { - smart_ptr r(*this); + self r(*this); ++(*this); return r; } - smart_ptr& operator--() + self& operator--() { return *this -= 1; } - smart_ptr operator--(int) + self operator--(int) { - smart_ptr r(*this); + self r(*this); --(*this); return r; } - T& operator[](size_t i) + T& operator[](int i) { - return pt[offset + i]; + return const_cast(std::as_const(*this)[i]); } - const T& operator[](size_t i) const + const T& operator[](int i) const { + assert(offset + i < Len && offset + i >= 0); return pt[offset + i]; } T* operator->() { - return pt + offset; + return const_cast(std::as_const(*this).operator->()); } const T* operator->() const @@ -185,17 +188,9 @@ struct S { export void test() { - int* a = new int[10]; - demo(a); - - smart_ptr b(new int(23)); - demo(b); - S* s1 = new S[10]; - int n1 = s1->a + 1; - smart_ptr s2(s1, true); - S s3(s2[4]); - s3 = s3; + smart_ptr a(new int(1)); + smart_ptr a(new int[20]); } diff --git a/2022-23/LezioniJava22-23/.idea/workspace.xml b/2022-23/LezioniJava22-23/.idea/workspace.xml index ddb17f3..cade2b9 100644 --- a/2022-23/LezioniJava22-23/.idea/workspace.xml +++ b/2022-23/LezioniJava22-23/.idea/workspace.xml @@ -125,7 +125,7 @@ file://$PROJECT_DIR$/it/unive/dais/po2/misc/Zoo.java - 33 + 31 diff --git a/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Zoo.java b/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Zoo.java index 9d1bd22..275d7e5 100644 --- a/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Zoo.java +++ b/2022-23/LezioniJava22-23/it/unive/dais/po2/misc/Zoo.java @@ -1,8 +1,6 @@ package it.unive.dais.po2.misc; public class Zoo { - - public static class Animal { protected int weight; diff --git a/2023-24/LezioniJava23-24/.gitignore b/2023-24/LezioniJava23-24/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/2023-24/LezioniJava23-24/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/2023-24/LezioniJava23-24/.idea/.gitignore b/2023-24/LezioniJava23-24/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/2023-24/LezioniJava23-24/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/2023-24/LezioniJava23-24/.idea/misc.xml b/2023-24/LezioniJava23-24/.idea/misc.xml new file mode 100644 index 0000000..d15472f --- /dev/null +++ b/2023-24/LezioniJava23-24/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2023-24/LezioniJava23-24/.idea/modules.xml b/2023-24/LezioniJava23-24/.idea/modules.xml new file mode 100644 index 0000000..aea0ebf --- /dev/null +++ b/2023-24/LezioniJava23-24/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/2023-24/LezioniJava23-24/.idea/uiDesigner.xml b/2023-24/LezioniJava23-24/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/2023-24/LezioniJava23-24/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/2023-24/LezioniJava23-24/.idea/vcs.xml b/2023-24/LezioniJava23-24/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/2023-24/LezioniJava23-24/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2023-24/LezioniJava23-24/LezioniJava23-24.iml b/2023-24/LezioniJava23-24/LezioniJava23-24.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/2023-24/LezioniJava23-24/LezioniJava23-24.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/2023-24/LezioniJava23-24/src/Misc1.java b/2023-24/LezioniJava23-24/src/Misc1.java new file mode 100644 index 0000000..320e9db --- /dev/null +++ b/2023-24/LezioniJava23-24/src/Misc1.java @@ -0,0 +1,40 @@ +public class Misc1 { + + public interface I { + void a(); + void b(); + default void c() { + a(); + b(); + } + } + + public static abstract class J { + public abstract void a(); + public abstract void b(); + public void c() { + a(); + b(); + } + } + + + + + public static class C implements I { + + @Override + public void a() { + + } + + @Override + public void b() { + + } + } + + public static void main(String[] args) { + I o = new C(); + } +} diff --git a/2023-24/LezioniJava23-24/src/ProveJDK1.java b/2023-24/LezioniJava23-24/src/ProveJDK1.java new file mode 100644 index 0000000..72ce414 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/ProveJDK1.java @@ -0,0 +1,26 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +public class ProveJDK1 { + + public static void main(String[] args) { + + List l = new ArrayList(); + l.add(21); + l.add(2); + l.add(456); + + Iterator it = l.iterator(); + while (it.hasNext()) { + int n = it.next(); + System.out.println(n); + } + + for (int i = 0; i < l.size(); ++i) { + int n = l.get(i); + System.out.println(n); + } + } +} diff --git a/2023-24/LezioniJava23-24/src/Zoo.java b/2023-24/LezioniJava23-24/src/Zoo.java new file mode 100644 index 0000000..de9bcb2 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/Zoo.java @@ -0,0 +1,45 @@ +public class Zoo { + + public static class Animal { + protected int weight; + + public Animal(int w) { + this.weight = w; + } + + public void eat(Animal a) { + this.weight += a.weight; + } + } + + public static class Dog extends Animal { + private boolean pedgree; + + public Dog(int w, boolean ped) { + super(w); + this.pedgree = ped; + } + + public void bark() { + System.out.println("bau!"); + } + @Override + public void eat(Animal a) { + this.weight += a.weight * 2; + } + } + + // SUBSUMPTION + + public static void main(String[] args) + { + Dog fido = new Dog(30, false); + Dog gigio = fido; + Animal pluto = new Dog(40, true); + pluto.eat(fido); + + pluto = gigio; + pluto.eat(gigio); + } + +} \ No newline at end of file diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java new file mode 100644 index 0000000..532dac9 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java @@ -0,0 +1,72 @@ +package tinyjdk; + +public class ArrayList implements List { + private Object[] a; + private int sz; + + public ArrayList() { + this.a = new Object[10]; + sz = 0; + } + + @Override + public void add(T x) { + if (sz >= a.length) { + Object[] old = a; + a = new Object[a.length * 2]; + for (int i = 0; i < old.length; ++i) + a[i] = old[i]; + } + a[sz++] = x; + } + + @Override + public void clear() { + sz = 0; + } + + @Override + public boolean contains(T x) { + return false; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public void remove(T x) { + + } + + @Override + public int size() { + return 0; + } + + @Override + public Iterator iterator() { + return null; + } + + @Override + public T get(int i) { + return (T) a[i]; + } + + @Override + public T set(int i, T x) { + return null; + } + + @Override + public void add(int i, T x) { + + } + + @Override + public T remove(int i) { + return null; + } +} diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/Collection.java b/2023-24/LezioniJava23-24/src/tinyjdk/Collection.java new file mode 100644 index 0000000..5986f9a --- /dev/null +++ b/2023-24/LezioniJava23-24/src/tinyjdk/Collection.java @@ -0,0 +1,17 @@ +package tinyjdk; + +public interface Collection extends Iterable { + void add(T x); + + default void addAll(Collection c) { + Iterator it = c.iterator(); + while (it.hasNext()) { + add(it.next()); + } + } + void clear(); + boolean contains(T x); + boolean isEmpty(); + void remove(T x); + int size(); +} diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/Iterable.java b/2023-24/LezioniJava23-24/src/tinyjdk/Iterable.java new file mode 100644 index 0000000..140cd2c --- /dev/null +++ b/2023-24/LezioniJava23-24/src/tinyjdk/Iterable.java @@ -0,0 +1,6 @@ +package tinyjdk; + +public interface Iterable { + Iterator iterator(); +} + diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/Iterator.java b/2023-24/LezioniJava23-24/src/tinyjdk/Iterator.java new file mode 100644 index 0000000..b80a1b9 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/tinyjdk/Iterator.java @@ -0,0 +1,6 @@ +package tinyjdk; + +public interface Iterator { + boolean hasNext(); + E next(); +} diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/List.java b/2023-24/LezioniJava23-24/src/tinyjdk/List.java new file mode 100644 index 0000000..5271ddb --- /dev/null +++ b/2023-24/LezioniJava23-24/src/tinyjdk/List.java @@ -0,0 +1,8 @@ +package tinyjdk; + +public interface List extends Collection { + T get(int i); + T set(int i, T x); + void add(int i, T x); + T remove(int i); +} From 82fc008824f88e9e895cf41f5a05324c92c85395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 22 Feb 2024 12:08:06 +0100 Subject: [PATCH 161/202] TinyJDK 2 --- .../src/tinyjdk/ArrayList.java | 29 +++++++++++++++---- .../tinyjdk/IndexOutOfBoundsException.java | 8 +++++ .../LezioniJava23-24/src/tinyjdk/Main.java | 7 +++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 2023-24/LezioniJava23-24/src/tinyjdk/IndexOutOfBoundsException.java create mode 100644 2023-24/LezioniJava23-24/src/tinyjdk/Main.java diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java index 532dac9..2a19dcb 100644 --- a/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java +++ b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java @@ -32,7 +32,7 @@ public boolean contains(T x) { @Override public boolean isEmpty() { - return false; + return sz == 0; } @Override @@ -42,22 +42,39 @@ public void remove(T x) { @Override public int size() { - return 0; + return sz; } + private static class MyIterator implements Iterator { + private int pos = 0; + @Override + public boolean hasNext() { + return pos < size(); + } + @Override + public T next() { + return get(pos++); + } + } @Override public Iterator iterator() { - return null; + return new MyIterator(); } - @Override public T get(int i) { - return (T) a[i]; + if (i < sz) + return (T) a[i]; + throw new RuntimeException(String.format("ArrayList.get(): index %d out of bounds %d", i, sz)); } @Override public T set(int i, T x) { - return null; + if (i < sz) { + T old = get(i); + a[i] = x; + return old; + } + throw new RuntimeException(String.format("ArrayList.set(): index %d out of bounds %d", i, sz)); } @Override diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/IndexOutOfBoundsException.java b/2023-24/LezioniJava23-24/src/tinyjdk/IndexOutOfBoundsException.java new file mode 100644 index 0000000..b198f18 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/tinyjdk/IndexOutOfBoundsException.java @@ -0,0 +1,8 @@ +package tinyjdk; + +public class IndexOutOfBoundsException extends Exception { + public IndexOutOfBoundsException(String msg) { + super(msg); + } +} + diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/Main.java b/2023-24/LezioniJava23-24/src/tinyjdk/Main.java new file mode 100644 index 0000000..05c3836 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/tinyjdk/Main.java @@ -0,0 +1,7 @@ +package tinyjdk; + +public class Main { + + public static void main(String[] args) { + } +} From 3b018c2d44662e84e0a458deaab4b59e350a3410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Mon, 26 Feb 2024 12:02:12 +0100 Subject: [PATCH 162/202] Iteratori in 4 modi --- 2023-24/LezioniJava23-24/src/Zoo.java | 6 +-- .../src/tinyjdk/ArrayList.java | 37 +++++++++++++++++-- .../src/tinyjdk/ArrayListIterator.java | 19 ++++++++++ 3 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 2023-24/LezioniJava23-24/src/tinyjdk/ArrayListIterator.java diff --git a/2023-24/LezioniJava23-24/src/Zoo.java b/2023-24/LezioniJava23-24/src/Zoo.java index de9bcb2..d282211 100644 --- a/2023-24/LezioniJava23-24/src/Zoo.java +++ b/2023-24/LezioniJava23-24/src/Zoo.java @@ -33,13 +33,9 @@ public void eat(Animal a) { public static void main(String[] args) { - Dog fido = new Dog(30, false); - Dog gigio = fido; + Dog fido = new Dog(50, false); Animal pluto = new Dog(40, true); pluto.eat(fido); - - pluto = gigio; - pluto.eat(gigio); } } \ No newline at end of file diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java index 2a19dcb..429d9e2 100644 --- a/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java +++ b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java @@ -6,7 +6,7 @@ public class ArrayList implements List { public ArrayList() { this.a = new Object[10]; - sz = 0; + this.sz = 0; } @Override @@ -44,9 +44,28 @@ public void remove(T x) { public int size() { return sz; } + // static nested iterator + private static class StaticMyIterator implements Iterator { + private int pos = 0; + private ArrayList enclosing; - private static class MyIterator implements Iterator { + public StaticMyIterator(ArrayList a) { + this.enclosing = a; + } + @Override + public boolean hasNext() { + return this.pos < enclosing.size(); + } + @Override + public T next() { + return enclosing.get(pos++); + } + } + + // non-static nested iterator + private class MyIterator implements Iterator { private int pos = 0; + @Override public boolean hasNext() { return pos < size(); @@ -56,10 +75,22 @@ public T next() { return get(pos++); } } + @Override public Iterator iterator() { - return new MyIterator(); + return new Iterator() { + @Override + public boolean hasNext() { + return false; + } + + @Override + public T next() { + return null; + } + }; } + @Override public T get(int i) { if (i < sz) diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/ArrayListIterator.java b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayListIterator.java new file mode 100644 index 0000000..07ee883 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayListIterator.java @@ -0,0 +1,19 @@ +package tinyjdk; + +public class ArrayListIterator implements Iterator { + private int pos = 0; + private ArrayList enclosing; + + public ArrayListIterator(ArrayList a) { + this.enclosing = a; + } + @Override + public boolean hasNext() { + return this.pos < enclosing.size(); + } + @Override + public T next() { + return enclosing.get(pos++); + } + +} From 46e9e1af1ab706d04be345c9ff092c9512396755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 27 Feb 2024 10:19:35 +0100 Subject: [PATCH 163/202] LinkedList --- .../src/tinyjdk/ArrayList.java | 24 +++++- .../src/tinyjdk/LinkedList.java | 85 +++++++++++++++++++ 2 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 2023-24/LezioniJava23-24/src/tinyjdk/LinkedList.java diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java index 429d9e2..3e7fc16 100644 --- a/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java +++ b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java @@ -27,6 +27,12 @@ public void clear() { @Override public boolean contains(T x) { + Iterator it = iterator(); + while (it.hasNext()) { + T o = it.next(); + if (o.equals(x)) + return true; + } return false; } @@ -37,7 +43,14 @@ public boolean isEmpty() { @Override public void remove(T x) { - + for (int i = 0; i < size(); ++i) { + T o = get(i); + if (o.equals(x)) { + for (int j = i ; j < size() - 1; ++j) + set(j, get(j + 1)); + --sz; + } + } } @Override @@ -78,15 +91,17 @@ public T next() { @Override public Iterator iterator() { + // anonymous class interator return new Iterator() { + private int pos = 0; @Override public boolean hasNext() { - return false; + return pos < size(); } @Override public T next() { - return null; + return get(pos++); } }; } @@ -110,11 +125,12 @@ public T set(int i, T x) { @Override public void add(int i, T x) { - + // TODO per casa } @Override public T remove(int i) { + // TODO per casa return null; } } diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/LinkedList.java b/2023-24/LezioniJava23-24/src/tinyjdk/LinkedList.java new file mode 100644 index 0000000..b67af95 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/tinyjdk/LinkedList.java @@ -0,0 +1,85 @@ +package tinyjdk; + +public class LinkedList implements List { + + private class Node { + public T data; + public Node next; + public Node(T data, Node next) { + this.data = data; + this.next = next; + } + } + private Node head; + private int sz; + public LinkedList() { + this.head = null; + sz = 0; + } + @Override + public void add(T x) { + if (head == null) { + head = new Node(x, null); + } + else { + // TODO da testare + Node n = head; + while (n.next != null) { + n = n.next; + } + n.next = new Node(x, null); + } + ++sz; + } + + @Override + public void clear() { + head = null; + sz = 0; + } + + @Override + public boolean contains(T x) { + return false; + } + + @Override + public boolean isEmpty() { + return head == null; + } + + @Override + public void remove(T x) { + + } + + @Override + public int size() { + return sz; + } + + @Override + public Iterator iterator() { + return null; + } + + @Override + public T get(int i) { + return null; + } + + @Override + public T set(int i, T x) { + return null; + } + + @Override + public void add(int i, T x) { + + } + + @Override + public T remove(int i) { + return null; + } +} From 38d806942274e68d124f9ac6ebea1b7803c452f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 5 Mar 2024 10:18:47 +0100 Subject: [PATCH 164/202] LinkedList e Set --- .../src/tinyjdk/ArrayList.java | 11 ---- .../src/tinyjdk/Collection.java | 10 ++- .../src/tinyjdk/LinkedList.java | 62 +++++++++++++++---- 2023-24/LezioniJava23-24/src/tinyjdk/Set.java | 4 ++ .../src/tinyjdk/StructuralSet.java | 34 ++++++++++ 5 files changed, 96 insertions(+), 25 deletions(-) create mode 100644 2023-24/LezioniJava23-24/src/tinyjdk/Set.java create mode 100644 2023-24/LezioniJava23-24/src/tinyjdk/StructuralSet.java diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java index 3e7fc16..f37bfb8 100644 --- a/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java +++ b/2023-24/LezioniJava23-24/src/tinyjdk/ArrayList.java @@ -25,17 +25,6 @@ public void clear() { sz = 0; } - @Override - public boolean contains(T x) { - Iterator it = iterator(); - while (it.hasNext()) { - T o = it.next(); - if (o.equals(x)) - return true; - } - return false; - } - @Override public boolean isEmpty() { return sz == 0; diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/Collection.java b/2023-24/LezioniJava23-24/src/tinyjdk/Collection.java index 5986f9a..2842897 100644 --- a/2023-24/LezioniJava23-24/src/tinyjdk/Collection.java +++ b/2023-24/LezioniJava23-24/src/tinyjdk/Collection.java @@ -10,7 +10,15 @@ default void addAll(Collection c) { } } void clear(); - boolean contains(T x); + default boolean contains(T x) { + Iterator it = iterator(); + while (it.hasNext()) { + T e = it.next(); + if (e.equals(x)) + return true; + } + return false; + } boolean isEmpty(); void remove(T x); int size(); diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/LinkedList.java b/2023-24/LezioniJava23-24/src/tinyjdk/LinkedList.java index b67af95..7ea16aa 100644 --- a/2023-24/LezioniJava23-24/src/tinyjdk/LinkedList.java +++ b/2023-24/LezioniJava23-24/src/tinyjdk/LinkedList.java @@ -2,7 +2,7 @@ public class LinkedList implements List { - private class Node { + protected class Node { public T data; public Node next; public Node(T data, Node next) { @@ -10,8 +10,8 @@ public Node(T data, Node next) { this.next = next; } } - private Node head; - private int sz; + protected Node head; + protected int sz; public LinkedList() { this.head = null; sz = 0; @@ -38,19 +38,30 @@ public void clear() { sz = 0; } - @Override - public boolean contains(T x) { - return false; - } - @Override public boolean isEmpty() { return head == null; } + // TODO da sistemare @Override public void remove(T x) { - + Node n = head; + if (n != null) { + if (n.data.equals(x)) { + head = n.next; + --sz; + } + else { + while (n.next != null) { + if (n.next.data.equals(x)) { + n.next = n.next.next; + --sz; + return; + } + } + } + } } @Override @@ -60,26 +71,51 @@ public int size() { @Override public Iterator iterator() { - return null; + return new Iterator() { + private Node n = head; + @Override + public boolean hasNext() { + return n != null; + } + @Override + public T next() { + T r = n.data; + n = n.next; + return r; + } + }; + } + + protected Node getNode(int i) { + if (i < 0 || i >= size()) + throw new RuntimeException(String.format("LinkedList.get(): index %d is out of bound (size = %d)", i, size())); + Node n = head; + for (; i > 0; --i) + n = n.next; + return n; } @Override public T get(int i) { - return null; + return getNode(i).data; } @Override public T set(int i, T x) { - return null; + Node n = getNode(i); + T old = n.data; + n.data = x; + return old; } @Override public void add(int i, T x) { - + // TODO per casa } @Override public T remove(int i) { + // TODO per casa return null; } } diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/Set.java b/2023-24/LezioniJava23-24/src/tinyjdk/Set.java new file mode 100644 index 0000000..739cb95 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/tinyjdk/Set.java @@ -0,0 +1,4 @@ +package tinyjdk; + +public interface Set extends Collection { +} diff --git a/2023-24/LezioniJava23-24/src/tinyjdk/StructuralSet.java b/2023-24/LezioniJava23-24/src/tinyjdk/StructuralSet.java new file mode 100644 index 0000000..ce14e89 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/tinyjdk/StructuralSet.java @@ -0,0 +1,34 @@ +package tinyjdk; +public class StructuralSet implements Set { + private List l = new ArrayList<>(); + @Override + public void add(T x) { + if (!l.contains(x)) + l.add(x); + } + + @Override + public void clear() { + l.clear(); + } + + @Override + public boolean isEmpty() { + return l.isEmpty(); + } + + @Override + public void remove(T x) { + l.remove(x); + } + + @Override + public int size() { + return l.size(); + } + + @Override + public Iterator iterator() { + return l.iterator(); + } +} From 08dd236415119aa9dae0b1dfa1c5135cd2ace2c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 5 Mar 2024 13:55:28 +0100 Subject: [PATCH 165/202] LinkedList.remove(T) fixed --- 2022-23/LezioniJava22-23/.idea/workspace.xml | 16 +++++++------ 2023-24/.idea/.gitignore | 8 +++++++ 2023-24/.idea/misc.xml | 6 +++++ 2023-24/.idea/modules.xml | 8 +++++++ 2023-24/.idea/vcs.xml | 6 +++++ 2023-24/2023-24.iml | 11 +++++++++ 2023-24/LezioniJava23-24/.idea/misc.xml | 2 +- .../src/tinyjdk/LinkedList.java | 24 +++++++------------ .../Scritto PO2 5 9 23/.idea/vcs.xml | 6 +++++ 9 files changed, 63 insertions(+), 24 deletions(-) create mode 100644 2023-24/.idea/.gitignore create mode 100644 2023-24/.idea/misc.xml create mode 100644 2023-24/.idea/modules.xml create mode 100644 2023-24/.idea/vcs.xml create mode 100644 2023-24/2023-24.iml create mode 100644 soluzione appelli/Scritto PO2 5 9 23/.idea/vcs.xml diff --git a/2022-23/LezioniJava22-23/.idea/workspace.xml b/2022-23/LezioniJava22-23/.idea/workspace.xml index cade2b9..45bee3d 100644 --- a/2022-23/LezioniJava22-23/.idea/workspace.xml +++ b/2022-23/LezioniJava22-23/.idea/workspace.xml @@ -4,7 +4,9 @@ c, Function f) { + Collection r = new ArrayList<>(); + for (A x : c) { + B b = f.apply(x); + r.add(b); + } + return r; + } + + public static void main2(String[] args) { + List l = List.of(1, 2, -3, 4); + + Collection r0 = map(l, x -> x > 0); + + Collection r1 = map(l, x -> x + 1); + Collection r2 = map(l, new Function() { + @Override + public Integer apply(Integer x) { + return x + 1; + } + }); + } + + public static void forEach(Collection c, Consumer f) { + for (T x : c) { + f.accept(x); + } + } + + public static void main(String[] args) { + List l = List.of(1, 2, 3, 4); + + forEach(l, x -> { x = x + 1; }); + forEach(l, new Consumer() { + @Override + public void accept(Integer x) { + if (x > 5) + x = x + 1; + } + }); + + forEach(l, x -> System.out.println(x)); + forEach(l, new Consumer() { + @Override + public void accept(Integer x) { + System.out.print(x); + } + }); + + } + + +} From 35c38f99f62f82f4a7297fba6e7a37f5a139c72f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 2 Apr 2024 10:18:49 +0200 Subject: [PATCH 173/202] Update HigherOrderFunctions.java --- .../src/functional/HigherOrderFunctions.java | 55 ++++++++++++------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/2023-24/LezioniJava23-24/src/functional/HigherOrderFunctions.java b/2023-24/LezioniJava23-24/src/functional/HigherOrderFunctions.java index ea7fa49..87e67a7 100644 --- a/2023-24/LezioniJava23-24/src/functional/HigherOrderFunctions.java +++ b/2023-24/LezioniJava23-24/src/functional/HigherOrderFunctions.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import java.util.List; import java.util.function.*; @@ -23,21 +24,40 @@ void main(){ */ public class HigherOrderFunctions { - - public static Collection map(Collection c, Function f) { - Collection r = new ArrayList<>(); + public static List map(Iterable c, Function f) { + List r = new ArrayList<>(); for (A x : c) { B b = f.apply(x); r.add(b); } return r; } + public static void forEach(Iterable c, Consumer f) { + for (T x : c) + f.accept(x); + } + public static List filter(Iterable c, Predicate f) { + List r = new ArrayList<>(); + for (T x : c) { + if (f.test(x)) + r.add(x); + } + return r; + } + public static void filter__impure(Iterable c, Function f) { + Iterator it = c.iterator(); + while (it.hasNext()) { + if (!f.apply(it.next())) + it.remove(); + } + + } - public static void main2(String[] args) { + public static void main(String[] args) { List l = List.of(1, 2, -3, 4); + // map Collection r0 = map(l, x -> x > 0); - Collection r1 = map(l, x -> x + 1); Collection r2 = map(l, new Function() { @Override @@ -45,26 +65,15 @@ public Integer apply(Integer x) { return x + 1; } }); - } - public static void forEach(Collection c, Consumer f) { - for (T x : c) { - f.accept(x); - } - } - - public static void main(String[] args) { - List l = List.of(1, 2, 3, 4); - - forEach(l, x -> { x = x + 1; }); + // forEach + forEach(l, x -> System.out.println(x)); forEach(l, new Consumer() { @Override public void accept(Integer x) { - if (x > 5) - x = x + 1; + System.out.print(x); } }); - forEach(l, x -> System.out.println(x)); forEach(l, new Consumer() { @Override @@ -73,7 +82,15 @@ public void accept(Integer x) { } }); + // filter + Collection c1 = filter(l, x -> x > 2); + + // filter__impure + filter(l, x -> x > 2); } + + + } From 5eb9f381e4bfab3a9a7e0ce076c4a7635de950ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 4 Apr 2024 12:01:56 +0200 Subject: [PATCH 174/202] Threads --- 2023-24/LezioniJava23-24/src/Zoo.java | 14 +++++++++- .../src/concurrent/Threads.java | 24 +++++++++++++++++ .../src/functional/HigherOrderFunctions.java | 26 ++++++++++++++----- 3 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 2023-24/LezioniJava23-24/src/concurrent/Threads.java diff --git a/2023-24/LezioniJava23-24/src/Zoo.java b/2023-24/LezioniJava23-24/src/Zoo.java index d282211..02d0a7f 100644 --- a/2023-24/LezioniJava23-24/src/Zoo.java +++ b/2023-24/LezioniJava23-24/src/Zoo.java @@ -3,6 +3,8 @@ public class Zoo { public static class Animal { protected int weight; + public int getWeight() { return weight; } + public Animal(int w) { this.weight = w; } @@ -29,7 +31,17 @@ public void eat(Animal a) { } } - // SUBSUMPTION + public static class Cat extends Animal { + + public Cat(int w) { + super(w); + } + + @Override + public void eat(Animal a) { + this.weight += a.weight / 2; + } + } public static void main(String[] args) { diff --git a/2023-24/LezioniJava23-24/src/concurrent/Threads.java b/2023-24/LezioniJava23-24/src/concurrent/Threads.java new file mode 100644 index 0000000..854f56f --- /dev/null +++ b/2023-24/LezioniJava23-24/src/concurrent/Threads.java @@ -0,0 +1,24 @@ +package concurrent; + +public class Threads { + + public static class MyThread extends Thread { + @Override + public void run() { + while (true) { + System.out.println("ciao"); + } + } + } + + public static void main(String[] args) { + Thread t = new MyThread(); + t.start(); + + while (true) { + System.out.println("pippo"); + } + + } + +} diff --git a/2023-24/LezioniJava23-24/src/functional/HigherOrderFunctions.java b/2023-24/LezioniJava23-24/src/functional/HigherOrderFunctions.java index 87e67a7..861e8db 100644 --- a/2023-24/LezioniJava23-24/src/functional/HigherOrderFunctions.java +++ b/2023-24/LezioniJava23-24/src/functional/HigherOrderFunctions.java @@ -24,14 +24,20 @@ void main(){ */ public class HigherOrderFunctions { - public static List map(Iterable c, Function f) { + + interface PippoFunction { + B ciccio(A x); + } + + public static List map(Iterable c, PippoFunction f) { List r = new ArrayList<>(); for (A x : c) { - B b = f.apply(x); + B b = f.ciccio(x); r.add(b); } return r; } + public static void forEach(Iterable c, Consumer f) { for (T x : c) f.accept(x); @@ -50,18 +56,16 @@ public static void filter__impure(Iterable c, Function f) { if (!f.apply(it.next())) it.remove(); } - } public static void main(String[] args) { List l = List.of(1, 2, -3, 4); // map - Collection r0 = map(l, x -> x > 0); Collection r1 = map(l, x -> x + 1); - Collection r2 = map(l, new Function() { + Collection r2 = map(l, new PippoFunction() { @Override - public Integer apply(Integer x) { + public Integer ciccio(Integer x) { return x + 1; } }); @@ -87,10 +91,18 @@ public void accept(Integer x) { // filter__impure filter(l, x -> x > 2); - } + // +/* List dogs = new ArrayList<>(); + Function f = (d) -> new Zoo.Cat(d.getWeight()); + PippoFunction g = (d) -> new Zoo.Cat(d.getWeight()); + // questa non compila giustamente + //List cats = map(dogs, f); + List cats2 = map(dogs, g);*/ + + } } From 4e3c045806a7cb50133f10d327f128336eca4ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 11 Apr 2024 14:00:50 +0200 Subject: [PATCH 175/202] Multithreading --- .../src/concurrent/ConsumerProducer.java | 62 +++++++++++++++++++ .../src/concurrent/Threads.java | 19 +++--- 2 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 2023-24/LezioniJava23-24/src/concurrent/ConsumerProducer.java diff --git a/2023-24/LezioniJava23-24/src/concurrent/ConsumerProducer.java b/2023-24/LezioniJava23-24/src/concurrent/ConsumerProducer.java new file mode 100644 index 0000000..a3438ac --- /dev/null +++ b/2023-24/LezioniJava23-24/src/concurrent/ConsumerProducer.java @@ -0,0 +1,62 @@ +package concurrent; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class ConsumerProducer { + + public static List buff = new ArrayList<>(); + + public static class Producer extends Thread { + public Producer() { + super("Producer"); + } + @Override + public void run() { + Random rnd = new Random(); + while (true) { + int n = rnd.nextInt(); + synchronized (buff) { // lock + buff.add(n); + buff.notify(); + System.out.printf("%s: added %d\n", getName(), n); + } // unlock + } + } + } + public static class Consumer extends Thread { + public Consumer() { + super("Consumer"); + } + @Override + public void run() { + while (true) { + synchronized (buff) { + if (buff.isEmpty()) { + try { + buff.wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + int n = buff.remove(0); + System.out.printf("%s: removed %d\n", getName(), n); + } + } + } + } + + public static void main(String[] args) { + Thread p = new Producer(); + Thread c = new Consumer(); + p.start(); + c.start(); + + try { + p.join(); + c.join(); + } catch (InterruptedException e) {} + } +} diff --git a/2023-24/LezioniJava23-24/src/concurrent/Threads.java b/2023-24/LezioniJava23-24/src/concurrent/Threads.java index 854f56f..fb26592 100644 --- a/2023-24/LezioniJava23-24/src/concurrent/Threads.java +++ b/2023-24/LezioniJava23-24/src/concurrent/Threads.java @@ -1,24 +1,27 @@ package concurrent; public class Threads { - + public static String suffix = "baudo"; + public static void loop(String msg) { + while (true) { + System.out.println(msg + " " + suffix); + } + } public static class MyThread extends Thread { @Override public void run() { - while (true) { - System.out.println("ciao"); - } + loop("ciao"); } } public static void main(String[] args) { Thread t = new MyThread(); t.start(); + loop("pippo"); - while (true) { - System.out.println("pippo"); - } - + // modo alternativo + Thread t2 = new Thread(() -> loop("ciccio")); + t2.start(); } } From 10f8df3b418d966625a2d7ef87fd1c06bf24b354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 16 Apr 2024 10:16:45 +0200 Subject: [PATCH 176/202] Update ConsumerProducer.java --- .../src/concurrent/ConsumerProducer.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/2023-24/LezioniJava23-24/src/concurrent/ConsumerProducer.java b/2023-24/LezioniJava23-24/src/concurrent/ConsumerProducer.java index a3438ac..eb64c1b 100644 --- a/2023-24/LezioniJava23-24/src/concurrent/ConsumerProducer.java +++ b/2023-24/LezioniJava23-24/src/concurrent/ConsumerProducer.java @@ -1,9 +1,7 @@ package concurrent; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; +import java.util.*; public class ConsumerProducer { @@ -22,7 +20,7 @@ public void run() { buff.add(n); buff.notify(); System.out.printf("%s: added %d\n", getName(), n); - } // unlock + } // unlock } } } @@ -33,7 +31,7 @@ public Consumer() { @Override public void run() { while (true) { - synchronized (buff) { + synchronized (buff) { // lock if (buff.isEmpty()) { try { buff.wait(); @@ -43,20 +41,25 @@ public void run() { } int n = buff.remove(0); System.out.printf("%s: removed %d\n", getName(), n); - } + } // unlock } } } public static void main(String[] args) { - Thread p = new Producer(); - Thread c = new Consumer(); - p.start(); - c.start(); - + List threads = new ArrayList<>(); + for (int i = 0; i < 20; ++i) { + Thread t = new Producer(); + t.start(); + threads.add(t); + t = new Consumer(); + t.start(); + threads.add(t); + } try { - p.join(); - c.join(); - } catch (InterruptedException e) {} + threads.get(0).join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } } } From 2deecd2cc787faf0383dd021bc17ec25034e0be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 18 Apr 2024 12:03:03 +0200 Subject: [PATCH 177/202] Co-varianza, Contro-varianza, Wildcards --- 2023-24/LezioniJava23-24/src/Zoo.java | 2 +- .../LezioniJava23-24/src/wildcards/Misc.java | 26 +++++++++++++++++++ .../src/wildcards/Overloading.java | 22 ++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 2023-24/LezioniJava23-24/src/wildcards/Misc.java create mode 100644 2023-24/LezioniJava23-24/src/wildcards/Overloading.java diff --git a/2023-24/LezioniJava23-24/src/Zoo.java b/2023-24/LezioniJava23-24/src/Zoo.java index 02d0a7f..9222f1b 100644 --- a/2023-24/LezioniJava23-24/src/Zoo.java +++ b/2023-24/LezioniJava23-24/src/Zoo.java @@ -25,7 +25,7 @@ public Dog(int w, boolean ped) { public void bark() { System.out.println("bau!"); } - @Override + public void eat(Animal a) { this.weight += a.weight * 2; } diff --git a/2023-24/LezioniJava23-24/src/wildcards/Misc.java b/2023-24/LezioniJava23-24/src/wildcards/Misc.java new file mode 100644 index 0000000..00ea50f --- /dev/null +++ b/2023-24/LezioniJava23-24/src/wildcards/Misc.java @@ -0,0 +1,26 @@ +package wildcards; + +import functional.HigherOrderFunctions; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + +public class Misc { + + public static List map(Iterable c, + Function f) { + List r = new ArrayList<>(); + for (A x : c) { + B b = f.apply(x); + r.add(b); + } + return r; + } + + public static void main(String[] args) { + List l1 = List.of("pippo", "franco", "ciccio", "gigi"); + List l2 = map(l1, (CharSequence s) -> s.length()); + } + +} diff --git a/2023-24/LezioniJava23-24/src/wildcards/Overloading.java b/2023-24/LezioniJava23-24/src/wildcards/Overloading.java new file mode 100644 index 0000000..56d3e62 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/wildcards/Overloading.java @@ -0,0 +1,22 @@ +package wildcards; + +import java.util.List; + +public class Overloading { + + public static class A { + public Number m() { return 1.3; } + } + + public static class B extends A { + @Override + public Integer m() { return 2; } + } + + + public static void main(String[] args) { + A a = new B(); + Number u = a.m(); + } + +} From 5d98856488f446daea258c72e98d82d522655d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 18 Apr 2024 15:42:39 +0200 Subject: [PATCH 178/202] Big refactor --- .../{functional => misc}/HigherOrderFunctions.java | 12 ++++++------ .../InterfacesVsAbstractClasses.java} | 4 +++- .../src/{ProveJDK1.java => misc/IteratorTest.java} | 5 +++-- .../src/{wildcards => misc}/Overloading.java | 2 +- .../src/{wildcards/Misc.java => misc/Wildcards.java} | 6 ++---- 2023-24/LezioniJava23-24/src/{ => misc}/Zoo.java | 2 ++ 6 files changed, 17 insertions(+), 14 deletions(-) rename 2023-24/LezioniJava23-24/src/{functional => misc}/HigherOrderFunctions.java (86%) rename 2023-24/LezioniJava23-24/src/{Misc1.java => misc/InterfacesVsAbstractClasses.java} (90%) rename 2023-24/LezioniJava23-24/src/{ProveJDK1.java => misc/IteratorTest.java} (90%) rename 2023-24/LezioniJava23-24/src/{wildcards => misc}/Overloading.java (94%) rename 2023-24/LezioniJava23-24/src/{wildcards/Misc.java => misc/Wildcards.java} (87%) rename 2023-24/LezioniJava23-24/src/{ => misc}/Zoo.java (98%) diff --git a/2023-24/LezioniJava23-24/src/functional/HigherOrderFunctions.java b/2023-24/LezioniJava23-24/src/misc/HigherOrderFunctions.java similarity index 86% rename from 2023-24/LezioniJava23-24/src/functional/HigherOrderFunctions.java rename to 2023-24/LezioniJava23-24/src/misc/HigherOrderFunctions.java index 861e8db..b266819 100644 --- a/2023-24/LezioniJava23-24/src/functional/HigherOrderFunctions.java +++ b/2023-24/LezioniJava23-24/src/misc/HigherOrderFunctions.java @@ -1,4 +1,4 @@ -package functional; +package misc; import java.util.ArrayList; import java.util.Collection; @@ -93,15 +93,15 @@ public void accept(Integer x) { filter(l, x -> x > 2); // -/* List dogs = new ArrayList<>(); +/* List dogs = new ArrayList<>(); - Function f = (d) -> new Zoo.Cat(d.getWeight()); - PippoFunction g = (d) -> new Zoo.Cat(d.getWeight()); + Function f = (d) -> new misc.Zoo.Cat(d.getWeight()); + PippoFunction g = (d) -> new misc.Zoo.Cat(d.getWeight()); // questa non compila giustamente - //List cats = map(dogs, f); + //List cats = map(dogs, f); - List cats2 = map(dogs, g);*/ + List cats2 = map(dogs, g);*/ } diff --git a/2023-24/LezioniJava23-24/src/Misc1.java b/2023-24/LezioniJava23-24/src/misc/InterfacesVsAbstractClasses.java similarity index 90% rename from 2023-24/LezioniJava23-24/src/Misc1.java rename to 2023-24/LezioniJava23-24/src/misc/InterfacesVsAbstractClasses.java index 320e9db..8cee49f 100644 --- a/2023-24/LezioniJava23-24/src/Misc1.java +++ b/2023-24/LezioniJava23-24/src/misc/InterfacesVsAbstractClasses.java @@ -1,4 +1,6 @@ -public class Misc1 { +package misc; + +public class InterfacesVsAbstractClasses { public interface I { void a(); diff --git a/2023-24/LezioniJava23-24/src/ProveJDK1.java b/2023-24/LezioniJava23-24/src/misc/IteratorTest.java similarity index 90% rename from 2023-24/LezioniJava23-24/src/ProveJDK1.java rename to 2023-24/LezioniJava23-24/src/misc/IteratorTest.java index 72ce414..587219b 100644 --- a/2023-24/LezioniJava23-24/src/ProveJDK1.java +++ b/2023-24/LezioniJava23-24/src/misc/IteratorTest.java @@ -1,9 +1,10 @@ +package misc; + import java.util.ArrayList; -import java.util.Collection; import java.util.Iterator; import java.util.List; -public class ProveJDK1 { +public class IteratorTest { public static void main(String[] args) { diff --git a/2023-24/LezioniJava23-24/src/wildcards/Overloading.java b/2023-24/LezioniJava23-24/src/misc/Overloading.java similarity index 94% rename from 2023-24/LezioniJava23-24/src/wildcards/Overloading.java rename to 2023-24/LezioniJava23-24/src/misc/Overloading.java index 56d3e62..7d7f408 100644 --- a/2023-24/LezioniJava23-24/src/wildcards/Overloading.java +++ b/2023-24/LezioniJava23-24/src/misc/Overloading.java @@ -1,4 +1,4 @@ -package wildcards; +package misc; import java.util.List; diff --git a/2023-24/LezioniJava23-24/src/wildcards/Misc.java b/2023-24/LezioniJava23-24/src/misc/Wildcards.java similarity index 87% rename from 2023-24/LezioniJava23-24/src/wildcards/Misc.java rename to 2023-24/LezioniJava23-24/src/misc/Wildcards.java index 00ea50f..cd09a88 100644 --- a/2023-24/LezioniJava23-24/src/wildcards/Misc.java +++ b/2023-24/LezioniJava23-24/src/misc/Wildcards.java @@ -1,12 +1,10 @@ -package wildcards; - -import functional.HigherOrderFunctions; +package misc; import java.util.ArrayList; import java.util.List; import java.util.function.Function; -public class Misc { +public class Wildcards { public static List map(Iterable c, Function f) { diff --git a/2023-24/LezioniJava23-24/src/Zoo.java b/2023-24/LezioniJava23-24/src/misc/Zoo.java similarity index 98% rename from 2023-24/LezioniJava23-24/src/Zoo.java rename to 2023-24/LezioniJava23-24/src/misc/Zoo.java index 9222f1b..963b967 100644 --- a/2023-24/LezioniJava23-24/src/Zoo.java +++ b/2023-24/LezioniJava23-24/src/misc/Zoo.java @@ -1,3 +1,5 @@ +package misc; + public class Zoo { public static class Animal { From 282a696cf751ec9947c9ebbf7f7a0196612f0077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 23 Apr 2024 10:18:51 +0200 Subject: [PATCH 179/202] Java sorting e inizio C++ --- .../LezioniCPP23-24/LezioniCPP/LezioniCPP.sln | 31 ++++ .../LezioniCPP/LezioniCPP/LezioniCPP.cpp | 40 ++++++ .../LezioniCPP/LezioniCPP/LezioniCPP.vcxproj | 135 ++++++++++++++++++ .../LezioniCPP/LezioniCPP.vcxproj.filters | 22 +++ .../LezioniCPP/LezioniCPP.vcxproj.user | 4 + .../LezioniJava23-24/src/misc/Sorting.java | 34 +++++ 6 files changed, 266 insertions(+) create mode 100644 2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP.sln create mode 100644 2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp create mode 100644 2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj create mode 100644 2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters create mode 100644 2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.user create mode 100644 2023-24/LezioniJava23-24/src/misc/Sorting.java diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP.sln b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP.sln new file mode 100644 index 0000000..fcc8ac4 --- /dev/null +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33627.172 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LezioniCPP", "LezioniCPP\LezioniCPP.vcxproj", "{F22B848D-8942-41FD-A3A2-BD7C5CF98C4C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F22B848D-8942-41FD-A3A2-BD7C5CF98C4C}.Debug|x64.ActiveCfg = Debug|x64 + {F22B848D-8942-41FD-A3A2-BD7C5CF98C4C}.Debug|x64.Build.0 = Debug|x64 + {F22B848D-8942-41FD-A3A2-BD7C5CF98C4C}.Debug|x86.ActiveCfg = Debug|Win32 + {F22B848D-8942-41FD-A3A2-BD7C5CF98C4C}.Debug|x86.Build.0 = Debug|Win32 + {F22B848D-8942-41FD-A3A2-BD7C5CF98C4C}.Release|x64.ActiveCfg = Release|x64 + {F22B848D-8942-41FD-A3A2-BD7C5CF98C4C}.Release|x64.Build.0 = Release|x64 + {F22B848D-8942-41FD-A3A2-BD7C5CF98C4C}.Release|x86.ActiveCfg = Release|Win32 + {F22B848D-8942-41FD-A3A2-BD7C5CF98C4C}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EC04A7C9-2ADD-4E4E-8C9D-8FAD4CDBE013} + EndGlobalSection +EndGlobal diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp new file mode 100644 index 0000000..0e21f8b --- /dev/null +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp @@ -0,0 +1,40 @@ + +#include + + + +class animal +{ +private: + int weight; + double speed; + +public: + animal(int w, double sp) : weight(w), speed(sp) {} + + animal(const animal& a) : weight(a.weight), speed(a.speed) {} + + int get_weight() + { + return weight; + } + + virtual void eat(const animal& a) + { + weight += a.weight; + } +}; + +class dog : public animal +{ + +}; + + + + + +int main() +{ +} + diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj new file mode 100644 index 0000000..c64a31d --- /dev/null +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {f22b848d-8942-41fd-a3a2-bd7c5cf98c4c} + LezioniCPP + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters new file mode 100644 index 0000000..d80ed55 --- /dev/null +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.user b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/2023-24/LezioniJava23-24/src/misc/Sorting.java b/2023-24/LezioniJava23-24/src/misc/Sorting.java new file mode 100644 index 0000000..88d0ff0 --- /dev/null +++ b/2023-24/LezioniJava23-24/src/misc/Sorting.java @@ -0,0 +1,34 @@ +package misc; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class Sorting { + + public static void sort(List list, Comparator c) { ... } + + public static class Animal { + public int weight; + public Animal(int w) { this.weight = w; } + } + public static class Dog extends Animal { + public boolean pedigree; + public Dog(int w, boolean p) { super(w); this.pedigree = p; } + } + + public static void main(String[] args) { + List l = new ArrayList<>(); + l.add(new Dog(50, false)); + l.add(new Dog(20, true)); + l.add(new Dog(30, true)); + + sort(l, new Comparator() { + @Override + public int compare(Animal o1, Animal o2) { + return o1.weight - o2.weight; + } + }); + } + +} From 0f81ad87b1bf5449847710316e98c8fcaf6f7acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 30 Apr 2024 15:31:35 +0200 Subject: [PATCH 180/202] Esercizio C++ appello 5 9 23 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mancava la soluzione di C++, ma in realtà era uguale all'esercizio dell'appello del 3 6 22 --- .../cpp/Appello_3_6_22/Appello_3_6_22.cpp | 82 +++++++++++ .../cpp/Appello_3_6_22/Appello_3_6_22.sln | 31 ++++ .../cpp/Appello_3_6_22/Appello_3_6_22.vcxproj | 135 ++++++++++++++++++ .../Appello_3_6_22.vcxproj.filters | 22 +++ .../Appello_3_6_22.vcxproj.user | 4 + .../src/cpp/Appello_5_9_23/Appello_5_9_23.cpp | 83 +++++++++++ .../src/cpp/Appello_5_9_23/Appello_5_9_23.sln | 31 ++++ .../cpp/Appello_5_9_23/Appello_5_9_23.vcxproj | 135 ++++++++++++++++++ .../Appello_5_9_23.vcxproj.filters | 22 +++ .../Appello_5_9_23.vcxproj.user | 4 + .../src/{ => java}/Main.java | 0 11 files changed, 549 insertions(+) create mode 100644 soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.cpp create mode 100644 soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.sln create mode 100644 soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj create mode 100644 soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.filters create mode 100644 soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.user create mode 100644 soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.cpp create mode 100644 soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.sln create mode 100644 soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj create mode 100644 soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj.filters create mode 100644 soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj.user rename soluzione appelli/Scritto PO2 5 9 23/src/{ => java}/Main.java (100%) diff --git a/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.cpp b/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.cpp new file mode 100644 index 0000000..d343e97 --- /dev/null +++ b/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.cpp @@ -0,0 +1,82 @@ + +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 5/9/2023 per ciò che riguarda il quesito 2, ovvero la domanda che coinvolge C++. +// I quesiti 1-5 riguardanti Java sono in un sorgente Java a parte, non qui. +// Il codice C++ qui esposto è standard C++ vanilla (a.k.a. C++03), sebbene il progetto VS sia configurato con il compilatore di default C++14 + +#include +#include + +using namespace std; + +// 6 +template +class matrix +{ +private: + size_t cols; + vector v; + +public: + matrix() : cols(0), v() {} + matrix(const matrix& m) : cols(m.cols), v(m.v) {} + matrix(size_t rows, size_t cols_, const T& v = T()) : cols(cols_), v(rows* cols, v) {} + + typedef T value_type; + typedef typename vector::iterator iterator; + typedef typename vector::const_iterator const_iterator; + + matrix& operator=(const matrix& m) + { + v = m.v; + return *this; + } + + T& operator()(size_t i, size_t j) + { + return v[i * cols + j]; + } + + const T& operator()(size_t i, size_t j) const + { + return (*this)(i, j); + } + + iterator begin() + { + return v.begin(); + } + + iterator end() + { + return v.end(); + } + + const_iterator begin() const + { + return begin(); + } + + const_iterator end() const + { + return end(); + } +}; + + +int main() +{ + matrix m1; // non inizializzata + matrix m2(10, 20); // 10*20 inizializzata col default constructor di double + matrix m3(m2); // costruita per copia + m1 = m2; // assegnamento + m3(3, 1) = 11.23; // operatore di accesso come left-value + + for (typename matrix::iterator it = m1.begin(); it != m1.end(); ++it) { + typename matrix::value_type& x = *it; // de-reference non-const + x = m2(0, 2); // operatore di accesso come right-value + } + + matrix ms(5, 4, "ciao"); // 5*4 inizializzata col la stringa passata come terzo argomento + for (typename matrix::const_iterator it = ms.begin(); it != ms.end(); ++it) + cout << *it; // de-reference const +} diff --git a/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.sln b/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.sln new file mode 100644 index 0000000..160d856 --- /dev/null +++ b/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32526.322 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Appello_3_6_22", "Appello_3_6_22.vcxproj", "{134AAFA0-F15F-4F91-B364-962161EC5D7F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x64.ActiveCfg = Debug|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x64.Build.0 = Debug|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x86.ActiveCfg = Debug|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x86.Build.0 = Debug|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x64.ActiveCfg = Release|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x64.Build.0 = Release|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x86.ActiveCfg = Release|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {49DE568A-D54C-485E-A095-5948A84AED91} + EndGlobalSection +EndGlobal diff --git a/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj b/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj new file mode 100644 index 0000000..04e7e58 --- /dev/null +++ b/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {134aafa0-f15f-4f91-b364-962161ec5d7f} + Appello3622 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level4 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.filters b/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.filters new file mode 100644 index 0000000..7676feb --- /dev/null +++ b/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.user b/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 5 9 23/cpp/Appello_3_6_22/Appello_3_6_22.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.cpp b/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.cpp new file mode 100644 index 0000000..b8a2c83 --- /dev/null +++ b/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.cpp @@ -0,0 +1,83 @@ + +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 3/6/2022 per ciò che riguarda il quesito 6, ovvero la domanda che coinvolge C++. +// I quesiti 1-5 riguardanti Java sono in un progetto IntelliJ a parte, non qui. +// Il codice C++ qui esposto è standard C++ vanilla (a.k.a. C++03), sebbene il progetto VS sia configurato con il compilatore di default C++14 + +#include +#include + +using namespace std; + +// 6 +template +class matrix +{ +private: + size_t cols; + vector v; + +public: + matrix() : cols(0), v() {} + matrix(size_t rows, size_t cols_) : cols(cols_), v(rows * cols) {} + matrix(size_t rows, size_t cols_, const T& v) : cols(cols_), v(rows* cols, v) {} + matrix(const matrix& m) : cols(m.cols), v(m.v) {} + + typedef T value_type; + typedef typename vector::iterator iterator; + typedef typename vector::const_iterator const_iterator; + + matrix& operator=(const matrix& m) + { + v = m.v; + return *this; + } + + T& operator()(size_t i, size_t j) + { + return v[i * cols + j]; + } + + const T& operator()(size_t i, size_t j) const + { + return (*this)(i, j); + } + + iterator begin() + { + return v.begin(); + } + + iterator end() + { + return v.end(); + } + + const_iterator begin() const + { + return begin(); + } + + const_iterator end() const + { + return end(); + } +}; + + +int main() +{ + matrix m1; // non inizializzata + matrix m2(10, 20); // 10*20 inizializzata col default constructor di double + matrix m3(m2); // costruita per copia + m1 = m2; // assegnamento + m3(3, 1) = 11.23; // operatore di accesso come left-value + + for (typename matrix::iterator it = m1.begin(); it != m1.end(); ++it) { + typename matrix::value_type& x = *it; // de-reference non-const + x = m2(0, 2); // operatore di accesso come right-value + } + + matrix ms(5, 4, "ciao"); // 5*4 inizializzata col la stringa passata come terzo argomento + for (typename matrix::const_iterator it = ms.begin(); it != ms.end(); ++it) + cout << *it; // de-reference const +} diff --git a/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.sln b/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.sln new file mode 100644 index 0000000..cb13ab3 --- /dev/null +++ b/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32526.322 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Appello_3_6_22", "..\..\..\cpp\Appello_3_6_22\Appello_3_6_22.vcxproj", "{134AAFA0-F15F-4F91-B364-962161EC5D7F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x64.ActiveCfg = Debug|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x64.Build.0 = Debug|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x86.ActiveCfg = Debug|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Debug|x86.Build.0 = Debug|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x64.ActiveCfg = Release|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x64.Build.0 = Release|x64 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x86.ActiveCfg = Release|Win32 + {134AAFA0-F15F-4F91-B364-962161EC5D7F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {49DE568A-D54C-485E-A095-5948A84AED91} + EndGlobalSection +EndGlobal diff --git a/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj b/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj new file mode 100644 index 0000000..04e7e58 --- /dev/null +++ b/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {134aafa0-f15f-4f91-b364-962161ec5d7f} + Appello3622 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level4 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj.filters b/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj.filters new file mode 100644 index 0000000..7676feb --- /dev/null +++ b/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj.user b/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 5 9 23/src/cpp/Appello_5_9_23/Appello_5_9_23.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 5 9 23/src/Main.java b/soluzione appelli/Scritto PO2 5 9 23/src/java/Main.java similarity index 100% rename from soluzione appelli/Scritto PO2 5 9 23/src/Main.java rename to soluzione appelli/Scritto PO2 5 9 23/src/java/Main.java From cbb97875683467f2e6cd1c8e64e16cd7388fb039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 30 Apr 2024 16:11:52 +0200 Subject: [PATCH 181/202] Update LezioniCPP.cpp --- .../LezioniCPP/LezioniCPP/LezioniCPP.cpp | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp index 0e21f8b..1332cbb 100644 --- a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp @@ -5,19 +5,18 @@ class animal { -private: +protected: int weight; double speed; public: + animal() : weight(), speed() {} animal(int w, double sp) : weight(w), speed(sp) {} - animal(const animal& a) : weight(a.weight), speed(a.speed) {} - int get_weight() - { - return weight; - } + const int& weight() const { return weight; } + + int& weight() { return weight; } virtual void eat(const animal& a) { @@ -27,10 +26,38 @@ class animal class dog : public animal { +private: + bool has_pedigree; + +public: + dog(int w, double sp, bool ped) : animal(w, sp), has_pedigree(ped) {} + void eat(const animal& a) { + weight() += a.weight() / 2; + } }; +void main() { + animal* a1 = new animal(7, 2.34); + animal a2(7, 2.34); + + animal a3(a2); + animal* a4 = new animal(a2); + + a2.eat(a2); + + dog fido(60, 45.34, false); + animal& a5 = fido; + a5.eat(a2); + + dog* fufi = new dog(3, 100., true); + animal* a6 = fufi; + a6->eat(a2); +} + + + From a54b51f6fd455964f8c7ec826f95165382a27707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 2 May 2024 12:07:46 +0200 Subject: [PATCH 182/202] Matrix in c++ --- .../LezioniCPP/LezioniCPP/LezioniCPP.cpp | 4 +++ .../LezioniCPP/LezioniCPP/matrix.cpp | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp index 1332cbb..26dd24a 100644 --- a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.cpp @@ -54,6 +54,10 @@ void main() { dog* fufi = new dog(3, 100., true); animal* a6 = fufi; a6->eat(a2); + + animal pippo(fido); + pippo.eat(a2); + } diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp new file mode 100644 index 0000000..520aedb --- /dev/null +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +template +class matrix +{ +private: + vector v; + +public: + matrix() : v() {} + matrix(const matrix& m) : v(m.v) {} + matrix(size_t rows, size_t cols) : v(rows * cols) {} + explicit matrix(size_t dim) : matrix(dim, dim) {} + + const T& at(size_t i, size_t j) const + { + return v[i * cols + j]; + } +}; + + +void main() { + + int x; + x = 7; + int y = 8; + x = y; + + matrix a; + a = matrix(); + matrix b(...); + b = a; +} \ No newline at end of file From dccfeea5bec0e9cfdcf4e9e19f75c8a3b492e8ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 7 May 2024 10:22:05 +0200 Subject: [PATCH 183/202] C++ matrix --- .../LezioniCPP/LezioniCPP/LezioniCPP.vcxproj | 1 + .../LezioniCPP/LezioniCPP.vcxproj.filters | 3 + .../LezioniCPP/LezioniCPP/matrix.cpp | 55 ++++++++++++++----- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj index c64a31d..9cfe6a9 100644 --- a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj @@ -128,6 +128,7 @@ + diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters index d80ed55..cca7a19 100644 --- a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters @@ -18,5 +18,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp index 520aedb..2d2dfaf 100644 --- a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp @@ -6,30 +6,59 @@ template class matrix { private: + size_t cols; vector v; public: matrix() : v() {} matrix(const matrix& m) : v(m.v) {} - matrix(size_t rows, size_t cols) : v(rows * cols) {} + + +// matrix(size_t rows, size_t _cols) : cols(_cols), v(rows* cols) {} + matrix(size_t rows, size_t _cols, const T& x = T()) + : cols(_cols), v(rows* cols, x) {} + + explicit matrix(size_t dim) : matrix(dim, dim) {} - const T& at(size_t i, size_t j) const + matrix& operator=(const matrix& m) + { + cols = m.cols; + v = m.v; + return *this; + } + + + const T& operator()(size_t i, size_t j) const + { + return v[i * cols + j]; + } + T& operator()(size_t i, size_t j) { return v[i * cols + j]; } -}; + size_t get_cols() const { return cols; } + size_t get_rows() const { return v.size() / cols; } + +}; + + +void main() +{ + matrix m(20, 30); + m(8, 10) = m(3, 4); + + matrix m2(40, 50); + m = m2; + m.operator=(m2); + + m = m2 = m; // m.operator=(m2.operator=(m)); + + matrix m3; + m3 = m; + matrix m4(m3); -void main() { +} - int x; - x = 7; - int y = 8; - x = y; - matrix a; - a = matrix(); - matrix b(...); - b = a; -} \ No newline at end of file From dc1bc6bd003a64eff08deac68eaa8f7bd5a98173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 9 May 2024 22:08:44 +0200 Subject: [PATCH 184/202] Update matrix.cpp --- .../LezioniCPP/LezioniCPP/matrix.cpp | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp index 2d2dfaf..28a7fd5 100644 --- a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp @@ -1,4 +1,5 @@ #include +#include using namespace std; @@ -11,16 +12,26 @@ class matrix public: matrix() : v() {} - matrix(const matrix& m) : v(m.v) {} + matrix(const matrix& m) : cols(m.cols), v(m.v) {} - -// matrix(size_t rows, size_t _cols) : cols(_cols), v(rows* cols) {} + template + matrix(const matrix& m) : cols(m.cols), v(m.get_rows() * m.get_cols()) + { + for (int i = 0; i < v.size(); ++i) + v[i] = m.v[i]; + } + matrix(size_t rows, size_t _cols, const T& x = T()) : cols(_cols), v(rows* cols, x) {} explicit matrix(size_t dim) : matrix(dim, dim) {} + operator const vector&() const + { + return v; + } + matrix& operator=(const matrix& m) { cols = m.cols; @@ -40,24 +51,27 @@ class matrix size_t get_cols() const { return cols; } size_t get_rows() const { return v.size() / cols; } - }; - -void main() +template +ostream& operator<<(ostream& os, const C& m) { - matrix m(20, 30); - m(8, 10) = m(3, 4); + for (typename C::iterator it = m.v.begin(); it != m.v.end(); ++it) + { + typename C::value_type x = *it; + os << x << " "; + } + return os; +} + - matrix m2(40, 50); - m = m2; - m.operator=(m2); - m = m2 = m; // m.operator=(m2.operator=(m)); +void main() +{ + matrix m1(20, 30); + matrix m2(m1); - matrix m3; - m3 = m; - matrix m4(m3); + cout << 7; } From 0b1a86901409860451ccd576d691ca1a80a246a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 16 May 2024 15:38:54 +0200 Subject: [PATCH 185/202] smart pointers --- .../LezioniCPP/LezioniCPP/matrix.cpp | 33 ++++++++++- .../LezioniCPP/LezioniCPP/smart_ptr.cpp | 59 +++++++++++++++++++ 2 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/smart_ptr.cpp diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp index 28a7fd5..827407c 100644 --- a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/matrix.cpp @@ -27,6 +27,30 @@ class matrix explicit matrix(size_t dim) : matrix(dim, dim) {} + typedef T value_type; + typedef vector::iterator iterator; + typedef vector::const_iterator const_iterator; + + iterator begin() + { + return v.begin(); + } + + iterator end() + { + return v.end(); + } + + const_iterator begin() const + { + return v.begin(); + } + + const_iterator end() const; + { + return v.end(); + } + operator const vector&() const { return v; @@ -39,7 +63,6 @@ class matrix return *this; } - const T& operator()(size_t i, size_t j) const { return v[i * cols + j]; @@ -69,9 +92,13 @@ ostream& operator<<(ostream& os, const C& m) void main() { matrix m1(20, 30); - matrix m2(m1); + + for (matrix::iterator it = m1.begin(); it != m1.end(); ++it) + { + matrix::value_type x = *it; + cout << x; + } - cout << 7; } diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/smart_ptr.cpp b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/smart_ptr.cpp new file mode 100644 index 0000000..2a0093e --- /dev/null +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/smart_ptr.cpp @@ -0,0 +1,59 @@ + + +template +class smart_ptr +{ +private: + T* pt; + const T* orig; + unsigned int* cnt; + +public: + explicit smart_ptr(T* _pt) : pt(_pt), orig(_pt), cnt(new unsigned int(1u)) {} + + smart_ptr(const smart_ptr& sp) : pt(sp.pt), orig(sp.orig), cnt(sp.cnt) + { + ++ *cnt; + } + + ~smart_ptr() + { + if (-- *cnt == 0) + { + delete orig; + delete cnt; + } + } + + T& operator*() + { + return *pt; + } + + const T& operator*() const + { + return *pt; + } + + smart_ptr& operator++() + { + ++pt; + return *this; + } + + smart_ptr operator++() + { + smart_ptr r(*this); + ++pt; + return r; + } +}; + + + +void main() +{ + int n = 7; + int m = n++ + 1; + +} \ No newline at end of file From 0bf40d2855486a852c22f25f093fdddf64b1a42e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 16 May 2024 15:39:21 +0200 Subject: [PATCH 186/202] smart pointers --- .../LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj | 1 + .../LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters | 3 +++ 2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/smart_ptr.cpp | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj index 9cfe6a9..5b07be8 100644 --- a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj @@ -129,6 +129,7 @@ + diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters index cca7a19..f14b371 100644 --- a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/LezioniCPP.vcxproj.filters @@ -21,5 +21,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/smart_ptr.cpp b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/smart_ptr.cpp index 2a0093e..7d79cc4 100644 --- a/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/smart_ptr.cpp +++ b/2023-24/LezioniCPP23-24/LezioniCPP/LezioniCPP/smart_ptr.cpp @@ -41,7 +41,7 @@ class smart_ptr return *this; } - smart_ptr operator++() + smart_ptr operator++(int) { smart_ptr r(*this); ++pt; From c55247eb2c898ae33e75aabbf485dc98e8c37632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 26 Jun 2024 17:27:29 +0200 Subject: [PATCH 187/202] Soluzioni appello 25 6 24 --- .../cpp/Appello_14_6_24/Appello_14_6_24.cpp | 35 +++++ .../cpp/Appello_14_6_24/Appello_14_6_24.sln | 31 ++++ .../Appello_14_6_24/Appello_14_6_24.vcxproj | 135 +++++++++++++++++ .../Appello_14_6_24.vcxproj.user | 4 + .../Scritto PO2 14 6 24/java/.gitignore | 29 ++++ .../Scritto PO2 14 6 24/java/.idea/.gitignore | 8 + .../java/.idea/codeStyles/Project.xml | 7 + .../java/.idea/codeStyles/codeStyleConfig.xml | 5 + .../Scritto PO2 14 6 24/java/.idea/misc.xml | 6 + .../java/.idea/modules.xml | 8 + .../java/Scritto PO2 13 6 24.iml | 11 ++ .../Scritto PO2 14 6 24/java/src/Es1.java | 85 +++++++++++ .../cpp/Appello_25_6_24/Appello_25_6_24.cpp | 81 ++++++++++ .../cpp/Appello_25_6_24/Appello_25_6_24.sln | 31 ++++ .../Appello_25_6_24/Appello_25_6_24.vcxproj | 135 +++++++++++++++++ .../Appello_25_6_24.vcxproj.user | 4 + .../Scritto PO2 25 6 24/java/.gitignore | 29 ++++ .../Scritto PO2 25 6 24/java/.idea/.gitignore | 8 + .../java/.idea/codeStyles/Project.xml | 7 + .../java/.idea/codeStyles/codeStyleConfig.xml | 5 + .../Scritto PO2 25 6 24/java/.idea/misc.xml | 6 + .../java/.idea/modules.xml | 8 + .../java/Scritto PO2 25 6 24.iml | 11 ++ .../java/src/.idea/.gitignore | 8 + .../java/src/.idea/misc.xml | 6 + .../java/src/.idea/modules.xml | 8 + .../Scritto PO2 25 6 24/java/src/Es1.java | 142 ++++++++++++++++++ .../java/src/Scritto PO2 25 6 24.iml | 11 ++ 28 files changed, 864 insertions(+) create mode 100644 soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.cpp create mode 100644 soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.sln create mode 100644 soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj create mode 100644 soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj.user create mode 100644 soluzione appelli/Scritto PO2 14 6 24/java/.gitignore create mode 100644 soluzione appelli/Scritto PO2 14 6 24/java/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 14 6 24/java/.idea/codeStyles/Project.xml create mode 100644 soluzione appelli/Scritto PO2 14 6 24/java/.idea/codeStyles/codeStyleConfig.xml create mode 100644 soluzione appelli/Scritto PO2 14 6 24/java/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 14 6 24/java/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 14 6 24/java/Scritto PO2 13 6 24.iml create mode 100644 soluzione appelli/Scritto PO2 14 6 24/java/src/Es1.java create mode 100644 soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.cpp create mode 100644 soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.sln create mode 100644 soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.vcxproj create mode 100644 soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.vcxproj.user create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/.gitignore create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/.idea/codeStyles/Project.xml create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/.idea/codeStyles/codeStyleConfig.xml create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/Scritto PO2 25 6 24.iml create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/src/Es1.java create mode 100644 soluzione appelli/Scritto PO2 25 6 24/java/src/Scritto PO2 25 6 24.iml diff --git a/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.cpp b/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.cpp new file mode 100644 index 0000000..df04c28 --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.cpp @@ -0,0 +1,35 @@ + +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 5/9/2023 per ciò che riguarda il quesito 2, ovvero la domanda che coinvolge C++. +// I quesiti 1-5 riguardanti Java sono in un sorgente Java a parte, non qui. +// Il codice C++ qui esposto è standard C++ vanilla (a.k.a. C++03), sebbene il progetto VS sia configurato con il compilatore di default C++14 + +#include +#include + +using namespace std; + +template +ostream& operator<<(ostream& os, const Container& c) +{ + os << "["; + for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) + os << " " << *it; + os << "]"; + return os; +} + +template +typename Container::value_type sum(const Container& c) +{ + typename Container::value_type r; + for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) + { + r += *it; + } + return r; +} + + +int main() +{ +} diff --git a/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.sln b/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.sln new file mode 100644 index 0000000..9683cae --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32526.322 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Appello_14_6_24", "Appello_14_6_24.vcxproj", "{C7C15495-50AD-4636-90B2-C312702B99FD}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x64.ActiveCfg = Debug|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x64.Build.0 = Debug|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x86.ActiveCfg = Debug|Win32 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x86.Build.0 = Debug|Win32 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x64.ActiveCfg = Release|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x64.Build.0 = Release|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x86.ActiveCfg = Release|Win32 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {49DE568A-D54C-485E-A095-5948A84AED91} + EndGlobalSection +EndGlobal diff --git a/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj b/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj new file mode 100644 index 0000000..748b08d --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {C7C15495-50AD-4636-90B2-C312702B99FD} + Appello3622 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level4 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj.user b/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 14 6 24/java/.gitignore b/soluzione appelli/Scritto PO2 14 6 24/java/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/java/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 14 6 24/java/.idea/.gitignore b/soluzione appelli/Scritto PO2 14 6 24/java/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/java/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/soluzione appelli/Scritto PO2 14 6 24/java/.idea/codeStyles/Project.xml b/soluzione appelli/Scritto PO2 14 6 24/java/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..919ce1f --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/java/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 14 6 24/java/.idea/codeStyles/codeStyleConfig.xml b/soluzione appelli/Scritto PO2 14 6 24/java/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/java/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 14 6 24/java/.idea/misc.xml b/soluzione appelli/Scritto PO2 14 6 24/java/.idea/misc.xml new file mode 100644 index 0000000..cd639fe --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/java/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 14 6 24/java/.idea/modules.xml b/soluzione appelli/Scritto PO2 14 6 24/java/.idea/modules.xml new file mode 100644 index 0000000..0c9761c --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/java/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 14 6 24/java/Scritto PO2 13 6 24.iml b/soluzione appelli/Scritto PO2 14 6 24/java/Scritto PO2 13 6 24.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/java/Scritto PO2 13 6 24.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 14 6 24/java/src/Es1.java b/soluzione appelli/Scritto PO2 14 6 24/java/src/Es1.java new file mode 100644 index 0000000..793f487 --- /dev/null +++ b/soluzione appelli/Scritto PO2 14 6 24/java/src/Es1.java @@ -0,0 +1,85 @@ +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; + +public class Es1 { + + public static Iterator mapIterator(Iterator it, Function f) { + return new Iterator<>() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public B next() { + return f.apply(it.next()); + } + }; + } + + public static void forEach(Iterable it, Consumer f) { + for (T x : it) + f.accept(x); + } + + public static class Pair { + public final X fst; + public final Y snd; + public Pair(X x, Y y) { + fst = x; + snd = y; + } + } + + public static void main1(String[] args) { + List, String>> l = new ArrayList<>(); + Iterator it = mapIterator(l.iterator(), (p) -> p.fst.apply(p.snd)); + } + + public static Iterator applyFuns(Iterable, A>> l) { + return mapIterator(l.iterator(), (p) -> p.fst.apply(p.snd)); + } + + public static void main2(String[] args) { + List, Integer>> l = new ArrayList<>(); + forEach(l, (p) -> p.fst.accept(p.snd)); + } + + public static void acceptFuns(Iterable, A>> l) { + forEach(l, (p) -> p.fst.accept(p.snd)); + } + + public static Iterator> asyncMapIterator(Iterator it, Function f) { + return new Iterator<>() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public Supplier next() { + final A x = it.next(); + final B[] r = (B[]) new Object[1]; + Thread t = new Thread(() -> { r[0] = f.apply(x); }); + t.start(); + return () -> { + try { + t.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return r[0]; + }; + } + }; + } + + public static void asyncForEach(Iterable it, Consumer f) { + for (T x : it) + new Thread(() -> f.accept(x)).start(); + } +} diff --git a/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.cpp b/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.cpp new file mode 100644 index 0000000..106371f --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.cpp @@ -0,0 +1,81 @@ + +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 25/6/2024 per ciò che riguarda il quesito 2, ovvero la domanda che coinvolge C++. +// I quesiti 1-5 riguardanti Java sono in un sorgente Java a parte, non qui. +// Il codice C++ qui esposto è standard C++ vanilla (a.k.a. C++03), sebbene il progetto VS sia configurato con il compilatore di default C++14 + +#include +#include + +using namespace std; + +template +class matrix +{ +private: + size_t cols; + vector v; + +public: + matrix() : cols(0), v() {} + matrix(const matrix& m) : cols(m.cols), v(m.v) {} + matrix(size_t rows, size_t cols_, const T& v = T()) : cols(cols_), v(rows* cols, v) {} + + typedef T value_type; + typedef typename vector::iterator iterator; + typedef typename vector::const_iterator const_iterator; + + matrix& operator=(const matrix& m) + { + v = m.v; + return *this; + } + + T& operator()(size_t i, size_t j) + { + return v[i * cols + j]; + } + + const T& operator()(size_t i, size_t j) const + { + return (*this)(i, j); + } + + iterator begin() + { + return v.begin(); + } + + iterator end() + { + return v.end(); + } + + const_iterator begin() const + { + return begin(); + } + + const_iterator end() const + { + return end(); + } +}; + + +int main() +{ + matrix m1; // non inizializzata + matrix m2(10, 20); // 10*20 inizializzata col default constructor di double + matrix m3(m2); // costruita per copia + m1 = m2; // assegnamento + m3(3, 1) = 11.23; // operatore di accesso come left-value + + for (typename matrix::iterator it = m1.begin(); it != m1.end(); ++it) { + typename matrix::value_type& x = *it; // de-reference non-const + x = m2(0, 2); // operatore di accesso come right-value + } + + matrix ms(5, 4, "ciao"); // 5*4 inizializzata col la stringa passata come terzo argomento + for (typename matrix::const_iterator it = ms.begin(); it != ms.end(); ++it) + cout << *it; // de-reference const +} diff --git a/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.sln b/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.sln new file mode 100644 index 0000000..22261e2 --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32526.322 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Appello_25_6_24", "Appello_25_6_24.vcxproj", "{C7C15495-50AD-4636-90B2-C312702B99FD}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x64.ActiveCfg = Debug|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x64.Build.0 = Debug|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x86.ActiveCfg = Debug|Win32 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x86.Build.0 = Debug|Win32 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x64.ActiveCfg = Release|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x64.Build.0 = Release|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x86.ActiveCfg = Release|Win32 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {49DE568A-D54C-485E-A095-5948A84AED91} + EndGlobalSection +EndGlobal diff --git a/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.vcxproj b/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.vcxproj new file mode 100644 index 0000000..f7c2f90 --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {C7C15495-50AD-4636-90B2-C312702B99FD} + Appello3622 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level4 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.vcxproj.user b/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/cpp/Appello_25_6_24/Appello_25_6_24.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/.gitignore b/soluzione appelli/Scritto PO2 25 6 24/java/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/.idea/.gitignore b/soluzione appelli/Scritto PO2 25 6 24/java/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/.idea/codeStyles/Project.xml b/soluzione appelli/Scritto PO2 25 6 24/java/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..919ce1f --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/.idea/codeStyles/codeStyleConfig.xml b/soluzione appelli/Scritto PO2 25 6 24/java/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/.idea/misc.xml b/soluzione appelli/Scritto PO2 25 6 24/java/.idea/misc.xml new file mode 100644 index 0000000..cd639fe --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/.idea/modules.xml b/soluzione appelli/Scritto PO2 25 6 24/java/.idea/modules.xml new file mode 100644 index 0000000..0c9761c --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/Scritto PO2 25 6 24.iml b/soluzione appelli/Scritto PO2 25 6 24/java/Scritto PO2 25 6 24.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/Scritto PO2 25 6 24.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/.gitignore b/soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/misc.xml b/soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/misc.xml new file mode 100644 index 0000000..1b2d693 --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/modules.xml b/soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/modules.xml new file mode 100644 index 0000000..76859a0 --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/src/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/src/Es1.java b/soluzione appelli/Scritto PO2 25 6 24/java/src/Es1.java new file mode 100644 index 0000000..2bfc1b8 --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/src/Es1.java @@ -0,0 +1,142 @@ +import java.util.*; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; + +public class Es1 { + + // 1.a + public static class Point { + public final double x, y; + + public Point(double x, double y) { + this.x = x; + this.y = y; + } + } + + // 1.b + public static class Segment { + public final Point a, b; + + public Segment(Point a, Point b) { + this.a = a; + this.b = b; + } + + public double length() { + return Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2)); + } + } + + // 1.c + public static abstract class Polygon implements Iterable { + protected final List points; + + protected Polygon(List points) { + assert points.size() >= 3; + this.points = points; + } + + public Iterator iterator() { + return new Iterator() { + private int i = 0; + + @Override + public boolean hasNext() { + return i < points.size(); + } + + @Override + public Segment next() { + return new Segment(points.get(i++), points.get(i % points.size())); + } + }; + } + + public double perimeter() { + double r = 0.; + for (Segment s : this) + r += s.length(); + return r; + } + + public abstract double area(); + } + + // 1.d.i + public static class Triangle extends Polygon { + public Triangle(Point p1, Point p2, Point p3) { + super(List.of(p1, p2, p3)); + } + + @Override + public double area() { + double p = perimeter() / 2.; + List segs = new ArrayList<>(); + for (Segment s : this) segs.add(s.length()); + return Math.sqrt(p * (p - segs.get(0)) * (p - segs.get(1)) * (p - segs.get(2))); + } + } + + // 1.d.ii + public static class Rectangle extends Polygon { + public Rectangle(Point p1, Point p3) { + super(List.of(p1, new Point(p3.x, p1.y), p3, new Point(p1.x, p3.y))); + } + + @Override + public double area() { + Iterator it = iterator(); + double b = it.next().length(), h = it.next().length(); + return b * h; + } + } + + // 1.d.iii + public static class Square extends Rectangle { + public Square(Point p1, double side) { + super(p1, new Point(p1.x + side, p1.y + side)); + } + + // 1.d.iv + // In generale non vale la pena fare questo override. + // Sicuramente non è necessario perché il metodo area() ereditato da Rectangle è più che sufficiente. + // Volendo si può fare un ragionamento sulla performance: calcolare il lato e poi calcolarne il quadrato è + // più performante rispetto a pescare i primi due lati dall'iteratore di Segment e moltiplicarne le lunghezze, + // però parliamo di un vantaggio davvero negligibile. + @Override + public double area() { + // Il costruttore potrebbe salvare side in un campo ed evitare la seguente sottrazione + // ma sarebbe una replicazione di dati non molto elegante. + double side = points.get(1).x - points.get(0).x; + return side * side; + } + } + + // 1.e.i + public static void main(String args[]) { + Square sq1 = new Square(new Point(11.235, -8.53), 0.1), + sq2 = new Square(new Point(1., 20.), 0.01), + sq3 = new Square(new Point(0., 0.), 0.2); + List squares = new ArrayList<>(); // nel testo d'esame qui c'era List.of(sq1, sq2, sq3) per brevità + squares.add(sq1); // ma List.of() produce liste immutabili, che non possono essere ordinate + squares.add(sq2); // quindi qui abbiamo fatto un arraylist popolato a mano + squares.add(sq3); + Square r = min(squares, (a, b) -> (int) (a.area() - b.area())); + } + + static T min(List l, Comparator c) { + Collections.sort(l, c); + return l.get(0); + } + + // 1.e.ii + // sq1 == r + // ATTENZIONE: il quadrato con l'area più piccola teoricamente è sq2; tuttavia a causa del cast a int nella lambda + // del Comparator tutte le differenze tra le aree vengono arrotondate a 0. L'algoritmo di sorting è stabile sull'ordine originale + // degli elementi, quindi alla fine viene fuori sq1 semplicemente perché è il primo della lista. + // Le altre opzioni possibili nel testo dell'esame sono tutte false; in particolar modo quelle che usano equals(), + // perché non c'è nessun override di equals() per la classe Square, quindi viene invocata la versione ereditata + // da Object che restituisce false di default. +} diff --git a/soluzione appelli/Scritto PO2 25 6 24/java/src/Scritto PO2 25 6 24.iml b/soluzione appelli/Scritto PO2 25 6 24/java/src/Scritto PO2 25 6 24.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/soluzione appelli/Scritto PO2 25 6 24/java/src/Scritto PO2 25 6 24.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file From bfc33aeb4106b999469e731893c6bd5a1d9c147b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 27 Nov 2024 12:19:55 +0100 Subject: [PATCH 188/202] Zoo --- 2024-25/LezioniJava24-25/.gitignore | 29 +++++++++ 2024-25/LezioniJava24-25/.idea/.gitignore | 8 +++ 2024-25/LezioniJava24-25/.idea/misc.xml | 6 ++ 2024-25/LezioniJava24-25/.idea/modules.xml | 8 +++ 2024-25/LezioniJava24-25/.idea/vcs.xml | 6 ++ 2024-25/LezioniJava24-25/LezioniJava24-25.iml | 11 ++++ .../src/misc/po2/unive/it/Zoo.java | 61 +++++++++++++++++++ 7 files changed, 129 insertions(+) create mode 100644 2024-25/LezioniJava24-25/.gitignore create mode 100644 2024-25/LezioniJava24-25/.idea/.gitignore create mode 100644 2024-25/LezioniJava24-25/.idea/misc.xml create mode 100644 2024-25/LezioniJava24-25/.idea/modules.xml create mode 100644 2024-25/LezioniJava24-25/.idea/vcs.xml create mode 100644 2024-25/LezioniJava24-25/LezioniJava24-25.iml create mode 100644 2024-25/LezioniJava24-25/src/misc/po2/unive/it/Zoo.java diff --git a/2024-25/LezioniJava24-25/.gitignore b/2024-25/LezioniJava24-25/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/2024-25/LezioniJava24-25/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/2024-25/LezioniJava24-25/.idea/.gitignore b/2024-25/LezioniJava24-25/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/2024-25/LezioniJava24-25/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/2024-25/LezioniJava24-25/.idea/misc.xml b/2024-25/LezioniJava24-25/.idea/misc.xml new file mode 100644 index 0000000..c3aa11c --- /dev/null +++ b/2024-25/LezioniJava24-25/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2024-25/LezioniJava24-25/.idea/modules.xml b/2024-25/LezioniJava24-25/.idea/modules.xml new file mode 100644 index 0000000..7b904e8 --- /dev/null +++ b/2024-25/LezioniJava24-25/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/2024-25/LezioniJava24-25/.idea/vcs.xml b/2024-25/LezioniJava24-25/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/2024-25/LezioniJava24-25/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2024-25/LezioniJava24-25/LezioniJava24-25.iml b/2024-25/LezioniJava24-25/LezioniJava24-25.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/2024-25/LezioniJava24-25/LezioniJava24-25.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/2024-25/LezioniJava24-25/src/misc/po2/unive/it/Zoo.java b/2024-25/LezioniJava24-25/src/misc/po2/unive/it/Zoo.java new file mode 100644 index 0000000..000b21a --- /dev/null +++ b/2024-25/LezioniJava24-25/src/misc/po2/unive/it/Zoo.java @@ -0,0 +1,61 @@ +package misc.po2.unive.it; + +public class Zoo { + + public static class Animal { + + protected int weight; + + public Animal(int weight) { + this.weight = weight; + } + + public void eat(Animal a) { + this.weight += a.weight; + } + } + + public static class Dog extends Animal { + private String owner; + + public Dog(int weight, String owner) { + super(weight); + this.owner = owner; + } + + public void bark() { + System.out.println("BAUUU!"); + } + + @Override + public void eat(Animal a) { + this.weight += a.weight * 2; + } + } + + public static class Cat extends Animal { + public Cat(int weight) { + super(weight); + } + + public void meow() { + System.out.println("Meow!"); + } + + @Override + public void eat(Animal a) { + this.weight += a.weight / 2; + } + } + + // POLIMORFISMO SUBTYPING + public static void main(String[] args) { + Animal tilde = new Animal(10); + Dog fido = new Dog(20, "Gigi"); // SUBSUMPTION + fido.eat(tilde); + Animal pippo = new Cat(30); + //pippo.meow(); + pippo.eat(fido); + } + +} From b0c14f8893590a1ece9b521a9de8b787fd19435c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 27 Nov 2024 19:21:36 +0100 Subject: [PATCH 189/202] TinyJDK --- .../src/misc/{po2/unive/it => }/Zoo.java | 2 +- .../LezioniJava24-25/src/tinyjdk/Collection.java | 9 +++++++++ .../LezioniJava24-25/src/tinyjdk/Iterable.java | 9 +++++++++ .../LezioniJava24-25/src/tinyjdk/Iterator.java | 6 ++++++ 2024-25/LezioniJava24-25/src/tinyjdk/List.java | 6 ++++++ 2024-25/LezioniJava24-25/src/tinyjdk/Tests.java | 15 +++++++++++++++ 6 files changed, 46 insertions(+), 1 deletion(-) rename 2024-25/LezioniJava24-25/src/misc/{po2/unive/it => }/Zoo.java (97%) create mode 100644 2024-25/LezioniJava24-25/src/tinyjdk/Collection.java create mode 100644 2024-25/LezioniJava24-25/src/tinyjdk/Iterable.java create mode 100644 2024-25/LezioniJava24-25/src/tinyjdk/Iterator.java create mode 100644 2024-25/LezioniJava24-25/src/tinyjdk/List.java create mode 100644 2024-25/LezioniJava24-25/src/tinyjdk/Tests.java diff --git a/2024-25/LezioniJava24-25/src/misc/po2/unive/it/Zoo.java b/2024-25/LezioniJava24-25/src/misc/Zoo.java similarity index 97% rename from 2024-25/LezioniJava24-25/src/misc/po2/unive/it/Zoo.java rename to 2024-25/LezioniJava24-25/src/misc/Zoo.java index 000b21a..c947cb1 100644 --- a/2024-25/LezioniJava24-25/src/misc/po2/unive/it/Zoo.java +++ b/2024-25/LezioniJava24-25/src/misc/Zoo.java @@ -1,4 +1,4 @@ -package misc.po2.unive.it; +package misc; public class Zoo { diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/Collection.java b/2024-25/LezioniJava24-25/src/tinyjdk/Collection.java new file mode 100644 index 0000000..3e3ca3b --- /dev/null +++ b/2024-25/LezioniJava24-25/src/tinyjdk/Collection.java @@ -0,0 +1,9 @@ +package tinyjdk; + +public interface Collection extends Iterable { + void add(T e); + boolean contains(T e); + int size(); + void remove(T e); + void clear(); +} diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/Iterable.java b/2024-25/LezioniJava24-25/src/tinyjdk/Iterable.java new file mode 100644 index 0000000..1772ae7 --- /dev/null +++ b/2024-25/LezioniJava24-25/src/tinyjdk/Iterable.java @@ -0,0 +1,9 @@ +package tinyjdk; + +public interface Iterable { + Iterator iterator(); +} + + + + diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/Iterator.java b/2024-25/LezioniJava24-25/src/tinyjdk/Iterator.java new file mode 100644 index 0000000..3f1efd3 --- /dev/null +++ b/2024-25/LezioniJava24-25/src/tinyjdk/Iterator.java @@ -0,0 +1,6 @@ +package tinyjdk; + +public interface Iterator { + boolean hasNext(); + T next(); +} diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/List.java b/2024-25/LezioniJava24-25/src/tinyjdk/List.java new file mode 100644 index 0000000..5af03bf --- /dev/null +++ b/2024-25/LezioniJava24-25/src/tinyjdk/List.java @@ -0,0 +1,6 @@ +package tinyjdk; + +public interface List extends Collection { + T get(int i); + void set(int i, T e); +} diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/Tests.java b/2024-25/LezioniJava24-25/src/tinyjdk/Tests.java new file mode 100644 index 0000000..50e1b74 --- /dev/null +++ b/2024-25/LezioniJava24-25/src/tinyjdk/Tests.java @@ -0,0 +1,15 @@ +package tinyjdk; + +public class Tests { + + public static void main(String[] args) { + Collection c; + c.add("gigi"); + c.contains("gianni"); + c.remove("gianni"); + c.size(); + c.iterator(); + } + + +} From 120efb0e6c129fe4a9acb1370794568b9470712b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 4 Dec 2024 15:35:40 +0100 Subject: [PATCH 190/202] ArrayList updated --- .../src/tinyjdk/ArrayList.java | 88 +++++++++++++++++++ .../tinyjdk/IndexOutOfBoundsException.java | 5 ++ .../LezioniJava24-25/src/tinyjdk/Tests.java | 7 +- 3 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java create mode 100644 2024-25/LezioniJava24-25/src/tinyjdk/IndexOutOfBoundsException.java diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java b/2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java new file mode 100644 index 0000000..e47529f --- /dev/null +++ b/2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java @@ -0,0 +1,88 @@ +package tinyjdk; + +public class ArrayList implements List { + + private T[] a; + private int sz; + + public ArrayList() { + this.a = (T[]) new Object[10]; + this.sz = 0; + } + + @Override + public int size() { + return sz; + } + + @Override + public void add(T e) { + if (sz >= a.length) { + T[] newa = (T[]) new Object[a.length * 2]; + for (int i = 0; i < a.length; ++i) { + newa[i] = a[i]; + } + a = newa; + } + a[sz++] = e; + } + + @Override + public T get(int i) { + if (i < 0 || i >= sz) throw new IndexOutOfBoundsException(); + return a[i]; + } + + @Override + public void set(int i, T e) { + if (i < 0 || i >= sz) throw new IndexOutOfBoundsException(); + a[i] = e; + } + + @Override + public void clear() { + sz = 0; + } + + + @Override + public boolean contains(T e) { + for (int i = 0; i < sz; ++i) { + if (a[i].equals(e)) + return true; + } + return false; + } + + + @Override + public void remove(T e) { + for (int i = 0; i < sz; ++i) { + if (a[i].equals(e)) { + for (int j = i + 1; j < sz; ++j) { + a[j - 1] = a[j]; + } + --sz; + break; + } + } + } + + + @Override + public Iterator iterator() { + return new Iterator() { + private int pos = 0; + + @Override + public boolean hasNext() { + return pos < sz; + } + + @Override + public T next() { + return a[pos++]; + } + }; + } +} diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/IndexOutOfBoundsException.java b/2024-25/LezioniJava24-25/src/tinyjdk/IndexOutOfBoundsException.java new file mode 100644 index 0000000..8f43a8d --- /dev/null +++ b/2024-25/LezioniJava24-25/src/tinyjdk/IndexOutOfBoundsException.java @@ -0,0 +1,5 @@ +package tinyjdk; + +public class IndexOutOfBoundsException extends RuntimeException { + +} diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/Tests.java b/2024-25/LezioniJava24-25/src/tinyjdk/Tests.java index 50e1b74..1a2a8a2 100644 --- a/2024-25/LezioniJava24-25/src/tinyjdk/Tests.java +++ b/2024-25/LezioniJava24-25/src/tinyjdk/Tests.java @@ -3,12 +3,7 @@ public class Tests { public static void main(String[] args) { - Collection c; - c.add("gigi"); - c.contains("gianni"); - c.remove("gianni"); - c.size(); - c.iterator(); + } From b36f549387d481a4e98afbdeaa25ea7b923b7355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Thu, 5 Dec 2024 17:18:24 +0100 Subject: [PATCH 191/202] Iterators --- .../src/tinyjdk/ArrayList.java | 66 ++++++++++++++++++- .../src/tinyjdk/ArrayListIterator.java | 21 ++++++ 2 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 2024-25/LezioniJava24-25/src/tinyjdk/ArrayListIterator.java diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java b/2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java index e47529f..635afbd 100644 --- a/2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java +++ b/2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java @@ -2,8 +2,8 @@ public class ArrayList implements List { - private T[] a; - private int sz; + T[] a; + int sz; public ArrayList() { this.a = (T[]) new Object[10]; @@ -69,8 +69,48 @@ public void remove(T e) { } + private class MyIteratorNestedNonstatic implements Iterator { + private int pos = 0; + + @Override + public boolean hasNext() { + return pos < sz; + } + + @Override + public T next() { + return a[pos++]; + } + } + + private static class MyIteratorNestedStatic implements Iterator { + private int pos = 0; + private ArrayList that; + + public MyIteratorNestedStatic(ArrayList that) { + this.that = that; + } + + @Override + public boolean hasNext() { + return pos < that.sz; + } + + @Override + public E next() { + return that.a[pos++]; + } + } + @Override public Iterator iterator() { + // con classe globale + //return new ArrayListIterator<>(this); + // con nested static + //return new MyIteratorNestedStatic<>(this); + // con nested non-static + //return new MyIteratorNestedNonstatic(); + // con anonymous class return new Iterator() { private int pos = 0; @@ -85,4 +125,26 @@ public T next() { } }; } + + + + + public static void printAll(Iterable c) { + Iterator it = c.iterator(); + while(it.hasNext()) { + T n = it.next(); + System.out.println(n); + } + } + + public static void main(String[] args) { + Iterable l = new ArrayList(); + + Iterator it = l.iterator(); + while(it.hasNext()) { + Integer n = it.next(); + System.out.println(n); + } + } + } diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/ArrayListIterator.java b/2024-25/LezioniJava24-25/src/tinyjdk/ArrayListIterator.java new file mode 100644 index 0000000..686b3c5 --- /dev/null +++ b/2024-25/LezioniJava24-25/src/tinyjdk/ArrayListIterator.java @@ -0,0 +1,21 @@ +package tinyjdk; + +public class ArrayListIterator implements Iterator { + private int pos = 0; + private ArrayList that; + + public ArrayListIterator(ArrayList that) { + this.that = that; + } + + @Override + public boolean hasNext() { + return pos < that.sz; + } + + @Override + public E next() { + return that.a[pos++]; + } +} + From 6e9cbaceb027b3d0e3f8bf3fcdc002174b9a04ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 6 Dec 2024 15:33:12 +0100 Subject: [PATCH 192/202] LinkedList --- .../src/tinyjdk/ArrayList.java | 2 - .../src/tinyjdk/LinkedList.java | 80 +++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 2024-25/LezioniJava24-25/src/tinyjdk/LinkedList.java diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java b/2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java index 635afbd..552424a 100644 --- a/2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java +++ b/2024-25/LezioniJava24-25/src/tinyjdk/ArrayList.java @@ -127,8 +127,6 @@ public T next() { } - - public static void printAll(Iterable c) { Iterator it = c.iterator(); while(it.hasNext()) { diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/LinkedList.java b/2024-25/LezioniJava24-25/src/tinyjdk/LinkedList.java new file mode 100644 index 0000000..512f3c2 --- /dev/null +++ b/2024-25/LezioniJava24-25/src/tinyjdk/LinkedList.java @@ -0,0 +1,80 @@ +package tinyjdk; + +import java.util.Collections; + +public class LinkedList implements List { + + protected class Node { + T data; + Node next; + + Node(T data) { + this.data = data; + this.next = null; + } + } + + protected Node head; + + public LinkedList() { + head = null; + } + + @Override + public void add(T e) { + if (head == null) { + head = new Node(e); + } + else { + Node n = head; + while (n.next != null) + n = n.next; + n.next = new Node(e); + } + } + + protected Node reach(int i) { + Node n = head; + for (; n != null && i > 0; --i) + n = n.next; + if (i == 0) + return n; + throw new RuntimeException("index out of bounds"); + } + + @Override + public T get(int i) { + return reach(i).data; + } + + @Override + public void set(int i, T e) { + reach(i).data = e; + } + + @Override + public boolean contains(T e) { + return false; + } + + @Override + public int size() { + return 0; + } + + @Override + public void remove(T e) { + + } + + @Override + public void clear() { + + } + + @Override + public Iterator iterator() { + return null; + } + +} From 1275e21bef446f588860d1da4d5b7d2e5d15f642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 11 Dec 2024 15:34:45 +0100 Subject: [PATCH 193/202] Pair --- .../src/tinyjdk/LinkedList.java | 25 ++++++++++++--- .../LezioniJava24-25/src/tinyjdk/Pair.java | 31 +++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 2024-25/LezioniJava24-25/src/tinyjdk/Pair.java diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/LinkedList.java b/2024-25/LezioniJava24-25/src/tinyjdk/LinkedList.java index 512f3c2..731da57 100644 --- a/2024-25/LezioniJava24-25/src/tinyjdk/LinkedList.java +++ b/2024-25/LezioniJava24-25/src/tinyjdk/LinkedList.java @@ -1,7 +1,5 @@ package tinyjdk; -import java.util.Collections; - public class LinkedList implements List { protected class Node { @@ -54,27 +52,44 @@ public void set(int i, T e) { @Override public boolean contains(T e) { + // TODO da fare per casa return false; } @Override public int size() { + // TODO return 0; } @Override public void remove(T e) { - + // TODO } @Override public void clear() { - + head = null; } + @Override public Iterator iterator() { - return null; + return new Iterator() { + private Node current = head; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public T next() { + T data = current.data; + current = current.next; + return data; + } + }; } } diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/Pair.java b/2024-25/LezioniJava24-25/src/tinyjdk/Pair.java new file mode 100644 index 0000000..9beb03e --- /dev/null +++ b/2024-25/LezioniJava24-25/src/tinyjdk/Pair.java @@ -0,0 +1,31 @@ +package tinyjdk; + + +public class Pair { + private A a; + private B b; + + public Pair(A a, B b) { + this.a = a; + this.b = b; + } + + public A getA() { return a;} + public B getB() { return b;} + public void setA(A a) { this.a = a;} + public void setB(B b) { this.b = b;} + + + + public static void main(String[] args) { + Pair p = new Pair<>(true, "ciao"); + p.a = false; + if (p.a) { + + } + else { + + } + } + +} From d97be321a101068bbe52b7016d41404ebeaa4ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 13 Dec 2024 14:02:14 +0100 Subject: [PATCH 194/202] Create Functional.java --- .../LezioniJava24-25/src/misc/Functional.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 2024-25/LezioniJava24-25/src/misc/Functional.java diff --git a/2024-25/LezioniJava24-25/src/misc/Functional.java b/2024-25/LezioniJava24-25/src/misc/Functional.java new file mode 100644 index 0000000..9b08bbd --- /dev/null +++ b/2024-25/LezioniJava24-25/src/misc/Functional.java @@ -0,0 +1,84 @@ +package misc; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class Functional { + + public interface Function { + B apply(A x); + } + + public interface Consumer { + void apply(T x); + } + + public static Collection map(Collection c, Function f) { + Collection r = new ArrayList(); + for (A a : c) { + B b = f.apply(a); + r.add(b); + } + return r; + } + + public static void iter(Collection c, Consumer f) { + for (T x : c) { + f.apply(x); + } + } + + public static Collection filter(Collection c, Function f) { + Collection r = new ArrayList<>(); + for (T x : c) { + if (f.apply(x)) { + r.add(x); + } + } + return r; + } + + + public static void main(String[] args) { + { + List l = List.of("ciao", "sono", "franco", "!"); + Collection r = map(l, new Function<>() { + public Integer apply(String x) { + return x.length(); + } + }); + } + + { + List l = List.of(-56, 345, 11, 0, -456, 23); + Collection r = map(l, new Function<>() { + public Boolean apply(Integer x) { + return x > 0; + } + }); + } + + { + List l = List.of(-56, 345, 11, 0, -456, 23); + iter(l, new Consumer() { + public void apply(Integer x) { + System.out.println(x); + } + }); + } + + { + List l = List.of(-56, 345, 11, 0, -456, 23); + Collection r = filter(l, new Function() { + public Boolean apply(Integer x) { + return x < 0; + } + }); + } + + } + + + +} From 3407f47c6a3d82cd0265ea065cd8f8e3bb240660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 13 Dec 2024 15:38:03 +0100 Subject: [PATCH 195/202] Functional --- .../LezioniJava24-25/src/misc/Functional.java | 55 ++++++++++++++++++- .../LezioniJava24-25/src/tinyjdk/Pair.java | 27 ++------- 2 files changed, 57 insertions(+), 25 deletions(-) diff --git a/2024-25/LezioniJava24-25/src/misc/Functional.java b/2024-25/LezioniJava24-25/src/misc/Functional.java index 9b08bbd..d6eebc2 100644 --- a/2024-25/LezioniJava24-25/src/misc/Functional.java +++ b/2024-25/LezioniJava24-25/src/misc/Functional.java @@ -3,13 +3,14 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.function.BiFunction; public class Functional { public interface Function { B apply(A x); } - + public interface Consumer { void apply(T x); } @@ -39,8 +40,57 @@ public static Collection filter(Collection c, Function f) return r; } + public interface BiFunction { + C apply(A a, B b); + } + + public static R fold(Collection c, R zero, BiFunction f) { + R r = zero; + for (T x : c) { + r = f.apply(x, r); + } + return r; + } + public static void main(String[] args) { + { + List l = List.of(1, 2, 3, 4, 5); + String r = fold(l, "", new BiFunction<>() { + public String apply(Integer x, String acc) { + return acc.concat(x.toString()); + } + }); + } + + { + List l = List.of(-56, 345, 11, 0, -456, 23); + int r = fold(l, 1, new BiFunction() { + public Integer apply(Integer x, Integer acc) { + return x * acc; + } + }); + } + + { + List l = List.of(-56.567, 345.356, 11.3254, 0.0, -456.345, 23.345); + double r = fold(l, 0.0, new BiFunction<>() { + public Double apply(Double x, Double acc) { + return x + acc; + } + }); + } + + { + List l = List.of("ciao", "sono", "franco", "!"); + String r = fold(l, "", new BiFunction<>() { + public String apply(String x, String acc) { + return x.concat(acc); + } + }); + } + + { List l = List.of("ciao", "sono", "franco", "!"); Collection r = map(l, new Function<>() { @@ -52,11 +102,12 @@ public Integer apply(String x) { { List l = List.of(-56, 345, 11, 0, -456, 23); - Collection r = map(l, new Function<>() { + Collection r = map(l, new Function() { public Boolean apply(Integer x) { return x > 0; } }); + Collection r = map(l, (x) -> x > 0); } { diff --git a/2024-25/LezioniJava24-25/src/tinyjdk/Pair.java b/2024-25/LezioniJava24-25/src/tinyjdk/Pair.java index 9beb03e..62cbaff 100644 --- a/2024-25/LezioniJava24-25/src/tinyjdk/Pair.java +++ b/2024-25/LezioniJava24-25/src/tinyjdk/Pair.java @@ -2,30 +2,11 @@ public class Pair { - private A a; - private B b; + public final A fst; + public final B snd; public Pair(A a, B b) { - this.a = a; - this.b = b; + this.fst = a; + this.snd = b; } - - public A getA() { return a;} - public B getB() { return b;} - public void setA(A a) { this.a = a;} - public void setB(B b) { this.b = b;} - - - - public static void main(String[] args) { - Pair p = new Pair<>(true, "ciao"); - p.a = false; - if (p.a) { - - } - else { - - } - } - } From ddf19f2dd18078a2cca65a1c4b479985b97b89fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 13 Dec 2024 15:38:36 +0100 Subject: [PATCH 196/202] Update Functional.java --- 2024-25/LezioniJava24-25/src/misc/Functional.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/2024-25/LezioniJava24-25/src/misc/Functional.java b/2024-25/LezioniJava24-25/src/misc/Functional.java index d6eebc2..161d8f7 100644 --- a/2024-25/LezioniJava24-25/src/misc/Functional.java +++ b/2024-25/LezioniJava24-25/src/misc/Functional.java @@ -10,7 +10,7 @@ public class Functional { public interface Function { B apply(A x); } - + public interface Consumer { void apply(T x); } @@ -107,7 +107,7 @@ public Boolean apply(Integer x) { return x > 0; } }); - Collection r = map(l, (x) -> x > 0); + Collection r2 = map(l, (x) -> x > 0); } { From a0b98771fce0151ff170da68b88087c6b8b52010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Sun, 12 Jan 2025 17:51:43 +0100 Subject: [PATCH 197/202] Last lesson --- .../LezioniJava24-25/src/misc/Functional.java | 7 ++- .../LezioniJava24-25/src/misc/Sorting.java | 47 +++++++++++++++++++ 2024-25/LezioniJava24-25/src/misc/Zoo.java | 16 ++++++- 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 2024-25/LezioniJava24-25/src/misc/Sorting.java diff --git a/2024-25/LezioniJava24-25/src/misc/Functional.java b/2024-25/LezioniJava24-25/src/misc/Functional.java index 161d8f7..5b9db97 100644 --- a/2024-25/LezioniJava24-25/src/misc/Functional.java +++ b/2024-25/LezioniJava24-25/src/misc/Functional.java @@ -7,6 +7,8 @@ public class Functional { + + public interface Function { B apply(A x); } @@ -102,12 +104,13 @@ public Integer apply(String x) { { List l = List.of(-56, 345, 11, 0, -456, 23); + final int k = 0; Collection r = map(l, new Function() { public Boolean apply(Integer x) { - return x > 0; + return x > k; } }); - Collection r2 = map(l, (x) -> x > 0); + Collection r2 = map(l, (x) -> x > k); } { diff --git a/2024-25/LezioniJava24-25/src/misc/Sorting.java b/2024-25/LezioniJava24-25/src/misc/Sorting.java new file mode 100644 index 0000000..d2bc824 --- /dev/null +++ b/2024-25/LezioniJava24-25/src/misc/Sorting.java @@ -0,0 +1,47 @@ +package misc; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class Sorting { + + + public static > void sort(List list) { ... } + + + /*public interface Comparator { + int compare(T o1, T o2); + }*/ + + public static void main(String[] args) { + { + List l = new ArrayList<>(); + l.add(new Zoo.Dog(20, "mio")); + l.add(new Zoo.Dog(29, "mio")); + l.add(new Zoo.Dog(28, "mio")); + sort(l); + + + + + Collections.sort(l, new Comparator<>() { + @Override + public int compare(Zoo.Animal a, Zoo.Animal b) { + return a.weight - b.weight; + } + }); + } + { + List l = List.of("ciao", "pippo", "gigio", "byebye"); + Collections.sort(l, new Comparator<>() { + public int compare(String a, String b) { + return b.length() - a.length(); + } + }); + } + } + + +} diff --git a/2024-25/LezioniJava24-25/src/misc/Zoo.java b/2024-25/LezioniJava24-25/src/misc/Zoo.java index c947cb1..ca244cf 100644 --- a/2024-25/LezioniJava24-25/src/misc/Zoo.java +++ b/2024-25/LezioniJava24-25/src/misc/Zoo.java @@ -2,7 +2,7 @@ public class Zoo { - public static class Animal { + public static class Animal implements Comparable { protected int weight; @@ -13,6 +13,11 @@ public Animal(int weight) { public void eat(Animal a) { this.weight += a.weight; } + + @Override + public int compareTo(Animal o) { + return this.weight - o.weight; + } } public static class Dog extends Animal { @@ -23,6 +28,15 @@ public Dog(int weight, String owner) { this.owner = owner; } + @Override + public int compareTo(Animal o) { + if (o instanceof Dog) { + Dog d = (Dog) o; + ... + } + else return super.compareTo(o); + } + public void bark() { System.out.println("BAUUU!"); } From cc21af372d446b4dc8145b63fb419df0a7dee88d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Wed, 24 Sep 2025 15:47:18 +0200 Subject: [PATCH 198/202] Nuovi appelli --- .../cpp/Appello_14_6_24/Appello_14_6_24.cpp | 35 +++++ .../cpp/Appello_14_6_24/Appello_14_6_24.sln | 31 ++++ .../Appello_14_6_24/Appello_14_6_24.vcxproj | 135 ++++++++++++++++++ .../Appello_14_6_24.vcxproj.user | 4 + .../java/.idea/workspace.xml | 50 +++++++ .../java/Scritto PO2 10 9 24/.gitignore | 29 ++++ .../java/Scritto PO2 10 9 24/.idea/.gitignore | 8 ++ .../java/Scritto PO2 10 9 24/.idea/misc.xml | 6 + .../Scritto PO2 10 9 24/.idea/modules.xml | 8 ++ .../Scritto PO2 10 9 24.iml | 11 ++ .../java/Scritto PO2 10 9 24/src/Es1.java | 10 ++ .../cpp/Appello_15_9_25/Appello_15_9_25.cpp | 29 ++++ .../cpp/Appello_15_9_25/Appello_15_9_25.sln | 31 ++++ .../Appello_15_9_25/Appello_15_9_25.vcxproj | 131 +++++++++++++++++ .../Appello_15_9_25.vcxproj.filters | 22 +++ .../Appello_15_9_25.vcxproj.user | 4 + .../java/Scritto PO2 15 9 25/.gitignore | 29 ++++ .../java/Scritto PO2 15 9 25/.idea/.gitignore | 8 ++ .../java/Scritto PO2 15 9 25/.idea/misc.xml | 6 + .../Scritto PO2 15 9 25/.idea/modules.xml | 8 ++ .../Scritto PO2 15 9 25.iml | 11 ++ .../java/Scritto PO2 15 9 25/src/Es1.java | 23 +++ 22 files changed, 629 insertions(+) create mode 100644 soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.cpp create mode 100644 soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.sln create mode 100644 soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj create mode 100644 soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj.user create mode 100644 soluzione appelli/Scritto PO2 10 9 24/java/.idea/workspace.xml create mode 100644 soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.gitignore create mode 100644 soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/Scritto PO2 10 9 24.iml create mode 100644 soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/src/Es1.java create mode 100644 soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.cpp create mode 100644 soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.sln create mode 100644 soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj create mode 100644 soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj.filters create mode 100644 soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj.user create mode 100644 soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.gitignore create mode 100644 soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/.gitignore create mode 100644 soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/misc.xml create mode 100644 soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/modules.xml create mode 100644 soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/Scritto PO2 15 9 25.iml create mode 100644 soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/src/Es1.java diff --git a/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.cpp b/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.cpp new file mode 100644 index 0000000..df04c28 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.cpp @@ -0,0 +1,35 @@ + +// Questo sorgente contiene le soluzioni dell'esame scritto di PO2 del 5/9/2023 per ciò che riguarda il quesito 2, ovvero la domanda che coinvolge C++. +// I quesiti 1-5 riguardanti Java sono in un sorgente Java a parte, non qui. +// Il codice C++ qui esposto è standard C++ vanilla (a.k.a. C++03), sebbene il progetto VS sia configurato con il compilatore di default C++14 + +#include +#include + +using namespace std; + +template +ostream& operator<<(ostream& os, const Container& c) +{ + os << "["; + for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) + os << " " << *it; + os << "]"; + return os; +} + +template +typename Container::value_type sum(const Container& c) +{ + typename Container::value_type r; + for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) + { + r += *it; + } + return r; +} + + +int main() +{ +} diff --git a/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.sln b/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.sln new file mode 100644 index 0000000..9683cae --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32526.322 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Appello_14_6_24", "Appello_14_6_24.vcxproj", "{C7C15495-50AD-4636-90B2-C312702B99FD}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x64.ActiveCfg = Debug|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x64.Build.0 = Debug|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x86.ActiveCfg = Debug|Win32 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Debug|x86.Build.0 = Debug|Win32 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x64.ActiveCfg = Release|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x64.Build.0 = Release|x64 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x86.ActiveCfg = Release|Win32 + {C7C15495-50AD-4636-90B2-C312702B99FD}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {49DE568A-D54C-485E-A095-5948A84AED91} + EndGlobalSection +EndGlobal diff --git a/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj b/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj new file mode 100644 index 0000000..748b08d --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {C7C15495-50AD-4636-90B2-C312702B99FD} + Appello3622 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level4 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj.user b/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 9 24/cpp/Appello_14_6_24/Appello_14_6_24.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 9 24/java/.idea/workspace.xml b/soluzione appelli/Scritto PO2 10 9 24/java/.idea/workspace.xml new file mode 100644 index 0000000..b8c4216 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 9 24/java/.idea/workspace.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + 1725443138444 + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.gitignore b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/.gitignore b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/misc.xml b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/misc.xml new file mode 100644 index 0000000..8633114 --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/modules.xml b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/modules.xml new file mode 100644 index 0000000..6303aee --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/Scritto PO2 10 9 24.iml b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/Scritto PO2 10 9 24.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/Scritto PO2 10 9 24.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/src/Es1.java b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/src/Es1.java new file mode 100644 index 0000000..3cd97fe --- /dev/null +++ b/soluzione appelli/Scritto PO2 10 9 24/java/Scritto PO2 10 9 24/src/Es1.java @@ -0,0 +1,10 @@ +public class Es1 { + + public static void m() { + double x = 3.; + int y = 5; + System.out.println(x / y); + } + + +} diff --git a/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.cpp b/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.cpp new file mode 100644 index 0000000..e8e88f5 --- /dev/null +++ b/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.cpp @@ -0,0 +1,29 @@ +// Appello_15_9_25.cpp +// + +#include +#include + +using namespace std; + +template +typename InputIterator::value_type average(InputIterator begin, InputIterator end) +{ + typename iterator_traits::value_type r{}; // chiamiamo esplcitamente il costruttore di default con la sintassi di C++14 (perché MSVS non supporta più il vanilla) + //anche InputIterator::value_type andava bene + + size_t n = 0; + for (; begin != end; ++n) + r += *begin++; // il value_type deve avere l'operatore += + if (n == 0) + throw runtime_error("average of empty range"); + return r / n; // e l'opereratore / con size_t some secondo argomento +} + + +int main() +{ + vector v = { 1, 2, 3, 4, 5 }; + cout << average(v.begin(), v.end()) << endl; +} + diff --git a/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.sln b/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.sln new file mode 100644 index 0000000..d1024f1 --- /dev/null +++ b/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36429.23 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Appello_15_9_25", "Appello_15_9_25.vcxproj", "{EAC4145E-6577-4D78-9117-35B81B4A4ADE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EAC4145E-6577-4D78-9117-35B81B4A4ADE}.Debug|x64.ActiveCfg = Debug|x64 + {EAC4145E-6577-4D78-9117-35B81B4A4ADE}.Debug|x64.Build.0 = Debug|x64 + {EAC4145E-6577-4D78-9117-35B81B4A4ADE}.Debug|x86.ActiveCfg = Debug|Win32 + {EAC4145E-6577-4D78-9117-35B81B4A4ADE}.Debug|x86.Build.0 = Debug|Win32 + {EAC4145E-6577-4D78-9117-35B81B4A4ADE}.Release|x64.ActiveCfg = Release|x64 + {EAC4145E-6577-4D78-9117-35B81B4A4ADE}.Release|x64.Build.0 = Release|x64 + {EAC4145E-6577-4D78-9117-35B81B4A4ADE}.Release|x86.ActiveCfg = Release|Win32 + {EAC4145E-6577-4D78-9117-35B81B4A4ADE}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {24327C14-2D26-49B5-A017-9A80DAEEC947} + EndGlobalSection +EndGlobal diff --git a/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj b/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj new file mode 100644 index 0000000..e912a6a --- /dev/null +++ b/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj @@ -0,0 +1,131 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {eac4145e-6577-4d78-9117-35b81b4a4ade} + Appello15925 + 10.0 + + + + Application + true + v145 + Unicode + + + Application + false + v145 + true + Unicode + + + Application + true + v145 + Unicode + + + Application + false + v145 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj.filters b/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj.filters new file mode 100644 index 0000000..9f933e4 --- /dev/null +++ b/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj.user b/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/soluzione appelli/Scritto PO2 15 9 25/cpp/Appello_15_9_25/Appello_15_9_25.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.gitignore b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/.gitignore b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/misc.xml b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/misc.xml new file mode 100644 index 0000000..31e1ebc --- /dev/null +++ b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/modules.xml b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/modules.xml new file mode 100644 index 0000000..4d0b0cf --- /dev/null +++ b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/Scritto PO2 15 9 25.iml b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/Scritto PO2 15 9 25.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/Scritto PO2 15 9 25.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/src/Es1.java b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/src/Es1.java new file mode 100644 index 0000000..467efef --- /dev/null +++ b/soluzione appelli/Scritto PO2 15 9 25/java/Scritto PO2 15 9 25/src/Es1.java @@ -0,0 +1,23 @@ +import javax.script.Bindings; +import java.time.Instant; +import java.util.*; +import java.util.function.Consumer; + +public class Es1 { + public enum Priority { HIGH, MEDIUM, LOW }; + + private Map> map = new EnumMap<>(Priority.class); + + public void register(Priority priority, Consumer consumer) { + map.put(priority, consumer); + } + + public void dispatcher() { + SortedSet>> set = new TreeSet<>(Comparator.comparing(Map.Entry::getKey)); + set.addAll(map.entrySet()); + for (Map.Entry> p : set) { + p.getValue().accept(Instant.now()); + } + } + +} From b2a1cc028994b829771a2b0f788dbdd8a85907ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 19 Dec 2025 15:36:39 +0100 Subject: [PATCH 199/202] Lezioni 2025-26 --- 2025-26/.gitignore | 29 +++++ 2025-26/.idea/.gitignore | 3 + 2025-26/.idea/misc.xml | 6 + 2025-26/.idea/modules.xml | 8 ++ 2025-26/PO2_2025_26.iml | 11 ++ 2025-26/src/IterTest.java | 18 +++ 2025-26/src/Zoo.java | 85 ++++++++++++++ .../src/tinyjdk/dais/unive/it/ArrayList.java | 76 +++++++++++++ .../src/tinyjdk/dais/unive/it/Collection.java | 13 +++ .../src/tinyjdk/dais/unive/it/Iterable.java | 5 + .../src/tinyjdk/dais/unive/it/Iterator.java | 6 + .../src/tinyjdk/dais/unive/it/LinkedList.java | 106 ++++++++++++++++++ 2025-26/src/tinyjdk/dais/unive/it/List.java | 22 ++++ .../src/tinyjdk/dais/unive/it/ListMap.java | 49 ++++++++ 2025-26/src/tinyjdk/dais/unive/it/Map.java | 21 ++++ 2025-26/src/tinyjdk/dais/unive/it/Test.java | 21 ++++ 16 files changed, 479 insertions(+) create mode 100644 2025-26/.gitignore create mode 100644 2025-26/.idea/.gitignore create mode 100644 2025-26/.idea/misc.xml create mode 100644 2025-26/.idea/modules.xml create mode 100644 2025-26/PO2_2025_26.iml create mode 100644 2025-26/src/IterTest.java create mode 100644 2025-26/src/Zoo.java create mode 100644 2025-26/src/tinyjdk/dais/unive/it/ArrayList.java create mode 100644 2025-26/src/tinyjdk/dais/unive/it/Collection.java create mode 100644 2025-26/src/tinyjdk/dais/unive/it/Iterable.java create mode 100644 2025-26/src/tinyjdk/dais/unive/it/Iterator.java create mode 100644 2025-26/src/tinyjdk/dais/unive/it/LinkedList.java create mode 100644 2025-26/src/tinyjdk/dais/unive/it/List.java create mode 100644 2025-26/src/tinyjdk/dais/unive/it/ListMap.java create mode 100644 2025-26/src/tinyjdk/dais/unive/it/Map.java create mode 100644 2025-26/src/tinyjdk/dais/unive/it/Test.java diff --git a/2025-26/.gitignore b/2025-26/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/2025-26/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/2025-26/.idea/.gitignore b/2025-26/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/2025-26/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/2025-26/.idea/misc.xml b/2025-26/.idea/misc.xml new file mode 100644 index 0000000..a9182a4 --- /dev/null +++ b/2025-26/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2025-26/.idea/modules.xml b/2025-26/.idea/modules.xml new file mode 100644 index 0000000..b6d4d12 --- /dev/null +++ b/2025-26/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/2025-26/PO2_2025_26.iml b/2025-26/PO2_2025_26.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/2025-26/PO2_2025_26.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/2025-26/src/IterTest.java b/2025-26/src/IterTest.java new file mode 100644 index 0000000..ed057dd --- /dev/null +++ b/2025-26/src/IterTest.java @@ -0,0 +1,18 @@ +import java.util.*; + +public class IterTest { + + public static void main(String[] args) { + Collection a = new ArrayList(); + a.add(10); + a.add(245); + a.add(387); + + Iterator it = a.iterator(); + while (it.hasNext()) { + Integer n = it.next(); + System.out.println(n); + } + + } +} diff --git a/2025-26/src/Zoo.java b/2025-26/src/Zoo.java new file mode 100644 index 0000000..69c3e7b --- /dev/null +++ b/2025-26/src/Zoo.java @@ -0,0 +1,85 @@ + + +/*struct S { + int n; + float x; +} + +struct U { + S parent; + char* s; +} + +void f(S this) { + if (this.n > 5) +} + +int m(U this) { return this.parent.n + strlen(this.s) } + + +public class S { + public int n; + public float x; + + public void f() { + if (this.n > 5) { ... } + } +} + +public class U extends S { + public String s; + public int m() { return n + s.length(); } + +} +*/ + + +public class Zoo { + public static class Animal { + protected int weight; + + public Animal(int weight) { + this.weight = weight; + } + + public void eat(Animal a) { + this.weight += a.weight; + } + } + + public static class Dog extends Animal { + private String hair; + + public Dog(int weight, String hair) { + super(weight); + this.hair = hair; + } + + @Override + public void eat(Animal a) { + this.weight += a.weight / 3; + } + + public String getHair() { return hair;} + } + + public static class Cat extends Animal { + public Cat(int weight) { + super(weight); + } + + public void eat(Animal a) { + this.weight += a.weight * 3; + } + } + + public static void main(String[] args) { + Animal pippo = new Dog(5, "liscio"); + Dog gigio = new Dog(7, "riccio"); + pippo.eat(gigio); + + Animal romeo = new Cat(40); + romeo.eat(gigio); + + } +} \ No newline at end of file diff --git a/2025-26/src/tinyjdk/dais/unive/it/ArrayList.java b/2025-26/src/tinyjdk/dais/unive/it/ArrayList.java new file mode 100644 index 0000000..d5e246c --- /dev/null +++ b/2025-26/src/tinyjdk/dais/unive/it/ArrayList.java @@ -0,0 +1,76 @@ +package tinyjdk.dais.unive.it; + +public class ArrayList implements List { + private T[] a; + private int sz; + + public ArrayList() { + clear(); + } + + @Override + public void add(T o) { + if (sz >= a.length) { + T[] newa = (T[]) new Object[a.length * 2]; + for (int i = 0; i < a.length; i++) + newa[i] = a[i]; + a = newa; + } + a[sz++] = o; + } + + @Override + public boolean contains(T o) { + for (int i = 0; i < sz; i++) { + T e = a[i]; + if (e.equals(o)) return true; + } + return false; + } + + @Override + public int size() { + return sz; + } + + @Override + public void remove(T o) { + // TODO + } + + @Override + public void clear() { + a = (T[]) new Object[100]; + sz = 0; + } + + @Override + public T get(int i) { + if (i >= 0 && i < sz) + return a[i]; + throw new RuntimeException(); + } + + @Override + public void set(int i, T e) { + if (i >= 0 && i < sz) + a[i] = e; + throw new RuntimeException(); + } + + + /*private class MyIterator implements Iterator { + private int pos = 0; + + @Override + public boolean hasNext() { + return pos < sz; + } + + @Override + public T next() { + return a[pos++]; + } + }*/ + +} diff --git a/2025-26/src/tinyjdk/dais/unive/it/Collection.java b/2025-26/src/tinyjdk/dais/unive/it/Collection.java new file mode 100644 index 0000000..821b3d9 --- /dev/null +++ b/2025-26/src/tinyjdk/dais/unive/it/Collection.java @@ -0,0 +1,13 @@ +package tinyjdk.dais.unive.it; + +public interface Collection extends Iterable { + void add(T o); + boolean contains(T o); + int size(); + void remove(T o); + void clear(); + + default boolean isEmpty() { + return size() == 0; + } +} diff --git a/2025-26/src/tinyjdk/dais/unive/it/Iterable.java b/2025-26/src/tinyjdk/dais/unive/it/Iterable.java new file mode 100644 index 0000000..247b2e5 --- /dev/null +++ b/2025-26/src/tinyjdk/dais/unive/it/Iterable.java @@ -0,0 +1,5 @@ +package tinyjdk.dais.unive.it; + +public interface Iterable { + Iterator iterator(); +} diff --git a/2025-26/src/tinyjdk/dais/unive/it/Iterator.java b/2025-26/src/tinyjdk/dais/unive/it/Iterator.java new file mode 100644 index 0000000..fb64eb4 --- /dev/null +++ b/2025-26/src/tinyjdk/dais/unive/it/Iterator.java @@ -0,0 +1,6 @@ +package tinyjdk.dais.unive.it; + +public interface Iterator { + boolean hasNext(); + T next(); +} diff --git a/2025-26/src/tinyjdk/dais/unive/it/LinkedList.java b/2025-26/src/tinyjdk/dais/unive/it/LinkedList.java new file mode 100644 index 0000000..be99534 --- /dev/null +++ b/2025-26/src/tinyjdk/dais/unive/it/LinkedList.java @@ -0,0 +1,106 @@ +package tinyjdk.dais.unive.it; + +public class LinkedList implements List { + + /* + struct node { + int data; + struct node* next; + } + */ + + private class Node { + T data; + Node next; + + public Node(T data) { + this.data = data; + this.next = null; + } + } + + private Node head = null; + + public LinkedList() { + } + + @Override + public void add(T e) { + if (head == null) + head = new Node(e); + else { + Node n = head; + while (n != null) { + n = n.next; + } + n = new Node(e); + } + } + + @Override + public int size() { + int cnt = 0; + for (Node n = head; n != null; ++cnt, n = n.next); + return cnt; + } + + + private Node seek(int i) { + assert (i >= 0); + Node n = head; + for (; i-- > 0; n = n.next) { + if (n.next == null) + throw new RuntimeException(); + } + return n; + } + + @Override + public T get(int i) { + return seek(i).data; + } + + @Override + public void set(int i, T e) { + seek(i).data = e; + } + + @Override + public boolean contains(T o) { + Node n = head; + while (n != null) { + if (n.data.equals(o)) return true; + n = n.next; + } + return false; + } + + @Override + public void remove(T o) { + // TODO + } + + @Override + public void clear() { + head = null; + } + + @Override + public Iterator iterator() { + return new Iterator() { + private Node current = head; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public T next() { + T r = current.data; + current = current.next; + return r; + } + }; + } +} diff --git a/2025-26/src/tinyjdk/dais/unive/it/List.java b/2025-26/src/tinyjdk/dais/unive/it/List.java new file mode 100644 index 0000000..9e6eef5 --- /dev/null +++ b/2025-26/src/tinyjdk/dais/unive/it/List.java @@ -0,0 +1,22 @@ +package tinyjdk.dais.unive.it; + +public interface List extends Collection { + T get(int index); + void set(int index, T element); + + default Iterator iterator() { + return new Iterator() { + private int pos = 0; + + @Override + public boolean hasNext() { + return pos < size(); + } + + @Override + public T next() { + return get(pos++); + } + }; + } +} diff --git a/2025-26/src/tinyjdk/dais/unive/it/ListMap.java b/2025-26/src/tinyjdk/dais/unive/it/ListMap.java new file mode 100644 index 0000000..7b5bf3b --- /dev/null +++ b/2025-26/src/tinyjdk/dais/unive/it/ListMap.java @@ -0,0 +1,49 @@ +package tinyjdk.dais.unive.it; + +public class ListMap implements Map { + private List> l; + + public ListMap() { + l = new ArrayList<>(); + } + + @Override + public void put(K key, V value) { + l.add(new Map.Pair<>(key, value)); + } + + @Override + public V get(K key) { + Iterator> it = l.iterator(); + while (it.hasNext()) { + Map.Pair p = it.next(); + if (p.key.equals(key)) { + return p.value; + } + } + throw new RuntimeException("key not found"); + } + + @Override + public int size() { + return l.size(); + } + + @Override + public boolean containsKey(K key) { + Iterator> it = l.iterator(); + while (it.hasNext()) { + Map.Pair p = it.next(); + if (p.key.equals(key)) { + return true; + } + } + return false; + } + + @Override + public Iterator> iterator() { + return l.iterator(); + } + +} diff --git a/2025-26/src/tinyjdk/dais/unive/it/Map.java b/2025-26/src/tinyjdk/dais/unive/it/Map.java new file mode 100644 index 0000000..0fd1720 --- /dev/null +++ b/2025-26/src/tinyjdk/dais/unive/it/Map.java @@ -0,0 +1,21 @@ +package tinyjdk.dais.unive.it; + +public interface Map extends Iterable> { + + class Pair { + public final K key; + public final V value; + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + } + + void put(K key, V value); + V get(K key); + int size(); + + default boolean isEmpty() { return size() == 0; } + + boolean containsKey(K key); +} diff --git a/2025-26/src/tinyjdk/dais/unive/it/Test.java b/2025-26/src/tinyjdk/dais/unive/it/Test.java new file mode 100644 index 0000000..0beb209 --- /dev/null +++ b/2025-26/src/tinyjdk/dais/unive/it/Test.java @@ -0,0 +1,21 @@ +package tinyjdk.dais.unive.it; + + + +public class Test { + public static void main(String[] args) { + } + + public static void foo(Collection c) { + c.add(1); + c.add(62); + c.add(234); + + Iterator it = c.iterator(); + while (it.hasNext()) { + Integer e = it.next(); + System.out.println(e); + } + + } +} From 2ce5e1e506fb47ab0f78a2177c53813fd268d571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 16 Jan 2026 13:51:02 +0100 Subject: [PATCH 200/202] Lambda --- 2025-26/.idea/vcs.xml | 6 ++ 2025-26/src/Lambda.java | 137 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 2025-26/.idea/vcs.xml create mode 100644 2025-26/src/Lambda.java diff --git a/2025-26/.idea/vcs.xml b/2025-26/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/2025-26/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2025-26/src/Lambda.java b/2025-26/src/Lambda.java new file mode 100644 index 0000000..ab5bf6c --- /dev/null +++ b/2025-26/src/Lambda.java @@ -0,0 +1,137 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Random; +import java.util.function.Function; + +public class Lambda { + + @FunctionalInterface + interface Function { + R apply(T x); + } + + interface Consumer { + void accept(T x); + } + + interface Supplier { + T get(); + } + + interface Runnable { + void run(); + } + + + public static void iter(Collection c, Consumer p) { + for (T e : c) { + p.accept(e); + } + } + + public static List map(Collection a, Function f) { + List l = new ArrayList<>(); + for (A e : a) { + B b = f.apply(e); + l.add(b); + } + return l; + } + + public static Collection filter(Collection c, Function f) { + List l = new ArrayList<>(); + for (T e : c) { + if (f.apply(e)) + l.add(e); + } + return l; + } + + public static List generate(int n, Supplier f) { + List l = new ArrayList<>(); + for (; n > 0; n--) { + T x = f.get(); + l.add(x); + } + return l; + } + + static void f(int x) { + generate(x, () -> x); + } + + public static void main(String[] args) { + { + List u = generate(20, () -> "ciao"); + + Random rnd = new Random(); + List u2 = generate(20, () -> rnd.nextInt(100)); + + int k = rnd.nextInt(100); + List u3 = generate(20, () -> k); + } + { + List a = new ArrayList<>(); + a.add(10); + a.add(-345); + a.add(33); + Collection u = filter(a, x -> x > 0); + } + + { + List a = new ArrayList<>(); + a.add(1); + a.add(2); + a.add(3); + iter(a, new Consumer<>() { + @Override + public void accept(Integer e) { + a.add(e); + } + }); + } + { + List a = new ArrayList<>(); + a.add("pi"); + a.add("po"); + a.add("pa"); + List u = map(a, new Function<>() { + @Override + public Integer apply(String e) { + return e.length(); + } + }); + } + { + List a = new ArrayList<>(); + a.add(1); + a.add(12); + a.add(23); + List u = map(a, new Function<>() { + @Override + public Boolean apply(Integer e) { + return e % 2 == 0; + } + }); + } + { + List a = new ArrayList<>(); + a.add(1); + a.add(12); + a.add(23); + List u = map(a, new Function<>() { + @Override + public Integer apply(Integer e) { + return (int) Math.sqrt(e); + } + }); + List u2 = map(a, x -> x + 1); + + + } + + } + + +} From ce60623bf54f0f9165ccb40484fc5b6a65f58b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Fri, 16 Jan 2026 15:35:23 +0100 Subject: [PATCH 201/202] Sorting --- 2025-26/src/Lambda.java | 37 +++++++++++++++++++++++++++++++++++++ 2025-26/src/Sorting.java | 15 +++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 2025-26/src/Sorting.java diff --git a/2025-26/src/Lambda.java b/2025-26/src/Lambda.java index ab5bf6c..0ba839e 100644 --- a/2025-26/src/Lambda.java +++ b/2025-26/src/Lambda.java @@ -23,6 +23,14 @@ interface Runnable { void run(); } + public static void delay(int ms, Runnable r) { + try { + Thread.sleep(ms); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + r.run(); + } public static void iter(Collection c, Consumer p) { for (T e : c) { @@ -62,6 +70,35 @@ static void f(int x) { } public static void main(String[] args) { + { + Runnable pippo = () -> System.out.println("ciao"); + + pippo.run(); + + + int x = 8 * 34 / 67; + + boolean y = x < 0; + + if (y) { + x = 8; + } + else { + System.out.println(x); + } + + } + + + + { + delay(1000, new Runnable() { + @Override + public void run() { + System.out.println("ciao!"); + } + }); + } { List u = generate(20, () -> "ciao"); diff --git a/2025-26/src/Sorting.java b/2025-26/src/Sorting.java new file mode 100644 index 0000000..9173bdf --- /dev/null +++ b/2025-26/src/Sorting.java @@ -0,0 +1,15 @@ +import tinyjdk.dais.unive.it.ArrayList; + +import java.util.Collections; +import java.util.List; + +public class Sorting { + + public static void main(String[] args) { + + List l = List.of(45, 78, 234, -456, 2); + + Collections.sort(l, (a, b) -> a - b); + } + +} From cc7b4c08ec9cc4bf6e77251741291a079ee0be3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvise=20Span=C3=B2?= Date: Tue, 3 Feb 2026 18:44:06 +0100 Subject: [PATCH 202/202] Update Sorting.java --- 2025-26/src/Sorting.java | 61 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/2025-26/src/Sorting.java b/2025-26/src/Sorting.java index 9173bdf..e75b6ac 100644 --- a/2025-26/src/Sorting.java +++ b/2025-26/src/Sorting.java @@ -1,15 +1,72 @@ import tinyjdk.dais.unive.it.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.List; public class Sorting { + public static class Piatto implements Comparable { + private List ingredienti; + + public Piatto(List ingredienti) { + this.ingredienti = ingredienti; + } + + @Override + public int compareTo(Piatto o) { + return ingredienti.size() - o.ingredienti.size(); + } + + public int numero_ingredienti() { return ingredienti.size(); } + } + + public static class PiattoVegano extends Piatto { + private int costo; + + public PiattoVegano(List ingredienti, int costo) { + super(ingredienti); + for (String s : ingredienti) { + assert(isVegan(s)); + } + this.costo = costo; + } + + public static boolean isVegan(String s) { return true; } + + @Override + public int compareTo(Piatto o) { + if (o instanceof PiattoVegano) + return costo - ((PiattoVegano) o).costo; + else return -1; + } + + } + public static void main(String[] args) { + { + Piatto carbonara = new Piatto(List.of("uova", "guanciale", "pecorino romano", "pasta")); + Piatto polloArrosto = new Piatto(List.of("pollo", "olio", "rosmarino", "patate")); + Piatto frittura = new Piatto(List.of("calamari", "gamberetti", "totani", "schie", "seppioline")); + List l = List.of(carbonara, polloArrosto, frittura); + Collections.sort(l); + } + + { + List l = List.of(45, 78, 234, -456, 2); + + Collections.sort(l, (a, b) -> a - b); + } + { + Zoo.Animal fido = new Zoo.Animal(40); + Zoo.Animal gigio = new Zoo.Animal(5); + Zoo.Animal pippo = new Zoo.Animal(140); + Zoo.Animal pluto = new Zoo.Animal(12); + List l = List.of(fido, gigio, pippo, pluto); - List l = List.of(45, 78, 234, -456, 2); + Collections.sort(l, (a1, a2) -> a2.weight - a1.weight); - Collections.sort(l, (a, b) -> a - b); + } } }