BitArray/BitFields - Review
Era Scarecrow
rtcvb32 at yahoo.com
Sat Jul 28 13:41:48 PDT 2012
BitFields:
https://github.com/rtcvb32/phobos/commit/729c96e2f20ddd0c05a1afb488030b5c4db3e012
new BitArray Overhead functions/templates:
https://github.com/rtcvb32/phobos/commit/55aaf82a39f3901f03f5f1ac26fd175c5b2cd167
BitArray:
https://github.com/rtcvb32/phobos/commit/568c0d1966208afe70cedcaddc3bb880dc3d481d
I'd like opinions of the code I've submitted, and certain
shortcomings that are present that appropriate changes would be
needed to fix. Only one of these has been a problem during
debugging and that's due to references. (All three patches must
be present to work)
BitArray:
1) Slices use references (sometimes) to previous bitarray. Since
it's a slice (like an array) this could be expected.
Solutions:
a) Drop the union, make all functions @safe (compared to
@trusted), and they are all ref/slices
b) Leave as sometimes ref, sometimes not; Use .dup when you NEED
to ensure different unique copies.
c) Apply COW (Copy-On-Write) where a known slice type (canExpand
is false) when a change would apply, it replaces the present
slice into it's own unique array.
d) All slices become new dup'ed allocated arrays (Most expensive
of them all)
2) opCast disabled: Anyone using opCast their code will break,
however depending on it's usage that may not be an issue. If
anyone can recommend alternative opCast(s) that won't prevent
normal use of casting, then they will be reimplemented.
3) dim - An artifact of the previous BitArray, and I think needs
to be removed as it has limited use.
4) Certain operators (opCat, opCat_r) I can't replace with
opBinary/opBinary_r.
5) Examples of additional use cases/construction that it
currently fails and should work.
6) Added rawWrite (for primitives), usefulness testing and
opinions.
7) opApply, toHash, opCmp, opEqual tested in various environments
(Like associative arrays) for problems, along with any possible
const issues.
BitFields:
I've concentrated on adding default values. A set of functions
previously present string functions 'myXXX' were present, why
were they present to begin with? Was it a limitation of what was
available at the time? Or was it to make compiling faster so it
didn't rely on other library functions? Or were the string
functions not available at the time? They might be removed once
this is truly answered.
//currently should work
struct A
{
mixin(bitfields!(
uint, "x", 2,
int, "y", 3,
uint, "z=1", 2,
bool, "flag=true", 1));
}
A obj;
assert(obj.x == 0);
assert(obj.z == 1); //default
assert(obj.flag == 1); //true/false should also work
More information about the Digitalmars-d-learn
mailing list