core: shortcut equal operator for identical object reference in NMDedupMultiIndex

And get rid of the unused obj_full_equality_allows_different_class.
It's hard to grasp how to implement different object types that can compare
despite having different klasses. The idea was, that stack allocated
objects (used as lookup needles), are some small lightweight objects,
that still compare equal to the full instance. But it's unused. Drop it.
This commit is contained in:
Thomas Haller
2017-07-10 16:36:58 +02:00
parent 930da031b2
commit ad5f5c81ef
3 changed files with 23 additions and 27 deletions

View File

@@ -145,7 +145,6 @@ _dedup_obj_full_equal (const NMDedupMultiObj *obj_a,
static const NMDedupMultiObjClass dedup_obj_class = { static const NMDedupMultiObjClass dedup_obj_class = {
.obj_clone = _dedup_obj_clone, .obj_clone = _dedup_obj_clone,
.obj_destroy = _dedup_obj_destroy, .obj_destroy = _dedup_obj_destroy,
.obj_full_equality_allows_different_class = FALSE,
.obj_full_hash = _dedup_obj_full_hash, .obj_full_hash = _dedup_obj_full_hash,
.obj_full_equal = _dedup_obj_full_equal, .obj_full_equal = _dedup_obj_full_equal,
}; };

View File

@@ -155,6 +155,17 @@ _entry_unpack (const NMDedupMultiEntry *entry,
nm_assert (NM_IN_SET (*out_lookup_head, FALSE, TRUE)); nm_assert (NM_IN_SET (*out_lookup_head, FALSE, TRUE));
ASSERT_idx_type (*out_idx_type); ASSERT_idx_type (*out_idx_type);
/* for lookup of the head, we allow to omit object, but only
* if the idx_type does not parition the objects. Otherwise, we
* require a obj to compare. */
nm_assert ( !*out_lookup_head
|| ( *out_obj
|| !(*out_idx_type)->klass->idx_obj_partition_equal));
/* lookup of the object requires always an object. */
nm_assert ( *out_lookup_head
|| *out_obj);
} }
static guint static guint
@@ -286,6 +297,7 @@ _add (NMDedupMultiIndex *self,
break; break;
}; };
nm_assert (obj->klass == ((const NMDedupMultiObj *) entry->obj)->klass);
if ( obj == entry->obj if ( obj == entry->obj
|| obj->klass->obj_full_equal (obj, || obj->klass->obj_full_equal (obj,
entry->obj)) { entry->obj)) {
@@ -775,24 +787,9 @@ static gboolean
_dict_idx_objs_equal (const NMDedupMultiObj *obj_a, _dict_idx_objs_equal (const NMDedupMultiObj *obj_a,
const NMDedupMultiObj *obj_b) const NMDedupMultiObj *obj_b)
{ {
const NMDedupMultiObjClass *klass; return obj_a == obj_b
|| ( obj_a->klass == obj_b->klass
klass = obj_a->klass; && obj_a->klass->obj_full_equal (obj_a, obj_b));
/* if the class differs, but at least one of them supports calls with
* differing klass, choose it.
*
* Implementing a klass that can compare equality for multiple
* types is hard to get right. E.g. hash(), equal() and get_ref()
* must all agree so that instances of different types look identical. */
if ( klass != obj_b->klass
&& !klass->obj_full_equality_allows_different_class) {
klass = obj_b->klass;
if (!klass->obj_full_equality_allows_different_class)
return FALSE;
}
return klass->obj_full_equal (obj_a, obj_b);
} }
void void

View File

@@ -69,8 +69,6 @@ struct _NMDedupMultiObjClass {
void (*obj_destroy) (NMDedupMultiObj *obj); void (*obj_destroy) (NMDedupMultiObj *obj);
gboolean obj_full_equality_allows_different_class;
/* the NMDedupMultiObj can be deduplicated. For that the obj_full_hash() /* the NMDedupMultiObj can be deduplicated. For that the obj_full_hash()
* and obj_full_equal() compare *all* fields of the object, even minor ones. */ * and obj_full_equal() compare *all* fields of the object, even minor ones. */
guint (*obj_full_hash) (const NMDedupMultiObj *obj); guint (*obj_full_hash) (const NMDedupMultiObj *obj);
@@ -167,9 +165,10 @@ nm_dedup_multi_idx_type_id_equal (const NMDedupMultiIdxType *idx_type,
/* const NMDedupMultiObj * */ gconstpointer obj_b) /* const NMDedupMultiObj * */ gconstpointer obj_b)
{ {
nm_assert (idx_type); nm_assert (idx_type);
return idx_type->klass->idx_obj_id_equal (idx_type, return obj_a == obj_b
obj_a, || idx_type->klass->idx_obj_id_equal (idx_type,
obj_b); obj_a,
obj_b);
} }
static inline gboolean static inline gboolean
@@ -181,9 +180,10 @@ nm_dedup_multi_idx_type_partition_equal (const NMDedupMultiIdxType *idx_type,
if (idx_type->klass->idx_obj_partition_equal) { if (idx_type->klass->idx_obj_partition_equal) {
nm_assert (obj_a); nm_assert (obj_a);
nm_assert (obj_b); nm_assert (obj_b);
return idx_type->klass->idx_obj_partition_equal (idx_type, return obj_a == obj_b
obj_a, || idx_type->klass->idx_obj_partition_equal (idx_type,
obj_b); obj_a,
obj_b);
} }
return TRUE; return TRUE;
} }