1 //------------------------------------------------------------------------------
2 // GB_hyper_realloc: reallocate a matrix hyperlist
3 //------------------------------------------------------------------------------
4
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7
8 //------------------------------------------------------------------------------
9
10 // Change the size of the A->h and A->p hyperlist.
11 // No change is made if A is not hypersparse.
12
13 #include "GB.h"
14
GB_hyper_realloc(GrB_Matrix A,int64_t plen_new,GB_Context Context)15 GrB_Info GB_hyper_realloc
16 (
17 GrB_Matrix A, // matrix with hyperlist to reallocate
18 int64_t plen_new, // new size of A->p and A->h
19 GB_Context Context
20 )
21 {
22
23 //--------------------------------------------------------------------------
24 // check inputs
25 //--------------------------------------------------------------------------
26
27 ASSERT (A != NULL) ;
28 ASSERT (GB_ZOMBIES_OK (A)) ; // pattern not accessed
29 ASSERT (GB_JUMBLED_OK (A)) ;
30 ASSERT (GB_PENDING_OK (A)) ;
31
32 //--------------------------------------------------------------------------
33 // reallocate the hyperlist
34 //--------------------------------------------------------------------------
35
36 if (GB_IS_HYPERSPARSE (A))
37 {
38 ASSERT (!A->p_shallow) ;
39 ASSERT (!A->h_shallow) ;
40
41 // old size of A->p and A->h
42 int64_t plen_old = A->plen ;
43
44 // change the size of A->h and A->p
45 bool ok1 = true, ok2 = true ;
46 GB_REALLOC (A->p, plen_new+1, plen_old+1, int64_t, &(A->p_size), &ok1,
47 Context) ;
48 GB_REALLOC (A->h, plen_new, plen_old, int64_t, &(A->h_size), &ok2,
49 Context) ;
50 bool ok = ok1 && ok2 ;
51
52 // always succeeds if the space shrinks
53 ASSERT (GB_IMPLIES (plen_new <= plen_old, ok)) ;
54
55 if (!ok)
56 {
57 // out of memory
58 GB_phbix_free (A) ;
59 return (GrB_OUT_OF_MEMORY) ;
60 }
61
62 // size of A->p and A->h has been changed
63 A->plen = plen_new ;
64 }
65
66 //--------------------------------------------------------------------------
67 // return result
68 //--------------------------------------------------------------------------
69
70 return (GrB_SUCCESS) ;
71 }
72
73