| 379 | } |
| 380 | |
| 381 | uint64_t Ca4Block::Write(std::streambuf& buffer) { |
| 382 | const bool update = FilePosition() > 0; // True if already written to file |
| 383 | if (update) { |
| 384 | return static_cast<size_t>(block_length_); |
| 385 | } |
| 386 | |
| 387 | block_type_ = "##CA"; |
| 388 | // Calculate the block length. It's a bit tricky due to the dynamic number |
| 389 | // of links. |
| 390 | block_length_ = static_cast<uint64_t>(24 + 8); // Next CA/CN block required |
| 391 | size_t nof_links = 1; |
| 392 | if (Storage() == ArrayStorage::DgTemplate) { |
| 393 | const size_t nof_data_links = data_links_.size(); |
| 394 | nof_links += nof_data_links; |
| 395 | block_length_ += static_cast<uint64_t>(nof_data_links) * 8; |
| 396 | } |
| 397 | |
| 398 | if ((Flags() & CaFlag::DynamicSize) != 0) { |
| 399 | const size_t nof_dynamic_links = Dimensions(); |
| 400 | nof_links += nof_dynamic_links * 3; |
| 401 | block_length_ += static_cast<uint64_t>(nof_dynamic_links) * 8 * 3; |
| 402 | } |
| 403 | |
| 404 | if ((Flags() & CaFlag::InputQuantity) != 0) { |
| 405 | const size_t nof_quantity_links = Dimensions(); |
| 406 | nof_links += nof_quantity_links * 3; |
| 407 | block_length_ += static_cast<uint64_t>(nof_quantity_links) * 8 * 3; |
| 408 | } |
| 409 | if ((Flags() & CaFlag::OutputQuantity) != 0) { |
| 410 | nof_links += 3; |
| 411 | block_length_ += 8 * 3; // Only one reference link is possible |
| 412 | } |
| 413 | if ((Flags() & CaFlag::ComparisonQuantity) != 0) { |
| 414 | nof_links += 3; |
| 415 | block_length_ += 8 * 3; // Only one reference link is possible |
| 416 | } |
| 417 | if ((Flags() & CaFlag::Axis) != 0) { |
| 418 | const size_t nof_axis_links = Dimensions(); |
| 419 | nof_links += nof_axis_links; |
| 420 | block_length_ += static_cast<uint64_t>(nof_axis_links) * 8; |
| 421 | } |
| 422 | if ((Flags() & CaFlag::Axis) != 0 && (Flags() & CaFlag::FixedAxis) == 0) { |
| 423 | const size_t nof_axis_links = Dimensions(); |
| 424 | nof_links += nof_axis_links * 3; |
| 425 | block_length_ += static_cast<uint64_t>(nof_axis_links) * 8 * 3; |
| 426 | } |
| 427 | |
| 428 | block_length_ += 1 + 1 + 2 + 4 + 4 + 4 + (8 * Dimensions()); |
| 429 | if ((Flags() & CaFlag::FixedAxis) != 0) { |
| 430 | block_length_ += SumOfArray() * 8; |
| 431 | } |
| 432 | switch (Storage() ) { |
| 433 | case ArrayStorage::DgTemplate: |
| 434 | case ArrayStorage::CgTemplate: |
| 435 | block_length_ += ProductOfArray() * 8; |
| 436 | break; |
| 437 | |
| 438 | default: |