LCOV - code coverage report
Current view: top level - src/utility - LAGraph_Vector_IsEqualOp.c (source / functions) Hit Total Coverage
Test: LAGraph code coverage report. Commit id: cc56ed4. Current time (UTC): 2024-08-30T17:14:30Z Lines: 27 27 100.0 %
Date: 2024-08-30 17:16:41 Functions: 1 1 100.0 %

          Line data    Source code
       1             : //------------------------------------------------------------------------------
       2             : // LAGraph_Vector_IsEqualOp: compare two vectors with a given op
       3             : //------------------------------------------------------------------------------
       4             : 
       5             : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
       6             : // SPDX-License-Identifier: BSD-2-Clause
       7             : //
       8             : // For additional details (including references to third party source code and
       9             : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
      10             : // Contributors.txt for a full list of contributors. Created, in part, with
      11             : // funding and support from the U.S. Government (see Acknowledgments.txt file).
      12             : // DM22-0790
      13             : 
      14             : // Contributed by Timothy A. Davis, Texas A&M University
      15             : 
      16             : //------------------------------------------------------------------------------
      17             : 
      18             : // LAGraph_Vector_IsEqualOp: checks if two vectors are equal (same size,
      19             : // structure, size, and values), using a provided binary operator.
      20             : 
      21             : // See also LAGraph_Matrix_IsEqualOp.
      22             : 
      23             : #define LG_FREE_WORK GrB_free (&C) ;
      24             : 
      25             : #include "LG_internal.h"
      26             : 
      27             : //------------------------------------------------------------------------------
      28             : // LAGraph_Vector_IsEqualOp:  compare two vectors using a given operator
      29             : //------------------------------------------------------------------------------
      30             : 
      31         275 : int LAGraph_Vector_IsEqualOp
      32             : (
      33             :     // output:
      34             :     bool *result,           // true if A == B, false if A != B or error
      35             :     // input:
      36             :     const GrB_Vector A,
      37             :     const GrB_Vector B,
      38             :     const GrB_BinaryOp op,        // comparator to use
      39             :     char *msg
      40             : )
      41             : {
      42             : 
      43             :     //--------------------------------------------------------------------------
      44             :     // check inputs
      45             :     //--------------------------------------------------------------------------
      46             : 
      47         275 :     LG_CLEAR_MSG ;
      48         275 :     GrB_Vector C = NULL ;
      49         275 :     LG_ASSERT (op != NULL && result != NULL, GrB_NULL_POINTER) ;
      50             : 
      51             :     //--------------------------------------------------------------------------
      52             :     // check for NULL and aliased vectors
      53             :     //--------------------------------------------------------------------------
      54             : 
      55         274 :     if (A == NULL || B == NULL || A == B)
      56             :     {
      57             :         // two NULL vectors are identical, as are two aliased matrices
      58           1 :         (*result) = (A == B) ;
      59           1 :         return (GrB_SUCCESS) ;
      60             :     }
      61             : 
      62             :     //--------------------------------------------------------------------------
      63             :     // compare the size of A and B
      64             :     //--------------------------------------------------------------------------
      65             : 
      66             :     GrB_Index nrows1, nrows2;
      67         273 :     GRB_TRY (GrB_Vector_size (&nrows1, A)) ;
      68         273 :     GRB_TRY (GrB_Vector_size (&nrows2, B)) ;
      69         273 :     if (nrows1 != nrows2)
      70             :     {
      71             :         // # of rows differ
      72           1 :         (*result) = false ;
      73           1 :         return (GrB_SUCCESS) ;
      74             :     }
      75             : 
      76             :     //--------------------------------------------------------------------------
      77             :     // compare the # entries in A and B
      78             :     //--------------------------------------------------------------------------
      79             : 
      80             :     GrB_Index nvals1, nvals2 ;
      81         272 :     GRB_TRY (GrB_Vector_nvals (&nvals1, A)) ;
      82         272 :     GRB_TRY (GrB_Vector_nvals (&nvals2, B)) ;
      83         272 :     if (nvals1 != nvals2)
      84             :     {
      85             :         // # of entries differ
      86          57 :         (*result) = false ;
      87          57 :         return (GrB_SUCCESS) ;
      88             :     }
      89             : 
      90             :     //--------------------------------------------------------------------------
      91             :     // C = A .* B, where the structure of C is the intersection of A and B
      92             :     //--------------------------------------------------------------------------
      93             : 
      94         215 :     GRB_TRY (GrB_Vector_new (&C, GrB_BOOL, nrows1)) ;
      95         215 :     GRB_TRY (GrB_eWiseMult (C, NULL, NULL, op, A, B, NULL)) ;
      96             : 
      97             :     //--------------------------------------------------------------------------
      98             :     // ensure C has the same number of entries as A and B
      99             :     //--------------------------------------------------------------------------
     100             : 
     101             :     GrB_Index nvals ;
     102         215 :     GRB_TRY (GrB_Vector_nvals (&nvals, C)) ;
     103         215 :     if (nvals != nvals1)
     104             :     {
     105             :         // structure of A and B are different
     106           1 :         LG_FREE_WORK ;
     107           1 :         (*result) = false ;
     108           1 :         return (GrB_SUCCESS) ;
     109             :     }
     110             : 
     111             :     //--------------------------------------------------------------------------
     112             :     // result = and (C)
     113             :     //--------------------------------------------------------------------------
     114             : 
     115         214 :     GRB_TRY (GrB_reduce (result, NULL, GrB_LAND_MONOID_BOOL, C, NULL)) ;
     116             : 
     117             :     //--------------------------------------------------------------------------
     118             :     // free workspace and return result
     119             :     //--------------------------------------------------------------------------
     120             : 
     121         214 :     LG_FREE_WORK ;
     122         214 :     return (GrB_SUCCESS) ;
     123             : }

Generated by: LCOV version 1.14