1 /* ========================================================================== */
2 /* === Source/Mongoose_QPDelta.cpp ========================================== */
3 /* ========================================================================== */
4 
5 /* -----------------------------------------------------------------------------
6  * Mongoose Graph Partitioning Library  Copyright (C) 2017-2018,
7  * Scott P. Kolodziej, Nuri S. Yeralan, Timothy A. Davis, William W. Hager
8  * Mongoose is licensed under Version 3 of the GNU General Public License.
9  * Mongoose is also available under other licenses; contact authors for details.
10  * -------------------------------------------------------------------------- */
11 
12 #include "Mongoose_QPDelta.hpp"
13 #include "Mongoose_Internal.hpp"
14 
15 namespace Mongoose
16 {
17 
Create(Int numVars)18 QPDelta *QPDelta::Create(Int numVars)
19 {
20     QPDelta *ret = (QPDelta *)SuiteSparse_calloc(1, sizeof(QPDelta));
21     if (!ret)
22         return NULL;
23 
24     ret->x = (double *)SuiteSparse_malloc(static_cast<size_t>(numVars),
25                                           sizeof(double));
26     ret->FreeSet_status
27         = (Int *)SuiteSparse_malloc(static_cast<size_t>(numVars), sizeof(Int));
28     ret->FreeSet_list = (Int *)SuiteSparse_malloc(
29         static_cast<size_t>(numVars + 1), sizeof(Int));
30     ret->gradient = (double *)SuiteSparse_malloc(static_cast<size_t>(numVars),
31                                                  sizeof(double));
32     ret->D        = (double *)SuiteSparse_malloc(static_cast<size_t>(numVars),
33                                           sizeof(double));
34 
35     for (int i = 0; i < WISIZE; i++)
36     {
37         ret->wi[i] = (Int *)SuiteSparse_malloc(static_cast<size_t>(numVars + 1),
38                                                sizeof(Int));
39     }
40 
41     for (Int i = 0; i < WXSIZE; i++)
42     {
43         ret->wx[i] = (double *)SuiteSparse_malloc(static_cast<size_t>(numVars),
44                                                   sizeof(double));
45     }
46 
47 #ifndef NDEBUG
48     ret->check_cost = INFINITY;
49 #endif
50 
51     if (!ret->x || !ret->FreeSet_status || !ret->FreeSet_list || !ret->gradient
52         || !ret->D || !ret->wi[0]
53         || !ret->wi[1]
54         //|| !ret->Change_location
55         || !ret->wx[0] || !ret->wx[1] || !ret->wx[2])
56     {
57         ret->~QPDelta();
58         ret = (QPDelta *)SuiteSparse_free(ret);
59     }
60 
61     return ret;
62 }
63 
~QPDelta()64 QPDelta::~QPDelta()
65 {
66     x              = (double *)SuiteSparse_free(x);
67     FreeSet_status = (Int *)SuiteSparse_free(FreeSet_status);
68     FreeSet_list   = (Int *)SuiteSparse_free(FreeSet_list);
69     gradient       = (double *)SuiteSparse_free(gradient);
70     D              = (double *)SuiteSparse_free(D);
71     // Change_location = (Int*) SuiteSparse_free(Change_location);
72 
73     for (Int i = 0; i < WISIZE; i++)
74     {
75         wi[i] = (Int *)SuiteSparse_free(wi[i]);
76     }
77 
78     for (Int i = 0; i < WXSIZE; i++)
79     {
80         wx[i] = (double *)SuiteSparse_free(wx[i]);
81     }
82 }
83 
84 } // end namespace Mongoose
85