File: GB_Iterator_rc_seek.c

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (129 lines) | stat: -rw-r--r-- 4,473 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//------------------------------------------------------------------------------
// GB_Iterator_rc_seek: seek a row/col iterator to A(:,j) or to jth vector of A
//------------------------------------------------------------------------------

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

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

// Seek a row iterator to A(j,:), a col iterator to A(:,j).  If jth_vector is
// true, seek to the jth vector instead.  For sparse, bitmap, or full matrices,
// this is the same as A(j,:) for a row iterator or A(:,j) for a col iterator.
// The jth_vector parameter only affects how hypersparse matrices are
// traversed.

#include "GB.h"

GrB_Info GB_Iterator_rc_seek
(
    GxB_Iterator iterator,
    GrB_Index j,
    bool jth_vector
)
{

    //--------------------------------------------------------------------------
    // check if the iterator is exhausted
    //--------------------------------------------------------------------------

    if (j >= ((jth_vector) ? iterator->anvec : iterator->avdim))
    { 
        iterator->pstart = 0 ;
        iterator->pend = 0 ;
        iterator->p = 0 ;
        iterator->k = iterator->anvec ;
        return (GxB_EXHAUSTED) ;
    }

    //--------------------------------------------------------------------------
    // attach the iterator to A(:,j)
    //--------------------------------------------------------------------------

    switch (iterator->A_sparsity)
    {
        default: 
        case GxB_SPARSE : 
        { 
            // attach to A(:,j), which is also the jth vector of A
            iterator->pstart = iterator->Ap [j] ;
            iterator->pend = iterator->Ap [j+1] ;
            iterator->p = iterator->pstart ;
            iterator->k = j ;
        }
        break ;

        case GxB_HYPERSPARSE : 
        {
            int64_t k ;
            if (jth_vector)
            { 
                // attach to the jth vector of A; this is much faster than
                // searching Ah for the value j, to attach to A(:,j)
                k = j ;
                iterator->pstart = iterator->Ap [k] ;
                iterator->pend = iterator->Ap [k+1] ;
                iterator->p = iterator->pstart ;
                iterator->k = k ;
            }
            else
            {
                // find k so that j = Ah [k], or if not found, return k as the
                // smallest value so that j < Ah [k]. 
                k = 0 ;
                const int64_t *restrict Ah = iterator->Ah ;
                if (j > 0)
                { 
                    bool found ;
                    int64_t pright = iterator->anvec-1 ;
                    GB_SPLIT_BINARY_SEARCH (j, Ah, k, pright, found) ;
                }
            }
            // If j is found, A(:,j) is the kth vector in the Ah hyperlist.
            // If j is not found, the iterator is placed at the first vector
            // after j in the hyperlist, if this vector exists.
            if (k >= iterator->anvec)
            { 
                // the kth vector does not exist
                iterator->pstart = 0 ;
                iterator->pend = 0 ;
                iterator->p = 0 ;
                iterator->k = iterator->anvec ;
                return (GxB_EXHAUSTED) ;
            }
            else
            { 
                // the kth vector exists
                iterator->pstart = iterator->Ap [k] ;
                iterator->pend = iterator->Ap [k+1] ;
                iterator->p = iterator->pstart ;
                iterator->k = k ;
            }
        }
        break ;

        case GxB_BITMAP : 
        { 
            // attach to A(:,j), which is also the jth vector of A
            iterator->pstart = j * iterator->avlen ;
            iterator->pend = (j+1) * iterator->avlen ;
            iterator->p = iterator->pstart ;
            iterator->k = j ;
            return (GB_Iterator_rc_bitmap_next (iterator)) ;
        }
        break ;

        case GxB_FULL : 
        { 
            // attach to A(:,j), which is also the jth vector of A
            iterator->pstart = j * iterator->avlen ;
            iterator->pend = (j+1) * iterator->avlen ;
            iterator->p = iterator->pstart ;
            iterator->k = j ;
        }
        break ;
    }

    return ((iterator->p >= iterator->pend) ? GrB_NO_VALUE : GrB_SUCCESS) ;
}