Skip to content

Commit

Permalink
shufr: use std::optional instead of xph::nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
XPhyro committed Sep 5, 2024
1 parent 1386dc4 commit 459f4b2
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/cpp/util/core/shufr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <fstream>
#include <iostream>
#include <limits>
#include <optional>
#include <random>
#include <ranges>
#include <set>
Expand All @@ -16,14 +17,13 @@

#include <xph/die.hpp>
#include <xph/exec_info.hpp>
#include <xph/nullable.hpp>
#include <xph/parse.hpp>

void parseargs(int& argc, char**& argv);

char optdelim = '\n';
bool optargisline = false, optprefixline = false, optuniquemax = false;
xph::nullable<std::size_t> optrangelow, optrangehigh, optcount, optunique, optprefixgroup;
std::optional<std::size_t> optrangelow, optrangehigh, optcount, optunique, optprefixgroup;

DEFINE_EXEC_INFO();

Expand All @@ -39,8 +39,7 @@ int main(int argc, char* argv[])
std::ranges::copy(std::views::counted(argv, argc), std::back_inserter(lines));
} else if (optrangelow && optrangehigh) {
for (std::ostringstream ss;
const auto& i : std::views::iota(static_cast<std::size_t>(optrangelow),
static_cast<std::size_t>(optrangehigh) + 1ul)) {
const auto& i : std::views::iota(*optrangelow, *optrangehigh + 1ul)) {
ss << i;
lines.push_back(ss.str());
ss.str("");
Expand All @@ -60,8 +59,8 @@ int main(int argc, char* argv[])
if (optuniquemax)
optunique = lines.size();
else if (optunique && optunique > lines.size())
xph::die("not enough lines to ensure unique sequences of size ", optunique);
if (optprefixgroup && static_cast<std::size_t>(optprefixgroup) == 0)
xph::die("not enough lines to ensure unique sequences of size ", *optunique);
if (optprefixgroup && *optprefixgroup == 0)
optprefixgroup = lines.size();

std::random_device rdev;
Expand All @@ -70,8 +69,8 @@ int main(int argc, char* argv[])
if (optunique <= 1ul) {
std::uniform_int_distribution<std::size_t> dist(0u, lines.size() - 1);
for (std::size_t count = 0; !optcount || count < optcount; ++count) {
if (optprefixgroup && !(count % static_cast<std::size_t>(optprefixgroup)))
std::cout << count / static_cast<std::size_t>(optprefixgroup) + 1 << optdelim;
if (optprefixgroup && !(count % *optprefixgroup))
std::cout << count / *optprefixgroup + 1 << optdelim;
if (optprefixline)
std::cout << count + 1 << ": ";
std::cout << lines[dist(rng)] << optdelim;
Expand Down Expand Up @@ -101,11 +100,11 @@ int main(int argc, char* argv[])
auto i = active_indices[r];
active_indices.erase(active_indices.begin() + r);

index_turns[i] = optunique;
index_turns[i] = *optunique;
inactive_indices.insert(i);

if (optprefixgroup && !(count % static_cast<std::size_t>(optprefixgroup)))
std::cout << count / static_cast<std::size_t>(optprefixgroup) + 1 << optdelim;
if (optprefixgroup && !(count % *optprefixgroup))
std::cout << count / *optprefixgroup + 1 << optdelim;
if (optprefixline)
std::cout << count + 1 << ": ";
std::cout << lines[i] << optdelim;
Expand Down

0 comments on commit 459f4b2

Please sign in to comment.