Version: 1.0
merge_set.h
Go to the documentation of this file.
1 // Copyright (c) 2020, the GRAPHGEN contributors, as
2 // shown by the AUTHORS file. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6 
7 #ifndef GRAPHGEN_MERGE_SET_H_
8 #define GRAPHGEN_MERGE_SET_H_
9 
10 #include <set>
11 
12 #include "connectivity_mat.h"
13 
14 struct MergeSet {
15  std::set<std::vector<std::string>> mergesets_;
17 
18  MergeSet(connectivity_mat &con) : con_{ con } {}
19 
20  void ReduceMergeSet(std::vector<std::string>& ms) {
21  for (size_t i = 0; i < ms.size(); ++i) {
22  for (size_t j = i + 1; j < ms.size(); ) {
23  if (con_(ms[i], ms[j])) {
24  // remove j-th element
25  ms.erase(begin(ms) + j);
26  }
27  else {
28  // move next
29  ++j;
30  }
31  }
32  }
33  }
34 
35  void ExpandAllEquivalences(std::vector<std::string> ms, size_t pos) {
36  if (pos >= ms.size()) {
37  sort(begin(ms), end(ms));
38  mergesets_.emplace(ms);
39  }
40  else {
41  std::string cur = ms[pos];
42  auto N = con_.data_.size();
43  for (size_t i = 0; i < N; ++i) {
44  std::string h = con_.GetHeader(i);
45  if (h != "x" && con_(cur, h)) {
46  ms[pos] = h;
47  ExpandAllEquivalences(ms, pos + 1);
48  }
49  }
50  }
51  }
52 
53  void BuildMergeSet() {
54  std::vector<std::string> ms;
55  // Create initial merge set
56  auto N = con_.data_.size();
57  for (size_t i = 0; i < N; ++i) {
58  std::string h = con_.GetHeader(i);
59  if (h != "x" && con_("x", h)) {
60  ms.push_back(h);
61  }
62  }
63  ReduceMergeSet(ms);
64  ExpandAllEquivalences(ms, 0);
65  }
66 };
67 
68 struct MultiMergeSet {
69  std::set<std::vector<std::string>> mergesets_;
71  std::vector<std::string> pixel_list_;
72  std::string x_pixel_;
73 
74  MultiMergeSet(connectivity_mat &con, const std::vector<std::string> &pixel_list, const std::string &x_pixel) : con_{ con }, pixel_list_{ pixel_list }, x_pixel_{ x_pixel } {}
75 
76  void ReduceMergeSet(std::vector<std::string>& ms) {
77  for (size_t i = 0; i < ms.size(); ++i) {
78  for (size_t j = i + 1; j < ms.size(); ) {
79  if (con_(ms[i], ms[j])) {
80  // remove j-th element
81  ms.erase(begin(ms) + j);
82  }
83  else {
84  // move next
85  ++j;
86  }
87  }
88  }
89  }
90 
91  // Check if the pixel j is part of the set of pixels to be labelled in the mask (to_be_labeled_pixels)
92  bool IsInThePixelList(const std::string &j) {
93  for (size_t i = 0; i < pixel_list_.size(); ++i) {
94  if (j == pixel_list_[i]) {
95  return true;
96  }
97  }
98  return false;
99  }
100 
101  void ExpandAllEquivalences(std::vector<std::string> ms, size_t pos) {
102  if (pos >= ms.size()) {
103  sort(begin(ms), end(ms));
104  mergesets_.emplace(ms);
105  }
106  else {
107  std::string cur = ms[pos];
108  auto N = con_.data_.size();
109  for (size_t i = 0; i < N; ++i) {
110  std::string h = con_.GetHeader(i);
111  if (!IsInThePixelList(h) && con_(cur, h)) {
112  ms[pos] = h;
113  ExpandAllEquivalences(ms, pos + 1);
114  }
115  }
116  }
117  }
118 
119  void BuildMergeSet() {
120  std::vector<std::string> ms;
121  // Create initial merge set
122  auto N = con_.data_.size();
123  for (size_t i = 0; i < N; ++i) {
124  std::string h = con_.GetHeader(i);
125  if (!IsInThePixelList(h) && con_(x_pixel_, h)) {
126  ms.push_back(h);
127  }
128  }
129  ReduceMergeSet(ms);
130  ExpandAllEquivalences(ms, 0);
131  }
132 };
133 
134 #endif // !GRAPHGEN_MERGE_SET_H_
135 
MergeSet(connectivity_mat &con)
Definition: merge_set.h:18
std::set< std::vector< std::string > > mergesets_
Definition: merge_set.h:15
void BuildMergeSet()
Definition: merge_set.h:53
void ReduceMergeSet(std::vector< std::string > &ms)
Definition: merge_set.h:20
void ExpandAllEquivalences(std::vector< std::string > ms, size_t pos)
Definition: merge_set.h:35
connectivity_mat & con_
Definition: merge_set.h:16
bool IsInThePixelList(const std::string &j)
Definition: merge_set.h:92
void BuildMergeSet()
Definition: merge_set.h:119
connectivity_mat & con_
Definition: merge_set.h:70
void ReduceMergeSet(std::vector< std::string > &ms)
Definition: merge_set.h:76
std::string x_pixel_
Definition: merge_set.h:72
std::set< std::vector< std::string > > mergesets_
Definition: merge_set.h:69
std::vector< std::string > pixel_list_
Definition: merge_set.h:71
MultiMergeSet(connectivity_mat &con, const std::vector< std::string > &pixel_list, const std::string &x_pixel)
Definition: merge_set.h:74
void ExpandAllEquivalences(std::vector< std::string > ms, size_t pos)
Definition: merge_set.h:101
const std::string & GetHeader(size_t i)
std::vector< std::vector< int > > data_