/* Copyright (c) 2023 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "document/DocumentDirtyState.h" #include #include #define stemobject_implementation DocumentDirtyState stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); DocumentDirtyState * DocumentDirtyState_create(unsigned int fieldCount) { stemobject_create_implementation(init, fieldCount) } bool DocumentDirtyState_init(DocumentDirtyState * self, unsigned int fieldCount) { call_super(init, self); self->fieldCount = fieldCount; self->fields = calloc(fieldCount, sizeof(*self->fields)); self->masterValue = 0; return true; } void DocumentDirtyState_dispose(DocumentDirtyState * self) { free(self->fields); call_super_virtual(dispose, self); } DocumentDirtyState * DocumentDirtyState_copy(DocumentDirtyState * self) { DocumentDirtyState * copy = DocumentDirtyState_create(self->fieldCount); DocumentDirtyState_copyStateFrom(copy, self); return copy; } void DocumentDirtyState_copyStateFrom(DocumentDirtyState * to, DocumentDirtyState * from) { assert(to->fieldCount == from->fieldCount); for (unsigned int fieldIndex = 0; fieldIndex < to->fieldCount; fieldIndex++) { to->fields[fieldIndex] = from->fields[fieldIndex]; } to->masterValue = from->masterValue; } bool DocumentDirtyState_isEqual(DocumentDirtyState * self, DocumentDirtyState * compare) { assert(self->fieldCount == compare->fieldCount); if (self->masterValue != compare->masterValue) { return false; } for (unsigned int fieldIndex = 0; fieldIndex < self->fieldCount; fieldIndex++) { if (self->fields[fieldIndex] != compare->fields[fieldIndex]) { return false; } } return true; } DocumentDirtyStateBits DocumentDirtyState_getChangedBits(DocumentDirtyState * self, DocumentDirtyState * compare) { assert(self->fieldCount == compare->fieldCount); DocumentDirtyStateBits changedBits = 0; for (unsigned int fieldIndex = 0; fieldIndex < self->fieldCount; fieldIndex++) { if (self->fields[fieldIndex] != compare->fields[fieldIndex]) { changedBits |= 1 << fieldIndex; } } return changedBits; } void DocumentDirtyState_invalidate(DocumentDirtyState * self) { for (unsigned int fieldIndex = 0; fieldIndex < self->fieldCount; fieldIndex++) { self->fields[fieldIndex] = UINT_MAX; } self->masterValue = UINT_MAX; } void DocumentDirtyState_advance(DocumentDirtyState * self, DocumentDirtyStateBits fieldBits) { for (unsigned int fieldIndex = 0; fieldIndex < self->fieldCount; fieldIndex++) { if (fieldBits & 1 << fieldIndex) { self->fields[fieldIndex]++; } } self->masterValue++; }