Kùzu 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 "constants.h"
8#include "types.h"
9#include <span>
10
11namespace kuzu {
12namespace common {
13
15 KUZU_API static const std::array<sel_t, DEFAULT_VECTOR_CAPACITY> INCREMENTAL_SELECTED_POS;
16
17 // In DYNAMIC mode, selectedPositions points to a mutable buffer that can be modified through
18 // getMutableBuffer In STATIC mode, selectedPositions points to the beginning of
19 // INCREMENTAL_SELECTED_POS In STATIC_FILTERED mode, selectedPositions points to some position
20 // in INCREMENTAL_SELECTED_POS
21 // If reading manually in STATIC_FILTERED mode, you should read getSelectedPositions()[0],
22 // and then you can assume that the next getSelSize() positions are selected
23 // (This also works in STATIC mode)
24 enum class State {
25 DYNAMIC,
26 STATIC,
27 STATIC_FILTERED,
28 };
29
30public:
31 explicit SelectionVector(sel_t capacity)
32 : selectedSize{0}, capacity{capacity}, selectedPositions{nullptr}, state{State::STATIC} {
33 selectedPositionsBuffer = std::make_unique<sel_t[]>(capacity);
35 }
36
38
39 bool isUnfiltered() const { return state == State::STATIC && selectedPositions[0] == 0; }
40
42 selectedPositions = const_cast<sel_t*>(INCREMENTAL_SELECTED_POS.data());
43 state = State::STATIC;
44 }
46 KU_ASSERT(size <= capacity);
47 selectedPositions = const_cast<sel_t*>(INCREMENTAL_SELECTED_POS.data());
48 selectedSize = size;
49 state = State::STATIC;
50 }
51 void setRange(sel_t startPos, sel_t size) {
52 KU_ASSERT(startPos + size <= capacity);
53 selectedPositions = const_cast<sel_t*>(INCREMENTAL_SELECTED_POS.data()) + startPos;
54 selectedSize = size;
55 state = State::STATIC_FILTERED;
56 }
57
58 // Set to filtered is not very accurate. It sets selectedPositions to a mutable array.
60 selectedPositions = selectedPositionsBuffer.get();
61 state = State::DYNAMIC;
62 }
63 void setToFiltered(sel_t size) {
64 KU_ASSERT(size <= capacity && selectedPositionsBuffer);
66 selectedSize = size;
67 }
68
69 // Copies the data in selectedPositions into selectedPositionsBuffer
70 void makeDynamic() {
71 memcpy(selectedPositionsBuffer.get(), selectedPositions, selectedSize * sizeof(sel_t));
72 state = State::DYNAMIC;
73 selectedPositions = selectedPositionsBuffer.get();
74 }
75
76 std::span<sel_t> getMutableBuffer() const {
77 return std::span<sel_t>(selectedPositionsBuffer.get(), capacity);
78 }
79 std::span<const sel_t> getSelectedPositions() const {
80 return std::span<const sel_t>(selectedPositions, selectedSize);
81 }
82
83 template<class Func>
84 void forEach(Func&& func) const {
85 if (state == State::DYNAMIC) {
86 for (size_t i = 0; i < selectedSize; i++) {
87 func(selectedPositions[i]);
88 }
89 } else {
90 const auto start = selectedPositions[0];
91 for (size_t i = start; i < start + selectedSize; i++) {
92 func(i);
93 }
94 }
95 }
96
97 sel_t getSelSize() const { return selectedSize; }
98 void setSelSize(sel_t size) {
99 KU_ASSERT(size <= capacity);
100 selectedSize = size;
101 }
102 void incrementSelSize(sel_t increment = 1) {
103 KU_ASSERT(selectedSize < capacity);
104 selectedSize += increment;
105 }
106
107 sel_t operator[](sel_t index) const {
108 KU_ASSERT(index < capacity);
109 return selectedPositions[index];
110 }
112 KU_ASSERT(index < capacity);
113 return selectedPositions[index];
114 }
115
116private:
117 sel_t selectedSize;
118 sel_t capacity;
119 std::unique_ptr<sel_t[]> selectedPositionsBuffer;
120 sel_t* selectedPositions;
121 State state;
122};
123
124} // namespace common
125} // namespace kuzu
#define KUZU_API
Definition api.h:25
#define KU_ASSERT(condition)
Definition assert.h:19
Definition sel_vector.h:14
void setToUnfiltered()
Definition sel_vector.h:41
void forEach(Func &&func) const
Definition sel_vector.h:84
void setToFiltered()
Definition sel_vector.h:59
void makeDynamic()
Definition sel_vector.h:70
SelectionVector()
Definition sel_vector.h:37
sel_t & operator[](sel_t index)
Definition sel_vector.h:111
void setRange(sel_t startPos, sel_t size)
Definition sel_vector.h:51
void setToUnfiltered(sel_t size)
Definition sel_vector.h:45
bool isUnfiltered() const
Definition sel_vector.h:39
std::span< const sel_t > getSelectedPositions() const
Definition sel_vector.h:79
void setToFiltered(sel_t size)
Definition sel_vector.h:63
sel_t getSelSize() const
Definition sel_vector.h:97
void incrementSelSize(sel_t increment=1)
Definition sel_vector.h:102
std::span< sel_t > getMutableBuffer() const
Definition sel_vector.h:76
SelectionVector(sel_t capacity)
Definition sel_vector.h:31
sel_t operator[](sel_t index) const
Definition sel_vector.h:107
void setSelSize(sel_t size)
Definition sel_vector.h:98
constexpr uint64_t DEFAULT_VECTOR_CAPACITY
Definition constants.h:24
uint64_t sel_t
Definition types.h:30
Definition array_utils.h:7