bitsets

static library for manipulating bit fields


Keywords
library.general
License
MIT
Install
dub fetch bitsets --version 0.1.0

Documentation

EnumSet

EnumSet is a D2 static library focused on enum sets.

Content

  • EnumRankInfo(E): static information about the enum E members.
  • EnumSet(E, S): a set of bits for the enum E and the container S.
  • EnumIndexedArray(E, T): encapsulates an array of T and uses enum E members for indexing or slicing.
  • EnumProcs(E, T): a call table for the callable T and the enum E used as selector.

Examples

Definition of the enum used in the two following example. it's important to understand that the members type and value don't matter.

enum mouseButtons{left = 0.123f, right = 0.53f, middle, shift, ctrl, x1, x2}

Generate the static information using EnumRankInfo(E). This template is mostly used internally but not only, as shown in the last example.

alias mbInfos = EnumRankInfo!mouseButtons;
assert(mbInfos.max == 6);
assert(mbInfos.count == 7);
// wait... not .53f ? It's the **rank** not the member **value**.
assert(mbInfos[mouseButtons.right] == 1);
assert(mbInfos[1] == mouseButtons.right); 

Definition and usage of a set of mouseButtons with EnumSet(E, S).

alias setOfMouseButtons = EnumSet!(mouseButtons, Set8);

// usually made in a low level window class
void winMouseMessage(in uint wParam)
{
    fMouseButtons = 0;
    // fMouseButtons is a private setOfMouseButtons.
    if ((wParam & MK_CONTROL) == MK_CONTROL) 
        fMouseButtons += mouseButtons.ctrl;
    if ((wParam & MK_LBUTTON) == MK_LBUTTON) 
        fMouseButtons += mouseButtons.left;
    if ((wParam & MK_RBUTTON) == MK_RBUTTON) 
        fMouseButtons += mouseButtons.right;
    // etc.
    foreach(child, fChildList)
        child.processMouseMessage(fLeft, fRight, fMouseButtons);
}

// child process example, we've get rid of the OS native API and we use our own.
// the 'in' operator is used to test the presence of a bit in the set.
void processMouseMessage(in uint left, in uint right, setOfMouseButtons mouseBtns)
{
    if (mouseButtons.right in mouseBtns)
        if (mouseButtons.ctrl in mouseBtns)
            eraseCanvas(RGBA(0.5f,0.1f,0.9f,1.0f));     
}

The last example illustrates EnumProcs(E, T). It's used to call a typed shaping function.

enum ShaperKind {sin, parabolic, triangular, fuzz, soft}

// shapers prototype and implementation
alias shaperProto = float function(in float x);
float shapeSin(in float x){return 0.0f;}
float shapePar(in float x){return 0.0f;}
float shapeTri(in float x){return 0.0f;}
float shapeFzz(in float x){return 0.0f;}
float shapeSft(in float x){return 0.0f;}

alias Shapers = EnumProcs!(ShaperKind, shaperProto);
Shapers shapers;
ShaperKind shapeKind;

// let's say it's called in a constructor.
void initVariables()
{
    shapeKind = ShaperKind.soft;
    shapers = Shapers(&shapeSin, &shapePar, 
        &shapeTri, &shapeFzz, &shapeSft);
}

// uses EnumRankInfo to translate the itemIndex of a list to a ShaperKind member.
void ui_selShapeChange(ui_BaseObject aNotifier)
{
    if (ui_selShape.itemIndex == -1) return;
    shapeKind = EnumRankInfo!ShaperKind[ui_selShape.itemIndex]; 
}

// buffer process, it's really like a switch(enum) statement.
// note that calling shapers in the loop is actually a bad idea (the function selector does not varies in the loop)
// but it's just an example.
void process(in float[] inBuff, out float[] outBuff, in uint buffSz)
{
    for(auto i = 0; i < buffSz; i++)
        // opCall(index, params...)
        outBuff[i] = shapers(shapeKind, inBuff[i]);
}

Notes

  • includes a Coedit project to build and test the lib. However as the source is monolithic it's easy to build or to add elsewhere.
  • MIT license.