From 2a43a12b7b870c6fd775f417c1c2e85222e47238 Mon Sep 17 00:00:00 2001 From: Greg Ercolano Date: Sun, 6 Nov 2022 21:59:36 -0800 Subject: Added empty(), ensure size() enlarges new vals = 0 --- FL/Fl_Int_Vector.H | 23 ++++++++++++++++------- src/Fl_Int_Vector.cxx | 22 ++++++++++++++-------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/FL/Fl_Int_Vector.H b/FL/Fl_Int_Vector.H index 89e2d29af..9a2cd45f9 100644 --- a/FL/Fl_Int_Vector.H +++ b/FL/Fl_Int_Vector.H @@ -32,14 +32,15 @@ Common use: \code + #include #include int main() { Fl_Int_Vector v; - // Create an array of values 1,2,3: - v.push_back(1); // add first element - v.push_back(2); // add second element - v.push_back(3); // add third element + // Create an array of values 11,22,33: + v.push_back(11); // add first element + v.push_back(22); // add second element + v.push_back(33); // add third element // Loop through printing the values for ( unsigned int i=0; i size, and if so init new values to 0. + Only currently advised use is to shrink the array size, i.e. (count < size()), + since assignment via index (e.g. v[x] = 123) is not yet supported. */ void Fl_Int_Vector::size(unsigned int count) { - if (count <= 0) { + if (count == 0) { // zero? special case frees memory if (arr_) free(arr_); arr_ = 0; size_ = 0; return; } - if (count > size_) { + if (count > size_) { // array enlarged? realloc + init new vals to 0 arr_ = (int *)realloc(arr_, count * sizeof(int)); - size_ = count; + while ( size_ < count ) { + arr_[size_++] = 0; + } + return; // leaves with size_ == count } + // count <= size_? just truncate + size_ = count; } -- cgit v1.2.3