1 /****************************************************************/
2 /* Parallel Combinatorial BLAS Library (for Graph Computations) */
3 /* version 1.6 -------------------------------------------------*/
4 /* date: 11/15/2016 --------------------------------------------*/
5 /* authors: Ariful Azad, Aydin Buluc, Adam Lugowski ------------*/
6 /****************************************************************/
7 /*
8  Copyright (c) 2010-2016, The Regents of the University of California
9 
10  Permission is hereby granted, free of charge, to any person obtaining a copy
11  of this software and associated documentation files (the "Software"), to deal
12  in the Software without restriction, including without limitation the rights
13  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the Software is
15  furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included in
18  all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  THE SOFTWARE.
27  */
28 
29 
30 #include "csc.h"
31 #include <cassert>
32 
33 namespace combblas {
34 
35 // Constructing empty Csc objects (size = 0) are not allowed.
36 template <class IT, class NT>
Csc(IT size,IT nCol)37 Csc<IT,NT>::Csc (IT size, IT nCol): nz(size),n(nCol)
38 {
39     assert(size != 0 && n != 0);
40     num = new NT[nz];
41     ir = new IT[nz];
42     jc = new IT[n+1];
43 }
44 
45 template <class IT, class NT>
Csc(const Csc<IT,NT> & rhs)46 Csc<IT,NT>::Csc (const Csc<IT,NT> & rhs): n(rhs.n), nz(rhs.nz)
47 {
48     if(nz > 0)
49     {
50         ir	= new IT[nz];
51         num	= new NT[nz];
52         std::copy(rhs.ir, rhs.ir+nz, ir); // copy(first, last, result)
53         std::copy(rhs.num, rhs.num+nz, num);
54     }
55     jc	= new IT[n+1];
56     std::copy(rhs.jc, rhs.jc+n+1, jc);
57 }
58 
59 template <class IT, class NT>
operator =(const Csc<IT,NT> & rhs)60 Csc<IT,NT> & Csc<IT,NT>::operator= (const Csc<IT,NT> & rhs)
61 {
62     if(this != &rhs)
63     {
64         if(nz > 0)	// if the existing object is not empty
65         {
66             // make it empty
67             delete [] num;
68             delete [] ir;
69         }
70         delete [] jc;
71 
72         nz	= rhs.nz;
73         n	= rhs.n;
74         if(nz > 0)	// if the copied object is not empty
75         {
76             ir		= new IT[nz];
77             num	= new NT[nz];
78             std::copy(rhs.ir, rhs.ir+nz, ir);
79             std::copy(rhs.num, rhs.num+nz, num);
80         }
81         jc	= new IT[n+1];
82         std::copy(rhs.jc, rhs.jc+n+1, jc);
83     }
84     return *this;
85 }
86 
87 template <class IT, class NT>
~Csc()88 Csc<IT,NT>::~Csc()
89 {
90     if( nz > 0)
91     {
92         delete [] num;
93         delete [] ir;
94     }
95     delete [] jc;
96 }
97 
98 //! Does not change the dimension
99 template <class IT, class NT>
Resize(IT nsize)100 void Csc<IT,NT>::Resize(IT nsize)
101 {
102     if(nsize == nz)
103     {
104         // No need to do anything!
105         return;
106     }
107     else if(nsize == 0)
108     {
109         delete []  num;
110         delete []  ir;
111         nz = 0;
112         return;
113     }
114 
115     NT * tmpnum = num;
116     IT * tmpir = ir;
117     num	= new NT[nsize];
118     ir	= new IT[nsize];
119 
120     if(nsize > nz)	// Grow it
121     {
122         std::copy(tmpir, tmpir + nz, ir);   //copy all old elements
123         std::copy(tmpnum, tmpnum + nz, num);
124     }
125     else	// Shrink it
126     {
127         std::copy(tmpir, tmpir + nsize, ir);   // copy only a portion of the old elements
128         std::copy(tmpnum, tmpnum + nsize, num);
129     }
130     delete [] tmpnum;		// delete the memory pointed by previous pointers
131     delete [] tmpir;
132     nz = nsize;
133 }
134 
135 }
136