Advanced Chunk Processing Library 0.2.0
A comprehensive C++ library for advanced data chunking strategies and processing operations
Loading...
Searching...
No Matches
chunk_windows.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "chunk.hpp"
4#include "chunk_common.hpp"
5#include <algorithm>
6#include <functional>
7#include <numeric>
8#include <vector>
9
10namespace chunk_windows {
11
12template <typename T>
14private:
16 size_t step_size_;
17
18 template <typename U>
19 static double compute_window_feature(const U& window) {
22 // Handle 2D arrays
23 double sum = 0.0;
24 for (const auto& inner : window) {
25 sum += compute_window_feature(inner);
26 }
27 return sum / window.size();
28 } else {
29 // Handle 1D arrays
30 return std::accumulate(window.begin(), window.end(), 0.0) / window.size();
31 }
32 } else {
33 // Handle scalar values
34 return static_cast<double>(window);
35 }
36 }
37
38 std::vector<T> process_multidimensional(const std::vector<T>& data,
39 std::function<T(const std::vector<T>&)> window_func) {
40 // Implementation for multi-dimensional data
41 std::vector<T> results;
42 for (size_t i = 0; i <= data.size() - window_size_; i += step_size_) {
43 std::vector<T> window(data.begin() + i, data.begin() + i + window_size_);
44 results.push_back(window_func(window));
45 }
46 return results;
47 }
48
49 std::vector<T> process_single_dimensional(const std::vector<T>& data,
50 std::function<T(const std::vector<T>&)> window_func) {
51 std::vector<T> results;
52 // Create sliding windows of the specified size
53 for (size_t i = 0; i <= data.size() - window_size_; i += step_size_) {
54 std::vector<T> window(data.begin() + i, data.begin() + i + window_size_);
55 results.push_back(window_func(window));
56 }
57 return results;
58 }
59
60 std::vector<T> process_window(const std::vector<T>& data,
61 std::function<T(const std::vector<T>&)> window_func) {
64 // Handle 2D arrays
65 return process_multidimensional(data, window_func);
66 } else {
67 // Handle 1D arrays
68 return process_single_dimensional(data, window_func);
69 }
70 } else {
71 // Handle scalar values
72 return process_single_dimensional(data, window_func);
73 }
74 }
75
76public:
77 SlidingWindowProcessor(size_t window_size, size_t step_size)
78 : window_size_(window_size), step_size_(step_size) {
79 if (window_size == 0)
80 throw std::invalid_argument("Window size cannot be zero");
81 if (step_size == 0)
82 throw std::invalid_argument("Step size cannot be zero");
83 }
84
85 std::vector<T> process(const std::vector<T>& data,
86 std::function<T(const std::vector<T>&)> window_func) {
87 if (data.size() < window_size_) {
88 return {}; // Return empty if data is smaller than window
89 }
90
91 return process_window(data, window_func);
92 }
93
94 // Add getters
95 size_t get_window_size() const {
96 return window_size_;
97 }
98 size_t get_step_size() const {
99 return step_size_;
100 }
101
102 // Add setters
103 void set_window_size(size_t size) {
104 if (size == 0)
105 throw std::invalid_argument("Window size cannot be zero");
106 window_size_ = size;
107 }
108 void set_step_size(size_t size) {
109 if (size == 0)
110 throw std::invalid_argument("Step size cannot be zero");
111 step_size_ = size;
112 }
113};
114
115// Common window operations
116template <typename T>
118public:
119 static T moving_average(const std::vector<T>& window) {
120 if (window.empty()) {
121 return T{}; // Return default value for empty input
122 }
123 return std::accumulate(window.begin(), window.end(), T{}) / static_cast<T>(window.size());
124 }
125
126 static T moving_median(std::vector<T> window) {
127 if (window.empty()) {
128 throw std::invalid_argument("Cannot compute median of empty window");
129 }
130 std::sort(window.begin(), window.end());
131 return window[window.size() / 2];
132 }
133
134 static T moving_max(const std::vector<T>& window) {
135 if (window.empty()) {
136 throw std::invalid_argument("Cannot compute max of empty window");
137 }
138 return *std::max_element(window.begin(), window.end());
139 }
140
141 static T moving_min(const std::vector<T>& window) {
142 if (window.empty()) {
143 throw std::invalid_argument("Cannot compute min of empty window");
144 }
145 return *std::min_element(window.begin(), window.end());
146 }
147};
148
149} // namespace chunk_windows
std::vector< T > process(const std::vector< T > &data, std::function< T(const std::vector< T > &)> window_func)
SlidingWindowProcessor(size_t window_size, size_t step_size)
std::vector< T > process_single_dimensional(const std::vector< T > &data, std::function< T(const std::vector< T > &)> window_func)
std::vector< T > process_multidimensional(const std::vector< T > &data, std::function< T(const std::vector< T > &)> window_func)
static double compute_window_feature(const U &window)
std::vector< T > process_window(const std::vector< T > &data, std::function< T(const std::vector< T > &)> window_func)
static T moving_median(std::vector< T > window)
static T moving_average(const std::vector< T > &window)
static T moving_max(const std::vector< T > &window)
static T moving_min(const std::vector< T > &window)