bounded-integer icon indicating copy to clipboard operation
bounded-integer copied to clipboard

Loops and other runtime constructs

Open dumblob opened this issue 3 years ago • 4 comments

Since ever I wanted to have such integer available. The issue is I could not find any nice way how to handle loops with during-runtime-determined number of iterations. This holds also for other runtime constructs if any.

How do you handle operations with integers inside such loops?

dumblob avatar Oct 13 '22 20:10 dumblob

This is where containers::integer_range comes in: https://github.com/davidstone/bounded-integer/blob/main/include/containers/integer_range.hpp

The source of that run-time value needs to have some known bounds (unless it's potentially infinitely large, which I'm planning on supporting eventually). Then you can use it as

for (auto x : containers::integer_range(number_of_iterations)) {
}

or

for (auto x : containers::integer_range(first, last)) {
}

or even

for (auto x : containers::integer_range(first, last, step_size)) {
}

and this will give x the type that it needs to hold the valid integers in that range. In some ways, this just pushes the problem back, because you need to specify the type of, say, number_of_iterations. But that's a problem that exists today, we just tend to not think about it and just use either int or std::size_t, even when those might not be the best choice.

davidstone avatar Oct 13 '22 21:10 davidstone

unless it's potentially infinitely large, which I'm planning on supporting eventually

This is actually what I am after :wink:.

Do you have already some ideas how to approach it? Or do you just want to kind of "give up" and resort to bigint or alike?

dumblob avatar Oct 17 '22 14:10 dumblob

I'm going to add support for arbitrarily large integers with compile-time bounds, and those would be computed in the same way as anything else in the library. In addition to that, sometimes it makes sense to just go to unbounded, which I also plan on implementing at some point, but that would be a user decision. It may also make sense to have a hybrid type that functions like std::string's small-buffer optimization.

davidstone avatar Nov 08 '22 00:11 davidstone

It may also make sense to have a hybrid type that functions like std::string's small-buffer optimization.

Would this have any user-facing interface or will it be just an implementation detail?

Otherwise great news!

dumblob avatar Nov 08 '22 08:11 dumblob