meta/fields_mask.go

62 lines
1.3 KiB
Go
Raw Permalink Normal View History

2022-10-01 21:09:54 +03:00
package meta
const (
FieldsMaskCapacity = 4
FieldsMaskItemBitSize = 64
)
func MakeFieldsMaskFromInt64(v int64) FieldsMask {
var mask FieldsMask
mask.SetItemFromInt64(0, v)
return mask
}
type FieldsMaskItem uint64
func (fmi FieldsMaskItem) FieldIsDirty(index uint64) bool {
return (1<<index)&fmi != 0
}
type FieldsMask struct {
masks [FieldsMaskCapacity]FieldsMaskItem
}
func (fm *FieldsMask) SetItemFromUint64(index int, value uint64) {
fm.masks[index] = FieldsMaskItem(value)
}
func (fm *FieldsMask) SetItemFromInt64(index int, value int64) {
fm.masks[index] = FieldsMaskItem(value)
}
func (fm FieldsMask) FieldChanged(index uint64) bool {
itemIndex := fm.itemIndex(index)
maskIndex := fm.maskIndex(index)
mask := fm.masks[itemIndex]
return mask.FieldIsDirty(maskIndex)
}
func (fm *FieldsMask) SetFieldChanged(index uint64) {
itemIndex := fm.itemIndex(index)
maskIndex := fm.maskIndex(index)
fm.masks[itemIndex] |= (1 << maskIndex)
}
func (fm FieldsMask) IsFilled() bool {
for _, mask := range fm.masks {
if mask > 0 {
return true
}
}
return false
}
func (fm FieldsMask) itemIndex(index uint64) uint64 {
return index / FieldsMaskItemBitSize
}
func (fm FieldsMask) maskIndex(index uint64) uint64 {
return index % FieldsMaskItemBitSize
}