meta/fields_mask.go

74 lines
1.4 KiB
Go
Raw Normal View History

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