Kùzu C++ API
Loading...
Searching...
No Matches
sel_vector.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
5#include "constants.h"
6#include "types.h"
7#include <span>
8
9namespace kuzu {
10namespace common {
11
13public:
14 KUZU_API static const std::array<sel_t, DEFAULT_VECTOR_CAPACITY> INCREMENTAL_SELECTED_POS;
15
16 explicit SelectionVector(sel_t capacity)
17 : selectedSize{0}, capacity{capacity}, selectedPositions{nullptr} {
18 selectedPositionsBuffer = std::make_unique<sel_t[]>(capacity);
20 }
21
23
24 bool isUnfiltered() const { return selectedPositions == INCREMENTAL_SELECTED_POS.data(); }
25 bool isContinuous() const {
26 return selectedPositions >= &*INCREMENTAL_SELECTED_POS.begin() &&
27 selectedPositions <= &*INCREMENTAL_SELECTED_POS.end();
28 }
29
31 selectedPositions = const_cast<sel_t*>(INCREMENTAL_SELECTED_POS.data());
32 }
34 KU_ASSERT(size <= capacity);
35 selectedPositions = const_cast<sel_t*>(INCREMENTAL_SELECTED_POS.data());
36 selectedSize = size;
37 }
38
39 // Set to filtered is not very accurate. It sets selectedPositions to a mutable array.
40 void setToFiltered() { selectedPositions = selectedPositionsBuffer.get(); }
41 void setToFiltered(sel_t size) {
42 KU_ASSERT(size <= capacity && selectedPositionsBuffer);
43 selectedPositions = selectedPositionsBuffer.get();
44 selectedSize = size;
45 }
46
47 std::span<sel_t> getMutableBuffer() const {
48 return std::span<sel_t>(selectedPositionsBuffer.get(), capacity);
49 }
50 std::span<const sel_t> getSelectedPositions() const {
51 return std::span<const sel_t>(selectedPositions, selectedSize);
52 }
53
54 template<class Func>
55 void forEach(Func&& func) const {
56 if (isContinuous()) {
57 const auto start = selectedPositions[0];
58 for (size_t i = start; i < start + selectedSize; i++) {
59 func(i);
60 }
61 } else {
62 for (size_t i = 0; i < selectedSize; i++) {
63 func(selectedPositions[i]);
64 }
65 }
66 }
67
68 sel_t getSelSize() const { return selectedSize; }
69 void setSelSize(sel_t size) {
70 KU_ASSERT(size <= capacity);
71 selectedSize = size;
72 }
73 void incrementSelSize(sel_t increment = 1) {
74 KU_ASSERT(selectedSize < capacity);
75 selectedSize += increment;
76 }
77
78 sel_t operator[](sel_t index) const {
79 KU_ASSERT(index < capacity);
80 return selectedPositions[index];
81 }
83 KU_ASSERT(index < capacity);
84 return selectedPositions[index];
85 }
86
87private:
88 sel_t selectedSize;
89 sel_t capacity;
90 std::unique_ptr<sel_t[]> selectedPositionsBuffer;
91 sel_t* selectedPositions;
92};
93
94} // namespace common
95} // namespace kuzu
#define KUZU_API
Definition api.h:25
#define KU_ASSERT(condition)
Definition assert.h:19
Definition sel_vector.h:12
void setToUnfiltered()
Definition sel_vector.h:30
void forEach(Func &&func) const
Definition sel_vector.h:55
bool isContinuous() const
Definition sel_vector.h:25
void setToFiltered()
Definition sel_vector.h:40
SelectionVector()
Definition sel_vector.h:22
sel_t & operator[](sel_t index)
Definition sel_vector.h:82
void setToUnfiltered(sel_t size)
Definition sel_vector.h:33
static KUZU_API const std::array< sel_t, DEFAULT_VECTOR_CAPACITY > INCREMENTAL_SELECTED_POS
Definition sel_vector.h:14
bool isUnfiltered() const
Definition sel_vector.h:24
std::span< const sel_t > getSelectedPositions() const
Definition sel_vector.h:50
void setToFiltered(sel_t size)
Definition sel_vector.h:41
sel_t getSelSize() const
Definition sel_vector.h:68
void incrementSelSize(sel_t increment=1)
Definition sel_vector.h:73
std::span< sel_t > getMutableBuffer() const
Definition sel_vector.h:47
SelectionVector(sel_t capacity)
Definition sel_vector.h:16
sel_t operator[](sel_t index) const
Definition sel_vector.h:78
void setSelSize(sel_t size)
Definition sel_vector.h:69
constexpr uint64_t DEFAULT_VECTOR_CAPACITY
Definition constants.h:24
uint64_t sel_t
Definition types.h:27
Definition array_utils.h:7