File: GB_extractTuples_prep.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (82 lines) | stat: -rw-r--r-- 3,136 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//------------------------------------------------------------------------------
// GB_extractTuples_prep: prepare an output GrB_Vector for extractTuples
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

// GB_extractTuples_prep ensures that an output GrB_Vector for I, J, or X is
// dense, of size nvals-by-1, and the right type.

#include "GB.h"
#include "extractTuples/GB_extractTuples.h"
#include "container/GB_container.h"

GrB_Info GB_extractTuples_prep
(
    GrB_Vector V,               // an output vector for I, J, or X
    uint64_t nvals,             // # of values V must hold
    const GrB_Type vtype        // desired type of V
)
{ 

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    if (V == NULL)
    { 
        // nothing to do; this component is not requested
        return (GrB_SUCCESS) ;
    }

    //--------------------------------------------------------------------------
    // quick return if V already has the right properties
    //--------------------------------------------------------------------------

    uint64_t required_size = nvals * vtype->size ;
    if (GB_IS_FULL (V) && V->nvals == nvals && V->vlen == nvals &&
        V->type == vtype && !(V->x_shallow) && V->x_size >= required_size)
    { 
        // nothing to do; the vector is already in the right format
        return (GrB_SUCCESS) ;
    }

    //--------------------------------------------------------------------------
    // remove V->x and free all other content
    //--------------------------------------------------------------------------

    void *Vx = V->x_shallow ? NULL : V->x ;
    size_t Vx_size = V->x_shallow ? 0 : V->x_size ;
    V->x = NULL ;
    GB_phybix_free ((GrB_Matrix) V) ;

    //--------------------------------------------------------------------------
    // ensure Vx is large enough
    //--------------------------------------------------------------------------

    if (required_size > Vx_size)
    { 
        // If Vx is not large enough, reallocate it.  If Vx was initially not
        // empty, it means the space is growing incrementally, so add 25% extra
        // space for future growth.
        GB_FREE_MEMORY (&Vx, Vx_size) ;
        int64_t n = nvals + ((Vx_size == 0) ? 0 : (nvals / 4)) ;
        Vx = GB_MALLOC_MEMORY (n, vtype->size, &Vx_size) ;
        if (Vx == NULL)
        { 
            return (GrB_OUT_OF_MEMORY) ;
        }
    }

    //--------------------------------------------------------------------------
    // load Vx back, to create V as a dense nvals-by-1 vector of type vtype
    //--------------------------------------------------------------------------

    GB_vector_load (V, &Vx, vtype, nvals, Vx_size, false) ;
    ASSERT_VECTOR_OK (V, "V prepped", GB0) ;
    return (GrB_SUCCESS) ;
}