1 /*
2    IGraph library.
3    Copyright (C) 2021  The igraph development team <igraph@igraph.org>
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 
19 #include <igraph.h>
20 #include "test_utilities.inc"
21 
fkeep_none(igraph_integer_t row,igraph_integer_t col,igraph_real_t value,void * other)22 igraph_integer_t fkeep_none(igraph_integer_t row, igraph_integer_t col, igraph_real_t value, void *other) {
23     IGRAPH_UNUSED(row);
24     IGRAPH_UNUSED(col);
25     IGRAPH_UNUSED(value);
26     IGRAPH_UNUSED(other);
27     return 0;
28 }
29 
fkeep(igraph_integer_t row,igraph_integer_t col,igraph_real_t value,void * other)30 igraph_integer_t fkeep(igraph_integer_t row, igraph_integer_t col, igraph_real_t value, void *other) {
31     if (row == 0 || col == 1 || value > *(int*)other) {
32         return 0;
33     }
34     return 1;
35 }
36 
main()37 int main() {
38     igraph_sparsemat_t spmat;
39     igraph_sparsemat_t spmat_comp;
40     int a = 0;
41 
42     printf("0x0 matrix.\n");
43     igraph_sparsemat_init(&spmat, 0, 0, /*nzmax*/0);
44     igraph_sparsemat_compress(&spmat, &spmat_comp);
45     IGRAPH_ASSERT(igraph_sparsemat_fkeep(&spmat_comp, &fkeep, &a) == IGRAPH_SUCCESS);
46     igraph_sparsemat_print(&spmat_comp, stdout);
47     igraph_sparsemat_destroy(&spmat);
48     igraph_sparsemat_destroy(&spmat_comp);
49 
50     printf("3x3 matrix.\n");
51     igraph_sparsemat_init(&spmat, 3, 3, /*nzmax*/7);
52     igraph_sparsemat_entry(&spmat, 0, 0, 5);
53     igraph_sparsemat_entry(&spmat, 1, 1, 6);
54     igraph_sparsemat_entry(&spmat, 2, 2, 7);
55     igraph_sparsemat_entry(&spmat, 3, 0, 1);
56     igraph_sparsemat_entry(&spmat, 0, 3, 2);
57     igraph_sparsemat_entry(&spmat, 2, 1, 3);
58     igraph_sparsemat_entry(&spmat, 1, 2, 4);
59     igraph_sparsemat_compress(&spmat, &spmat_comp);
60     a = 6;
61     printf("Remove row 0, column 1, and values above 6:\n");
62     IGRAPH_ASSERT(igraph_sparsemat_fkeep(&spmat_comp, &fkeep, &a) == IGRAPH_SUCCESS);
63     igraph_sparsemat_print(&spmat_comp, stdout);
64     printf("Remove everything:\n");
65     IGRAPH_ASSERT(igraph_sparsemat_fkeep(&spmat_comp, &fkeep_none, &a) == IGRAPH_SUCCESS);
66     igraph_sparsemat_print(&spmat_comp, stdout);
67     igraph_sparsemat_destroy(&spmat);
68     igraph_sparsemat_destroy(&spmat_comp);
69 
70     VERIFY_FINALLY_STACK();
71     igraph_set_error_handler(igraph_error_handler_ignore);
72 
73     printf("uncompressed matrix.\n");
74     igraph_sparsemat_init(&spmat, 0, 0, /*nzmax*/0);
75     IGRAPH_ASSERT(igraph_sparsemat_fkeep(&spmat, &fkeep, &a) == IGRAPH_EINVAL);
76     igraph_sparsemat_destroy(&spmat);
77 
78     VERIFY_FINALLY_STACK();
79     return 0;
80 }
81