From 3705989c943d8f16e2a6f8b3dfe747ea6257da0a Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Tue, 6 Apr 2021 15:57:12 +0200 Subject: [PATCH] Optimize length method for ELIST, ELIST2 Signed-off-by: Stefan Weil --- src/ccutil/elst.cpp | 16 ---------------- src/ccutil/elst.h | 34 +++++++++++++++------------------- src/ccutil/elst2.cpp | 16 ---------------- src/ccutil/elst2.h | 34 +++++++++++++++------------------- 4 files changed, 30 insertions(+), 70 deletions(-) diff --git a/src/ccutil/elst.cpp b/src/ccutil/elst.cpp index 31da7815a..c884dbab1 100644 --- a/src/ccutil/elst.cpp +++ b/src/ccutil/elst.cpp @@ -76,22 +76,6 @@ void ELIST::assign_to_sublist( // to this list last = start_it->extract_sublist(end_it); } -/*********************************************************************** - * ELIST::length - * - * Return count of elements on list - **********************************************************************/ - -int32_t ELIST::length() const { // count elements - ELIST_ITERATOR it(const_cast(this)); - int32_t count = 0; - - for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { - count++; - } - return count; -} - /*********************************************************************** * ELIST::sort * diff --git a/src/ccutil/elst.h b/src/ccutil/elst.h index 1301b31d0..f21a9caf9 100644 --- a/src/ccutil/elst.h +++ b/src/ccutil/elst.h @@ -147,7 +147,17 @@ public: ELIST_ITERATOR *start_it, // from list start ELIST_ITERATOR *end_it); // from list end - int32_t length() const; // # elements in list + // # elements in list + int32_t length() const { + int32_t count = 0; + if (last != nullptr) { + count = 1; + for (auto it = last->next; it != last; it = it->next) { + count++; + } + } + return count; + } void sort( // sort elements int comparator( // comparison routine @@ -273,7 +283,10 @@ public: void exchange( // positions of 2 links ELIST_ITERATOR *other_it); // other iterator - int32_t length() const; //# elements in list + //# elements in list + int32_t length() const { + return list->length(); + } void sort( // sort elements int comparator( // comparison routine @@ -734,23 +747,6 @@ inline bool ELIST_ITERATOR::cycled_list() const { return ((list->empty()) || ((current == cycle_pt) && started_cycling)); } -/*********************************************************************** - * ELIST_ITERATOR::length() - * - * Return the length of the list - * - **********************************************************************/ - -inline int32_t ELIST_ITERATOR::length() const { -#ifndef NDEBUG - if (!list) { - NO_LIST.error("ELIST_ITERATOR::length", ABORT, nullptr); - } -#endif - - return list->length(); -} - /*********************************************************************** * ELIST_ITERATOR::sort() * diff --git a/src/ccutil/elst2.cpp b/src/ccutil/elst2.cpp index a172829ad..a8b1357f0 100644 --- a/src/ccutil/elst2.cpp +++ b/src/ccutil/elst2.cpp @@ -77,22 +77,6 @@ void ELIST2::assign_to_sublist( // to this list last = start_it->extract_sublist(end_it); } -/*********************************************************************** - * ELIST2::length - * - * Return count of elements on list - **********************************************************************/ - -int32_t ELIST2::length() const { // count elements - ELIST2_ITERATOR it(const_cast(this)); - int32_t count = 0; - - for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { - count++; - } - return count; -} - /*********************************************************************** * ELIST2::sort * diff --git a/src/ccutil/elst2.h b/src/ccutil/elst2.h index 81ea92777..bd27a851c 100644 --- a/src/ccutil/elst2.h +++ b/src/ccutil/elst2.h @@ -123,7 +123,17 @@ public: ELIST2_ITERATOR *start_it, // from list start ELIST2_ITERATOR *end_it); // from list end - int32_t length() const; // # elements in list + // # elements in list + int32_t length() const { + int32_t count = 0; + if (last != nullptr) { + count = 1; + for (auto it = last->next; it != last; it = it->next) { + count++; + } + } + return count; + } void sort( // sort elements int comparator( // comparison routine @@ -237,7 +247,10 @@ public: void exchange( // positions of 2 links ELIST2_ITERATOR *other_it); // other iterator - int32_t length() const; //# elements in list + //# elements in list + int32_t length() const { + return list->length(); + } void sort( // sort elements int comparator( // comparison routine @@ -752,23 +765,6 @@ inline bool ELIST2_ITERATOR::cycled_list() const { return ((list->empty()) || ((current == cycle_pt) && started_cycling)); } -/*********************************************************************** - * ELIST2_ITERATOR::length() - * - * Return the length of the list - * - **********************************************************************/ - -inline int32_t ELIST2_ITERATOR::length() const { -#ifndef NDEBUG - if (!list) { - NO_LIST.error("ELIST2_ITERATOR::length", ABORT, nullptr); - } -#endif - - return list->length(); -} - /*********************************************************************** * ELIST2_ITERATOR::sort() *