Skip to content
Snippets Groups Projects
Commit e5158535 authored by david's avatar david
Browse files

Ajout des fonctions augmentations et reudction capacité java

parent 3f2725fd
No related branches found
No related tags found
No related merge requests found
...@@ -25,10 +25,9 @@ public class ArrayListProxy<T> { ...@@ -25,10 +25,9 @@ public class ArrayListProxy<T> {
*/ */
boolean append(T x){ boolean append(T x){
boolean memory_allocation = false; boolean memory_allocation = false;
if( enlarging_capacity() ){ if( do_we_need_to_enlarge_capacity() ){
memory_allocation = true; memory_allocation = true;
capacity *= 2; enlarge_capacity();
data.ensureCapacity( capacity );
} }
data.add(x); data.add(x);
return memory_allocation; return memory_allocation;
...@@ -44,10 +43,9 @@ public class ArrayListProxy<T> { ...@@ -44,10 +43,9 @@ public class ArrayListProxy<T> {
boolean pop_back(){ boolean pop_back(){
boolean memory_reduction = false; boolean memory_reduction = false;
if(!data.isEmpty()){ if(!data.isEmpty()){
if( reducing_capacity() ){ if( do_we_need_to_reduce_capacity() ){
memory_reduction = true; memory_reduction = true;
capacity /= 2; reduce_capacity();
data.ensureCapacity( capacity );
} }
data.remove(data.size()-1); data.remove(data.size()-1);
} }
...@@ -77,20 +75,36 @@ public class ArrayListProxy<T> { ...@@ -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. 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. @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; 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. 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. @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; 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. // Tableau dynamique en Java. Sa capacité réelle est masquée, mais on peut avoir un contrôle dessus.
private ArrayList<T> data; private ArrayList<T> data;
// Capacité réelle du tableau data. // Capacité réelle du tableau data.
private int capacity; private int capacity;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment