/* Return the value (0 or 1) of bit n in bitmask bmp */ staticunsignedint _getbit(conststruct bitmask *bmp, unsignedint n)
{ if (n < bmp->size) return (bmp->maskp[n/bitsperlong] >> (n % bitsperlong)) & 1; else return0;
}
/* Set bit n in bitmask bmp to value v (0 or 1) */ staticvoid _setbit(struct bitmask *bmp, unsignedint n, unsignedint v)
{ if (n < bmp->size) { if (v)
bmp->maskp[n/bitsperlong] |= 1UL << (n % bitsperlong); else
bmp->maskp[n/bitsperlong] &=
~(1UL << (n % bitsperlong));
}
}
staticconstchar *nexttoken(constchar *q, int sep)
{ if (q)
q = strchr(q, sep); if (q)
q++; return q;
}
/* Set a single bit i in bitmask */ struct bitmask *bitmask_setbit(struct bitmask *bmp, unsignedint i)
{
_setbit(bmp, i, 1); return bmp;
}
/* Set all bits in bitmask: bmp = ~0 */ struct bitmask *bitmask_setall(struct bitmask *bmp)
{ unsignedint i; for (i = 0; i < bmp->size; i++)
_setbit(bmp, i, 1); return bmp;
}
/* Clear all bits in bitmask: bmp = 0 */ struct bitmask *bitmask_clearall(struct bitmask *bmp)
{ unsignedint i; for (i = 0; i < bmp->size; i++)
_setbit(bmp, i, 0); return bmp;
}
/* True if all bits are clear */ int bitmask_isallclear(conststruct bitmask *bmp)
{ unsignedint i; for (i = 0; i < bmp->size; i++) if (_getbit(bmp, i)) return0; return1;
}
/* True if specified bit i is set */ int bitmask_isbitset(conststruct bitmask *bmp, unsignedint i)
{ return _getbit(bmp, i);
}
/* Number of lowest set bit (min) */ unsignedint bitmask_first(conststruct bitmask *bmp)
{ return bitmask_next(bmp, 0);
}
/* Number of highest set bit (max) */ unsignedint bitmask_last(conststruct bitmask *bmp)
{ unsignedint i; unsignedint m = bmp->size; for (i = 0; i < bmp->size; i++) if (_getbit(bmp, i))
m = i; return m;
}
/* Number of next set bit at or above given bit i */ unsignedint bitmask_next(conststruct bitmask *bmp, unsignedint i)
{ unsignedint n; for (n = i; n < bmp->size; n++) if (_getbit(bmp, n)) break; return n;
}
q = buf; while (p = q, q = nexttoken(q, ','), p) { unsignedint a; /* begin of range */ unsignedint b; /* end of range */ unsignedint s; /* stride */ constchar *c1, *c2; /* next tokens after '-' or ',' */ char nextc; /* char after sscanf %u match */ int sret; /* sscanf return (number of matches) */
sret = sscanf(p, "%u%c", &a, &nextc); if (!scan_was_ok(sret, nextc, ",-")) goto err;
b = a;
s = 1;
c1 = nexttoken(p, '-');
c2 = nexttoken(p, ','); if (c1 != NULL && (c2 == NULL || c1 < c2)) {
sret = sscanf(c1, "%u%c", &b, &nextc); if (!scan_was_ok(sret, nextc, ",:")) goto err;
c1 = nexttoken(c1, ':'); if (c1 != NULL && (c2 == NULL || c1 < c2)) {
sret = sscanf(c1, "%u%c", &s, &nextc); if (!scan_was_ok(sret, nextc, ",")) goto err;
}
} if (!(a <= b)) goto err; if (b >= bmp->size) goto err; while (a <= b) {
_setbit(bmp, a, 1);
a += s;
}
} return0;
err:
bitmask_clearall(bmp); return -1;
}
int bitmask_displaylist(char *buf, int buflen, conststruct bitmask *bmp)
{ int len = 0; /* current bit is 'cur', most recently seen range is [rbot, rtop] */ unsignedint cur, rbot, rtop;
if (buflen > 0)
*buf = 0;
rbot = cur = bitmask_first(bmp); while (cur < bmp->size) {
rtop = cur;
cur = bitmask_next(bmp, cur+1); if (cur >= bmp->size || cur > rtop + 1) {
len = emit(buf, buflen, rbot, rtop, len);
rbot = cur;
}
} return len;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.