|
21 | 21 | #define DENSE_STORE_IMPL_HPP |
22 | 22 |
|
23 | 23 | #include <algorithm> |
| 24 | +#include <filesystem> |
| 25 | + |
| 26 | +#include "common_defs.hpp" |
24 | 27 |
|
25 | 28 | namespace datasketches { |
26 | 29 |
|
@@ -247,6 +250,14 @@ void DenseStore<Derived, Allocator>::reset_bins(size_type from_index, size_type |
247 | 250 | std::fill(bins.begin() + from_index - offset, bins.begin() + to_index - offset + 1, 0); |
248 | 251 | } |
249 | 252 |
|
| 253 | +template<class Derived, typename Allocator> |
| 254 | +bool DenseStore<Derived, Allocator>::operator==(const DenseStore<Derived, Allocator>& other) const { |
| 255 | + return offset == other.offset && |
| 256 | + min_index == other.min_index && |
| 257 | + max_index == other.max_index && |
| 258 | + bins == other.bins; |
| 259 | +} |
| 260 | + |
250 | 261 | template<class Derived, typename Allocator> |
251 | 262 | Derived& DenseStore<Derived, Allocator>::derived() { |
252 | 263 | return static_cast<Derived&>(*this); |
@@ -338,11 +349,62 @@ typename DenseStore<Derived, Allocator>::reverse_iterator::reference DenseStore< |
338 | 349 |
|
339 | 350 | template<class Derived, typename Allocator> |
340 | 351 | void DenseStore<Derived, Allocator>::serialize(std::ostream& os) const { |
| 352 | + derived().serialize(os); |
| 353 | +} |
| 354 | + |
| 355 | +template<class Derived, typename Allocator> |
| 356 | +Derived DenseStore<Derived, Allocator>::deserialize(std::istream& is) { |
| 357 | + return Derived::deserialize(is); |
| 358 | +} |
| 359 | + |
| 360 | +template<class Derived, typename Allocator> |
| 361 | +void DenseStore<Derived, Allocator>::serialize_common(std::ostream& os) const { |
341 | 362 | if (is_empty()) { |
342 | 363 | return; |
343 | 364 | } |
344 | 365 |
|
| 366 | + // Serialize the range information |
| 367 | + write(os, min_index); |
| 368 | + write(os, max_index); |
| 369 | + write(os, offset); |
345 | 370 |
|
| 371 | + // Serialize the bins array (only the used portion) |
| 372 | + const size_type num_bins = bins.size(); |
| 373 | + write(os, num_bins); |
| 374 | + |
| 375 | + size_type non_empty_bins = 0; |
| 376 | + for (const double& count : bins) { |
| 377 | + non_empty_bins += (count > 0.0); |
| 378 | + } |
| 379 | + write(os, non_empty_bins); |
| 380 | + |
| 381 | + for (const Bin& bin : *this) { |
| 382 | + write(os, bin.getIndex()); |
| 383 | + write(os, bin.getCount()); |
| 384 | + } |
| 385 | +} |
| 386 | + |
| 387 | +template<class Derived, typename Allocator> |
| 388 | +void DenseStore<Derived, Allocator>::deserialize_common(Derived& store, std::istream& is) { |
| 389 | + if (is.peek() == std::istream::traits_type::eof()) { |
| 390 | + return; |
| 391 | + } |
| 392 | + // Deserialize the range information |
| 393 | + store.min_index = read<size_type>(is); |
| 394 | + store.max_index = read<size_type>(is); |
| 395 | + store.offset = read<size_type>(is); |
| 396 | + |
| 397 | + // Deserialize the bins array |
| 398 | + const auto num_bins = read<size_type>(is); |
| 399 | + store.bins.resize(num_bins, 0.0); |
| 400 | + |
| 401 | + const auto non_empty_bins = read<size_type>(is); |
| 402 | + // Read the actual bin counts |
| 403 | + for (size_type i = 0; i < non_empty_bins; ++i) { |
| 404 | + const auto index = read<int>(is); |
| 405 | + const auto count = read<double>(is); |
| 406 | + store.add(index, count); |
| 407 | + } |
346 | 408 | } |
347 | 409 | } |
348 | 410 |
|
|
0 commit comments