Skip to content

Commit

Permalink
updated structure LineInfo for more effective CSV hanling
Browse files Browse the repository at this point in the history
  • Loading branch information
daddel80 committed Jan 26, 2025
1 parent 8debda2 commit 48ed323
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/FenwickTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "FenwickTree.h"
#include <Windows.h>

// Constructor: Initialize with 0 size.
FenwickTree::FenwickTree() : _size(0) {}

// Initialize the Fenwick Tree with a given size (1-based indexing).
void FenwickTree::init(size_t n) {
_size = n;
fenwicksum.assign(n + 1, 0); // 1 extra for 1-based indexing
}

// Update: Add 'delta' to all elements >= index (1-based indexing).
void FenwickTree::update(size_t index, LRESULT delta) {
while (index <= _size) {
fenwicksum[index] += delta;
index += static_cast<size_t>(index & -static_cast<std::make_signed_t<size_t>>(index)); // move to the next node
}
}

// Query: Get the prefix sum from 1 to index (1-based indexing).
LRESULT FenwickTree::prefixSum(size_t index) const {
LRESULT result = 0;
while (index > 0) {
result += fenwicksum[index];
index -= static_cast<size_t>(index & -static_cast<std::make_signed_t<size_t>>(index)); // move to the parent node
}
return result;
}

// Get the size of the Fenwick Tree.
size_t FenwickTree::size() const {
return _size;
}
22 changes: 22 additions & 0 deletions src/FenwickTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef FENWICKTREE_H
#define FENWICKTREE_H

#include <vector>
#include <cstddef> // for size_t
#include <Windows.h>

// FenwickTree (Binary Indexed Tree) for efficient prefix sum calculations.
class FenwickTree {
private:
std::vector<LRESULT> fenwicksum;
size_t _size;

public:
FenwickTree();
void init(size_t n);
void update(size_t index, LRESULT delta);
LRESULT prefixSum(size_t index) const;
size_t size() const;
};

#endif // FENWICKTREE_H

0 comments on commit 48ed323

Please sign in to comment.