1 /* Copyright (c) 1997-2021
2 Ewgenij Gawrilow, Michael Joswig, and the polymake team
3 Technische Universität Berlin, Germany
4 https://polymake.org
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version: http://www.gnu.org/licenses/gpl.txt.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 --------------------------------------------------------------------------------
16 */
17
18 #include "polymake/topaz/merge_vertices.h"
19
20 namespace polymake { namespace topaz {
21
merge_vertices(Array<std::string> & L1,const Array<std::string> & L2)22 hash_map<Int, Int> merge_vertices(Array<std::string>& L1, const Array<std::string>& L2)
23 {
24 const Int n_vert1 = L1.size();
25 const Int n_vert2 = L2.size();
26 hash_map<Int, Int> M(n_vert2);
27
28 // compute vertex maps
29 hash_map<std::string, Int> map(n_vert1);
30 Int count = 0;
31
32 for (auto l=entire(L1); !l.at_end(); ++l, ++count)
33 map[*l] = count;
34
35 L1.resize(n_vert1 + n_vert2);
36 Int diff = n_vert1;
37 count = 0;
38 for (auto l=entire(L2); !l.at_end(); ++l, ++count)
39 if (map.find(*l) != map.end()) { // label equal to *l found in L1
40 M[count] = map[*l];
41 --diff;
42 } else {
43 M[count] = diff + count;
44 L1[diff + count] = *l;
45 }
46 L1.resize(diff + count);
47
48 return M;
49 }
50
merge_disjoint_vertices(Array<std::string> & L1,const Array<std::string> & L2)51 void merge_disjoint_vertices(Array<std::string>& L1, const Array<std::string>& L2)
52 {
53 const Int n_vert1 = L1.size();
54 const Int n_vert2 = L2.size();
55
56 // adjust labels
57 L1.resize(n_vert1 + n_vert2);
58
59 for (Int v = 0; v < n_vert1; ++v)
60 L1[v] = L1[v] + "_1";
61
62 for (Int v = 0; v < n_vert2; ++v)
63 L1[n_vert1 + v] = L2[v]+"_2";
64 }
65
66 } }
67
68 // Local Variables:
69 // mode:C++
70 // c-basic-offset:3
71 // indent-tabs-mode:nil
72 // End:
73