|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | + |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +// We create a basic NAry table type with type V (some std::variant<types>) |
| 7 | +template <typename V> using Row = vector<V>; |
| 8 | + |
| 9 | +template <typename V> using Table = vector<Row<V>>; |
| 10 | + |
| 11 | +template <typename V> |
| 12 | +size_t select_eq(Table<V> &outputBuffer, const Table<V> &inputBuffer, |
| 13 | + V eq_value, size_t attribOffset) { |
| 14 | + |
| 15 | + for (const Row<V> &r : inputBuffer) { |
| 16 | + if (r[attribOffset] == eq_value) { |
| 17 | + outputBuffer.push_back(r); |
| 18 | + } |
| 19 | + } |
| 20 | + return outputBuffer.size(); |
| 21 | +} |
| 22 | + |
| 23 | +void example_bulk() { |
| 24 | + // we can make a basic example with int only values as |
| 25 | + // CREATE TABLE Orders (orderId int, status int, urgency int); |
| 26 | + // C-style enums used for brevity |
| 27 | + enum Urgency { URGENT, NOT_URGENT, IGNORE }; |
| 28 | + |
| 29 | + enum Status { COMPLETE, IN_PROCESS, PENDING }; |
| 30 | + |
| 31 | + Table<int> Orders{ |
| 32 | + {1, COMPLETE, IGNORE}, |
| 33 | + {2, PENDING, IN_PROCESS}, |
| 34 | + {3, PENDING, URGENT}, |
| 35 | + {4, PENDING, URGENT}, |
| 36 | + }; |
| 37 | + |
| 38 | + Table<int> PendingOrders, UrgentAndPendingOrders; |
| 39 | + |
| 40 | + select_eq<int>(PendingOrders, Orders, PENDING, 1); |
| 41 | + select_eq<int>(UrgentAndPendingOrders, PendingOrders, URGENT, 2); |
| 42 | + |
| 43 | + for (auto &r : UrgentAndPendingOrders) { |
| 44 | + cout << "id: " << r[0] << endl; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +using Candidates = vector<uint32_t>; |
| 49 | + |
| 50 | +template<typename V> |
| 51 | +size_t add_candidates(const Table<V>& underlyingBuffer, Candidates& outputRows) { |
| 52 | + for (uint32_t i = 0; i < underlyingBuffer.size(); i++) { |
| 53 | + outputRows.push_back(i); |
| 54 | + } |
| 55 | + return outputRows.size(); |
| 56 | +} |
| 57 | + |
| 58 | +template<typename V> |
| 59 | +size_t select_eq(const Table<V>& underlyingBuffer, Candidates& outputRows, const Candidates& inputRows, V eq_value, size_t attribOffset) { |
| 60 | + for (const uint32_t index : inputRows) { |
| 61 | + if (underlyingBuffer[index][attribOffset] == eq_value) { |
| 62 | + outputRows.push_back(index); |
| 63 | + } |
| 64 | + } |
| 65 | + return outputRows.size(); |
| 66 | +} |
| 67 | + |
| 68 | +void example_reference_bulk() { |
| 69 | + // we can make a basic example with int only values as |
| 70 | + // CREATE TABLE Orders (orderId int, status int, urgency int); |
| 71 | + // C-style enums used for brevity |
| 72 | + enum Urgency { URGENT, NOT_URGENT, IGNORE }; |
| 73 | + |
| 74 | + enum Status { COMPLETE, IN_PROCESS, PENDING }; |
| 75 | + |
| 76 | + Table<int> Orders{ |
| 77 | + {1, COMPLETE, IGNORE}, |
| 78 | + {2, PENDING, IN_PROCESS}, |
| 79 | + {3, PENDING, URGENT}, |
| 80 | + {4, PENDING, URGENT}, |
| 81 | + }; |
| 82 | + |
| 83 | + |
| 84 | + Candidates OrdersCandidates, PendingOrders, UrgentAndPendingOrders; |
| 85 | + add_candidates(Orders, OrdersCandidates); |
| 86 | + select_eq<int>(Orders, PendingOrders, OrdersCandidates, PENDING, 1); |
| 87 | + select_eq<int>(Orders, UrgentAndPendingOrders, PendingOrders, URGENT, 2); |
| 88 | + |
| 89 | + for (auto &r : UrgentAndPendingOrders) { |
| 90 | + cout << "id: " << Orders[r][0] << endl; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +int main() { |
| 95 | + example_reference_bulk(); |
| 96 | +} |
0 commit comments