2022-10-01 21:09:54 +03:00
|
|
|
package meta
|
|
|
|
|
|
|
|
const (
|
|
|
|
FieldsMaskCapacity = 4
|
2023-06-06 15:14:33 +03:00
|
|
|
FieldsMaskPartBitSize = 64
|
2022-10-01 21:09:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func MakeFieldsMaskFromInt64(v int64) FieldsMask {
|
|
|
|
var mask FieldsMask
|
2023-06-06 15:14:33 +03:00
|
|
|
mask.SetPartFromInt64(0, v)
|
2022-10-01 21:09:54 +03:00
|
|
|
|
|
|
|
return mask
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
type FieldsMaskPart uint64
|
2022-10-01 21:09:54 +03:00
|
|
|
|
2023-06-06 15:14:33 +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 {
|
2023-06-06 15:14:33 +03:00
|
|
|
parts [FieldsMaskCapacity]FieldsMaskPart
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (fm *FieldsMask) SetPartFromUint64(index int, value uint64) {
|
|
|
|
fm.parts[index] = FieldsMaskPart(value)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +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 {
|
2023-06-06 15:14:33 +03:00
|
|
|
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) {
|
2023-06-06 15:14:33 +03:00
|
|
|
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 {
|
2023-06-06 15:14:33 +03:00
|
|
|
for _, mask := range fm.parts {
|
2022-10-01 21:09:54 +03:00
|
|
|
if mask > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-06-17 10:45:52 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (fm FieldsMask) partIndex(index uint64) uint64 {
|
|
|
|
return index / FieldsMaskPartBitSize
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (fm FieldsMask) fieldIndex(index uint64) uint64 {
|
|
|
|
return index % FieldsMaskPartBitSize
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|