diff options
Diffstat (limited to 'include/util/Cursor.h')
| -rw-r--r-- | include/util/Cursor.h | 89 |
1 files changed, 46 insertions, 43 deletions
diff --git a/include/util/Cursor.h b/include/util/Cursor.h index e8ba53d..e7963b1 100644 --- a/include/util/Cursor.h +++ b/include/util/Cursor.h @@ -1,8 +1,8 @@ /* * include/util/Cursor.h * - * Copyright (C) 2023 Douglas B. Rumbaugh <drumbaugh@psu.edu> - * Dong Xie <dongx@psu.edu> + * Copyright (C) 2023-2024 Douglas B. Rumbaugh <drumbaugh@psu.edu> + * Dong Xie <dongx@psu.edu> * * Distributed under the Modified BSD License. * @@ -21,16 +21,15 @@ #include <vector> namespace de { -template<typename R> -struct Cursor { - R *ptr; - R *end; - size_t cur_rec_idx; - size_t rec_cnt; +template <typename R> struct Cursor { + const R *ptr; + const R *end; + size_t cur_rec_idx; + size_t rec_cnt; - friend bool operator==(const Cursor &a, const Cursor &b) { - return a.ptr == b.ptr && a.end == b.end; - } + friend bool operator==(const Cursor &a, const Cursor &b) { + return a.ptr == b.ptr && a.end == b.end; + } }; /* @@ -43,51 +42,55 @@ struct Cursor { * be updated to be equal to end, and false will be returned. Iterators will * not be closed. */ -template<typename R> -inline static bool advance_cursor(Cursor<R> &cur) { - cur.ptr++; - cur.cur_rec_idx++; +template <typename R> inline static bool advance_cursor(Cursor<R> &cur) { + cur.ptr++; + cur.cur_rec_idx++; - if (cur.cur_rec_idx >= cur.rec_cnt) return false; + if (cur.cur_rec_idx >= cur.rec_cnt) + return false; - if (cur.ptr >= cur.end) { - return false; - } - return true; + if (cur.ptr >= cur.end) { + return false; + } + return true; } /* * Process the list of cursors to return the cursor containing the next * largest element. Does not advance any of the cursors. If current is - * specified, then skip the current head of that cursor during checking. - * This allows for "peaking" at the next largest element after the current + * specified, then skip the current head of that cursor during checking. + * This allows for "peaking" at the next largest element after the current * largest is processed. */ template <typename R> -inline static Cursor<R> *get_next(std::vector<Cursor<R>> &cursors, Cursor<R> *current=nullptr) { - const R *min_rec = nullptr; - Cursor<R> *result = nullptr; - // FIXME: for large cursor vectors, it may be worth it to use a - // PriorityQueue here instead of scanning. - for (size_t i=0; i< cursors.size(); i++) { - if (cursors[i] == (Cursor<R>) {0} ) continue; - - const R *rec = (&cursors[i] == current) ? cursors[i].ptr + 1 : cursors[i].ptr; - if (rec >= cursors[i].end) continue; +inline static Cursor<R> *get_next(std::vector<Cursor<R>> &cursors, + Cursor<R> *current = nullptr) { + const R *min_rec = nullptr; + Cursor<R> *result = nullptr; + // FIXME: for large cursor vectors, it may be worth it to use a + // PriorityQueue here instead of scanning. + for (size_t i = 0; i < cursors.size(); i++) { + if (cursors[i] == (Cursor<R>){0}) + continue; - if (min_rec == nullptr) { - result = &cursors[i]; - min_rec = rec; - continue; - } + const R *rec = + (&cursors[i] == current) ? cursors[i].ptr + 1 : cursors[i].ptr; + if (rec >= cursors[i].end) + continue; - if (*rec < *min_rec) { - result = &cursors[i]; - min_rec = rec; - } + if (min_rec == nullptr) { + result = &cursors[i]; + min_rec = rec; + continue; } - return result; -} + if (*rec < *min_rec) { + result = &cursors[i]; + min_rec = rec; + } + } + return result; } + +} // namespace de |