Kuzu C++ API
Loading...
Searching...
No Matches
sel_vector.h
Go to the documentation of this file.
1#pragma once
2
3#include <string.h>
4
5#include <memory>
6
7#include "types.h"
8#include <span>
9
10namespace kuzu {
11namespace common {
12
13class ValueVector;
14
15// A lightweight, immutable view over a SelectionVector, or a subsequence of a selection vector
16// SelectionVectors are also SelectionViews so that you can pass a SelectionVector to functions
17// which take a SelectionView&
19protected:
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 {
28 };
29
30public:
31 // STATIC selectionView over 0..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 KU_ASSERT(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
76 std::span<const sel_t> getSelectedPositions() const {
77 return std::span<const sel_t>(selectedPositions, selectedSize);
78 }
79
80protected:
81 static SelectionView slice(std::span<const sel_t> selectedPositions, State state) {
83 }
84
85 // Intended to be used only as a subsequence of a SelectionVector in SelectionVector::slice
89
90protected:
94};
95
97public:
98 explicit SelectionVector(sel_t capacity)
99 : SelectionView{std::span<const sel_t>(), State::STATIC},
100 selectedPositionsBuffer{std::make_unique<sel_t[]>(capacity)}, capacity{capacity} {
102 }
103
104 // This View should be considered invalid if the SelectionVector it was created from has been
105 // modified
107 return SelectionView::slice(getSelectedPositions().subspan(startIndex, selectedSize),
108 state);
109 }
110
112
115 void setRange(sel_t startPos, sel_t size) {
116 KU_ASSERT(startPos + size <= capacity);
117 selectedPositions = selectedPositionsBuffer.get();
118 for (auto i = 0u; i < size; ++i) {
119 selectedPositionsBuffer[i] = startPos + i;
120 }
121 selectedSize = size;
123 }
124
125 // Set to filtered is not very accurate. It sets selectedPositions to a mutable array.
127 selectedPositions = selectedPositionsBuffer.get();
129 }
130 void setToFiltered(sel_t size) {
131 KU_ASSERT(size <= capacity && selectedPositionsBuffer);
133 selectedSize = size;
134 }
135
136 // Copies the data in selectedPositions into selectedPositionsBuffer
137 void makeDynamic() {
138 memcpy(selectedPositionsBuffer.get(), selectedPositions, selectedSize * sizeof(sel_t));
140 selectedPositions = selectedPositionsBuffer.get();
141 }
142
143 std::span<sel_t> getMutableBuffer() const {
144 return std::span<sel_t>(selectedPositionsBuffer.get(), capacity);
145 }
146
147 void setSelSize(sel_t size) {
148 KU_ASSERT(size <= capacity);
149 selectedSize = size;
150 }
151 void incrementSelSize(sel_t increment = 1) {
152 KU_ASSERT(selectedSize < capacity);
153 selectedSize += increment;
154 }
155
156 sel_t operator[](sel_t index) const {
157 KU_ASSERT(index < capacity);
158 return const_cast<sel_t&>(selectedPositions[index]);
159 }
161 KU_ASSERT(index < capacity);
162 return const_cast<sel_t&>(selectedPositions[index]);
163 }
164
165 static std::vector<SelectionVector*> fromValueVectors(
166 const std::vector<std::shared_ptr<common::ValueVector>>& vec);
167
168private:
169 std::unique_ptr<sel_t[]> selectedPositionsBuffer;
170 sel_t capacity;
171};
172
173} // namespace common
174} // namespace kuzu
#define KUZU_API
Definition api.h:25
#define KU_ASSERT(condition)
Definition assert.h:24
static std::vector< SelectionVector * > fromValueVectors(const std::vector< std::shared_ptr< common::ValueVector > > &vec)
KUZU_API void setToUnfiltered()
void setToFiltered()
Definition sel_vector.h:126
void makeDynamic()
Definition sel_vector.h:137
sel_t & operator[](sel_t index)
Definition sel_vector.h:160
SelectionView slice(sel_t startIndex, sel_t selectedSize) const
Definition sel_vector.h:106
void setRange(sel_t startPos, sel_t size)
Definition sel_vector.h:115
KUZU_API void setToUnfiltered(sel_t size)
void setToFiltered(sel_t size)
Definition sel_vector.h:130
void incrementSelSize(sel_t increment=1)
Definition sel_vector.h:151
std::span< sel_t > getMutableBuffer() const
Definition sel_vector.h:143
SelectionVector(sel_t capacity)
Definition sel_vector.h:98
sel_t operator[](sel_t index) const
Definition sel_vector.h:156
void setSelSize(sel_t size)
Definition sel_vector.h:147
sel_t getSelSize() const
Definition sel_vector.h:66
sel_t operator[](sel_t index) const
Definition sel_vector.h:68
SelectionView(sel_t selectedSize)
State
Definition sel_vector.h:25
@ DYNAMIC
Definition sel_vector.h:26
@ STATIC
Definition sel_vector.h:27
std::span< const sel_t > getSelectedPositions() const
Definition sel_vector.h:76
void forEachBreakWhenFalse(Func &&func) const
Definition sel_vector.h:49
SelectionView(std::span< const sel_t > selectedPositions, State state)
Definition sel_vector.h:86
void forEach(Func &&func) const
Definition sel_vector.h:35
static SelectionView slice(std::span< const sel_t > selectedPositions, State state)
Definition sel_vector.h:81
bool isStatic() const
Definition sel_vector.h:74
sel_t selectedSize
Definition sel_vector.h:92
bool isUnfiltered() const
Definition sel_vector.h:73
State state
Definition sel_vector.h:93
const sel_t * selectedPositions
Definition sel_vector.h:91
Definition value_vector.h:21
Definition array_utils.h:7
uint64_t sel_t
Definition types.h:30
Definition array_utils.h:7