Skip to content

Commit

Permalink
feat: util/fn/digits.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
arumakan1727 committed Aug 5, 2023
1 parent 9fb632e commit 9c8f324
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/armkn/util/fn/digits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <vector>
#include "../alias/stdint.hpp"

/// 実際の表記と順序が逆になるので注意;
/// `digits(123)` => `{3, 2, 1}`
template <u32 BASE = 10>
inline auto digits(u64 n) -> std::vector<int> {
static_assert(BASE >= 2);
if (n == 0) return {0};
std::vector<int> d;
d.reserve(64);
while (n > 0) {
d.push_back(n % BASE);
n /= BASE;
}
d.shrink_to_fit();
return d;
}

0 comments on commit 9c8f324

Please sign in to comment.