Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

GF2Solver.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/List.h>
36 
37 #include <iomanip>
38 
39 namespace ogdf {
40 
41 class GF2Solver {
42  static constexpr int chunkSize = 13;
43  static constexpr int chunkSize2 = 9;
44 
45  template<int Dim, typename Next>
46  struct ChunkBase {
47  int m_x[Dim];
48  int m_max;
49  Next* m_next;
50 
51  bool full() const { return m_max == Dim - 1; }
52 
54  m_max = -1;
55  m_next = nullptr;
56  for (int i = 0; i < Dim; i++) {
57  m_x[i] = 0;
58  }
59  }
60 
62  };
63 
64  struct Chunk : public ChunkBase<chunkSize, Chunk> {
66 
67  void add(int x) { m_x[++m_max] = x; }
68 
70  };
71 
72  struct Chunk2 : public ChunkBase<chunkSize2, Chunk2> {
74 
76 
77  void add(int x, ListIterator<int> it) {
78  ++m_max;
79  m_x[m_max] = x;
80  m_it[m_max] = it;
81  }
82 
84  };
85 
86 #if 0
87  struct Row {
88  Chunk *m_pHead;
89  Chunk *m_pTail;
90 
91  Row() {
92  m_pHead = m_pTail = nullptr;
93  }
94 
95  void addChunk(Chunk *p) {
96  if(m_pHead == nullptr)
97  m_pHead = m_pTail = p;
98  else {
99  m_pTail->m_next = p;
100  m_pTail = p;
101  }
102  }
103  };
104 #endif
105 
106  struct Row2 {
109 
110  Row2() { m_pHead = m_pTail = nullptr; }
111 
112  void addChunk(Chunk2* p) {
113  if (m_pHead == nullptr) {
114  m_pHead = m_pTail = p;
115  } else {
116  m_pTail->m_next = p;
117  m_pTail = p;
118  }
119  }
120  };
121 
124 
125 #if 0
126  Chunk *getChunk() {
127  if(m_freelist != nullptr) {
128  Chunk *p = m_freelist;
129  m_freelist = p->m_next;
130  p->m_next = nullptr;
131  p->m_max = -1;
132  return p;
133  }
134  return new Chunk;
135  }
136 #endif
137 
139  if (m_freelist2 != nullptr) {
140  Chunk2* p = m_freelist2;
141  m_freelist2 = p->m_next;
142  p->m_next = nullptr;
143  p->m_max = -1;
144  return p;
145  }
146  return new Chunk2;
147  }
148 
149 #if 0
150  void freeChunk(Chunk *p) {
151  p->m_next = m_freelist;
152  m_freelist = p;
153  }
154 #endif
155 
156  void freeChunk2(Chunk2* p) {
157  p->m_next = m_freelist2;
158  m_freelist2 = p;
159  }
160 
161 #if 0
162  void freeChunks(Chunk *pHead, Chunk *pTail) {
163  pTail->m_next = m_freelist;
164  m_freelist = pHead;
165  }
166 #endif
167 
168  void freeChunks2(Chunk2* pHead, Chunk2* pTail) {
169  pTail->m_next = m_freelist2;
170  m_freelist2 = pHead;
171  }
172 
173 #if 0
174  bool contains(const Row &r, int x) const;
175 
176  void symDiff(Row &r, const Row &other);
177 #endif
178  void symDiff2(int r1, int r2, Array<Row2>& rows, Array<List<int>>& cols);
179 
180 public:
181  class Equation {
183 
184  public:
185  Equation() { }
186 
187  void print() { std::cout << m_objects << std::endl; }
188 
189  ListConstIterator<int> begin() const { return m_objects.begin(); }
190 
191  ListConstIterator<int> end() const { return m_objects.end(); }
192 
193 #if 0
194  bool contains(OBJ obj) const {
195  for(OBJ x : m_objects) {
196  if(x == obj)
197  return true;
198  else if(x > obj)
199  return false;
200  }
201  return false;
202  }
203 #endif
204 
205  int size() const { return m_objects.size(); }
206 
207  Equation& operator|=(int obj) {
208  ListIterator<int> it = m_objects.begin();
209  while (it.valid() && *it < obj) {
210  ++it;
211  }
212  if (it.valid()) {
213  if (*it != obj) {
214  m_objects.insertBefore(obj, it);
215  }
216  } else {
217  m_objects.pushBack(obj);
218  }
219 
220  return *this;
221  }
222 
223 #if 0
224  Equation &operator^=(const Equation &other) {
225  ListConstIterator<OBJ> itOther = other.m_objects.begin();
226  ListIterator<OBJ> it = m_objects.begin();
227 
228  while(itOther.valid())
229  {
230  if(!it.valid()) {
231  m_objects.pushBack(*itOther);
232  ++itOther;
233 
234  } else if(*it == *itOther) {
235  ListIterator<OBJ> itDel = it;
236  ++it; ++itOther;
237  m_objects.del(itDel);
238 
239  } else if(*itOther < *it) {
240  m_objects.insertBefore(*itOther, it);
241  ++itOther;
242 
243  } else {
244  ++it;
245  }
246  }
247 
248  return *this;
249  }
250 #endif
251 
253  };
254 
255  class Matrix {
259 
260  public:
261  Matrix() : m_equations(0, 255, nullptr), m_numRows(0), m_numCols(0) { }
262 
264  for (int i = 0; i < m_numRows; ++i) {
265  delete m_equations[i];
266  }
267  }
268 
270  OGDF_ASSERT(i >= 0);
271  OGDF_ASSERT(i < m_numRows);
272  return *m_equations[i];
273  }
274 
275  const Equation& operator[](int i) const {
276  OGDF_ASSERT(i >= 0);
277  OGDF_ASSERT(i < m_numRows);
278  return *m_equations[i];
279  }
280 
281  int numRows() const { return m_numRows; }
282 
283  int numColumns() const { return m_numCols; }
284 
285  int addRow() {
286  int i = m_numRows++;
287  if (i == m_equations.size()) {
288  m_equations.grow(m_equations.size(), nullptr);
289  }
290  m_equations[i] = new Equation;
291 
292  return i;
293  }
294 
295  int addColumn() { return m_numCols++; }
296 
297  void clear() {
298  for (int i = 0; i < m_numRows; ++i) {
299  delete m_equations[i];
300  }
301 
302  m_equations.init(0, 255, nullptr);
303  m_numRows = m_numCols = 0;
304  }
305 
306  void print() const {
307  for (int i = 0; i < m_numRows; ++i) {
308  std::cout << std::setw(4) << i << ": ";
309  m_equations[i]->print();
310  }
311  }
312  };
313 
315  : m_freelist(nullptr), m_freelist2(nullptr), m_matrix(Mx) { }
316 
317  ~GF2Solver();
318 
319  bool solve();
320  bool solve2();
321 
322 
323 private:
325 };
326 
327 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::GF2Solver::Equation::size
int size() const
Definition: GF2Solver.h:205
ogdf::GF2Solver::Chunk2::add
void add(int x, ListIterator< int > it)
Definition: GF2Solver.h:77
ogdf::GF2Solver::Matrix::numRows
int numRows() const
Definition: GF2Solver.h:281
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::GF2Solver::freeChunk2
void freeChunk2(Chunk2 *p)
Definition: GF2Solver.h:156
ogdf::GF2Solver::Matrix::addColumn
int addColumn()
Definition: GF2Solver.h:295
ogdf::GF2Solver::Row2::Row2
Row2()
Definition: GF2Solver.h:110
ogdf::GF2Solver::Matrix::m_equations
Array< Equation * > m_equations
Definition: GF2Solver.h:256
ogdf::GF2Solver::ChunkBase::full
bool full() const
Definition: GF2Solver.h:51
ogdf::GF2Solver::Equation
Definition: GF2Solver.h:181
ogdf::ListIteratorBase::valid
bool valid() const
Returns true iff the iterator points to an element.
Definition: List.h:143
ogdf::GF2Solver
Definition: GF2Solver.h:41
ogdf::GF2Solver::Row2::m_pTail
Chunk2 * m_pTail
Definition: GF2Solver.h:108
ogdf::GF2Solver::Equation::print
void print()
Definition: GF2Solver.h:187
ogdf::GF2Solver::solve2
bool solve2()
OGDF_NEW_DELETE
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
Definition: memory.h:84
ogdf::GF2Solver::Chunk2::Chunk2
Chunk2()
Definition: GF2Solver.h:75
ogdf::GF2Solver::ChunkBase
Definition: GF2Solver.h:46
ogdf::GF2Solver::Matrix::clear
void clear()
Definition: GF2Solver.h:297
ogdf::GF2Solver::Row2::addChunk
void addChunk(Chunk2 *p)
Definition: GF2Solver.h:112
ogdf::GF2Solver::m_freelist2
Chunk2 * m_freelist2
Definition: GF2Solver.h:123
ogdf::GF2Solver::m_freelist
Chunk * m_freelist
Definition: GF2Solver.h:122
ogdf::GF2Solver::Equation::begin
ListConstIterator< int > begin() const
Definition: GF2Solver.h:189
ogdf::GF2Solver::Equation::Equation
Equation()
Definition: GF2Solver.h:185
ogdf::GF2Solver::Matrix::operator[]
const Equation & operator[](int i) const
Definition: GF2Solver.h:275
r
int r[]
Definition: hierarchical-ranking.cpp:8
ogdf::GF2Solver::Matrix::~Matrix
~Matrix()
Definition: GF2Solver.h:263
ogdf::Array
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:214
ogdf::GF2Solver::Equation::m_objects
List< int > m_objects
Definition: GF2Solver.h:182
ogdf::GF2Solver::~GF2Solver
~GF2Solver()
ogdf::GF2Solver::Matrix::Matrix
Matrix()
Definition: GF2Solver.h:261
ogdf::GF2Solver::getChunk2
Chunk2 * getChunk2()
Definition: GF2Solver.h:138
ogdf::List< int >
ogdf::GF2Solver::Matrix::m_numCols
int m_numCols
Definition: GF2Solver.h:258
ogdf::GF2Solver::Matrix::print
void print() const
Definition: GF2Solver.h:306
ogdf::GF2Solver::ChunkBase::ChunkBase
ChunkBase()
Definition: GF2Solver.h:53
ogdf::GF2Solver::Chunk2::m_it
ListIterator< int > m_it[chunkSize2]
Definition: GF2Solver.h:73
ogdf::GF2Solver::Row2
Definition: GF2Solver.h:106
ogdf::GF2Solver::symDiff2
void symDiff2(int r1, int r2, Array< Row2 > &rows, Array< List< int >> &cols)
ogdf::GF2Solver::Chunk::Chunk
Chunk()
Definition: GF2Solver.h:65
ogdf::GF2Solver::Row2::m_pHead
Chunk2 * m_pHead
Definition: GF2Solver.h:107
ogdf::GF2Solver::ChunkBase::m_x
int m_x[Dim]
Definition: GF2Solver.h:47
ogdf::List::del
void del(iterator it)
Removes it from the list.
Definition: List.h:1601
ogdf::GF2Solver::Chunk2
Definition: GF2Solver.h:72
ogdf::GF2Solver::Matrix
Definition: GF2Solver.h:255
ogdf::GF2Solver::Chunk
Definition: GF2Solver.h:64
ogdf::GF2Solver::GF2Solver
GF2Solver(GF2Solver::Matrix &Mx)
Definition: GF2Solver.h:314
ogdf::GF2Solver::chunkSize
static constexpr int chunkSize
Definition: GF2Solver.h:42
ogdf::GF2Solver::Matrix::addRow
int addRow()
Definition: GF2Solver.h:285
List.h
Declaration of doubly linked lists and iterators.
ogdf::GF2Solver::freeChunks2
void freeChunks2(Chunk2 *pHead, Chunk2 *pTail)
Definition: GF2Solver.h:168
ogdf::GF2Solver::Matrix::m_numRows
int m_numRows
Definition: GF2Solver.h:257
ogdf::GF2Solver::Chunk::add
void add(int x)
Definition: GF2Solver.h:67
ogdf::List::size
int size() const
Returns the number of elements in the list.
Definition: List.h:1478
ogdf::GF2Solver::Matrix::numColumns
int numColumns() const
Definition: GF2Solver.h:283
ogdf::GF2Solver::Equation::end
ListConstIterator< int > end() const
Definition: GF2Solver.h:191
ogdf::ListIteratorBase
Encapsulates a pointer to a list element.
Definition: List.h:46
ogdf::GF2Solver::solve
bool solve()
ogdf::GF2Solver::chunkSize2
static constexpr int chunkSize2
Definition: GF2Solver.h:43
ogdf::GF2Solver::Matrix::operator[]
Equation & operator[](int i)
Definition: GF2Solver.h:269
ogdf::GF2Solver::ChunkBase::m_max
int m_max
Definition: GF2Solver.h:48
ogdf::GF2Solver::Equation::operator|=
Equation & operator|=(int obj)
Definition: GF2Solver.h:207
ogdf::List::pushBack
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1537
ogdf::GF2Solver::m_matrix
Matrix & m_matrix
Definition: GF2Solver.h:324
ogdf::List::insertBefore
iterator insertBefore(const E &x, iterator it)
Inserts element x before it.
Definition: List.h:1556
ogdf::GF2Solver::ChunkBase::m_next
Next * m_next
Definition: GF2Solver.h:49