A lightweight, immutable view over a SelectionVector, or a subsequence of a selection vector SelectionVectors are also SelectionViews so that you can pass a SelectionVector to functions which take a SelectionView&
| 16 | // SelectionVectors are also SelectionViews so that you can pass a SelectionVector to functions |
| 17 | // which take a SelectionView& |
| 18 | class SelectionView { |
| 19 | protected: |
| 20 | // In DYNAMIC mode, selectedPositions points to a mutable buffer that can be modified through |
| 21 | // getMutableBuffer In STATIC mode, selectedPositions points to somewhere in |
| 22 | // INCREMENTAL_SELECTED_POS |
| 23 | // Note that the vector is considered unfiltered only if it is both STATIC and the first |
| 24 | // selected position is 0 |
| 25 | enum class State { |
| 26 | DYNAMIC, |
| 27 | STATIC, |
| 28 | }; |
| 29 | |
| 30 | public: |
| 31 | // STATIC selectionView over 0..selectedSize |
| 32 | explicit SelectionView(sel_t selectedSize); |
| 33 | |
| 34 | template<class Func> |
| 35 | void forEach(Func&& func) const { |
| 36 | if (state == State::DYNAMIC) { |
| 37 | for (size_t i = 0; i < selectedSize; i++) { |
| 38 | func(selectedPositions[i]); |
| 39 | } |
| 40 | } else { |
| 41 | const auto start = selectedPositions[0]; |
| 42 | for (size_t i = start; i < start + selectedSize; i++) { |
| 43 | func(i); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | template<class Func> |
| 49 | void forEachBreakWhenFalse(Func&& func) const { |
| 50 | if (state == State::DYNAMIC) { |
| 51 | for (size_t i = 0; i < selectedSize; i++) { |
| 52 | if (!func(selectedPositions[i])) { |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | } else { |
| 57 | const auto start = selectedPositions[0]; |
| 58 | for (size_t i = start; i < start + selectedSize; i++) { |
| 59 | if (!func(i)) { |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | sel_t getSelSize() const { return selectedSize; } |
| 67 | |
| 68 | sel_t operator[](sel_t index) const { |
| 69 | DASSERT(index < selectedSize); |
| 70 | return selectedPositions[index]; |
| 71 | } |
| 72 | |
| 73 | bool isUnfiltered() const { return state == State::STATIC && selectedPositions[0] == 0; } |
| 74 | bool isStatic() const { return state == State::STATIC; } |
| 75 |