Numberick icon indicating copy to clipboard operation
Numberick copied to clipboard

IntegerView(s)

Open oscbyspro opened this issue 2 years ago • 6 comments

I need lots of free-function-y stuff to get things done without tying myself to any particular implementation. But free-function-y stuff is messy. The function signatures get way too long or way too terse. It is difficult to track, and tracking preconditions is worse.

So I've been thinking. I might want integer-view-y stuff instead, so things get neatly encapsulated, preconditions get checked on instantiation, and the type system can help out. I imagine these generic over various collections, but I probably need borrowing and consuming for that. I suppose mutable buffer pointers might do.

withStrictUnsignedInteger(...)
withNormalizedUnsignedInteger(...)
SuccinctUnsignedIntegerView(rebasing: view)
NormalizedUnsignedIntegerView(validating: view)

oscbyspro avatar Sep 16 '23 18:09 oscbyspro

One with...(_:) instead of multiple free-function shims is Greatness™

I have a dream ...of views without closures.

oscbyspro avatar Sep 17 '23 09:09 oscbyspro

Hm. I've had a hard time getting this to perform well with NBKDoubleWidth (shifts). It appears that NBKStrictBitPattern<UnsafeMutableBufferPointer<UInt>> is slow. I'm not sure why. I got close with NBKStrictBitPattern<UnsafeMutableWords>, however, where UnsafeMutableWords is a custom NBKDoubleWidth pointer.

oscbyspro avatar Sep 18 '23 12:09 oscbyspro

I'm basically hoping I can turn these into views when Swift gets those, that would be great. Maybe I should still go the custom-NBKDoubleWidth-pointer route until then, because the need for static methods is so silly. Hm. I'll think about it.

oscbyspro avatar Sep 20 '23 17:09 oscbyspro

So I realized something. Given how binary integers are tree-like, I could wrap sub-views in super-views. Maybe the compiler likes that better than converting back and forth between views? Conversions seem like they might require lots of ownership fiddling that isn't possible in Swift 5.7 or 5.8. Maybe I can do something cool with @dynamicMemberLookup too.

Edit: Doesn't seem like wrapping performs any better or worse (maybe non-pointers care).

Edit: Nope. Nope. Nope. @dynamicMemberLookup turns out to be dynamic. Who'd a thunk it?

oscbyspro avatar Sep 20 '23 18:09 oscbyspro

Free functions are hard to beat. Should I use static methods and retry when Swift gets proper views? Hm.

oscbyspro avatar Sep 21 '23 06:09 oscbyspro

I kept the namespaces but removed the views. I feel like the namespaces help keep things nice and tidy.

oscbyspro avatar Sep 21 '23 09:09 oscbyspro