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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
//------------------------------------------------------------------------------
// GB_ijlength: get the length and kind of an index list I
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// Determine the length of I, and process the colon notation I = begin:inc:end.
// No error checking is done.
#include "GB_ij.h"
// ensure an unsigned integer does not cause signed integer overflow
#define GB_LIMIT(u) (int64_t) (GB_IMIN (u, INT64_MAX))
void GB_ijlength // get the length and kind of an index list I
(
const GrB_Index *I, // list of indices (actual or implicit)
const int64_t ni, // length I, or special
const int64_t limit, // indices must be in the range 0 to limit-1
int64_t *nI, // actual length of I
int *Ikind, // kind of I: GB_ALL, GB_RANGE, GB_STRIDE, GB_LIST
int64_t Icolon [3] // begin:inc:end for all but GB_LIST
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
ASSERT (I != NULL) ;
ASSERT (limit >= 0) ;
ASSERT (limit <= GB_NMAX) ;
//--------------------------------------------------------------------------
// determine the length of I
//--------------------------------------------------------------------------
if (I == GrB_ALL)
{
//----------------------------------------------------------------------
// I = ":" = 0:limit-1
//----------------------------------------------------------------------
(*Ikind) = GB_ALL ;
Icolon [GxB_BEGIN] = 0 ;
Icolon [GxB_INC ] = 1 ;
Icolon [GxB_END ] = limit-1 ;
(*nI) = limit ;
}
else if (ni == GxB_RANGE)
{
//----------------------------------------------------------------------
// I = ibegin:iend
//----------------------------------------------------------------------
(*Ikind) = GB_RANGE ;
// the array I must have size at least 2
int64_t ibegin = GB_LIMIT (I [GxB_BEGIN]) ;
int64_t iend = GB_LIMIT (I [GxB_END ]) ;
ASSERT (ibegin >= 0) ;
if (ibegin == 0 && iend == limit-1)
{
// 0:limit-1 is the same as ":"
(*Ikind) = GB_ALL ;
}
Icolon [GxB_BEGIN] = ibegin ;
Icolon [GxB_INC ] = 1 ;
Icolon [GxB_END ] = iend ;
if (ibegin > iend)
{
// the list is empty
(*nI) = 0 ;
}
else // ibegin <= iend
{
// list ibegin:iend is not empty
(*nI) = (iend - ibegin + 1) ;
}
}
else if (ni == GxB_STRIDE)
{
//----------------------------------------------------------------------
// I = ibegin:iinc:iend where iinc >= 0
//----------------------------------------------------------------------
(*Ikind) = GB_STRIDE ;
// The array I must have size at least 3. It is an unsigned uint64_t
// array, so integers must be positive.
int64_t ibegin = GB_LIMIT (I [GxB_BEGIN]) ;
int64_t iinc = GB_LIMIT (I [GxB_INC ]) ;
int64_t iend = GB_LIMIT (I [GxB_END ]) ;
ASSERT (ibegin >= 0) ;
ASSERT (iinc >= 0) ;
ASSERT (iend >= 0) ;
if (iinc == 1)
{
if (ibegin == 0 && iend == limit-1)
{
// 0:1:limit-1 is the same as ":"
(*Ikind) = GB_ALL ;
}
else
{
// ibegin:1:iend is the same as ibegin:iend
(*Ikind) = GB_RANGE ;
}
}
// an increment of 0 means the list is empty
if (iinc == 0)
{
(*nI) = 0 ;
}
else // iinc > 0
{
if (ibegin > iend)
{
// the list ibegin:iinc:iend is empty (for example 10:1:0)
(*nI) = 0 ;
}
else // ibegin <= iend
{
// the list is non-empty (for example 4:2:7 = [4 6])
(*nI) = ((iend - ibegin) / iinc) + 1 ;
}
}
Icolon [GxB_BEGIN] = ibegin ;
Icolon [GxB_INC ] = iinc ;
Icolon [GxB_END ] = iend ;
}
else if (ni == GxB_BACKWARDS)
{
//----------------------------------------------------------------------
// I = ibegin:iinc:iend where iinc <= 0
//----------------------------------------------------------------------
(*Ikind) = GB_STRIDE ;
// The array I must have size at least 3. It is an unsigned uint64_t
// array, so integers must be positive.
int64_t ibegin = GB_LIMIT (I [GxB_BEGIN]) ;
int64_t iinc = GB_LIMIT (I [GxB_INC ]) ;
int64_t iend = GB_LIMIT (I [GxB_END ]) ;
ASSERT (iinc >= 0) ;
// the stride is backwards, so negate iinc
iinc = -iinc ;
ASSERT (ibegin >= 0) ;
ASSERT (iinc <= 0) ;
ASSERT (iend >= 0) ;
// an increment of 0 means the list is empty
if (iinc == 0)
{
(*nI) = 0 ;
}
else // iinc < 0
{
if (ibegin < iend)
{
// the list ibegin:iinc:iend is empty (for example 1:-1:10)
(*nI) = 0 ;
}
else
{
// the list is non-empty (for example 7:-2:4 = [7 5])
// two positive numbers are divided here
(*nI) = ((ibegin - iend) / (-iinc)) + 1 ;
}
}
Icolon [GxB_BEGIN] = ibegin ;
Icolon [GxB_INC ] = iinc ;
Icolon [GxB_END ] = iend ;
}
else
{
//----------------------------------------------------------------------
// I is an array of indices
//----------------------------------------------------------------------
(*Ikind) = GB_LIST ;
// not computed
Icolon [GxB_BEGIN] = 0 ;
Icolon [GxB_INC ] = 0 ;
Icolon [GxB_END ] = 0 ;
(*nI) = ni ;
}
}
|