Documentation
¶
Overview ¶
Package bitmap 提供位图:用 1 bit 表示一个布尔状态,极省内存的大规模标记与集合运算。
典型:
- 签到:每天一张 bitmap,第 i 位=用户 i 是否签到——1000 万用户仅需 ~1.25MB/天; 多天 bitmap 做 And 可算"连续签到",Count 算"当日签到数";
- 去重 / 存在性标记:第 i 位=ID i 是否出现过(ID 稠密时比 map 省几十倍内存);
- 权限位 / 特征位:一个整数装不下时用 bitmap 表示大量开关。
与 pkg/utils/bloom 的区别:bloom 是概率型(有假阳性、省内存、不可枚举),用于 "可能存在"的快速判断;bitmap 是精确型(位与 ID 一一对应,可精确 Count/枚举), 用于 ID 稠密、需要精确计数与集合运算的场景。
底层 []uint64,按需增长。非并发安全(读多写少可在上层加锁;或每个写者独占一张)。 零值可用(空位图),也可用 New 预分配。
Index ¶
- func ConsecutiveFromEnd(days []*Bitmap, uid int) int
- type Bitmap
- func (b *Bitmap) And(other *Bitmap) *Bitmap
- func (b *Bitmap) AndNot(other *Bitmap) *Bitmap
- func (b *Bitmap) Clear(i int)
- func (b *Bitmap) Clone() *Bitmap
- func (b *Bitmap) Count() int
- func (b *Bitmap) Flip(i int) bool
- func (b *Bitmap) Or(other *Bitmap) *Bitmap
- func (b *Bitmap) Reset()
- func (b *Bitmap) Set(i int)
- func (b *Bitmap) Slice() []int
- func (b *Bitmap) Test(i int) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConsecutiveFromEnd ¶
ConsecutiveFromEnd 对一组"按时间顺序"的每日签到位图,返回用户 uid 从最后一天 往前数的连续签到天数(最后一天未签到则为 0)。days 顺序:days[len-1] 为最新一天。
例:days = [周一,周二,周三],uid 在周二、周三签到 → 从末尾连续 2 天。
Types ¶
type Bitmap ¶
type Bitmap struct {
// contains filtered or unexported fields
}
Bitmap 是一个可增长的位图。零值可用。非并发安全。
Click to show internal directories.
Click to hide internal directories.