Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Req. init?(words:) method #89

Closed
oscbyspro opened this issue Sep 25, 2023 · 5 comments
Closed

Req. init?(words:) method #89

oscbyspro opened this issue Sep 25, 2023 · 5 comments
Labels
addition oh, so shiny!
Milestone

Comments

@oscbyspro
Copy link
Owner

oscbyspro commented Sep 25, 2023

Being able to create integers from words from non-binary-integer sources seems useful to me.

init?(words: some RandomAccessCollection<UInt>) // with `Self.isSigned` signedness

-- 29/09/2023 --

init?(words: some RandomAccessCollection<UInt>, isSigned: Bool) // with `isSigned` signedness

In an immediate use case, I want to abstract integer-from-string decoding without adding niche string conversion requirements (#86). I can reasonably do this by populating a temporary words allocation and then converting that to an integer. I'm sure there are other use cases as well. Swift's BinaryInteger integer protocol only lets you do this when the words come from another binary integer, however. An init?(words:) method fixes that.


There are other variants of this method that could be useful, but I want a compelling use case:

init[?]([clamping|truncating]Words:isSigned:)
init[?]([clamping|truncating][Signed|Unsigned]Words:)
@oscbyspro oscbyspro added the addition oh, so shiny! label Sep 25, 2023
@oscbyspro oscbyspro added this to the v0.13.0 milestone Sep 25, 2023
@oscbyspro
Copy link
Owner Author

oscbyspro commented Sep 25, 2023

Also, is it OK to default empty words to signed integer zero? There are no such binary integer collections in Swift, but I think that's what the algorithm does without intervention. Alternatively, should the initialization fail if the collection is empty even for unsigned integers?

@oscbyspro
Copy link
Owner Author

oscbyspro commented Sep 25, 2023

Hm. I perhaps an acceptable rule is that inputs are valid if and only if they are valid outputs, meaning that words contains exactly the contents of self.words? That would ban empty collections for all integers in this package. But that would not treat the words as an integer, because integers can be infinitely sign-extended (so there's an infinite number of collections for every possible integer value). Edit: it's probably not an acceptable rule.

@oscbyspro
Copy link
Owner Author

oscbyspro commented Sep 29, 2023

Hm. I can make Signed<Magnitude>'s StaticBigInt initializer super elegant if the Magnitude has:

init?(words: some RandomAccessCollection<UInt>, isSigned: Bool) // static signedness is OK too

Edit: Welp. What I wanted to do didn't work out, but I think this method is useful enough to keep.

@oscbyspro
Copy link
Owner Author

I also caught a bug in the initial core integer method, as I did not pass isSigned to NBKChunkedInt.

@oscbyspro
Copy link
Owner Author

I haven't thought much about it, but I figure it's possible to support some non-random-access sequences using:

init(words: some Sequence<UInt>, last: UInt, isSigned: Bool)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition oh, so shiny!
Projects
None yet
Development

No branches or pull requests

1 participant