oscbyspro / Ultimathnum

Binary arithmetic reimagined in Swift
Apache License 2.0
4 stars 1 forks source link

Simpler bit counting methods #16

Closed oscbyspro closed 3 weeks ago

oscbyspro commented 3 weeks ago

Having used the count(Bit.Selection) methods for a while now, I think I would prefer something simpler.

| current                  | perhaps this is a better idea |
|--------------------------|-------------------------------|
| size()                   |                        size() |
| count(.entropy)          |                     entropy() |
|--------------------------|-------------------------------|
| count(            1 )    |                      count(1) |
| count( .ascending(1))    |                  ascending(1) |
| count(.descending(1))    |                 descending(1) |
|--------------------------|-------------------------------|
| count( .nonascending(1)) |               nonascending(1) | or size() -  ascending(1)
| count(.nondescending(1)) |              nondescending(1) | or size() - descending(1)
|--------------------------|-------------------------------|
| count(   .appendix)      |          descending(appendix) |
| count(.nonappendix)      |       nondescending(appendix) | or size() - descending(appendix)
oscbyspro commented 3 weeks ago

Hm. I suppose it's possible to preserve the count(...) format with something like count(ascending:).

oscbyspro commented 3 weeks ago

Some of it is motivated by brevity, but I also don't want alphabet soup.

oscbyspro commented 3 weeks ago

Hm. I thought about adding BinaryInteger/entropy() but I'm not yet sure. Conceptually the return type should be a signed machine word because the magnitude of an unsigned 1-bit integer cannot represent the entropy 2-bit of 1. At the same time, returning non-magnitudes when bit counting is rather unexpected. I suppose doing nothing is an option. It's a bit wordy at the moment, but view types would fix that:

x.elements.entropy() // vs x.withUnsafeBinaryIntegerElements({ $0.entropy() })

Edit: 1-bit integers are banned because machines don't like them, but still.