Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
cutbuffer.inc
Go to the documentation of this file.
1
29#pragma once
30
36//#include <ogdf/lib/abacus/sorter.h>
37
38#include <ogdf/basic/comparer.h>
39
40#pragma GCC visibility push(default)
41namespace abacus {
42
43
44template<class BaseType, class CoType>
46{
47 for (int i = 0; i < n_; i++) {
48 psRef_[i]->conVar()->unlock();
49 delete psRef_[i];
50 }
51}
52
53
54template<class BaseType, class CoType>
56 PoolSlot<BaseType, CoType> *slot,
57 bool keepInPool)
58{
59 if (n_ == size())
60 return 1;
61 else {
62 psRef_[n_] = new PoolSlotRef<BaseType, CoType>(slot);
63 keepInPool_[n_] = keepInPool;
64 ranking_ = false;
65 slot->conVar()->lock();
66 ++n_;
67 return 0;
68 }
69}
70
71
72template<class BaseType, class CoType>
74 PoolSlot<BaseType, CoType> *slot,
75 bool keepInPool,
76 double rank)
77{
78 if (n_ == size())
79 return 1;
80 else {
81 psRef_[n_] = new PoolSlotRef<BaseType, CoType>(slot);
82 keepInPool_[n_] = keepInPool;
83 rank_[n_] = rank;
84 ++n_;
85 slot->conVar()->lock();
86 return 0;
87 }
88}
89
90
91template<class BaseType, class CoType>
92void CutBuffer<BaseType, CoType>::remove(ArrayBuffer<int> &index)
93{
94 PoolSlotRef<BaseType, CoType> *psr;
95
96 const int nIndex = index.size();
97
98 for (int i = 0; i < nIndex; i++) {
99 psr = psRef_[index[i]];
100 psr->conVar()->unlock();
101 PoolSlot<BaseType, CoType> *ps = psr->slot();
102 delete psr;
103 if (ps->conVar()->deletable())
104 ps->removeConVarFromPool();
105 }
106 psRef_.leftShift(index);
107 keepInPool_.leftShift(index);
108 rank_.leftShift(index);
109
110 n_ -= nIndex;
111}
112
113
114template<class BaseType, class CoType>
115void CutBuffer<BaseType, CoType>::sort(int threshold)
116{
117 if (ranking_) {
118 if (n_ > threshold) {
119 // sort the buffered items
120 /* AbaSorter<int, double> sorter;
121 Array<int> index(n_);
122 Array<double> keys(n_);
123
124 for (int i = 0; i < n_; i++) {
125 index[i] = i;
126 keys[i] = -rank_[i];
127 }
128
129 sorter.quickSort(n_, index, keys);
130 */
131 Array< ogdf::Prioritized<int> > things(n_);
132 for (int i = 0; i < n_; i++) {
133 things[i].setItem(i);
134 things[i].setPriority(-rank_[i]);
135 }
136 things.quicksort();
137
138
139 // reorder the buffered items
140 Array<PoolSlotRef<BaseType, CoType>*> psRefSorted(n_);
141 Array<bool> keepInPoolSorted(n_);
142
143 for (int i = 0; i < n_; i++) {
144 psRefSorted[i] = psRef_[things[i].item()];
145 keepInPoolSorted[i] = keepInPool_[things[i].item()];
146 }
147
148 for (int i = 0; i < n_; i++) {
149 psRef_[i] = psRefSorted[i];
150 keepInPool_[i] = keepInPoolSorted[i];
151 }
152
153 Logger::ilout(Logger::Level::Minor) << "\titems ranked: accepted in " << -things[0].priority() << " ... "
154 << -things[threshold - 1].priority() << ", rejected in "
155 << -things[threshold].priority() << " ... " << -things[n_ - 1].priority() << std::endl;
156
157 }
158 else
159 Logger::ilout(Logger::Level::Minor) << "\tnot enough items, no ranking required" << std::endl;
160 }
161 else
162 Logger::ilout(Logger::Level::Minor) << "\tranking of buffered items not possible" << std::endl;
163}
164
165
166template<class BaseType, class CoType>
168 int max,
169 ArrayBuffer<PoolSlot<BaseType, CoType>*> &newSlots)
170{
171 // unlock the buffered items
172 for (int i = 0; i < n_; i++)
173 psRef_[i]->conVar()->unlock();
174
175 // determine the number of items to extract
176 int nExtract;
177
178 if (n_ < max) nExtract = n_;
179 else nExtract = max;
180
181 // delete the nonextracted items
182 /* We have to check if the constraint/variable can be deleted, because the
183 * pool slot might be shared with another constraint/variable that is not
184 * deleted.
185 *
186 * The deletion of the extracted items must be performed before the
187 * deletion of the non-extracted ones. Otherwise if a \a NONDUPLPOOL
188 * is used, it can happen that a constraint is removed from the pool
189 * that is the duplicate of an extracted one.
190 */
191 PoolSlot<BaseType, CoType> *s;
192
193 for (int i = nExtract; i < n_; i++) {
194 if (!keepInPool_[i]) {
195 s = psRef_[i]->slot();
196 delete psRef_[i];
197 if (s->conVar()->deletable())
198 s->removeConVarFromPool();
199 }
200 else delete psRef_[i];
201 }
202
203 n_ = 0;
204
205 // extract the items
206 for (int i = 0; i < nExtract; i++) {
207 newSlots.push(psRef_[i]->slot());
208 delete psRef_[i];
209 }
210
211 // allow ranking in next iteration
212 ranking_ = true;
213}
214
215}
216#pragma GCC visibility pop
~CutBuffer()
The destructor.
void remove(ArrayBuffer< int > &index)
Removes the elements specified by index from the buffer.
int insert(PoolSlot< BaseType, CoType > *slot, bool keepInPool)
Adds a slot to the buffer.
void sort(int threshold)
Sorts the items according to their ranks.
void extract(int max, ArrayBuffer< PoolSlot< BaseType, CoType > * > &newSlots)
Takes the first max items from the buffer and clears the buffer.
static std::ostream & ilout(Level level=Level::Default)
Definition Logger.h:213
Declarations for Comparer objects.
constraint.
cutbuffer.
poolslot.
poolslotref
variable.