Advanced Chunk Processing Library 0.2.0
A comprehensive C++ library for advanced data chunking strategies and processing operations
Loading...
Searching...
No Matches
chunk_processing::Chunk< T > Class Template Reference

A template class for managing and processing data in chunks. More...

#include <chunk.hpp>

+ Collaboration diagram for chunk_processing::Chunk< T >:

Classes

class  JaggedVectorHandler
 

Public Member Functions

 Chunk (size_t chunk_size=1)
 
void add (const std::vector< T > &elements)
 
void add (const T &element)
 
template<typename U = T>
std::enable_if_t< is_vector< U >::value > add (const U &nested_data)
 
std::vector< std::vector< T > > chunk_by_size (size_t size)
 
std::vector< std::vector< T > > chunk_by_threshold (T threshold)
 
size_t chunk_count () const
 
size_t get_chunk_size () const
 
std::vector< std::vector< T > > get_chunks () const
 
const std::vector< T > & get_data () const
 
template<typename U >
std::vector< std::vector< U > > handle_jagged_2d (const std::vector< std::vector< U > > &data)
 
template<typename U >
std::vector< std::vector< std::vector< U > > > handle_jagged_3d (const std::vector< std::vector< std::vector< U > > > &data)
 
void set_chunk_size (size_t new_size)
 
size_t size () const
 
template<typename U >
void validate_dimensions (const std::vector< U > &data, size_t expected_size=0)
 

Static Public Member Functions

static constexpr size_t dimensions ()
 

Private Member Functions

std::vector< std::vector< T > > make_chunks (size_t size) const
 
void update_chunks ()
 
void validate_size (size_t size, const std::string &param) const
 

Static Private Member Functions

template<typename U >
static constexpr size_t get_depth ()
 

Private Attributes

size_t chunk_size_
 
std::vector< std::vector< T > > chunks_
 
std::vector< T > data_
 

Detailed Description

template<typename T>
class chunk_processing::Chunk< T >

A template class for managing and processing data in chunks.

Template Parameters
TThe type of elements stored in the chunk

Definition at line 16 of file chunk.hpp.

Constructor & Destructor Documentation

◆ Chunk()

template<typename T >
chunk_processing::Chunk< T >::Chunk ( size_t  chunk_size = 1)
inlineexplicit

Definition at line 126 of file chunk.hpp.

126 : chunk_size_(chunk_size) {
127 validate_size(chunk_size, "Chunk size");
128 }
void validate_size(size_t size, const std::string &param) const
Definition chunk.hpp:89

Member Function Documentation

◆ add() [1/3]

template<typename T >
void chunk_processing::Chunk< T >::add ( const std::vector< T > &  elements)
inline

Definition at line 135 of file chunk.hpp.

135 {
136 data_.insert(data_.end(), elements.begin(), elements.end());
138 }
std::vector< T > data_
Definition chunk.hpp:19

◆ add() [2/3]

template<typename T >
void chunk_processing::Chunk< T >::add ( const T &  element)
inline

Definition at line 130 of file chunk.hpp.

130 {
131 data_.push_back(element);
133 }

Referenced by PYBIND11_MODULE(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ add() [3/3]

template<typename T >
template<typename U = T>
std::enable_if_t< is_vector< U >::value > chunk_processing::Chunk< T >::add ( const U &  nested_data)
inline

Definition at line 209 of file chunk.hpp.

209 {
210 // First validate dimensions
211 if (chunk_processing::is_jagged(nested_data)) {
212 throw std::invalid_argument("Jagged arrays are not supported");
213 }
214
215 // For 3D arrays, check additional level
218 reinterpret_cast<const std::vector<
219 std::vector<std::vector<typename U::value_type::value_type>>>&>(
220 nested_data))) {
221 throw std::invalid_argument("Jagged 3D arrays are not supported");
222 }
223 }
224
225 data_.push_back(nested_data);
227 }
bool is_jagged(const std::vector< std::vector< T > > &data)
bool is_jagged_3d(const std::vector< std::vector< std::vector< T > > > &data)

References chunk_processing::is_jagged(), and chunk_processing::is_jagged_3d().

◆ chunk_by_size()

template<typename T >
std::vector< std::vector< T > > chunk_processing::Chunk< T >::chunk_by_size ( size_t  size)
inline

Definition at line 140 of file chunk.hpp.

140 {
141 if (data_.empty()) {
142 throw std::invalid_argument("Cannot chunk empty data");
143 }
144 if (size == 0) {
145 throw std::invalid_argument("Chunk size cannot be zero");
146 }
147 return make_chunks(size);
148 }
std::vector< std::vector< T > > make_chunks(size_t size) const
Definition chunk.hpp:95
size_t size() const
Definition chunk.hpp:186

◆ chunk_by_threshold()

template<typename T >
std::vector< std::vector< T > > chunk_processing::Chunk< T >::chunk_by_threshold ( threshold)
inline

Definition at line 150 of file chunk.hpp.

150 {
151 if (data_.empty()) {
152 throw std::invalid_argument("Cannot chunk empty data");
153 }
154 if (data_.size() < chunk_size_) {
155 throw std::invalid_argument("Input size must be at least chunk size");
156 }
157 if (threshold <= T{}) {
158 throw std::invalid_argument("Threshold must be positive");
159 }
160
161 std::vector<std::vector<T>> result;
162 std::vector<T> current_chunk;
163 T running_sum = T{};
164
165 for (const T& value : data_) {
166 if (running_sum + value > threshold && !current_chunk.empty()) {
167 result.push_back(current_chunk);
168 current_chunk.clear();
169 running_sum = T{};
170 }
171 current_chunk.push_back(value);
172 running_sum += value;
173 }
174
175 if (!current_chunk.empty()) {
176 result.push_back(current_chunk);
177 }
178
179 return result;
180 }

◆ chunk_count()

template<typename T >
size_t chunk_processing::Chunk< T >::chunk_count ( ) const
inline

Definition at line 190 of file chunk.hpp.

190 {
191 return (data_.size() + chunk_size_ - 1) / chunk_size_;
192 }

◆ dimensions()

template<typename T >
static constexpr size_t chunk_processing::Chunk< T >::dimensions ( )
inlinestaticconstexpr

Definition at line 230 of file chunk.hpp.

230 {
231 return get_depth<T>();
232 }

◆ get_chunk_size()

template<typename T >
size_t chunk_processing::Chunk< T >::get_chunk_size ( ) const
inline

Definition at line 194 of file chunk.hpp.

194 {
195 return chunk_size_;
196 }

◆ get_chunks()

template<typename T >
std::vector< std::vector< T > > chunk_processing::Chunk< T >::get_chunks ( ) const
inline

Definition at line 182 of file chunk.hpp.

182 {
183 return chunks_;
184 }
std::vector< std::vector< T > > chunks_
Definition chunk.hpp:20

Referenced by PYBIND11_MODULE(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ get_data()

template<typename T >
const std::vector< T > & chunk_processing::Chunk< T >::get_data ( ) const
inline

Definition at line 197 of file chunk.hpp.

197 {
198 return data_;
199 }

◆ get_depth()

template<typename T >
template<typename U >
static constexpr size_t chunk_processing::Chunk< T >::get_depth ( )
inlinestaticconstexprprivate

Definition at line 119 of file chunk.hpp.

119 {
120 if constexpr (is_vector<U>::value)
121 return 1 + get_depth<typename U::value_type>();
122 return 0;
123 }

◆ handle_jagged_2d()

template<typename T >
template<typename U >
std::vector< std::vector< U > > chunk_processing::Chunk< T >::handle_jagged_2d ( const std::vector< std::vector< U > > &  data)
inline

Definition at line 236 of file chunk.hpp.

236 {
237 if (!chunk_processing::is_jagged(data)) {
238 return data; // Already uniform
239 }
241 }
static std::vector< std::vector< U > > normalize(const std::vector< std::vector< U > > &jagged)
Definition chunk.hpp:26

References chunk_processing::is_jagged().

Referenced by TEST_F(), and TEST_F().

◆ handle_jagged_3d()

template<typename T >
template<typename U >
std::vector< std::vector< std::vector< U > > > chunk_processing::Chunk< T >::handle_jagged_3d ( const std::vector< std::vector< std::vector< U > > > &  data)
inline

Definition at line 245 of file chunk.hpp.

245 {
247 return data; // Already uniform
248 }
250 }
static std::vector< std::vector< std::vector< U > > > normalize_3d(const std::vector< std::vector< std::vector< U > > > &jagged_3d)
Definition chunk.hpp:50

References chunk_processing::is_jagged_3d().

Referenced by TEST_F(), and TEST_F().

◆ make_chunks()

template<typename T >
std::vector< std::vector< T > > chunk_processing::Chunk< T >::make_chunks ( size_t  size) const
inlineprivate

Definition at line 95 of file chunk.hpp.

95 {
96 std::vector<std::vector<T>> result;
97 result.reserve((data_.size() + size - 1) / size);
98
99 for (size_t i = 0; i < data_.size(); i += size) {
100 size_t chunk_end = std::min(i + size, data_.size());
101 result.emplace_back(data_.begin() + i, data_.begin() + chunk_end);
102 }
103 return result;
104 }

◆ set_chunk_size()

template<typename T >
void chunk_processing::Chunk< T >::set_chunk_size ( size_t  new_size)
inline

Definition at line 201 of file chunk.hpp.

201 {
202 validate_size(new_size, "Chunk size");
203 chunk_size_ = new_size;
205 }

◆ size()

template<typename T >
size_t chunk_processing::Chunk< T >::size ( ) const
inline

Definition at line 186 of file chunk.hpp.

186 {
187 return data_.size();
188 }

Referenced by TEST_F().

◆ update_chunks()

template<typename T >
void chunk_processing::Chunk< T >::update_chunks ( )
inlineprivate

Definition at line 106 of file chunk.hpp.

106 {
107 chunks_.clear();
108 for (size_t i = 0; i < data_.size(); i += chunk_size_) {
109 std::vector<T> chunk;
110 for (size_t j = 0; j < chunk_size_ && i + j < data_.size(); ++j) {
111 chunk.push_back(data_[i + j]);
112 }
113 chunks_.push_back(chunk);
114 }
115 }

◆ validate_dimensions()

template<typename T >
template<typename U >
void chunk_processing::Chunk< T >::validate_dimensions ( const std::vector< U > &  data,
size_t  expected_size = 0 
)
inline

Definition at line 253 of file chunk.hpp.

253 {
255 // For jagged arrays, normalize instead of throwing error
256 if (chunk_processing::is_jagged(data)) {
257 auto normalized = handle_jagged_2d(data);
258 if (expected_size > 0 && normalized.size() != expected_size) {
259 throw std::invalid_argument("Inconsistent dimensions after normalization");
260 }
261 return;
262 }
263
264 // For 3D arrays, handle jagged data similarly
267 reinterpret_cast<const std::vector<
268 std::vector<std::vector<typename U::value_type::value_type>>>&>(
269 data))) {
270 auto normalized = handle_jagged_3d(data);
271 if (expected_size > 0 && normalized.size() != expected_size) {
272 throw std::invalid_argument(
273 "Inconsistent dimensions after 3D normalization");
274 }
275 return;
276 }
277 }
278
279 // Check size consistency for non-jagged arrays
280 if (expected_size > 0 && data.size() != expected_size) {
281 throw std::invalid_argument("Inconsistent dimensions in nested array");
282 }
283
284 // Recursively validate inner dimensions
285 if (!data.empty()) {
286 validate_dimensions(data[0], data[0].size());
287 }
288 }
289 }
void validate_dimensions(const std::vector< U > &data, size_t expected_size=0)
Definition chunk.hpp:253
std::vector< std::vector< U > > handle_jagged_2d(const std::vector< std::vector< U > > &data)
Definition chunk.hpp:236
std::vector< std::vector< std::vector< U > > > handle_jagged_3d(const std::vector< std::vector< std::vector< U > > > &data)
Definition chunk.hpp:245

References chunk_processing::is_jagged(), and chunk_processing::is_jagged_3d().

Referenced by TEST_F().

◆ validate_size()

template<typename T >
void chunk_processing::Chunk< T >::validate_size ( size_t  size,
const std::string &  param 
) const
inlineprivate

Definition at line 89 of file chunk.hpp.

89 {
90 if (size == 0) {
91 throw std::invalid_argument(param + " must be greater than 0");
92 }
93 }

Member Data Documentation

◆ chunk_size_

template<typename T >
size_t chunk_processing::Chunk< T >::chunk_size_
private

Definition at line 18 of file chunk.hpp.

◆ chunks_

template<typename T >
std::vector<std::vector<T> > chunk_processing::Chunk< T >::chunks_
private

Definition at line 20 of file chunk.hpp.

◆ data_

template<typename T >
std::vector<T> chunk_processing::Chunk< T >::data_
private

Definition at line 19 of file chunk.hpp.


The documentation for this class was generated from the following file: