add count(), fix bad llist logic in remove()

git-svn-id: http://skia.googlecode.com/svn/trunk@2958 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
mike@reedtribe.org 2012-01-04 11:37:46 +00:00
parent e4058b402f
commit 0e3c9caae5
2 changed files with 23 additions and 2 deletions

View File

@ -76,6 +76,12 @@ public:
*/
void addBool(const char name[], bool value);
/**
* Return the number of slots/fields in this object. These can be
* iterated using Iter.
*/
int count() const;
/**
* Returns true if a slot matching the name and Type is found.
*/

View File

@ -206,6 +206,14 @@ SkJSON::Object::~Object() {
LEAK_CODE(SkASSERT(gObjectCount > 0); SkDebugf("~object[%d]\n", --gObjectCount);)
}
int SkJSON::Object::count() const {
int n = 0;
for (const Slot* slot = fHead; slot; slot = slot->fNext) {
n += 1;
}
return n;
}
SkJSON::Object::Slot* SkJSON::Object::addSlot(Slot* slot) {
SkASSERT(NULL == slot->fNext);
if (NULL == fHead) {
@ -327,23 +335,30 @@ bool SkJSON::Object::findBool(const char name[], bool* value) const {
}
bool SkJSON::Object::remove(const char name[], Type t) {
SkDEBUGCODE(int count = this->count();)
Slot* prev = NULL;
Slot* slot = fHead;
while (slot) {
Slot* next = slot->fNext;
if (t == slot->type() && !strcmp(slot->name(), name)) {
if (fHead == slot) {
if (prev) {
SkASSERT(fHead != slot);
prev->fNext = next;
} else {
SkASSERT(fHead == slot);
fHead = next;
}
if (fTail == slot) {
fTail = next;
fTail = prev;
}
delete slot;
SkASSERT(count - 1 == this->count());
return true;
}
prev = slot;
slot = next;
}
SkASSERT(count == this->count());
return false;
}