BitArray - preview of what's to come?

Era Scarecrow rtcvb32 at yahoo.com
Wed May 30 04:04:36 PDT 2012


  Got some improvements, although the bulk work needs to be tested 
more otherwise it all seems to work quite well. Once I figure out 
how to post to GitHub I'll upload a version for everyone to play 
with and review. Code's not beautiful, but other than the length 
function, most of it is easy to follow.

  I'll see if I can post it tomorrow, depending on how much my 
brain wants to be my friend.

On Sunday, 27 May 2012 at 07:04:36 UTC, Alex Rønne Petersen 
wrote:
> It could definitely use some improvement. In particular:
>
> * It still uses the deprecated operator overload methods, 
> rather than opBinary(Right) and opUnary.

  Fixed, except I can't seem to get around using opCat, beyond 
that it uses only the newer methods. and less code in those spots.

> * It's not quite const/immutable-friendly.

  It's const friendly now; which includes slices; That doesn't 
include immutable though as I'll probably have to make a whole 
bunch of new functions just to handle immutable but otherwise it 
doesn't seem like it's out of reach.

> * It forces the GC on you. Probably not easy to solve until 
> Andrei comes up with an allocators design.

  With the compact version (up to 256 bits) the GC is not needed.

> * Needs more pure and nothrow.

  Since bitmanips templates (The version I have anyways) aren't 
pure/nothrow, neither can the rest of it be. If/when those get 
fixed I'll tag it as best I can.

> * Needs more @safe/@trusted.

  Tried to tag as much as I can. Half of it's @safe, the other 
half @trusted.

> * The init() methods are very unintuitive at first glance.

  Still not very intuitive; however constructors are also 
available that do as good if not better than init.

> * Should probably deprecate the reverse and sort properties.

  Depreciated.


  Other features: Slice operations available, so you can slice 
specific sets of bits. Since opDollar doesn't work right I can't 
rely on them just yet. uses and accepts ulong for the slices and 
opIndex. The BitArray footprint is about 36 bytes for 32bit 
systems, and likely 40bytes for 64bit systems.

  bool[4] x = [1, 1, 0, 0];
  BitArray ba = BitArray(x);

  ba = ba[2 .. ba.length]; //or BitArray(x, 2, x.length)
  assert(ba == x[2 .. $]);


// const

  ba = BitArray(x);
  const BitArray cba = ba;
  assert(cba == ba);

  const BitArray slice = ba[2 .. ba.length]; //slices work too!
  assert(ba[2 .. cba.length] == cba);
  ba[0 .. 2] = cba[];


// compact / GC-less.

  assert(ba.isCompact()); //if it's true, no GC involved.
  BitArray ca = ba;
  ca[0] = 0;
  assert(ca != ba);

  ba.length = 1000;
  assert(!ba.isCompact()); //GC'd
  ba = ba[0 .. 4];
  assert(!ba.isCompact()); //slice doesn't automatically convert 
to compact

  ca = ba;
  ca[0] = 0;
  assert(ba == ca); //like a regular slice, shares memory


More information about the Digitalmars-d-learn mailing list