meta/fields_mask.go

62 lines
1.3 KiB
Go

package meta
const (
FieldsMaskCapacity = 4
FieldsMaskPartBitSize = 64
)
func MakeFieldsMaskFromInt64(v int64) FieldsMask {
var mask FieldsMask
mask.SetPartFromInt64(0, v)
return mask
}
type FieldsMaskPart uint64
func (part FieldsMaskPart) FieldIsDirty(index uint64) bool {
return (1<<index)&part != 0
}
type FieldsMask struct {
parts [FieldsMaskCapacity]FieldsMaskPart
}
func (fm *FieldsMask) SetPartFromUint64(index int, value uint64) {
fm.parts[index] = FieldsMaskPart(value)
}
func (fm *FieldsMask) SetPartFromInt64(index int, value int64) {
fm.parts[index] = FieldsMaskPart(value)
}
func (fm FieldsMask) FieldChanged(index uint64) bool {
partIndex := fm.partIndex(index)
fieldIndex := fm.fieldIndex(index)
part := fm.parts[partIndex]
return part.FieldIsDirty(fieldIndex)
}
func (fm *FieldsMask) SetFieldChanged(index uint64) {
partIndex := fm.partIndex(index)
fieldIndex := fm.fieldIndex(index)
fm.parts[partIndex] |= (1 << fieldIndex)
}
func (fm FieldsMask) IsFilled() bool {
for _, mask := range fm.parts {
if mask > 0 {
return true
}
}
return false
}
func (fm FieldsMask) partIndex(index uint64) uint64 {
return index / FieldsMaskPartBitSize
}
func (fm FieldsMask) fieldIndex(index uint64) uint64 {
return index % FieldsMaskPartBitSize
}