--- deliantra/server/common/object.C 2008/05/03 09:04:17 1.227 +++ deliantra/server/common/object.C 2008/05/03 11:14:50 1.228 @@ -141,33 +141,18 @@ static bool compare_ob_value_lists_one (const object *wants, const object *has) { - key_value *wants_field; - - /* n-squared behaviour (see get_ob_key_link()), but I'm hoping both + /* n-squared behaviour (see kv_get), but I'm hoping both * objects with lists are rare, and lists stay short. If not, use a * different structure or at least keep the lists sorted... */ /* For each field in wants, */ - for (wants_field = wants->key_values; wants_field; wants_field = wants_field->next) - { - key_value *has_field; - - /* Look for a field in has with the same key. */ - has_field = get_ob_key_link (has, wants_field->key); - - if (!has_field) - return 0; /* No field with that name. */ - - /* Found the matching field. */ - if (has_field->value != wants_field->value) - return 0; /* Values don't match, so this half of the comparison is false. */ - - /* If we get here, we found a match. Now for the next field in wants. */ - } + for (key_value *kv = wants->key_values; kv; kv = kv->next) + if (has->kv_get (kv->key) != kv->value) + return false; /* If we get here, every field in wants has a matching field in has. */ - return 1; + return true; } /* Returns TRUE if ob1 has the same key_values as ob2. */ @@ -177,7 +162,8 @@ /* However, there may be fields in has which aren't partnered in wants, * so we need to run the comparison *twice*. :( */ - return compare_ob_value_lists_one (ob1, ob2) && compare_ob_value_lists_one (ob2, ob1); + return compare_ob_value_lists_one (ob1, ob2) + && compare_ob_value_lists_one (ob2, ob1); } /* Function examines the 2 objects given to it, and returns true if @@ -2376,143 +2362,46 @@ return 0; } -/* If ob has a field named key, return the link from the list, - * otherwise return NULL. - * - * key must be a passed in shared string - otherwise, this won't - * do the desired thing. - */ -key_value * -get_ob_key_link (const object *ob, const char *key) -{ - for (key_value *link = ob->key_values; link; link = link->next) - if (link->key == key) - return link; - - return 0; -} - -/* - * Returns the value of op has an extra_field for key, or NULL. - * - * The argument doesn't need to be a shared string. - * - * The returned string is shared. - */ -const char * -get_ob_key_value (const object *op, const char *const key) +const shstr & +object::kv_get (const shstr &key) const { - key_value *link; - shstr_cmp canonical_key (key); + for (key_value *kv = key_values; kv; kv = kv->next) + if (kv->key == key) + return kv->value; - if (!canonical_key) - { - /* 1. There being a field named key on any object - * implies there'd be a shared string to find. - * 2. Since there isn't, no object has this field. - * 3. Therefore, *this* object doesn't have this field. - */ - return 0; - } - - /* This is copied from get_ob_key_link() above - - * only 4 lines, and saves the function call overhead. - */ - for (link = op->key_values; link; link = link->next) - if (link->key == canonical_key) - return link->value; - - return 0; + return shstr_null; } -/* - * Updates the canonical_key in op to value. - * - * canonical_key is a shared string (value doesn't have to be). - * - * Unless add_key is TRUE, it won't add fields, only change the value of existing - * keys. - * - * Returns TRUE on success. - */ -int -set_ob_key_value_s (object *op, const shstr & canonical_key, const char *value, int add_key) +void +object::kv_set (const shstr &key, const shstr &value) { - key_value *field = NULL, *last = NULL; - - for (field = op->key_values; field != NULL; field = field->next) - { - if (field->key != canonical_key) - { - last = field; - continue; - } - - if (value) - field->value = value; - else - { - /* Basically, if the archetype has this key set, - * we need to store the null value so when we save - * it, we save the empty value so that when we load, - * we get this value back again. - */ - if (get_ob_key_link (op->arch, canonical_key)) - field->value = 0; - else - { - if (last) - last->next = field->next; - else - op->key_values = field->next; - - delete field; - } - } - return TRUE; - } - /* IF we get here, key doesn't exist */ - - /* No field, we'll have to add it. */ - - if (!add_key) - return FALSE; - - /* There isn't any good reason to store a null - * value in the key/value list. If the archetype has - * this key, then we should also have it, so shouldn't - * be here. If user wants to store empty strings, - * should pass in "" - */ - if (value == NULL) - return TRUE; + for (key_value *kv = key_values; kv; kv = kv->next) + if (kv->key == key) + { + kv->value = value; + return; + } - field = new key_value; + key_value *kv = new key_value; - field->key = canonical_key; - field->value = value; - /* Usual prepend-addition. */ - field->next = op->key_values; - op->key_values = field; + kv->next = key_values; + kv->key = key; + kv->value = value; - return TRUE; + key_values = kv; } -/* - * Updates the key in op to value. - * - * If add_key is FALSE, this will only update existing keys, - * and not add new ones. - * In general, should be little reason FALSE is ever passed in for add_key - * - * Returns TRUE on success. - */ -int -set_ob_key_value (object *op, const char *key, const char *value, int add_key) +void +object::kv_del (const shstr &key) { - shstr key_ (key); - - return set_ob_key_value_s (op, key_, value, add_key); + for (key_value **kvp = &key_values; *kvp; kvp = &(*kvp)->next) + if ((*kvp)->key == key) + { + key_value *kv = *kvp; + *kvp = (*kvp)->next; + delete kv; + return; + } } object::depth_iterator::depth_iterator (object *container)