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