From 3f2725fde77517ac7a2b0affd88433ae7cbbdcd0 Mon Sep 17 00:00:00 2001 From: david <david@lipn.fr> Date: Mon, 9 Sep 2019 13:52:25 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20des=20fonctions=20augmentations=20et=20?= =?UTF-8?q?reudction=20capacit=C3=A9=20c++?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TP1/CPP/arraylist.hpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/TP1/CPP/arraylist.hpp b/TP1/CPP/arraylist.hpp index 68ad087..9f4d5d6 100644 --- a/TP1/CPP/arraylist.hpp +++ b/TP1/CPP/arraylist.hpp @@ -29,9 +29,9 @@ public: */ bool append(P x){ bool memory_allocation = false; - if( enlarging_capacity() ){ + if( do_we_need_to_enlarge_capacity() ){ memory_allocation = true; - data.reserve( data.capacity() *2 ); + enlarge_capacity(); } data.push_back(x); return memory_allocation; @@ -47,9 +47,9 @@ public: bool pop_back(){ bool memory_reduction = false; if(!data.empty()){ - if( reducing_capacity() ){ + if( do_we_need_to_reduce_capacity() ){ memory_reduction = true; - data.reserve( data.capacity() /2 ); + reduce_capacity(); } data.pop_back(); } @@ -81,18 +81,32 @@ private: 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. */ - bool enlarging_capacity(){ + bool do_we_need_to_enlarge_capacity(){ return data.size() >= (data.capacity() * 3)/4; } + /** + Cette fonction augmente la capacité du tableau. + */ + void enlarge_capacity(){ + data.reserve( data.capacity() *2 ); + } + /** 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. */ - bool reducing_capacity(){ + bool do_we_need_to_reduce_capacity(){ return data.size() <= data.capacity()/4 && data.size() >4; } + /** + Cette fonction reduit la capacité du tableau. + */ + void reduce_capacity(){ + data.reserve( data.capacity() /2 ); + } + }; -- GitLab