112 lines
2.5 KiB
C
112 lines
2.5 KiB
C
|
|
|
|
#include <HBit.h>
|
|
|
|
#ifndef LogD
|
|
#define LogD(...)
|
|
#endif
|
|
|
|
static void InitHBit(HBitType *data) {
|
|
if ((data[0] & kHBitInitFlag) == 0) {
|
|
return;
|
|
}
|
|
|
|
_HBitBase *base = (_HBitBase *)data;
|
|
base->flag &= ~kHBitInitFlag;
|
|
uint8_t *len = (uint8_t *)&base->len;
|
|
base->len = _HBIT_INIT_SIZE_INIT(len);
|
|
}
|
|
|
|
static HBitLenType GetBitLen(const HBitType *data) {
|
|
const _HBitBase *base = (const _HBitBase *)data;
|
|
InitHBit((HBitType *)data);
|
|
return base->len;
|
|
}
|
|
|
|
static void SetBit(HBitType *data, HBitIndexType index, HBitType value) {
|
|
_HBitBase *base = (_HBitBase *)data;
|
|
base->data[index / 8] = (base->data[index / 8] & ~(0x01 << (index % 8))) | ((!!value) << (index % 8));
|
|
}
|
|
|
|
static HBitType GetBit(const HBitType *data, HBitIndexType index) {
|
|
_HBitBase *base = (_HBitBase *)data;
|
|
return (base->data[index / 8] >> (index % 8)) & 0x01;
|
|
}
|
|
|
|
void HBitSet(HBitType *data, HBitIndexType index, HBitType value) {
|
|
if (index >= GetBitLen(data)) {
|
|
LogD("error index[%d], len[%d]", index, GetBitLen(data));
|
|
return ;
|
|
}
|
|
|
|
SetBit(data, index, value);
|
|
}
|
|
|
|
|
|
HBitType HBitGet(const HBitType *data, HBitIndexType index) {
|
|
if (index >= GetBitLen(data)) {
|
|
LogD("error index[%d], len[%d]", index, GetBitLen(data));
|
|
return 0;
|
|
}
|
|
|
|
return GetBit(data, index);
|
|
}
|
|
|
|
|
|
void HBitFill(HBitType *data, HBitType value) {
|
|
InitHBit(data);
|
|
_HBitBase *base = (_HBitBase *)data;
|
|
for (int i = 0; i < base->len / 8; ++i) {
|
|
base->data[i] = value ? 0xFF : 0x00;
|
|
}
|
|
|
|
for (int i = base->len / 8 * 8; i < base->len; ++i) {
|
|
SetBit(data, i, value);
|
|
}
|
|
}
|
|
|
|
|
|
void HBitReverse(HBitType *data, HBitIndexType index) {
|
|
HBitSet(data, index, !HBitGet(data, index));
|
|
}
|
|
|
|
HBitType HBitAny(const HBitType *data) {
|
|
const _HBitBase *base = (_HBitBase *)data;
|
|
InitHBit((HBitType *)data);
|
|
for (int i = 0; i < base->len / 8; ++i) {
|
|
if (base->data[i] != 0x00) {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
for (int i = base->len / 8 * 8; i < base->len; ++i) {
|
|
if (HBitGet(data, i)) {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
HBitType HBitAll(const HBitType *data) {
|
|
const _HBitBase *base = (_HBitBase *)data;
|
|
InitHBit((HBitType *)data);
|
|
for (int i = 0; i < base->len / 8; ++i) {
|
|
if (base->data[i] != 0xFF) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
for (int i = base->len / 8 * 8; i < base->len; ++i) {
|
|
if (!HBitGet(data, i)) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
HBitType HBitNone(const HBitType *data) {
|
|
return !HBitAny(data);
|
|
}
|