generated from seqan/app-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
517 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// -------------------------------------------------------------------------------------------------- | ||
// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin | ||
// Copyright (c) 2016-2023, Knut Reinert & MPI für molekulare Genetik | ||
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License | ||
// shipped with this file and also available at: https://github.com/seqan/raptor/blob/main/LICENSE.md | ||
// -------------------------------------------------------------------------------------------------- | ||
|
||
/*!\file | ||
* \brief Provides raptor::formatted_bytes. | ||
* \author Enrico Seiler <enrico.seiler AT fu-berlin.de> | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <string> | ||
|
||
namespace raptor | ||
{ | ||
|
||
[[nodiscard]] inline std::string formatted_bytes(size_t const bytes) | ||
{ | ||
assert(bytes > 0); | ||
|
||
size_t iterations{}; | ||
size_t integer{bytes}; | ||
|
||
while (integer >> 10u && iterations < 6u) | ||
{ | ||
integer >>= 10u; | ||
++iterations; | ||
} | ||
|
||
// While this is a bit more involved, we can avoid using floating point numbers. | ||
auto first_decimal_position = [&]() | ||
{ | ||
assert(iterations > 0u); | ||
size_t decimal{bytes}; | ||
decimal -= integer << (iterations * 10u); // Substract bytes represented by integer, e.g. -5GiB | ||
decimal >>= (iterations - 1u) * 10u; // Shift to next smallest unit, e.g. 800MiB | ||
decimal = decimal * 1000u / 1024u; // Account for using decimal system, i.e. 800MiB != 0.8GiB | ||
size_t const diff{decimal - (decimal / 100u) * 100u}; // We want to round up to 1 decimal position | ||
uint32_t const round_up{diff >= 50u}; | ||
decimal += round_up * 100u - diff; | ||
decimal /= 100u; | ||
return decimal; | ||
}; | ||
|
||
auto formatted_string = [&]() | ||
{ | ||
static constexpr int8_t int_to_char_offset{'0'}; // int 0 as char: char{0 + 48} = '0' | ||
size_t const decimal = iterations ? first_decimal_position() : 0u; | ||
assert(decimal <= 10u); | ||
|
||
if (!iterations) // No decimals for Bytes | ||
return std::to_string(integer); | ||
else if (decimal < 10u) // No need to round integer part | ||
return std::to_string(integer) + '.' + static_cast<char>(decimal + int_to_char_offset); | ||
else // Round integer part, e.g., 5.99 MiB should report 6.0 MiB | ||
{ | ||
++integer; | ||
// Check whether rounding results in a change of unit, e.g. 1023.99MiB to 1.0GiB | ||
if (integer >> 10u) | ||
{ | ||
++iterations; | ||
integer >>= 10u; | ||
} | ||
return std::to_string(integer) + ".0"; | ||
} | ||
}; | ||
|
||
std::string const formatted{formatted_string()}; | ||
switch (iterations) | ||
{ | ||
case 0: | ||
return "[Bytes]: " + formatted; | ||
case 1: | ||
return "[KiB]: " + formatted; | ||
case 2: | ||
return "[MiB]: " + formatted; | ||
case 3: | ||
return "[GiB]: " + formatted; | ||
case 4: | ||
return "[TiB]: " + formatted; | ||
case 5: | ||
return "[PiB]: " + formatted; | ||
default: | ||
return "[EiB]: " + formatted; | ||
} | ||
} | ||
|
||
} // namespace raptor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// -------------------------------------------------------------------------------------------------- | ||
// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin | ||
// Copyright (c) 2016-2023, Knut Reinert & MPI für molekulare Genetik | ||
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License | ||
// shipped with this file and also available at: https://github.com/seqan/raptor/blob/main/LICENSE.md | ||
// -------------------------------------------------------------------------------------------------- | ||
|
||
/*!\file | ||
* \brief Provides raptor::formatted_index_size. | ||
* \author Enrico Seiler <enrico.seiler AT fu-berlin.de> | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
#include <filesystem> | ||
#include <string> | ||
|
||
#include <raptor/argument_parsing/formatted_bytes.hpp> | ||
|
||
namespace raptor | ||
{ | ||
|
||
[[nodiscard]] inline size_t index_size_in_KiB(std::filesystem::path index_path, uint8_t const parts) | ||
{ | ||
size_t index_size_in_bytes{}; | ||
if (parts == 1u) | ||
{ | ||
index_size_in_bytes = std::filesystem::file_size(index_path); | ||
} | ||
else | ||
{ | ||
for (size_t part = 0u; part < parts; ++part) | ||
{ | ||
index_size_in_bytes += std::filesystem::file_size(index_path.string() + "_" + std::to_string(part)); | ||
} | ||
} | ||
|
||
return index_size_in_bytes >> 10; | ||
} | ||
|
||
[[nodiscard]] inline std::string formatted_index_size(std::filesystem::path index_path, uint8_t const parts) | ||
{ | ||
return formatted_bytes(index_size_in_KiB(index_path, parts) << 10); | ||
} | ||
|
||
} // namespace raptor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.