diff options
| author | Greg Ercolano <erco@seriss.com> | 2022-11-06 21:59:36 -0800 |
|---|---|---|
| committer | Greg Ercolano <erco@seriss.com> | 2022-11-07 10:33:35 -0800 |
| commit | 2a43a12b7b870c6fd775f417c1c2e85222e47238 (patch) | |
| tree | d4f8f82f0ac774d98f7aa1b8bcc2c3d134954833 | |
| parent | 51ce2b9235f387e98c52374e4b7b5c906d7d778d (diff) | |
Added empty(), ensure size() enlarges new vals = 0
| -rw-r--r-- | FL/Fl_Int_Vector.H | 23 | ||||
| -rw-r--r-- | 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 <stdio.h> #include <FL/Fl_Int_Vector.H> 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<v.size(); i++ ) @@ -52,7 +53,7 @@ \endcode \todo - - Add other std::vector methods like empty(), erase(), etc. + - Add other std::vector methods like erase(), etc. - Make memory blocking size flexible, and add related methods like capacity(), reserve(), shrink_to_fit(), etc. - Add non-std methods that are nevertheless needed, e.g. insert(index,val), delete(index), delete(start, end), swap(a_idx,b_idx) - Add a way to change the elements by index, e.g. v[2] = 222; (which is currently NOT supported) @@ -123,7 +124,7 @@ public: Removes the last element the last element and returns its value. \warning You must not call pop_back() if the array is empty, i.e. if (size() == 0). - \todo Internals should probably assert(size_ != 0) + \todo Internals should maybe assert(size_ != 0) */ int pop_back() { int tmp = arr_[size_ - 1]; @@ -141,11 +142,19 @@ public: /** Return the last element in the array. \warning You must not call back() if the array is empty, i.e. if (size() == 0). - \todo Internals should probably assert(size_ != 0) + \todo Internals should maybe assert(size_ != 0) */ int back() { return arr_[size_ - 1]; } + + /** + Checks if array has no elements. + Same as a test for (size() == 0). + */ + bool empty() const { + return (size_ == 0) ? true : false; + } }; #endif // Fl_Int_Vector_H diff --git a/src/Fl_Int_Vector.cxx b/src/Fl_Int_Vector.cxx index 08e45c4cc..0815661dc 100644 --- a/src/Fl_Int_Vector.cxx +++ b/src/Fl_Int_Vector.cxx @@ -36,24 +36,30 @@ Fl_Int_Vector::~Fl_Int_Vector() { /** Set the size of the array to \p count. - A size of zero empties the array completely and frees all memory. + Setting size to zero clears the array and frees any memory it used. + + Shrinking truncates the array and frees memory of truncated elements. + Enlarging creates new elements that are zero in value. \warning - - Only advised use currently is to shrink the array size, i.e. (count < size()). - - Currently enlarging the array leaves the new values uninitialized. - - When assignment via indexes is supported, i.e. v[x] = 123, array enlargement should zero new values - \todo Check if count > 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; } |
