From e5158535220f8cf6b719e77155bc21e6ff2d1a5b Mon Sep 17 00:00:00 2001 From: david <david@lipn.fr> Date: Mon, 9 Sep 2019 14:06:51 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20des=20fonctions=20augmentations=20et=20?= =?UTF-8?q?reudction=20capacit=C3=A9=20java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TP1/Java/ArrayListProxy.java | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/TP1/Java/ArrayListProxy.java b/TP1/Java/ArrayListProxy.java index 33a6955..f2d7bbc 100644 --- a/TP1/Java/ArrayListProxy.java +++ b/TP1/Java/ArrayListProxy.java @@ -25,10 +25,9 @@ public class ArrayListProxy<T> { */ boolean append(T x){ boolean memory_allocation = false; - if( enlarging_capacity() ){ + if( do_we_need_to_enlarge_capacity() ){ memory_allocation = true; - capacity *= 2; - data.ensureCapacity( capacity ); + enlarge_capacity(); } data.add(x); return memory_allocation; @@ -44,10 +43,9 @@ public class ArrayListProxy<T> { boolean pop_back(){ boolean memory_reduction = false; if(!data.isEmpty()){ - if( reducing_capacity() ){ + if( do_we_need_to_reduce_capacity() ){ memory_reduction = true; - capacity /= 2; - data.ensureCapacity( capacity ); + reduce_capacity(); } data.remove(data.size()-1); } @@ -77,20 +75,36 @@ public class ArrayListProxy<T> { Cette fonction détermine la règle selon laquelle un espace mémoire plus grand sera alloué ou non. @returns true si le tableau doit être agrandi, false sinon. */ - private boolean enlarging_capacity() { + private boolean do_we_need_to_enlarge_capacity() { return data.size() >= (capacity * 3)/4; } + /** + Cette fonction augmente la capacité du tableau. + */ + private void enlarge_capacity(){ + capacity *= 2; + data.ensureCapacity( capacity ); + } + /** Cette fonction détermine la règle selon laquelle un espace mémoire plus petit sera alloué ou non. @returns true si le tableau doit être réduit, false sinon. */ - private boolean reducing_capacity(){ + private boolean do_we_need_to_reduce_capacity(){ return data.size() <= capacity/4 && data.size() >4; } + /** + Cette fonction reduit la capacité du tableau. + */ + void reduce_capacity(){ + capacity /= 2; + data.ensureCapacity( capacity ); + } + // Tableau dynamique en Java. Sa capacité réelle est masquée, mais on peut avoir un contrôle dessus. private ArrayList<T> data; // Capacité réelle du tableau data. private int capacity; -} \ No newline at end of file +} -- GitLab