'Shared code for __eq__ and __ne__, parameterized on the actual
comparison operator to use.'

def _equality_check(op):

    def method(self, other):
        if isinstance(other, LabelArray):
            self_mv = self.missing_value
            other_mv = other.missing_value
            if (self_mv != other_mv):
                raise MissingValueMismatch(self_mv, other_mv)
            self_categories = self.categories
            other_categories = other.categories
            if (not compare_arrays(self_categories, other_categories)):
                raise CategoryMismatch(self_categories, other_categories)
            return ((op(self.as_int_array(), other.as_int_array()) & self.not_missing()) & other.not_missing())
        elif isinstance(other, ndarray):
            return (op(self.as_string_array(), other) & self.not_missing())
        elif isinstance(other, self.SUPPORTED_SCALAR_TYPES):
            i = self._reverse_categories.get(other, (-1))
            return (op(self.as_int_array(), i) & self.not_missing())
        return op(super(LabelArray, self), other)
    return method
