Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Structural equality tests. #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion INSTALL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Alternative install with conda (for developing)
# Install the xnd package into a local directory.

# Create and activate a new conda environment:
conda create --name xnd python=3.7
conda create --name xnd python=3.7 make conda-build
conda activate xnd

# Use conda to install ndtypes (if you also want to work on ndtypes, please
Expand Down
12 changes: 10 additions & 2 deletions libxnd/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ GM_CUDA_CXXFLAGS = $(strip $(CONFIGURE_CUDA_CXXFLAGS) $(CUDA_CXXFLAGS))
default: $(LIBSTATIC) $(LIBSHARED)


OBJS = bitmaps.o bounds.o copy.o equal.o shape.o split.o xnd.o
OBJS = bitmaps.o bounds.o copy.o equal.o identical.o shape.o split.o xnd.o

SHARED_OBJS = .objs/bitmaps.o .objs/bounds.o .objs/copy.o .objs/equal.o .objs/shape.o .objs/split.o .objs/xnd.o
SHARED_OBJS = .objs/bitmaps.o .objs/bounds.o .objs/copy.o .objs/equal.o .objs/identical.o .objs/shape.o .objs/split.o .objs/xnd.o

ifdef CUDA_CXX
OBJS += cuda_memory.o
Expand Down Expand Up @@ -87,6 +87,14 @@ Makefile equal.c xnd.h
Makefile equal.c xnd.h
$(CC) $(XND_CFLAGS_SHARED) -c equal.c -o .objs/equal.o

identical.o:\
Makefile identical.c xnd.h
$(CC) $(XND_CFLAGS) -c identical.c

.objs/identical.o:\
Makefile identical.c xnd.h
$(CC) $(XND_CFLAGS_SHARED) -c identical.c -o .objs/identical.o

shape.o:\
Makefile shape.c overflow.h xnd.h
$(CC) $(XND_CFLAGS) -c shape.c
Expand Down
115 changes: 115 additions & 0 deletions libxnd/bitmaps.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,115 @@ bitmap_init(xnd_bitmap_t *b, const ndt_t *t, int64_t nitems, ndt_context_t *ctx)
}
}

/*****************************************************************************/
/* Structural identity */
/*****************************************************************************/

static int
bitmap_identical(const xnd_bitmap_t *xb, const xnd_bitmap_t *yb,
const ndt_t *t, int64_t nitems,
ndt_context_t *ctx) {

int64_t shape, i, k;
int64_t n;

assert(ndt_is_concrete(t));

if (ndt_is_optional(t) && memcmp(xb->data, yb->data, (nitems + 7) / 8)) {
return 0;
}

if (!ndt_subtree_is_optional(t)) {
return 1;
}

switch (t->tag) {
case FixedDim: {
shape = t->FixedDim.shape;
return bitmap_identical(xb, yb, t->FixedDim.type, nitems * shape, ctx);
}

case VarDim: {
assert(nitems == 1);
n = nitems;
if (t->ndim == 1) {
int32_t noffsets = t->Concrete.VarDim.offsets->n;
n = t->Concrete.VarDim.offsets->v[noffsets - 1];
}
return bitmap_identical(xb, yb, t->VarDim.type, n, ctx);
}

case Tuple: {
shape = t->Tuple.shape;

for (i = 0; i < nitems; i++) {
for (k = 0; k < shape; k++) {
if (!bitmap_identical(xb->next + i * shape + k,
yb->next + i * shape + k, t->Tuple.types[k], 1,
ctx))
return 0;
}
}

return 1;
}

case Record: {
shape = t->Record.shape;

for (i = 0; i < nitems; i++) {
for (k = 0; k < shape; k++) {
if (!bitmap_identical(xb->next + i * shape + k,
yb->next + i * shape + k, t->Record.types[k], 1,
ctx)) {
return 0;
}
}
}

return 1;
}

case Ref: {

for (i = 0; i < nitems; i++) {
if (!bitmap_identical(xb->next + i, yb->next + i, t->Ref.type, 1, ctx)) {
return 0;
}
}

return 1;
}

case Constr: {

for (i = 0; i < nitems; i++) {
if (!bitmap_identical(xb->next + i, yb->next + i, t->Constr.type, 1,
ctx)) {
return 0;
}
}

return 1;
}

case Nominal: {

for (i = 0; i < nitems; i++) {
if (!bitmap_identical(xb->next + i, yb->next + i, t->Nominal.type, 1,
ctx)) {
return 0;
}
}

return 1;
}

default:
return 1;
}
}

int
xnd_bitmap_init(xnd_bitmap_t *b, const ndt_t *t, ndt_context_t *ctx)
{
Expand Down Expand Up @@ -349,3 +458,9 @@ xnd_is_na(const xnd_t *x)

return !_xnd_is_valid(x);
}

int
xnd_bitmap_identical(const xnd_bitmap_t *xb, const xnd_bitmap_t *yb,
const ndt_t *t, ndt_context_t *ctx) {
return bitmap_identical(xb, yb, t, 1, ctx);
}
213 changes: 213 additions & 0 deletions libxnd/identical.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2017-2018, plures
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "ndtypes.h"
#include "xnd.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>

/*****************************************************************************/
/* Structural identity */
/*****************************************************************************/

static int identical_same_type_and_bitmap(const xnd_t *x, const xnd_t *y,
ndt_context_t *ctx);

static int identical_with_pointers(const xnd_t *x, const xnd_t *y,
ndt_context_t *ctx) {
const ndt_t *const t = x->type;
int n;
int64_t i;

switch (t->tag) {

case Ref: {
const xnd_t xnext = xnd_ref_next(x, ctx);
if (xnext.ptr == NULL) {
return -1;
}
const xnd_t ynext = xnd_ref_next(y, ctx);
if (ynext.ptr == NULL) {
return -1;
}
return identical_same_type_and_bitmap(&xnext, &ynext, ctx);
}

case Bytes:
if (XND_BYTES_SIZE(x->ptr) != XND_BYTES_SIZE(y->ptr)) {
return 0;
}
return memcmp(XND_BYTES_DATA(x->ptr), XND_BYTES_DATA(y->ptr),
XND_BYTES_SIZE(x->ptr)) == 0;

case String:
return strcmp(XND_POINTER_DATA(x->ptr), XND_POINTER_DATA(y->ptr)) == 0;

case FixedDim: {
for (i = 0; i < t->FixedDim.shape; i++) {
const xnd_t xnext = xnd_fixed_dim_next(x, i);
const xnd_t ynext = xnd_fixed_dim_next(y, i);
n = identical_same_type_and_bitmap(&xnext, &ynext, ctx);
if (n <= 0)
return n;
}
return 1;
}

case VarDim: {
int64_t xstart, xstep, xshape;
int64_t ystart, ystep, yshape;
int64_t i;
xshape = ndt_var_indices(&xstart, &xstep, t, x->index, ctx);
if (xshape < 0) {
return -1;
}
yshape = ndt_var_indices(&ystart, &ystep, t, y->index, ctx);
if (yshape < 0) {
return -1;
}
if (yshape != xshape) {
return 0;
}
for (i = 0; i < xshape; i++) {
const xnd_t xnext = xnd_var_dim_next(x, xstart, xstep, i);
const xnd_t ynext = xnd_var_dim_next(y, ystart, ystep, i);
n = identical_same_type_and_bitmap(&xnext, &ynext, ctx);
if (n <= 0)
return n;
}
return 1;
}

case Tuple: {
for (i = 0; i < t->Tuple.shape; i++) {
const xnd_t xnext = xnd_tuple_next(x, i, ctx);
if (xnext.ptr == NULL) {
return -1;
}
const xnd_t ynext = xnd_tuple_next(y, i, ctx);
if (ynext.ptr == NULL) {
return -1;
}
n = identical_same_type_and_bitmap(&xnext, &ynext, ctx);
if (n <= 0)
return n;
}
return 1;
}

case Record: {
for (i = 0; i < t->Record.shape; i++) {
const xnd_t xnext = xnd_record_next(x, i, ctx);
if (xnext.ptr == NULL) {
return -1;
}
const xnd_t ynext = xnd_record_next(y, i, ctx);
if (ynext.ptr == NULL) {
return -1;
}
n = identical_same_type_and_bitmap(&xnext, &ynext, ctx);
if (n <= 0)
return n;
}
return 1;
}

case Constr: {
const xnd_t xnext = xnd_constr_next(x, ctx);
if (xnext.ptr == NULL) {
return -1;
}
const xnd_t ynext = xnd_constr_next(y, ctx);
if (ynext.ptr == NULL) {
return -1;
}
return identical_same_type_and_bitmap(&xnext, &ynext, ctx);
}

default:
/* NOT REACHED */
ndt_err_format(ctx, NDT_NotImplementedError, "type tag");
return -1;
}

/* NOT REACHED: tags should be exhaustive */
ndt_err_format(ctx, NDT_RuntimeError, "invalid type tag");
return -1;
}

static int identical_same_type_and_bitmap(const xnd_t *x, const xnd_t *y,
ndt_context_t *ctx) {
const ndt_t *const t = x->type;
// check for identical index state
if (x->index != y->index) {
return 0;
}
// check pointer free instances
if (ndt_is_pointer_free(t)) {
return memcmp(x->ptr, y->ptr, t->datasize) == 0;
}
// xnd instance contains Ref, Bytes, or String items that hold
// pointers
return identical_with_pointers(x, y, ctx);
}

int xnd_identical(const xnd_t *x, const xnd_t *y, ndt_context_t *ctx) {
const ndt_t *const t = x->type;
const ndt_t *const u = y->type;
int n;
assert(ndt_is_concrete(t) && ndt_is_concrete(u));
// some quick checks
if (x == y) {
return 1;
}
if (t->datasize != u->datasize) {
return 0;
}
// check for identical types
n = ndt_equal(t, u);
if (n <= 0) {
return n;
}
// check for empty objects
if (t->datasize == 0) {
return 1;
}
// check for identical bitmaps
n = xnd_bitmap_identical(&x->bitmap, &y->bitmap, t, ctx);
if (n <= 0) {
return n;
}
return identical_same_type_and_bitmap(x, y, ctx);
}
3 changes: 3 additions & 0 deletions libxnd/xnd.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ XND_API xnd_t *xnd_split(const xnd_t *x, int64_t *n, int max_outer, ndt_context_

XND_API int xnd_equal(const xnd_t *x, const xnd_t *y, ndt_context_t *ctx);
XND_API int xnd_strict_equal(const xnd_t *x, const xnd_t *y, ndt_context_t *ctx);
XND_API int xnd_identical(const xnd_t *x, const xnd_t *y, ndt_context_t *ctx);

XND_API int xnd_copy(xnd_t *y, const xnd_t *x, uint32_t flags, ndt_context_t *ctx);

Expand All @@ -213,6 +214,8 @@ XND_API void xnd_set_valid(xnd_t *x);
XND_API void xnd_set_na(xnd_t *x);
XND_API int xnd_is_valid(const xnd_t *x);
XND_API int xnd_is_na(const xnd_t *x);
XND_API int xnd_bitmap_identical(const xnd_bitmap_t *xb, const xnd_bitmap_t *yb,
const ndt_t *t, ndt_context_t *ctx);


/*****************************************************************************/
Expand Down
Loading